#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int maxn = 3e5+5;
const ll inf = 1e18;
int n, m;
ll ans = 0;
struct dsu{
ll root = -1;
set<pair<int, int>> in;
set<pair<int, int>> out;
};
dsu up[maxn];
int Find(int x){
if(up[x].root < 0)return x;
else{
up[x].root = Find(up[x].root);
return up[x].root;
}
}
struct edge{
int a, b;
};
queue<edge> E;
void Union(int a, int b){
auto it1 = up[b].out.lower_bound({a, 0});
vector<pair<int, int>> tmp;
for(it1; it1 != up[b].out.end(); it1++){
if(it1->f != a)break;
ans+=up[a].root;
tmp.push_back(*it1);
}
for(auto x: tmp)up[b].out.erase(x);
tmp.clear();
for(auto it2 = up[a].in.lower_bound({Find(b), 0}); it2 != up[a].in.end(); it2++){
if(it2->f != b)break;
tmp.push_back(*it2);
}
for(auto x: tmp)up[a].in.erase(x);
if(up[a].in.size() + up[a].out.size() < up[b].in.size() + up[b].out.size())swap(a, b);
ans+=up[a].root*(-up[a].root-1);
ans+=up[b].root*(-up[b].root-1);
ans -= up[b].root*up[a].in.size();
for(auto it = up[b].in.begin(); it != up[b].in.end(); it++){
ans+=up[b].root;
up[it->f].out.erase({b, it->s});
E.push({it->s, a});
}
for(auto it = up[b].out.begin(); it != up[b].out.end(); it++){
ans+=up[it->f].root;
up[it->f].in.erase({b, it->s});
E.push({it->s, it->f});
}
up[a].root+=up[b].root;
up[b].root = a;
ans-=up[a].root*(-up[a].root-1);
}
void add(edge x){
int a = Find(x.a);
int b = Find(x.b);
if(a==b)return;
auto it = up[a].in.lower_bound({b, 0});
if(it != up[a].in.end() && it->f == b)Union(a, b);
else{
if(up[a].out.find({b, x.a}) != up[a].out.end())return;
ans -= up[b].root;
up[a].out.insert({b, x.a});
up[b].in.insert({a, x.a});
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
while(m--){
int a, b;
cin >> a >> b;
E.push({a, b});
while(!E.empty()){
auto x = E.front();
E.pop();
add(x);
}
cout << ans << "\n";
}
}
Compilation message
joitter2.cpp: In function 'void Union(int, int)':
joitter2.cpp:41:9: warning: statement has no effect [-Wunused-value]
41 | for(it1; it1 != up[b].out.end(); it1++){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
30856 KB |
Output is correct |
2 |
Correct |
16 ms |
30848 KB |
Output is correct |
3 |
Correct |
19 ms |
30836 KB |
Output is correct |
4 |
Correct |
18 ms |
30780 KB |
Output is correct |
5 |
Correct |
19 ms |
30832 KB |
Output is correct |
6 |
Correct |
18 ms |
30804 KB |
Output is correct |
7 |
Correct |
17 ms |
30776 KB |
Output is correct |
8 |
Correct |
16 ms |
30804 KB |
Output is correct |
9 |
Correct |
18 ms |
30840 KB |
Output is correct |
10 |
Correct |
16 ms |
30776 KB |
Output is correct |
11 |
Correct |
16 ms |
30804 KB |
Output is correct |
12 |
Correct |
21 ms |
30804 KB |
Output is correct |
13 |
Correct |
18 ms |
30776 KB |
Output is correct |
14 |
Correct |
20 ms |
30852 KB |
Output is correct |
15 |
Correct |
16 ms |
30804 KB |
Output is correct |
16 |
Correct |
16 ms |
30804 KB |
Output is correct |
17 |
Correct |
17 ms |
30852 KB |
Output is correct |
18 |
Correct |
16 ms |
30856 KB |
Output is correct |
19 |
Correct |
15 ms |
30760 KB |
Output is correct |
20 |
Correct |
16 ms |
30820 KB |
Output is correct |
21 |
Correct |
17 ms |
30884 KB |
Output is correct |
22 |
Correct |
17 ms |
30804 KB |
Output is correct |
23 |
Correct |
17 ms |
30800 KB |
Output is correct |
24 |
Correct |
18 ms |
30856 KB |
Output is correct |
25 |
Correct |
19 ms |
30804 KB |
Output is correct |
26 |
Correct |
16 ms |
30804 KB |
Output is correct |
27 |
Correct |
16 ms |
30812 KB |
Output is correct |
28 |
Correct |
15 ms |
30824 KB |
Output is correct |
29 |
Correct |
15 ms |
30744 KB |
Output is correct |
30 |
Correct |
16 ms |
30812 KB |
Output is correct |
31 |
Correct |
17 ms |
30860 KB |
Output is correct |
32 |
Correct |
18 ms |
30744 KB |
Output is correct |
33 |
Correct |
16 ms |
30804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
30856 KB |
Output is correct |
2 |
Correct |
16 ms |
30848 KB |
Output is correct |
3 |
Correct |
19 ms |
30836 KB |
Output is correct |
4 |
Correct |
18 ms |
30780 KB |
Output is correct |
5 |
Correct |
19 ms |
30832 KB |
Output is correct |
6 |
Correct |
18 ms |
30804 KB |
Output is correct |
7 |
Correct |
17 ms |
30776 KB |
Output is correct |
8 |
Correct |
16 ms |
30804 KB |
Output is correct |
9 |
Correct |
18 ms |
30840 KB |
Output is correct |
10 |
Correct |
16 ms |
30776 KB |
Output is correct |
11 |
Correct |
16 ms |
30804 KB |
Output is correct |
12 |
Correct |
21 ms |
30804 KB |
Output is correct |
13 |
Correct |
18 ms |
30776 KB |
Output is correct |
14 |
Correct |
20 ms |
30852 KB |
Output is correct |
15 |
Correct |
16 ms |
30804 KB |
Output is correct |
16 |
Correct |
16 ms |
30804 KB |
Output is correct |
17 |
Correct |
17 ms |
30852 KB |
Output is correct |
18 |
Correct |
16 ms |
30856 KB |
Output is correct |
19 |
Correct |
15 ms |
30760 KB |
Output is correct |
20 |
Correct |
16 ms |
30820 KB |
Output is correct |
21 |
Correct |
17 ms |
30884 KB |
Output is correct |
22 |
Correct |
17 ms |
30804 KB |
Output is correct |
23 |
Correct |
17 ms |
30800 KB |
Output is correct |
24 |
Correct |
18 ms |
30856 KB |
Output is correct |
25 |
Correct |
19 ms |
30804 KB |
Output is correct |
26 |
Correct |
16 ms |
30804 KB |
Output is correct |
27 |
Correct |
16 ms |
30812 KB |
Output is correct |
28 |
Correct |
15 ms |
30824 KB |
Output is correct |
29 |
Correct |
15 ms |
30744 KB |
Output is correct |
30 |
Correct |
16 ms |
30812 KB |
Output is correct |
31 |
Correct |
17 ms |
30860 KB |
Output is correct |
32 |
Correct |
18 ms |
30744 KB |
Output is correct |
33 |
Correct |
16 ms |
30804 KB |
Output is correct |
34 |
Correct |
18 ms |
30936 KB |
Output is correct |
35 |
Correct |
118 ms |
36048 KB |
Output is correct |
36 |
Correct |
100 ms |
38100 KB |
Output is correct |
37 |
Correct |
106 ms |
38180 KB |
Output is correct |
38 |
Correct |
114 ms |
37964 KB |
Output is correct |
39 |
Correct |
17 ms |
30832 KB |
Output is correct |
40 |
Correct |
17 ms |
30944 KB |
Output is correct |
41 |
Correct |
17 ms |
31004 KB |
Output is correct |
42 |
Correct |
17 ms |
30804 KB |
Output is correct |
43 |
Correct |
18 ms |
31060 KB |
Output is correct |
44 |
Correct |
18 ms |
30976 KB |
Output is correct |
45 |
Correct |
22 ms |
30804 KB |
Output is correct |
46 |
Correct |
23 ms |
31020 KB |
Output is correct |
47 |
Correct |
19 ms |
30924 KB |
Output is correct |
48 |
Correct |
17 ms |
30932 KB |
Output is correct |
49 |
Correct |
25 ms |
31536 KB |
Output is correct |
50 |
Correct |
106 ms |
38328 KB |
Output is correct |
51 |
Correct |
21 ms |
31188 KB |
Output is correct |
52 |
Correct |
103 ms |
37084 KB |
Output is correct |
53 |
Correct |
28 ms |
31444 KB |
Output is correct |
54 |
Correct |
120 ms |
37656 KB |
Output is correct |
55 |
Correct |
19 ms |
31248 KB |
Output is correct |
56 |
Correct |
19 ms |
31272 KB |
Output is correct |
57 |
Correct |
19 ms |
31188 KB |
Output is correct |
58 |
Correct |
20 ms |
31212 KB |
Output is correct |
59 |
Correct |
18 ms |
30832 KB |
Output is correct |
60 |
Correct |
112 ms |
35776 KB |
Output is correct |
61 |
Correct |
19 ms |
31000 KB |
Output is correct |
62 |
Correct |
105 ms |
37776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
30856 KB |
Output is correct |
2 |
Correct |
16 ms |
30848 KB |
Output is correct |
3 |
Correct |
19 ms |
30836 KB |
Output is correct |
4 |
Correct |
18 ms |
30780 KB |
Output is correct |
5 |
Correct |
19 ms |
30832 KB |
Output is correct |
6 |
Correct |
18 ms |
30804 KB |
Output is correct |
7 |
Correct |
17 ms |
30776 KB |
Output is correct |
8 |
Correct |
16 ms |
30804 KB |
Output is correct |
9 |
Correct |
18 ms |
30840 KB |
Output is correct |
10 |
Correct |
16 ms |
30776 KB |
Output is correct |
11 |
Correct |
16 ms |
30804 KB |
Output is correct |
12 |
Correct |
21 ms |
30804 KB |
Output is correct |
13 |
Correct |
18 ms |
30776 KB |
Output is correct |
14 |
Correct |
20 ms |
30852 KB |
Output is correct |
15 |
Correct |
16 ms |
30804 KB |
Output is correct |
16 |
Correct |
16 ms |
30804 KB |
Output is correct |
17 |
Correct |
17 ms |
30852 KB |
Output is correct |
18 |
Correct |
16 ms |
30856 KB |
Output is correct |
19 |
Correct |
15 ms |
30760 KB |
Output is correct |
20 |
Correct |
16 ms |
30820 KB |
Output is correct |
21 |
Correct |
17 ms |
30884 KB |
Output is correct |
22 |
Correct |
17 ms |
30804 KB |
Output is correct |
23 |
Correct |
17 ms |
30800 KB |
Output is correct |
24 |
Correct |
18 ms |
30856 KB |
Output is correct |
25 |
Correct |
19 ms |
30804 KB |
Output is correct |
26 |
Correct |
16 ms |
30804 KB |
Output is correct |
27 |
Correct |
16 ms |
30812 KB |
Output is correct |
28 |
Correct |
15 ms |
30824 KB |
Output is correct |
29 |
Correct |
15 ms |
30744 KB |
Output is correct |
30 |
Correct |
16 ms |
30812 KB |
Output is correct |
31 |
Correct |
17 ms |
30860 KB |
Output is correct |
32 |
Correct |
18 ms |
30744 KB |
Output is correct |
33 |
Correct |
16 ms |
30804 KB |
Output is correct |
34 |
Correct |
18 ms |
30936 KB |
Output is correct |
35 |
Correct |
118 ms |
36048 KB |
Output is correct |
36 |
Correct |
100 ms |
38100 KB |
Output is correct |
37 |
Correct |
106 ms |
38180 KB |
Output is correct |
38 |
Correct |
114 ms |
37964 KB |
Output is correct |
39 |
Correct |
17 ms |
30832 KB |
Output is correct |
40 |
Correct |
17 ms |
30944 KB |
Output is correct |
41 |
Correct |
17 ms |
31004 KB |
Output is correct |
42 |
Correct |
17 ms |
30804 KB |
Output is correct |
43 |
Correct |
18 ms |
31060 KB |
Output is correct |
44 |
Correct |
18 ms |
30976 KB |
Output is correct |
45 |
Correct |
22 ms |
30804 KB |
Output is correct |
46 |
Correct |
23 ms |
31020 KB |
Output is correct |
47 |
Correct |
19 ms |
30924 KB |
Output is correct |
48 |
Correct |
17 ms |
30932 KB |
Output is correct |
49 |
Correct |
25 ms |
31536 KB |
Output is correct |
50 |
Correct |
106 ms |
38328 KB |
Output is correct |
51 |
Correct |
21 ms |
31188 KB |
Output is correct |
52 |
Correct |
103 ms |
37084 KB |
Output is correct |
53 |
Correct |
28 ms |
31444 KB |
Output is correct |
54 |
Correct |
120 ms |
37656 KB |
Output is correct |
55 |
Correct |
19 ms |
31248 KB |
Output is correct |
56 |
Correct |
19 ms |
31272 KB |
Output is correct |
57 |
Correct |
19 ms |
31188 KB |
Output is correct |
58 |
Correct |
20 ms |
31212 KB |
Output is correct |
59 |
Correct |
18 ms |
30832 KB |
Output is correct |
60 |
Correct |
112 ms |
35776 KB |
Output is correct |
61 |
Correct |
19 ms |
31000 KB |
Output is correct |
62 |
Correct |
105 ms |
37776 KB |
Output is correct |
63 |
Correct |
345 ms |
64528 KB |
Output is correct |
64 |
Correct |
329 ms |
64344 KB |
Output is correct |
65 |
Correct |
311 ms |
64580 KB |
Output is correct |
66 |
Correct |
84 ms |
35144 KB |
Output is correct |
67 |
Correct |
151 ms |
39444 KB |
Output is correct |
68 |
Correct |
88 ms |
35276 KB |
Output is correct |
69 |
Correct |
222 ms |
39892 KB |
Output is correct |
70 |
Correct |
116 ms |
35176 KB |
Output is correct |
71 |
Correct |
126 ms |
35144 KB |
Output is correct |
72 |
Correct |
179 ms |
39844 KB |
Output is correct |
73 |
Correct |
187 ms |
39804 KB |
Output is correct |
74 |
Correct |
578 ms |
57208 KB |
Output is correct |
75 |
Correct |
326 ms |
45264 KB |
Output is correct |
76 |
Correct |
462 ms |
50632 KB |
Output is correct |
77 |
Correct |
491 ms |
50768 KB |
Output is correct |
78 |
Correct |
119 ms |
39956 KB |
Output is correct |
79 |
Correct |
268 ms |
43604 KB |
Output is correct |
80 |
Correct |
133 ms |
39884 KB |
Output is correct |
81 |
Correct |
214 ms |
43144 KB |
Output is correct |
82 |
Correct |
496 ms |
53468 KB |
Output is correct |
83 |
Correct |
454 ms |
53412 KB |
Output is correct |
84 |
Correct |
344 ms |
53512 KB |
Output is correct |
85 |
Correct |
383 ms |
53432 KB |
Output is correct |
86 |
Correct |
105 ms |
34452 KB |
Output is correct |
87 |
Correct |
158 ms |
36592 KB |
Output is correct |
88 |
Correct |
180 ms |
39984 KB |
Output is correct |
89 |
Correct |
439 ms |
50232 KB |
Output is correct |