#include <bits/stdc++.h>
using namespace std;
#define int long long
int p[200100],sz[200100],ans;
set<int> ch[200100], adj[200100], radj[200100];
queue<pair<int, int>> to_merge;
void ME(int x, int y) {
adj[x].insert(y);
radj[y].insert(x);
if (adj[y].count(x)) to_merge.push({x, y});
}
int allssz(int x) {
return ch[x].size() + adj[x].size() + radj[x].size();
}
int abp(int n) {
if(p[n]-n)
p[n]=abp(p[n]);
return p[n];
}
void Union(int x, int y) {
if (x == y) return;
if (allssz(x) < allssz(y))
swap(x, y);
ans += sz[y] * ch[x].size() + sz[x] * ch[y].size();
p[y] = x;
sz[x] += sz[y];
for (int i : ch[y])
if (ch[x].count(i))
ans -= sz[x];
else
ch[x].insert(i);
adj[x].erase(y), radj[x].erase(y);
adj[y].erase(x), radj[y].erase(x);
for (int i : adj[y]) {
radj[i].erase(y);
ME(x, i);
}
for (int i : radj[y]) {
adj[i].erase(y);
ME(i, x);
}
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
iota(p,p+200100,0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
sz[i] = 1;
ch[i].insert(i);
}
while (m--) {
int x, y;
cin >> x >> y;
y = abp(y);
if (abp(x) != y && !ch[y].count(x)) {
ch[y].insert(x);
ans += sz[y];
x = abp(x);
ME(x, y);
while (to_merge.size()) {
tie(x, y) = to_merge.front();
to_merge.pop();
Union(abp(x), abp(y));
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
30076 KB |
Output is correct |
2 |
Correct |
14 ms |
30016 KB |
Output is correct |
3 |
Correct |
15 ms |
30044 KB |
Output is correct |
4 |
Correct |
15 ms |
30100 KB |
Output is correct |
5 |
Correct |
14 ms |
30036 KB |
Output is correct |
6 |
Correct |
14 ms |
30036 KB |
Output is correct |
7 |
Correct |
14 ms |
30036 KB |
Output is correct |
8 |
Correct |
14 ms |
30100 KB |
Output is correct |
9 |
Correct |
15 ms |
30080 KB |
Output is correct |
10 |
Correct |
14 ms |
30036 KB |
Output is correct |
11 |
Correct |
14 ms |
30108 KB |
Output is correct |
12 |
Correct |
17 ms |
30004 KB |
Output is correct |
13 |
Correct |
14 ms |
30036 KB |
Output is correct |
14 |
Correct |
14 ms |
30036 KB |
Output is correct |
15 |
Correct |
15 ms |
30100 KB |
Output is correct |
16 |
Correct |
15 ms |
30036 KB |
Output is correct |
17 |
Correct |
15 ms |
29996 KB |
Output is correct |
18 |
Correct |
14 ms |
30096 KB |
Output is correct |
19 |
Correct |
13 ms |
30036 KB |
Output is correct |
20 |
Correct |
15 ms |
30080 KB |
Output is correct |
21 |
Correct |
15 ms |
30104 KB |
Output is correct |
22 |
Correct |
14 ms |
30016 KB |
Output is correct |
23 |
Correct |
15 ms |
30076 KB |
Output is correct |
24 |
Correct |
14 ms |
30096 KB |
Output is correct |
25 |
Correct |
15 ms |
30044 KB |
Output is correct |
26 |
Correct |
15 ms |
30092 KB |
Output is correct |
27 |
Correct |
14 ms |
30096 KB |
Output is correct |
28 |
Correct |
19 ms |
30228 KB |
Output is correct |
29 |
Correct |
15 ms |
30096 KB |
Output is correct |
30 |
Correct |
15 ms |
30064 KB |
Output is correct |
31 |
Correct |
14 ms |
30036 KB |
Output is correct |
32 |
Correct |
15 ms |
30036 KB |
Output is correct |
33 |
Correct |
15 ms |
30056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
30076 KB |
Output is correct |
2 |
Correct |
14 ms |
30016 KB |
Output is correct |
3 |
Correct |
15 ms |
30044 KB |
Output is correct |
4 |
Correct |
15 ms |
30100 KB |
Output is correct |
5 |
Correct |
14 ms |
30036 KB |
Output is correct |
6 |
Correct |
14 ms |
30036 KB |
Output is correct |
7 |
Correct |
14 ms |
30036 KB |
Output is correct |
8 |
Correct |
14 ms |
30100 KB |
Output is correct |
9 |
Correct |
15 ms |
30080 KB |
Output is correct |
10 |
Correct |
14 ms |
30036 KB |
Output is correct |
11 |
Correct |
14 ms |
30108 KB |
Output is correct |
12 |
Correct |
17 ms |
30004 KB |
Output is correct |
13 |
Correct |
14 ms |
30036 KB |
Output is correct |
14 |
Correct |
14 ms |
30036 KB |
Output is correct |
15 |
Correct |
15 ms |
30100 KB |
Output is correct |
16 |
Correct |
15 ms |
30036 KB |
Output is correct |
17 |
Correct |
15 ms |
29996 KB |
Output is correct |
18 |
Correct |
14 ms |
30096 KB |
Output is correct |
19 |
Correct |
13 ms |
30036 KB |
Output is correct |
20 |
Correct |
15 ms |
30080 KB |
Output is correct |
21 |
Correct |
15 ms |
30104 KB |
Output is correct |
22 |
Correct |
14 ms |
30016 KB |
Output is correct |
23 |
Correct |
15 ms |
30076 KB |
Output is correct |
24 |
Correct |
14 ms |
30096 KB |
Output is correct |
25 |
Correct |
15 ms |
30044 KB |
Output is correct |
26 |
Correct |
15 ms |
30092 KB |
Output is correct |
27 |
Correct |
14 ms |
30096 KB |
Output is correct |
28 |
Correct |
19 ms |
30228 KB |
Output is correct |
29 |
Correct |
15 ms |
30096 KB |
Output is correct |
30 |
Correct |
15 ms |
30064 KB |
Output is correct |
31 |
Correct |
14 ms |
30036 KB |
Output is correct |
32 |
Correct |
15 ms |
30036 KB |
Output is correct |
33 |
Correct |
15 ms |
30056 KB |
Output is correct |
34 |
Correct |
16 ms |
30236 KB |
Output is correct |
35 |
Correct |
73 ms |
35912 KB |
Output is correct |
36 |
Correct |
92 ms |
38788 KB |
Output is correct |
37 |
Correct |
98 ms |
39052 KB |
Output is correct |
38 |
Correct |
88 ms |
38488 KB |
Output is correct |
39 |
Correct |
17 ms |
30420 KB |
Output is correct |
40 |
Correct |
18 ms |
30804 KB |
Output is correct |
41 |
Correct |
18 ms |
30744 KB |
Output is correct |
42 |
Correct |
16 ms |
30368 KB |
Output is correct |
43 |
Correct |
18 ms |
30372 KB |
Output is correct |
44 |
Correct |
16 ms |
30420 KB |
Output is correct |
45 |
Correct |
16 ms |
30352 KB |
Output is correct |
46 |
Correct |
16 ms |
30420 KB |
Output is correct |
47 |
Correct |
18 ms |
30604 KB |
Output is correct |
48 |
Correct |
18 ms |
30548 KB |
Output is correct |
49 |
Correct |
22 ms |
31388 KB |
Output is correct |
50 |
Correct |
92 ms |
39056 KB |
Output is correct |
51 |
Correct |
20 ms |
30700 KB |
Output is correct |
52 |
Correct |
80 ms |
37160 KB |
Output is correct |
53 |
Correct |
22 ms |
31308 KB |
Output is correct |
54 |
Correct |
88 ms |
38160 KB |
Output is correct |
55 |
Correct |
19 ms |
30804 KB |
Output is correct |
56 |
Correct |
23 ms |
30872 KB |
Output is correct |
57 |
Correct |
19 ms |
30744 KB |
Output is correct |
58 |
Correct |
18 ms |
30856 KB |
Output is correct |
59 |
Correct |
16 ms |
30804 KB |
Output is correct |
60 |
Correct |
68 ms |
35788 KB |
Output is correct |
61 |
Correct |
18 ms |
30600 KB |
Output is correct |
62 |
Correct |
86 ms |
38396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
30076 KB |
Output is correct |
2 |
Correct |
14 ms |
30016 KB |
Output is correct |
3 |
Correct |
15 ms |
30044 KB |
Output is correct |
4 |
Correct |
15 ms |
30100 KB |
Output is correct |
5 |
Correct |
14 ms |
30036 KB |
Output is correct |
6 |
Correct |
14 ms |
30036 KB |
Output is correct |
7 |
Correct |
14 ms |
30036 KB |
Output is correct |
8 |
Correct |
14 ms |
30100 KB |
Output is correct |
9 |
Correct |
15 ms |
30080 KB |
Output is correct |
10 |
Correct |
14 ms |
30036 KB |
Output is correct |
11 |
Correct |
14 ms |
30108 KB |
Output is correct |
12 |
Correct |
17 ms |
30004 KB |
Output is correct |
13 |
Correct |
14 ms |
30036 KB |
Output is correct |
14 |
Correct |
14 ms |
30036 KB |
Output is correct |
15 |
Correct |
15 ms |
30100 KB |
Output is correct |
16 |
Correct |
15 ms |
30036 KB |
Output is correct |
17 |
Correct |
15 ms |
29996 KB |
Output is correct |
18 |
Correct |
14 ms |
30096 KB |
Output is correct |
19 |
Correct |
13 ms |
30036 KB |
Output is correct |
20 |
Correct |
15 ms |
30080 KB |
Output is correct |
21 |
Correct |
15 ms |
30104 KB |
Output is correct |
22 |
Correct |
14 ms |
30016 KB |
Output is correct |
23 |
Correct |
15 ms |
30076 KB |
Output is correct |
24 |
Correct |
14 ms |
30096 KB |
Output is correct |
25 |
Correct |
15 ms |
30044 KB |
Output is correct |
26 |
Correct |
15 ms |
30092 KB |
Output is correct |
27 |
Correct |
14 ms |
30096 KB |
Output is correct |
28 |
Correct |
19 ms |
30228 KB |
Output is correct |
29 |
Correct |
15 ms |
30096 KB |
Output is correct |
30 |
Correct |
15 ms |
30064 KB |
Output is correct |
31 |
Correct |
14 ms |
30036 KB |
Output is correct |
32 |
Correct |
15 ms |
30036 KB |
Output is correct |
33 |
Correct |
15 ms |
30056 KB |
Output is correct |
34 |
Correct |
16 ms |
30236 KB |
Output is correct |
35 |
Correct |
73 ms |
35912 KB |
Output is correct |
36 |
Correct |
92 ms |
38788 KB |
Output is correct |
37 |
Correct |
98 ms |
39052 KB |
Output is correct |
38 |
Correct |
88 ms |
38488 KB |
Output is correct |
39 |
Correct |
17 ms |
30420 KB |
Output is correct |
40 |
Correct |
18 ms |
30804 KB |
Output is correct |
41 |
Correct |
18 ms |
30744 KB |
Output is correct |
42 |
Correct |
16 ms |
30368 KB |
Output is correct |
43 |
Correct |
18 ms |
30372 KB |
Output is correct |
44 |
Correct |
16 ms |
30420 KB |
Output is correct |
45 |
Correct |
16 ms |
30352 KB |
Output is correct |
46 |
Correct |
16 ms |
30420 KB |
Output is correct |
47 |
Correct |
18 ms |
30604 KB |
Output is correct |
48 |
Correct |
18 ms |
30548 KB |
Output is correct |
49 |
Correct |
22 ms |
31388 KB |
Output is correct |
50 |
Correct |
92 ms |
39056 KB |
Output is correct |
51 |
Correct |
20 ms |
30700 KB |
Output is correct |
52 |
Correct |
80 ms |
37160 KB |
Output is correct |
53 |
Correct |
22 ms |
31308 KB |
Output is correct |
54 |
Correct |
88 ms |
38160 KB |
Output is correct |
55 |
Correct |
19 ms |
30804 KB |
Output is correct |
56 |
Correct |
23 ms |
30872 KB |
Output is correct |
57 |
Correct |
19 ms |
30744 KB |
Output is correct |
58 |
Correct |
18 ms |
30856 KB |
Output is correct |
59 |
Correct |
16 ms |
30804 KB |
Output is correct |
60 |
Correct |
68 ms |
35788 KB |
Output is correct |
61 |
Correct |
18 ms |
30600 KB |
Output is correct |
62 |
Correct |
86 ms |
38396 KB |
Output is correct |
63 |
Correct |
531 ms |
83240 KB |
Output is correct |
64 |
Correct |
513 ms |
83280 KB |
Output is correct |
65 |
Correct |
490 ms |
83280 KB |
Output is correct |
66 |
Correct |
196 ms |
49292 KB |
Output is correct |
67 |
Correct |
389 ms |
76184 KB |
Output is correct |
68 |
Correct |
193 ms |
49288 KB |
Output is correct |
69 |
Correct |
376 ms |
50484 KB |
Output is correct |
70 |
Correct |
201 ms |
49328 KB |
Output is correct |
71 |
Correct |
240 ms |
49312 KB |
Output is correct |
72 |
Correct |
369 ms |
60468 KB |
Output is correct |
73 |
Correct |
412 ms |
61920 KB |
Output is correct |
74 |
Correct |
1061 ms |
84332 KB |
Output is correct |
75 |
Correct |
588 ms |
57256 KB |
Output is correct |
76 |
Correct |
774 ms |
69216 KB |
Output is correct |
77 |
Correct |
746 ms |
69508 KB |
Output is correct |
78 |
Correct |
260 ms |
52984 KB |
Output is correct |
79 |
Correct |
334 ms |
57424 KB |
Output is correct |
80 |
Correct |
214 ms |
51800 KB |
Output is correct |
81 |
Correct |
314 ms |
55356 KB |
Output is correct |
82 |
Correct |
771 ms |
69944 KB |
Output is correct |
83 |
Correct |
794 ms |
69996 KB |
Output is correct |
84 |
Correct |
552 ms |
68652 KB |
Output is correct |
85 |
Correct |
606 ms |
68620 KB |
Output is correct |
86 |
Correct |
415 ms |
82156 KB |
Output is correct |
87 |
Correct |
324 ms |
84372 KB |
Output is correct |
88 |
Correct |
413 ms |
61272 KB |
Output is correct |
89 |
Correct |
650 ms |
67252 KB |
Output is correct |