#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;
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);
p[v] = u;
if((int)s[v].size() > (int)s[u].size()) {
s[v].swap(s[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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19044 KB |
Output is correct |
2 |
Correct |
3 ms |
19032 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 |
3 ms |
19036 KB |
Output is correct |
7 |
Correct |
3 ms |
19292 KB |
Output is correct |
8 |
Correct |
3 ms |
19292 KB |
Output is correct |
9 |
Correct |
5 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 |
19048 KB |
Output is correct |
13 |
Correct |
3 ms |
19036 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19292 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
3 ms |
19032 KB |
Output is correct |
18 |
Correct |
3 ms |
19248 KB |
Output is correct |
19 |
Correct |
3 ms |
19036 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
3 ms |
19264 KB |
Output is correct |
22 |
Correct |
3 ms |
19216 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
3 ms |
19288 KB |
Output is correct |
26 |
Correct |
3 ms |
19248 KB |
Output is correct |
27 |
Correct |
3 ms |
19036 KB |
Output is correct |
28 |
Correct |
3 ms |
19252 KB |
Output is correct |
29 |
Correct |
3 ms |
19252 KB |
Output is correct |
30 |
Correct |
3 ms |
19036 KB |
Output is correct |
31 |
Correct |
3 ms |
19036 KB |
Output is correct |
32 |
Correct |
3 ms |
19032 KB |
Output is correct |
33 |
Correct |
3 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19044 KB |
Output is correct |
2 |
Correct |
3 ms |
19032 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 |
3 ms |
19036 KB |
Output is correct |
7 |
Correct |
3 ms |
19292 KB |
Output is correct |
8 |
Correct |
3 ms |
19292 KB |
Output is correct |
9 |
Correct |
5 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 |
19048 KB |
Output is correct |
13 |
Correct |
3 ms |
19036 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19292 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
3 ms |
19032 KB |
Output is correct |
18 |
Correct |
3 ms |
19248 KB |
Output is correct |
19 |
Correct |
3 ms |
19036 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
3 ms |
19264 KB |
Output is correct |
22 |
Correct |
3 ms |
19216 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
3 ms |
19288 KB |
Output is correct |
26 |
Correct |
3 ms |
19248 KB |
Output is correct |
27 |
Correct |
3 ms |
19036 KB |
Output is correct |
28 |
Correct |
3 ms |
19252 KB |
Output is correct |
29 |
Correct |
3 ms |
19252 KB |
Output is correct |
30 |
Correct |
3 ms |
19036 KB |
Output is correct |
31 |
Correct |
3 ms |
19036 KB |
Output is correct |
32 |
Correct |
3 ms |
19032 KB |
Output is correct |
33 |
Correct |
3 ms |
19292 KB |
Output is correct |
34 |
Correct |
6 ms |
19284 KB |
Output is correct |
35 |
Correct |
56 ms |
25264 KB |
Output is correct |
36 |
Correct |
71 ms |
28264 KB |
Output is correct |
37 |
Correct |
86 ms |
28416 KB |
Output is correct |
38 |
Correct |
70 ms |
27984 KB |
Output is correct |
39 |
Correct |
5 ms |
19548 KB |
Output is correct |
40 |
Correct |
6 ms |
19804 KB |
Output is correct |
41 |
Correct |
6 ms |
19804 KB |
Output is correct |
42 |
Correct |
5 ms |
19548 KB |
Output is correct |
43 |
Correct |
144 ms |
65524 KB |
Output is correct |
44 |
Correct |
142 ms |
65104 KB |
Output is correct |
45 |
Correct |
4 ms |
19548 KB |
Output is correct |
46 |
Correct |
4 ms |
19548 KB |
Output is correct |
47 |
Correct |
9 ms |
20660 KB |
Output is correct |
48 |
Correct |
9 ms |
21084 KB |
Output is correct |
49 |
Incorrect |
15 ms |
20572 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19044 KB |
Output is correct |
2 |
Correct |
3 ms |
19032 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 |
3 ms |
19036 KB |
Output is correct |
7 |
Correct |
3 ms |
19292 KB |
Output is correct |
8 |
Correct |
3 ms |
19292 KB |
Output is correct |
9 |
Correct |
5 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 |
19048 KB |
Output is correct |
13 |
Correct |
3 ms |
19036 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19292 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
3 ms |
19032 KB |
Output is correct |
18 |
Correct |
3 ms |
19248 KB |
Output is correct |
19 |
Correct |
3 ms |
19036 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
3 ms |
19264 KB |
Output is correct |
22 |
Correct |
3 ms |
19216 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
3 ms |
19288 KB |
Output is correct |
26 |
Correct |
3 ms |
19248 KB |
Output is correct |
27 |
Correct |
3 ms |
19036 KB |
Output is correct |
28 |
Correct |
3 ms |
19252 KB |
Output is correct |
29 |
Correct |
3 ms |
19252 KB |
Output is correct |
30 |
Correct |
3 ms |
19036 KB |
Output is correct |
31 |
Correct |
3 ms |
19036 KB |
Output is correct |
32 |
Correct |
3 ms |
19032 KB |
Output is correct |
33 |
Correct |
3 ms |
19292 KB |
Output is correct |
34 |
Correct |
6 ms |
19284 KB |
Output is correct |
35 |
Correct |
56 ms |
25264 KB |
Output is correct |
36 |
Correct |
71 ms |
28264 KB |
Output is correct |
37 |
Correct |
86 ms |
28416 KB |
Output is correct |
38 |
Correct |
70 ms |
27984 KB |
Output is correct |
39 |
Correct |
5 ms |
19548 KB |
Output is correct |
40 |
Correct |
6 ms |
19804 KB |
Output is correct |
41 |
Correct |
6 ms |
19804 KB |
Output is correct |
42 |
Correct |
5 ms |
19548 KB |
Output is correct |
43 |
Correct |
144 ms |
65524 KB |
Output is correct |
44 |
Correct |
142 ms |
65104 KB |
Output is correct |
45 |
Correct |
4 ms |
19548 KB |
Output is correct |
46 |
Correct |
4 ms |
19548 KB |
Output is correct |
47 |
Correct |
9 ms |
20660 KB |
Output is correct |
48 |
Correct |
9 ms |
21084 KB |
Output is correct |
49 |
Incorrect |
15 ms |
20572 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |