# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1046545 | Andrey | Marshmallow Molecules (CCO19_day2problem2) | C++14 | 29 ms | 19272 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
vector<int> dsu(100001);
vector<int> haha[100001];
vector<int> bruh[100001];
set<int> idk[100001];
int calc(int a) {
int c = a;
while(dsu[c] != c) {
c = dsu[c];
}
int e = a;
while(dsu[e] != e) {
int d = dsu[e];
dsu[e] = c;
e = d;
}
return c;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m,a,b;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
dsu[i] = i;
}
long long ans = 0;
for(int i = 0; i < m; i++) {
cin >> a >> b;
haha[a].push_back(b);
bruh[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
vector<int> wut(0);
int big = -1,p = -1;
for(int v: bruh[i]) {
int x = calc(v);
wut.push_back(x);
if((int)idk[x].size() > big) {
big = idk[x].size();
p = x;
}
}
if(p > -1) {
swap(idk[i],idk[p]);
sort(wut.begin(),wut.end());
for(int j = 0; j < wut.size(); j++) {
if(j == 0 || wut[j] != wut[j-1]) {
int c = wut[j];
if(c == p) {
continue;
}
dsu[c] = i;
while(!idk[c].empty()) {
int x = *idk[c].lower_bound(-INT_MAX);
idk[i].insert(x);
idk[c].erase(x);
}
}
}
}
for(int v: haha[i]) {
idk[i].insert(v);
}
idk[i].erase(i);
ans+=idk[i].size();
}
cout << ans;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |