#define local
#ifndef local
#include ""
#endif
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e18 + 7, mod = 1e9 + 7;
mt19937 rng(1);
int rnd(int l, int r){
int temp = rng() % (r - l + 1);
return abs(temp) + l;
}
int n, m;
set<ii> edges;
unordered_set<int> to[N], from[N];// to includes all nodes that are inside to
unordered_set<int> tocomp[N], fromcomp[N];
int rt[N], sz[N];
int answer;
int root(int x){
return (x == rt[x] ? x : rt[x] = root(rt[x]));
}
queue<ii> q;
void merge(int x, int y){
x = root(x), y = root(y);
if(x == y) return;
// cout << x << " " << y << "\n";
if(sz[x] < sz[y]) swap(x, y);
answer -= sz[x] * (to[x].size() - sz[x]);
answer -= (sz[x] * (sz[x] - 1));
answer -= sz[y] * (to[y].size() - sz[y]);
answer -= (sz[y] * (sz[y] - 1));
rt[y] = x;
sz[x] += sz[y];
//for(auto it : to[y]) to[x].insert(it);
for(auto it : from[y]) from[x].insert(it);
for(auto it : to[y]) to[x].insert(it);
for(auto it : fromcomp[y]){
fromcomp[x].insert(it);
tocomp[it].insert(x);
}
for(auto it : tocomp[y]){
tocomp[x].insert(it);
fromcomp[it].insert(x);
}
for(auto it : tocomp[y]){
//cout << it << "\n";
if(tocomp[it].find(x) != tocomp[it].end()) q.push({x, it});
}
for(auto it : fromcomp[y]){
//cout << it << "\n";
if(fromcomp[it].find(x) != fromcomp[it].end()) q.push({x, it});
}
//for(auto it : tocomp[x]) cout << it << " ";
//cout << "\n";
//for(auto it : fromcomp[x]) cout << it << " ";
//cout << "\n";
//for(auto it : tocomp[5]) cout << it << " ";
//cout <<"\n";
from[y].clear();
to[y].clear();
tocomp[y].clear();
fromcomp[y].clear();
answer += sz[x] * (to[x].size() - sz[x]);
answer += sz[x] * (sz[x] - 1);
}
#ifdef local
void process(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
rt[i] = i, sz[i] = 1, to[i].insert(i), from[i].insert(i), tocomp[i].insert(i), fromcomp[i].insert(i);
}
for(int i = 1; i <= m; i++){
int x, y;
cin >> x >> y;
int xx = root(x), yy = root(y);
if(xx != yy){
if(to[yy].find(x) == to[yy].end()){
to[yy].insert(x);
answer += sz[yy];
}
from[xx].insert(y);
fromcomp[xx].insert(yy);
tocomp[yy].insert(xx);
if(fromcomp[yy].find(xx) != fromcomp[yy].end()) q.push({xx, yy});
}
while(!q.empty()){
merge(q.front().fi, q.front().se);
q.pop();
}
//edges.insert({x, yy});
cout << answer << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
process();
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
219412 KB |
Output is correct |
2 |
Correct |
96 ms |
219468 KB |
Output is correct |
3 |
Correct |
96 ms |
219468 KB |
Output is correct |
4 |
Correct |
97 ms |
219496 KB |
Output is correct |
5 |
Correct |
96 ms |
219416 KB |
Output is correct |
6 |
Correct |
102 ms |
219544 KB |
Output is correct |
7 |
Correct |
106 ms |
219588 KB |
Output is correct |
8 |
Correct |
105 ms |
219620 KB |
Output is correct |
9 |
Correct |
97 ms |
219540 KB |
Output is correct |
10 |
Correct |
95 ms |
219456 KB |
Output is correct |
11 |
Correct |
99 ms |
219660 KB |
Output is correct |
12 |
Correct |
96 ms |
219496 KB |
Output is correct |
13 |
Correct |
96 ms |
219496 KB |
Output is correct |
14 |
Correct |
113 ms |
219492 KB |
Output is correct |
15 |
Correct |
94 ms |
219468 KB |
Output is correct |
16 |
Correct |
102 ms |
219620 KB |
Output is correct |
17 |
Correct |
94 ms |
219544 KB |
Output is correct |
18 |
Correct |
96 ms |
219548 KB |
Output is correct |
19 |
Correct |
96 ms |
219536 KB |
Output is correct |
20 |
Correct |
95 ms |
219532 KB |
Output is correct |
21 |
Correct |
95 ms |
219532 KB |
Output is correct |
22 |
Correct |
97 ms |
219532 KB |
Output is correct |
23 |
Correct |
102 ms |
219512 KB |
Output is correct |
24 |
Correct |
96 ms |
219520 KB |
Output is correct |
25 |
Correct |
95 ms |
219540 KB |
Output is correct |
26 |
Correct |
96 ms |
219452 KB |
Output is correct |
27 |
Correct |
97 ms |
219544 KB |
Output is correct |
28 |
Correct |
97 ms |
219552 KB |
Output is correct |
29 |
Correct |
98 ms |
219468 KB |
Output is correct |
30 |
Correct |
95 ms |
219496 KB |
Output is correct |
31 |
Correct |
97 ms |
219544 KB |
Output is correct |
32 |
Correct |
95 ms |
219424 KB |
Output is correct |
33 |
Correct |
96 ms |
219468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
219412 KB |
Output is correct |
2 |
Correct |
96 ms |
219468 KB |
Output is correct |
3 |
Correct |
96 ms |
219468 KB |
Output is correct |
4 |
Correct |
97 ms |
219496 KB |
Output is correct |
5 |
Correct |
96 ms |
219416 KB |
Output is correct |
6 |
Correct |
102 ms |
219544 KB |
Output is correct |
7 |
Correct |
106 ms |
219588 KB |
Output is correct |
8 |
Correct |
105 ms |
219620 KB |
Output is correct |
9 |
Correct |
97 ms |
219540 KB |
Output is correct |
10 |
Correct |
95 ms |
219456 KB |
Output is correct |
11 |
Correct |
99 ms |
219660 KB |
Output is correct |
12 |
Correct |
96 ms |
219496 KB |
Output is correct |
13 |
Correct |
96 ms |
219496 KB |
Output is correct |
14 |
Correct |
113 ms |
219492 KB |
Output is correct |
15 |
Correct |
94 ms |
219468 KB |
Output is correct |
16 |
Correct |
102 ms |
219620 KB |
Output is correct |
17 |
Correct |
94 ms |
219544 KB |
Output is correct |
18 |
Correct |
96 ms |
219548 KB |
Output is correct |
19 |
Correct |
96 ms |
219536 KB |
Output is correct |
20 |
Correct |
95 ms |
219532 KB |
Output is correct |
21 |
Correct |
95 ms |
219532 KB |
Output is correct |
22 |
Correct |
97 ms |
219532 KB |
Output is correct |
23 |
Correct |
102 ms |
219512 KB |
Output is correct |
24 |
Correct |
96 ms |
219520 KB |
Output is correct |
25 |
Correct |
95 ms |
219540 KB |
Output is correct |
26 |
Correct |
96 ms |
219452 KB |
Output is correct |
27 |
Correct |
97 ms |
219544 KB |
Output is correct |
28 |
Correct |
97 ms |
219552 KB |
Output is correct |
29 |
Correct |
98 ms |
219468 KB |
Output is correct |
30 |
Correct |
95 ms |
219496 KB |
Output is correct |
31 |
Correct |
97 ms |
219544 KB |
Output is correct |
32 |
Correct |
95 ms |
219424 KB |
Output is correct |
33 |
Correct |
96 ms |
219468 KB |
Output is correct |
34 |
Correct |
96 ms |
219724 KB |
Output is correct |
35 |
Correct |
159 ms |
226060 KB |
Output is correct |
36 |
Correct |
199 ms |
229676 KB |
Output is correct |
37 |
Correct |
165 ms |
229852 KB |
Output is correct |
38 |
Correct |
172 ms |
229340 KB |
Output is correct |
39 |
Correct |
98 ms |
220816 KB |
Output is correct |
40 |
Correct |
104 ms |
221516 KB |
Output is correct |
41 |
Correct |
114 ms |
221468 KB |
Output is correct |
42 |
Correct |
104 ms |
220848 KB |
Output is correct |
43 |
Correct |
100 ms |
220876 KB |
Output is correct |
44 |
Correct |
99 ms |
220884 KB |
Output is correct |
45 |
Correct |
99 ms |
220836 KB |
Output is correct |
46 |
Correct |
100 ms |
220784 KB |
Output is correct |
47 |
Correct |
102 ms |
221064 KB |
Output is correct |
48 |
Correct |
102 ms |
221016 KB |
Output is correct |
49 |
Correct |
108 ms |
221828 KB |
Output is correct |
50 |
Correct |
171 ms |
229940 KB |
Output is correct |
51 |
Correct |
105 ms |
221248 KB |
Output is correct |
52 |
Correct |
162 ms |
227616 KB |
Output is correct |
53 |
Correct |
122 ms |
221624 KB |
Output is correct |
54 |
Correct |
168 ms |
228668 KB |
Output is correct |
55 |
Correct |
100 ms |
221260 KB |
Output is correct |
56 |
Correct |
102 ms |
221340 KB |
Output is correct |
57 |
Correct |
102 ms |
221260 KB |
Output is correct |
58 |
Correct |
101 ms |
221324 KB |
Output is correct |
59 |
Correct |
105 ms |
221772 KB |
Output is correct |
60 |
Correct |
158 ms |
226716 KB |
Output is correct |
61 |
Correct |
105 ms |
221100 KB |
Output is correct |
62 |
Correct |
172 ms |
229236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
219412 KB |
Output is correct |
2 |
Correct |
96 ms |
219468 KB |
Output is correct |
3 |
Correct |
96 ms |
219468 KB |
Output is correct |
4 |
Correct |
97 ms |
219496 KB |
Output is correct |
5 |
Correct |
96 ms |
219416 KB |
Output is correct |
6 |
Correct |
102 ms |
219544 KB |
Output is correct |
7 |
Correct |
106 ms |
219588 KB |
Output is correct |
8 |
Correct |
105 ms |
219620 KB |
Output is correct |
9 |
Correct |
97 ms |
219540 KB |
Output is correct |
10 |
Correct |
95 ms |
219456 KB |
Output is correct |
11 |
Correct |
99 ms |
219660 KB |
Output is correct |
12 |
Correct |
96 ms |
219496 KB |
Output is correct |
13 |
Correct |
96 ms |
219496 KB |
Output is correct |
14 |
Correct |
113 ms |
219492 KB |
Output is correct |
15 |
Correct |
94 ms |
219468 KB |
Output is correct |
16 |
Correct |
102 ms |
219620 KB |
Output is correct |
17 |
Correct |
94 ms |
219544 KB |
Output is correct |
18 |
Correct |
96 ms |
219548 KB |
Output is correct |
19 |
Correct |
96 ms |
219536 KB |
Output is correct |
20 |
Correct |
95 ms |
219532 KB |
Output is correct |
21 |
Correct |
95 ms |
219532 KB |
Output is correct |
22 |
Correct |
97 ms |
219532 KB |
Output is correct |
23 |
Correct |
102 ms |
219512 KB |
Output is correct |
24 |
Correct |
96 ms |
219520 KB |
Output is correct |
25 |
Correct |
95 ms |
219540 KB |
Output is correct |
26 |
Correct |
96 ms |
219452 KB |
Output is correct |
27 |
Correct |
97 ms |
219544 KB |
Output is correct |
28 |
Correct |
97 ms |
219552 KB |
Output is correct |
29 |
Correct |
98 ms |
219468 KB |
Output is correct |
30 |
Correct |
95 ms |
219496 KB |
Output is correct |
31 |
Correct |
97 ms |
219544 KB |
Output is correct |
32 |
Correct |
95 ms |
219424 KB |
Output is correct |
33 |
Correct |
96 ms |
219468 KB |
Output is correct |
34 |
Correct |
96 ms |
219724 KB |
Output is correct |
35 |
Correct |
159 ms |
226060 KB |
Output is correct |
36 |
Correct |
199 ms |
229676 KB |
Output is correct |
37 |
Correct |
165 ms |
229852 KB |
Output is correct |
38 |
Correct |
172 ms |
229340 KB |
Output is correct |
39 |
Correct |
98 ms |
220816 KB |
Output is correct |
40 |
Correct |
104 ms |
221516 KB |
Output is correct |
41 |
Correct |
114 ms |
221468 KB |
Output is correct |
42 |
Correct |
104 ms |
220848 KB |
Output is correct |
43 |
Correct |
100 ms |
220876 KB |
Output is correct |
44 |
Correct |
99 ms |
220884 KB |
Output is correct |
45 |
Correct |
99 ms |
220836 KB |
Output is correct |
46 |
Correct |
100 ms |
220784 KB |
Output is correct |
47 |
Correct |
102 ms |
221064 KB |
Output is correct |
48 |
Correct |
102 ms |
221016 KB |
Output is correct |
49 |
Correct |
108 ms |
221828 KB |
Output is correct |
50 |
Correct |
171 ms |
229940 KB |
Output is correct |
51 |
Correct |
105 ms |
221248 KB |
Output is correct |
52 |
Correct |
162 ms |
227616 KB |
Output is correct |
53 |
Correct |
122 ms |
221624 KB |
Output is correct |
54 |
Correct |
168 ms |
228668 KB |
Output is correct |
55 |
Correct |
100 ms |
221260 KB |
Output is correct |
56 |
Correct |
102 ms |
221340 KB |
Output is correct |
57 |
Correct |
102 ms |
221260 KB |
Output is correct |
58 |
Correct |
101 ms |
221324 KB |
Output is correct |
59 |
Correct |
105 ms |
221772 KB |
Output is correct |
60 |
Correct |
158 ms |
226716 KB |
Output is correct |
61 |
Correct |
105 ms |
221100 KB |
Output is correct |
62 |
Correct |
172 ms |
229236 KB |
Output is correct |
63 |
Correct |
835 ms |
320460 KB |
Output is correct |
64 |
Correct |
811 ms |
320468 KB |
Output is correct |
65 |
Correct |
817 ms |
320464 KB |
Output is correct |
66 |
Correct |
479 ms |
287588 KB |
Output is correct |
67 |
Correct |
1935 ms |
341936 KB |
Output is correct |
68 |
Correct |
449 ms |
287516 KB |
Output is correct |
69 |
Correct |
523 ms |
293680 KB |
Output is correct |
70 |
Correct |
471 ms |
287580 KB |
Output is correct |
71 |
Correct |
526 ms |
287572 KB |
Output is correct |
72 |
Correct |
1200 ms |
303396 KB |
Output is correct |
73 |
Correct |
1266 ms |
308472 KB |
Output is correct |
74 |
Correct |
1712 ms |
332140 KB |
Output is correct |
75 |
Correct |
694 ms |
299472 KB |
Output is correct |
76 |
Correct |
1185 ms |
309768 KB |
Output is correct |
77 |
Correct |
1185 ms |
310044 KB |
Output is correct |
78 |
Correct |
445 ms |
292244 KB |
Output is correct |
79 |
Correct |
534 ms |
298100 KB |
Output is correct |
80 |
Correct |
423 ms |
292264 KB |
Output is correct |
81 |
Correct |
521 ms |
298320 KB |
Output is correct |
82 |
Correct |
993 ms |
313140 KB |
Output is correct |
83 |
Correct |
949 ms |
313100 KB |
Output is correct |
84 |
Correct |
782 ms |
312316 KB |
Output is correct |
85 |
Correct |
816 ms |
312268 KB |
Output is correct |
86 |
Correct |
2005 ms |
361948 KB |
Output is correct |
87 |
Correct |
1993 ms |
364240 KB |
Output is correct |
88 |
Correct |
1218 ms |
305972 KB |
Output is correct |
89 |
Correct |
1117 ms |
307392 KB |
Output is correct |