#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int T = 1000;
multiset <int> in[N], out[N];
vector <int> node[N];
long long ans;
int par[N];
int n,m;
int getpar(int u) {
return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}
void join(int u, int v) {
u = getpar(u);
v = getpar(v);
if (u == v)
return;
if (in[u].size() + out[u].size() + node[u].size() < in[v].size() + out[v].size() + node[v].size())
swap(u, v);
// cerr << u << " " << v << "\n";
ans += 2LL * par[u] * par[v];
out[u].erase(v);
for (int x : node[v]) {
if (in[u].count(x)) {
in[u].erase(x);
ans -= (-par[u]);
}
node[u].push_back(x);
}
ans += (long long) in[u].size() * (-par[v]);
vector <int> add;
for (int x : in[v]) {
int px = getpar(x);
if (px == u)
ans -= (-par[v]);
else if (!in[u].count(x)) {
out[px].erase(out[px].find(v));
out[px].insert(u);
in[u].insert(x);
ans += (-par[u]);
if (out[u].count(px))
add.push_back(px);
} else
ans -= (-par[v]);
}
for (int x : out[v]) if (x != u) {
out[u].insert(x);
if (out[x].count(u))
add.push_back(x);
}
par[u] += par[v];
par[v] = u;
for (int x : add)
join(u, x);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
node[i].push_back(i);
par[i] = -1;
}
for (int i = 1; i <= m; i++) {
int u,v;
cin >> u >> v;
int pu = getpar(u);
int pv = getpar(v);
if (pu != pv && !in[pv].count(u)) {
in[pv].insert(u);
out[pu].insert(pv);
ans += -par[pv];
if (out[pv].count(pu))
join(pu, pv);
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
12060 KB |
Output is correct |
4 |
Correct |
7 ms |
12040 KB |
Output is correct |
5 |
Correct |
7 ms |
12032 KB |
Output is correct |
6 |
Correct |
7 ms |
11964 KB |
Output is correct |
7 |
Correct |
8 ms |
11980 KB |
Output is correct |
8 |
Correct |
8 ms |
12012 KB |
Output is correct |
9 |
Correct |
8 ms |
12108 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
12064 KB |
Output is correct |
15 |
Correct |
7 ms |
12032 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
12060 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
9 ms |
12060 KB |
Output is correct |
22 |
Correct |
7 ms |
12056 KB |
Output is correct |
23 |
Correct |
8 ms |
11980 KB |
Output is correct |
24 |
Correct |
7 ms |
12060 KB |
Output is correct |
25 |
Correct |
8 ms |
11980 KB |
Output is correct |
26 |
Correct |
7 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
8 ms |
11980 KB |
Output is correct |
29 |
Correct |
7 ms |
11980 KB |
Output is correct |
30 |
Correct |
7 ms |
12044 KB |
Output is correct |
31 |
Correct |
8 ms |
11980 KB |
Output is correct |
32 |
Correct |
7 ms |
12012 KB |
Output is correct |
33 |
Correct |
8 ms |
12080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
12060 KB |
Output is correct |
4 |
Correct |
7 ms |
12040 KB |
Output is correct |
5 |
Correct |
7 ms |
12032 KB |
Output is correct |
6 |
Correct |
7 ms |
11964 KB |
Output is correct |
7 |
Correct |
8 ms |
11980 KB |
Output is correct |
8 |
Correct |
8 ms |
12012 KB |
Output is correct |
9 |
Correct |
8 ms |
12108 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
12064 KB |
Output is correct |
15 |
Correct |
7 ms |
12032 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
12060 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
9 ms |
12060 KB |
Output is correct |
22 |
Correct |
7 ms |
12056 KB |
Output is correct |
23 |
Correct |
8 ms |
11980 KB |
Output is correct |
24 |
Correct |
7 ms |
12060 KB |
Output is correct |
25 |
Correct |
8 ms |
11980 KB |
Output is correct |
26 |
Correct |
7 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
8 ms |
11980 KB |
Output is correct |
29 |
Correct |
7 ms |
11980 KB |
Output is correct |
30 |
Correct |
7 ms |
12044 KB |
Output is correct |
31 |
Correct |
8 ms |
11980 KB |
Output is correct |
32 |
Correct |
7 ms |
12012 KB |
Output is correct |
33 |
Correct |
8 ms |
12080 KB |
Output is correct |
34 |
Correct |
10 ms |
12096 KB |
Output is correct |
35 |
Correct |
99 ms |
17588 KB |
Output is correct |
36 |
Correct |
123 ms |
20040 KB |
Output is correct |
37 |
Correct |
125 ms |
20208 KB |
Output is correct |
38 |
Correct |
119 ms |
19872 KB |
Output is correct |
39 |
Correct |
9 ms |
12364 KB |
Output is correct |
40 |
Correct |
10 ms |
12424 KB |
Output is correct |
41 |
Correct |
10 ms |
12452 KB |
Output is correct |
42 |
Correct |
10 ms |
12328 KB |
Output is correct |
43 |
Correct |
10 ms |
12364 KB |
Output is correct |
44 |
Correct |
10 ms |
12328 KB |
Output is correct |
45 |
Correct |
10 ms |
12280 KB |
Output is correct |
46 |
Correct |
9 ms |
12364 KB |
Output is correct |
47 |
Correct |
10 ms |
12448 KB |
Output is correct |
48 |
Correct |
10 ms |
12364 KB |
Output is correct |
49 |
Correct |
17 ms |
13072 KB |
Output is correct |
50 |
Correct |
122 ms |
20260 KB |
Output is correct |
51 |
Correct |
13 ms |
12620 KB |
Output is correct |
52 |
Correct |
109 ms |
18756 KB |
Output is correct |
53 |
Correct |
16 ms |
13024 KB |
Output is correct |
54 |
Correct |
115 ms |
19524 KB |
Output is correct |
55 |
Correct |
11 ms |
12584 KB |
Output is correct |
56 |
Correct |
11 ms |
12528 KB |
Output is correct |
57 |
Correct |
11 ms |
12876 KB |
Output is correct |
58 |
Correct |
11 ms |
12948 KB |
Output is correct |
59 |
Correct |
9 ms |
12364 KB |
Output is correct |
60 |
Correct |
100 ms |
17376 KB |
Output is correct |
61 |
Correct |
12 ms |
12432 KB |
Output is correct |
62 |
Correct |
120 ms |
19744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
12060 KB |
Output is correct |
4 |
Correct |
7 ms |
12040 KB |
Output is correct |
5 |
Correct |
7 ms |
12032 KB |
Output is correct |
6 |
Correct |
7 ms |
11964 KB |
Output is correct |
7 |
Correct |
8 ms |
11980 KB |
Output is correct |
8 |
Correct |
8 ms |
12012 KB |
Output is correct |
9 |
Correct |
8 ms |
12108 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
12064 KB |
Output is correct |
15 |
Correct |
7 ms |
12032 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
12060 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
9 ms |
12060 KB |
Output is correct |
22 |
Correct |
7 ms |
12056 KB |
Output is correct |
23 |
Correct |
8 ms |
11980 KB |
Output is correct |
24 |
Correct |
7 ms |
12060 KB |
Output is correct |
25 |
Correct |
8 ms |
11980 KB |
Output is correct |
26 |
Correct |
7 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
8 ms |
11980 KB |
Output is correct |
29 |
Correct |
7 ms |
11980 KB |
Output is correct |
30 |
Correct |
7 ms |
12044 KB |
Output is correct |
31 |
Correct |
8 ms |
11980 KB |
Output is correct |
32 |
Correct |
7 ms |
12012 KB |
Output is correct |
33 |
Correct |
8 ms |
12080 KB |
Output is correct |
34 |
Correct |
10 ms |
12096 KB |
Output is correct |
35 |
Correct |
99 ms |
17588 KB |
Output is correct |
36 |
Correct |
123 ms |
20040 KB |
Output is correct |
37 |
Correct |
125 ms |
20208 KB |
Output is correct |
38 |
Correct |
119 ms |
19872 KB |
Output is correct |
39 |
Correct |
9 ms |
12364 KB |
Output is correct |
40 |
Correct |
10 ms |
12424 KB |
Output is correct |
41 |
Correct |
10 ms |
12452 KB |
Output is correct |
42 |
Correct |
10 ms |
12328 KB |
Output is correct |
43 |
Correct |
10 ms |
12364 KB |
Output is correct |
44 |
Correct |
10 ms |
12328 KB |
Output is correct |
45 |
Correct |
10 ms |
12280 KB |
Output is correct |
46 |
Correct |
9 ms |
12364 KB |
Output is correct |
47 |
Correct |
10 ms |
12448 KB |
Output is correct |
48 |
Correct |
10 ms |
12364 KB |
Output is correct |
49 |
Correct |
17 ms |
13072 KB |
Output is correct |
50 |
Correct |
122 ms |
20260 KB |
Output is correct |
51 |
Correct |
13 ms |
12620 KB |
Output is correct |
52 |
Correct |
109 ms |
18756 KB |
Output is correct |
53 |
Correct |
16 ms |
13024 KB |
Output is correct |
54 |
Correct |
115 ms |
19524 KB |
Output is correct |
55 |
Correct |
11 ms |
12584 KB |
Output is correct |
56 |
Correct |
11 ms |
12528 KB |
Output is correct |
57 |
Correct |
11 ms |
12876 KB |
Output is correct |
58 |
Correct |
11 ms |
12948 KB |
Output is correct |
59 |
Correct |
9 ms |
12364 KB |
Output is correct |
60 |
Correct |
100 ms |
17376 KB |
Output is correct |
61 |
Correct |
12 ms |
12432 KB |
Output is correct |
62 |
Correct |
120 ms |
19744 KB |
Output is correct |
63 |
Correct |
419 ms |
49244 KB |
Output is correct |
64 |
Correct |
446 ms |
49072 KB |
Output is correct |
65 |
Correct |
432 ms |
49084 KB |
Output is correct |
66 |
Correct |
164 ms |
29756 KB |
Output is correct |
67 |
Correct |
289 ms |
35572 KB |
Output is correct |
68 |
Correct |
162 ms |
29756 KB |
Output is correct |
69 |
Correct |
346 ms |
29788 KB |
Output is correct |
70 |
Correct |
174 ms |
29756 KB |
Output is correct |
71 |
Correct |
163 ms |
29752 KB |
Output is correct |
72 |
Correct |
344 ms |
32728 KB |
Output is correct |
73 |
Correct |
317 ms |
32980 KB |
Output is correct |
74 |
Correct |
794 ms |
54464 KB |
Output is correct |
75 |
Correct |
490 ms |
35692 KB |
Output is correct |
76 |
Correct |
615 ms |
42944 KB |
Output is correct |
77 |
Correct |
558 ms |
42944 KB |
Output is correct |
78 |
Correct |
181 ms |
29636 KB |
Output is correct |
79 |
Correct |
367 ms |
36320 KB |
Output is correct |
80 |
Correct |
190 ms |
29580 KB |
Output is correct |
81 |
Correct |
309 ms |
34196 KB |
Output is correct |
82 |
Correct |
457 ms |
40412 KB |
Output is correct |
83 |
Correct |
508 ms |
40548 KB |
Output is correct |
84 |
Correct |
425 ms |
56120 KB |
Output is correct |
85 |
Correct |
430 ms |
56120 KB |
Output is correct |
86 |
Correct |
208 ms |
33688 KB |
Output is correct |
87 |
Correct |
233 ms |
35768 KB |
Output is correct |
88 |
Correct |
323 ms |
32924 KB |
Output is correct |
89 |
Correct |
533 ms |
41624 KB |
Output is correct |