#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else
#define debug(X) ;
#endif
#define ll long long
#define all(v) (v).begin(), (v).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define ROF(i,r,l) for(int i=(r);i>=(l);--i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
class UF {
public:
int N;
vector<int> e;
vector<set<int>> to;
vector<set<int>> c;
vector<set<int>> rev;
queue<pair<int, int>> to_merge;
ll res;
UF(int n) {
N = n;
e.resize(N, -1);
to.resize(N);
c.resize(N);
rev.resize(N);
REP(i, N) c[i].insert(i);
res = 0;
}
int get(int a) {return e[a]<0?a:e[a] = get(e[a]);}
int sz(int a) {return -e[get(a)];}
int all_sz(int a) {return ssize(rev[a])+ssize(to[a])+ssize(c[a]);}
void addEdge(int a, int b) {
a = get(a); b = get(b);
if (a == b) return;
to[a].insert(b);
rev[b].insert(a);
if (to[b].count(a)) to_merge.push(mp(a, b));
}
void merge(int a, int b) {
a = get(a); b = get(b);
if (a == b) return;
res += (ll)sz(a)*(ll)ssize(c[b])+(ll)sz(b)*(ll)ssize(c[a]);
if (all_sz(a) > all_sz(b)) swap(a, b);
for (int x : c[a]) {
if (c[b].count(x)) res -= sz(a)+sz(b);
else c[b].insert(x);
}
to[a].erase(b); to[b].erase(a);
rev[b].erase(a); rev[a].erase(b);
for (int x : to[a]) {
rev[x].erase(a);
addEdge(b, x);
}
for (int x : rev[a]) {
to[x].erase(a);
addEdge(x, b);
}
e[b] += e[a];
e[a] = b;
}
void add(int a, int b) {
b = get(b);
if (get(a) != b && !c[b].count(a)){
c[b].insert(a);
res += sz(b);
a = get(a);
addEdge(a, b);
while (to_merge.size() > 0) {
pair<int, int> p = to_merge.front();
to_merge.pop();
merge(p.fi, p.se);
}
}
}
};
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
UF u(n);
REP(i, m) {
int a, b;
cin >> a >> b;
a--; b--;
u.add(a, b);
cout << u.res << '\n';
debug(u.e);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
412 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
55 ms |
4068 KB |
Output is correct |
36 |
Correct |
76 ms |
6668 KB |
Output is correct |
37 |
Correct |
73 ms |
6708 KB |
Output is correct |
38 |
Correct |
70 ms |
6484 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
4 ms |
1372 KB |
Output is correct |
41 |
Correct |
4 ms |
1368 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
2 ms |
860 KB |
Output is correct |
47 |
Correct |
4 ms |
1116 KB |
Output is correct |
48 |
Correct |
4 ms |
1116 KB |
Output is correct |
49 |
Correct |
8 ms |
1884 KB |
Output is correct |
50 |
Correct |
77 ms |
6880 KB |
Output is correct |
51 |
Correct |
5 ms |
1112 KB |
Output is correct |
52 |
Correct |
67 ms |
5336 KB |
Output is correct |
53 |
Correct |
8 ms |
1640 KB |
Output is correct |
54 |
Correct |
67 ms |
5984 KB |
Output is correct |
55 |
Correct |
5 ms |
1372 KB |
Output is correct |
56 |
Correct |
5 ms |
1632 KB |
Output is correct |
57 |
Correct |
4 ms |
1372 KB |
Output is correct |
58 |
Correct |
4 ms |
1372 KB |
Output is correct |
59 |
Correct |
3 ms |
1372 KB |
Output is correct |
60 |
Correct |
53 ms |
3664 KB |
Output is correct |
61 |
Correct |
5 ms |
1116 KB |
Output is correct |
62 |
Correct |
70 ms |
6256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
412 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
55 ms |
4068 KB |
Output is correct |
36 |
Correct |
76 ms |
6668 KB |
Output is correct |
37 |
Correct |
73 ms |
6708 KB |
Output is correct |
38 |
Correct |
70 ms |
6484 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
4 ms |
1372 KB |
Output is correct |
41 |
Correct |
4 ms |
1368 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
2 ms |
860 KB |
Output is correct |
47 |
Correct |
4 ms |
1116 KB |
Output is correct |
48 |
Correct |
4 ms |
1116 KB |
Output is correct |
49 |
Correct |
8 ms |
1884 KB |
Output is correct |
50 |
Correct |
77 ms |
6880 KB |
Output is correct |
51 |
Correct |
5 ms |
1112 KB |
Output is correct |
52 |
Correct |
67 ms |
5336 KB |
Output is correct |
53 |
Correct |
8 ms |
1640 KB |
Output is correct |
54 |
Correct |
67 ms |
5984 KB |
Output is correct |
55 |
Correct |
5 ms |
1372 KB |
Output is correct |
56 |
Correct |
5 ms |
1632 KB |
Output is correct |
57 |
Correct |
4 ms |
1372 KB |
Output is correct |
58 |
Correct |
4 ms |
1372 KB |
Output is correct |
59 |
Correct |
3 ms |
1372 KB |
Output is correct |
60 |
Correct |
53 ms |
3664 KB |
Output is correct |
61 |
Correct |
5 ms |
1116 KB |
Output is correct |
62 |
Correct |
70 ms |
6256 KB |
Output is correct |
63 |
Correct |
460 ms |
64280 KB |
Output is correct |
64 |
Correct |
448 ms |
63816 KB |
Output is correct |
65 |
Correct |
492 ms |
64096 KB |
Output is correct |
66 |
Correct |
162 ms |
31056 KB |
Output is correct |
67 |
Correct |
331 ms |
58048 KB |
Output is correct |
68 |
Correct |
184 ms |
31056 KB |
Output is correct |
69 |
Correct |
307 ms |
32288 KB |
Output is correct |
70 |
Correct |
172 ms |
31060 KB |
Output is correct |
71 |
Correct |
170 ms |
31056 KB |
Output is correct |
72 |
Correct |
336 ms |
42064 KB |
Output is correct |
73 |
Correct |
348 ms |
43604 KB |
Output is correct |
74 |
Correct |
842 ms |
64528 KB |
Output is correct |
75 |
Correct |
461 ms |
41244 KB |
Output is correct |
76 |
Correct |
625 ms |
53096 KB |
Output is correct |
77 |
Correct |
633 ms |
53444 KB |
Output is correct |
78 |
Correct |
216 ms |
36948 KB |
Output is correct |
79 |
Correct |
339 ms |
41380 KB |
Output is correct |
80 |
Correct |
181 ms |
35672 KB |
Output is correct |
81 |
Correct |
276 ms |
39516 KB |
Output is correct |
82 |
Correct |
748 ms |
54268 KB |
Output is correct |
83 |
Correct |
758 ms |
54252 KB |
Output is correct |
84 |
Correct |
617 ms |
52624 KB |
Output is correct |
85 |
Correct |
544 ms |
52744 KB |
Output is correct |
86 |
Correct |
288 ms |
66144 KB |
Output is correct |
87 |
Correct |
289 ms |
68344 KB |
Output is correct |
88 |
Correct |
387 ms |
45300 KB |
Output is correct |
89 |
Correct |
719 ms |
51168 KB |
Output is correct |