#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
int n, m, p[N];
int get(int v) {
if(p[v] == v) return v;
return p[v] = get(p[v]);
}
set<int> s[N],g[N],t[N],gr[N];
ll cl(int v) {
return (int)s[v].size() * 1ll * ((int)s[v].size() - 1);
}
ll d(int v) {
return (int)s[v].size() * 1ll * (int)t[v].size();
}
ll res = 0;
int sz(int x) {
return (int)g[x].size() + (int)gr[x].size() + (int)t[x].size() + (int)s[x].size();
}
void merge(int v,int u) {
v = get(v);
u = get(u);
if(v == u) return;
res -= cl(v);
res -= cl(u);
res -= d(v);
res -= d(u);
if(sz(u) < sz(v)) {
swap(u,v);
}
p[v] = u;
for(int i:s[v]) {
s[u].insert(i);
t[u].erase(i);
}
for(int i:t[v]) {
if(s[u].find(i) != s[u].end()) {
t[u].erase(i);
} else {
t[u].insert(i);
}
}
g[u].erase(v);
g[v].erase(u);
vector<pair<int,int>> mrg;
for(int i:g[v]) {
if(g[i].count(u)) {
mrg.push_back({i,u});
}
g[u].insert(i);
gr[i].erase(v);
gr[i].insert(u);
}
gr[v].erase(u);
gr[u].erase(v);
for(int i:gr[v]) {
if(g[u].count(i)) {
mrg.push_back({i,u});
}
gr[u].insert(i);
g[i].erase(v);
g[i].insert(u);
}
res += cl(u);
res += d(u);
for(auto [x,y]:mrg) {
merge(x,y);
}
}
void test() {
cin >> n >> m;
for(int i = 1;i <= n;i++) {
p[i] = i;
s[i].insert(i);
}
for(int tc = 1;tc <= m;tc++) {
int a,b;
cin >> a >> b;
int A = get(a),B = get(b);
if(A == B || t[B].count(a)) {
cout << res << '\n';
continue;
}
// cout << A << ' ' << B << '\n';
// for(int j:g[B]) {
// }
if(g[B].count(A)) {
merge(A,B);
} else {
g[A].insert(B);
gr[B].insert(A);
t[B].insert(a);
res += (int)s[B].size();
}
cout << res << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19032 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Correct |
3 ms |
19036 KB |
Output is correct |
6 |
Correct |
2 ms |
19040 KB |
Output is correct |
7 |
Correct |
3 ms |
19032 KB |
Output is correct |
8 |
Correct |
3 ms |
19292 KB |
Output is correct |
9 |
Correct |
3 ms |
19292 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
3 ms |
19036 KB |
Output is correct |
12 |
Correct |
3 ms |
19036 KB |
Output is correct |
13 |
Correct |
3 ms |
19220 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19040 KB |
Output is correct |
17 |
Correct |
2 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
19036 KB |
Output is correct |
19 |
Correct |
3 ms |
19244 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
3 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19252 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
3 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
3 ms |
19036 KB |
Output is correct |
27 |
Correct |
3 ms |
19036 KB |
Output is correct |
28 |
Correct |
3 ms |
19036 KB |
Output is correct |
29 |
Correct |
3 ms |
19124 KB |
Output is correct |
30 |
Correct |
3 ms |
19032 KB |
Output is correct |
31 |
Correct |
3 ms |
19032 KB |
Output is correct |
32 |
Correct |
3 ms |
19032 KB |
Output is correct |
33 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19032 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Correct |
3 ms |
19036 KB |
Output is correct |
6 |
Correct |
2 ms |
19040 KB |
Output is correct |
7 |
Correct |
3 ms |
19032 KB |
Output is correct |
8 |
Correct |
3 ms |
19292 KB |
Output is correct |
9 |
Correct |
3 ms |
19292 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
3 ms |
19036 KB |
Output is correct |
12 |
Correct |
3 ms |
19036 KB |
Output is correct |
13 |
Correct |
3 ms |
19220 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19040 KB |
Output is correct |
17 |
Correct |
2 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
19036 KB |
Output is correct |
19 |
Correct |
3 ms |
19244 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
3 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19252 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
3 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
3 ms |
19036 KB |
Output is correct |
27 |
Correct |
3 ms |
19036 KB |
Output is correct |
28 |
Correct |
3 ms |
19036 KB |
Output is correct |
29 |
Correct |
3 ms |
19124 KB |
Output is correct |
30 |
Correct |
3 ms |
19032 KB |
Output is correct |
31 |
Correct |
3 ms |
19032 KB |
Output is correct |
32 |
Correct |
3 ms |
19032 KB |
Output is correct |
33 |
Correct |
3 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
19292 KB |
Output is correct |
35 |
Correct |
49 ms |
22940 KB |
Output is correct |
36 |
Correct |
67 ms |
25680 KB |
Output is correct |
37 |
Correct |
80 ms |
25688 KB |
Output is correct |
38 |
Correct |
63 ms |
25200 KB |
Output is correct |
39 |
Correct |
4 ms |
19544 KB |
Output is correct |
40 |
Correct |
5 ms |
19804 KB |
Output is correct |
41 |
Correct |
5 ms |
19848 KB |
Output is correct |
42 |
Correct |
4 ms |
19548 KB |
Output is correct |
43 |
Correct |
5 ms |
19548 KB |
Output is correct |
44 |
Correct |
5 ms |
19480 KB |
Output is correct |
45 |
Correct |
4 ms |
19548 KB |
Output is correct |
46 |
Correct |
4 ms |
19548 KB |
Output is correct |
47 |
Correct |
5 ms |
19544 KB |
Output is correct |
48 |
Correct |
5 ms |
19548 KB |
Output is correct |
49 |
Correct |
10 ms |
20316 KB |
Output is correct |
50 |
Correct |
70 ms |
28364 KB |
Output is correct |
51 |
Correct |
9 ms |
19804 KB |
Output is correct |
52 |
Correct |
57 ms |
26680 KB |
Output is correct |
53 |
Correct |
11 ms |
20312 KB |
Output is correct |
54 |
Correct |
61 ms |
27380 KB |
Output is correct |
55 |
Correct |
7 ms |
19800 KB |
Output is correct |
56 |
Correct |
6 ms |
19804 KB |
Output is correct |
57 |
Correct |
6 ms |
20284 KB |
Output is correct |
58 |
Correct |
7 ms |
20316 KB |
Output is correct |
59 |
Correct |
5 ms |
19804 KB |
Output is correct |
60 |
Correct |
47 ms |
24700 KB |
Output is correct |
61 |
Correct |
6 ms |
19804 KB |
Output is correct |
62 |
Correct |
62 ms |
27736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19032 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Correct |
3 ms |
19036 KB |
Output is correct |
6 |
Correct |
2 ms |
19040 KB |
Output is correct |
7 |
Correct |
3 ms |
19032 KB |
Output is correct |
8 |
Correct |
3 ms |
19292 KB |
Output is correct |
9 |
Correct |
3 ms |
19292 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
3 ms |
19036 KB |
Output is correct |
12 |
Correct |
3 ms |
19036 KB |
Output is correct |
13 |
Correct |
3 ms |
19220 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19040 KB |
Output is correct |
17 |
Correct |
2 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
19036 KB |
Output is correct |
19 |
Correct |
3 ms |
19244 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
3 ms |
19292 KB |
Output is correct |
22 |
Correct |
4 ms |
19252 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
3 ms |
19036 KB |
Output is correct |
25 |
Correct |
4 ms |
19036 KB |
Output is correct |
26 |
Correct |
3 ms |
19036 KB |
Output is correct |
27 |
Correct |
3 ms |
19036 KB |
Output is correct |
28 |
Correct |
3 ms |
19036 KB |
Output is correct |
29 |
Correct |
3 ms |
19124 KB |
Output is correct |
30 |
Correct |
3 ms |
19032 KB |
Output is correct |
31 |
Correct |
3 ms |
19032 KB |
Output is correct |
32 |
Correct |
3 ms |
19032 KB |
Output is correct |
33 |
Correct |
3 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
19292 KB |
Output is correct |
35 |
Correct |
49 ms |
22940 KB |
Output is correct |
36 |
Correct |
67 ms |
25680 KB |
Output is correct |
37 |
Correct |
80 ms |
25688 KB |
Output is correct |
38 |
Correct |
63 ms |
25200 KB |
Output is correct |
39 |
Correct |
4 ms |
19544 KB |
Output is correct |
40 |
Correct |
5 ms |
19804 KB |
Output is correct |
41 |
Correct |
5 ms |
19848 KB |
Output is correct |
42 |
Correct |
4 ms |
19548 KB |
Output is correct |
43 |
Correct |
5 ms |
19548 KB |
Output is correct |
44 |
Correct |
5 ms |
19480 KB |
Output is correct |
45 |
Correct |
4 ms |
19548 KB |
Output is correct |
46 |
Correct |
4 ms |
19548 KB |
Output is correct |
47 |
Correct |
5 ms |
19544 KB |
Output is correct |
48 |
Correct |
5 ms |
19548 KB |
Output is correct |
49 |
Correct |
10 ms |
20316 KB |
Output is correct |
50 |
Correct |
70 ms |
28364 KB |
Output is correct |
51 |
Correct |
9 ms |
19804 KB |
Output is correct |
52 |
Correct |
57 ms |
26680 KB |
Output is correct |
53 |
Correct |
11 ms |
20312 KB |
Output is correct |
54 |
Correct |
61 ms |
27380 KB |
Output is correct |
55 |
Correct |
7 ms |
19800 KB |
Output is correct |
56 |
Correct |
6 ms |
19804 KB |
Output is correct |
57 |
Correct |
6 ms |
20284 KB |
Output is correct |
58 |
Correct |
7 ms |
20316 KB |
Output is correct |
59 |
Correct |
5 ms |
19804 KB |
Output is correct |
60 |
Correct |
47 ms |
24700 KB |
Output is correct |
61 |
Correct |
6 ms |
19804 KB |
Output is correct |
62 |
Correct |
62 ms |
27736 KB |
Output is correct |
63 |
Correct |
262 ms |
72020 KB |
Output is correct |
64 |
Correct |
259 ms |
71840 KB |
Output is correct |
65 |
Correct |
282 ms |
72016 KB |
Output is correct |
66 |
Correct |
111 ms |
37940 KB |
Output is correct |
67 |
Correct |
195 ms |
62032 KB |
Output is correct |
68 |
Correct |
108 ms |
37972 KB |
Output is correct |
69 |
Correct |
227 ms |
37968 KB |
Output is correct |
70 |
Correct |
129 ms |
37972 KB |
Output is correct |
71 |
Correct |
131 ms |
37972 KB |
Output is correct |
72 |
Correct |
215 ms |
46416 KB |
Output is correct |
73 |
Correct |
224 ms |
48208 KB |
Output is correct |
74 |
Correct |
488 ms |
72020 KB |
Output is correct |
75 |
Correct |
357 ms |
45024 KB |
Output is correct |
76 |
Correct |
404 ms |
57544 KB |
Output is correct |
77 |
Correct |
408 ms |
57784 KB |
Output is correct |
78 |
Correct |
125 ms |
40424 KB |
Output is correct |
79 |
Correct |
236 ms |
44960 KB |
Output is correct |
80 |
Correct |
140 ms |
39252 KB |
Output is correct |
81 |
Correct |
238 ms |
42556 KB |
Output is correct |
82 |
Correct |
481 ms |
57848 KB |
Output is correct |
83 |
Correct |
457 ms |
57940 KB |
Output is correct |
84 |
Correct |
472 ms |
75412 KB |
Output is correct |
85 |
Correct |
444 ms |
75344 KB |
Output is correct |
86 |
Correct |
167 ms |
66132 KB |
Output is correct |
87 |
Correct |
174 ms |
68420 KB |
Output is correct |
88 |
Correct |
212 ms |
47440 KB |
Output is correct |
89 |
Correct |
408 ms |
54736 KB |
Output is correct |