#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]);
dsu[p] = i;
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
Main.cpp: In function 'int main()':
Main.cpp:54:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int j = 0; j < wut.size(); j++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10076 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10076 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
1 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
1 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10076 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10076 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
1 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
1 ms |
10076 KB |
Output is correct |
16 |
Correct |
23 ms |
12636 KB |
Output is correct |
17 |
Correct |
22 ms |
12636 KB |
Output is correct |
18 |
Correct |
20 ms |
12572 KB |
Output is correct |
19 |
Correct |
21 ms |
12636 KB |
Output is correct |
20 |
Correct |
20 ms |
12528 KB |
Output is correct |
21 |
Correct |
4 ms |
10584 KB |
Output is correct |
22 |
Correct |
22 ms |
12636 KB |
Output is correct |
23 |
Correct |
21 ms |
12672 KB |
Output is correct |
24 |
Correct |
21 ms |
12636 KB |
Output is correct |
25 |
Correct |
22 ms |
12632 KB |
Output is correct |
26 |
Correct |
21 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
17536 KB |
Output is correct |
2 |
Correct |
49 ms |
17756 KB |
Output is correct |
3 |
Correct |
57 ms |
17232 KB |
Output is correct |
4 |
Correct |
49 ms |
18980 KB |
Output is correct |
5 |
Correct |
50 ms |
17660 KB |
Output is correct |
6 |
Correct |
45 ms |
17236 KB |
Output is correct |
7 |
Correct |
47 ms |
18512 KB |
Output is correct |
8 |
Correct |
50 ms |
17748 KB |
Output is correct |
9 |
Correct |
50 ms |
17236 KB |
Output is correct |
10 |
Correct |
48 ms |
18772 KB |
Output is correct |
11 |
Correct |
50 ms |
17752 KB |
Output is correct |
12 |
Correct |
46 ms |
17260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10076 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10076 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
1 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
10076 KB |
Output is correct |
13 |
Correct |
2 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
1 ms |
10076 KB |
Output is correct |
16 |
Correct |
23 ms |
12636 KB |
Output is correct |
17 |
Correct |
22 ms |
12636 KB |
Output is correct |
18 |
Correct |
20 ms |
12572 KB |
Output is correct |
19 |
Correct |
21 ms |
12636 KB |
Output is correct |
20 |
Correct |
20 ms |
12528 KB |
Output is correct |
21 |
Correct |
4 ms |
10584 KB |
Output is correct |
22 |
Correct |
22 ms |
12636 KB |
Output is correct |
23 |
Correct |
21 ms |
12672 KB |
Output is correct |
24 |
Correct |
21 ms |
12636 KB |
Output is correct |
25 |
Correct |
22 ms |
12632 KB |
Output is correct |
26 |
Correct |
21 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
55 ms |
17536 KB |
Output is correct |
29 |
Correct |
49 ms |
17756 KB |
Output is correct |
30 |
Correct |
57 ms |
17232 KB |
Output is correct |
31 |
Correct |
49 ms |
18980 KB |
Output is correct |
32 |
Correct |
50 ms |
17660 KB |
Output is correct |
33 |
Correct |
45 ms |
17236 KB |
Output is correct |
34 |
Correct |
47 ms |
18512 KB |
Output is correct |
35 |
Correct |
50 ms |
17748 KB |
Output is correct |
36 |
Correct |
50 ms |
17236 KB |
Output is correct |
37 |
Correct |
48 ms |
18772 KB |
Output is correct |
38 |
Correct |
50 ms |
17752 KB |
Output is correct |
39 |
Correct |
46 ms |
17260 KB |
Output is correct |
40 |
Correct |
53 ms |
16260 KB |
Output is correct |
41 |
Correct |
46 ms |
16340 KB |
Output is correct |
42 |
Correct |
55 ms |
16472 KB |
Output is correct |
43 |
Correct |
54 ms |
16428 KB |
Output is correct |
44 |
Correct |
51 ms |
17240 KB |
Output is correct |
45 |
Correct |
45 ms |
17244 KB |
Output is correct |
46 |
Correct |
42 ms |
18008 KB |
Output is correct |
47 |
Correct |
47 ms |
18012 KB |
Output is correct |
48 |
Correct |
38 ms |
18768 KB |
Output is correct |
49 |
Correct |
39 ms |
18852 KB |
Output is correct |
50 |
Correct |
47 ms |
18524 KB |
Output is correct |
51 |
Correct |
62 ms |
17632 KB |
Output is correct |
52 |
Correct |
46 ms |
17244 KB |
Output is correct |
53 |
Correct |
43 ms |
17244 KB |
Output is correct |
54 |
Correct |
50 ms |
16984 KB |
Output is correct |
55 |
Correct |
52 ms |
17936 KB |
Output is correct |
56 |
Correct |
44 ms |
18048 KB |
Output is correct |
57 |
Correct |
39 ms |
19084 KB |
Output is correct |