#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(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) {
cerr << "MERGE: " << a << ' ' << b << '\n';
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) {
cerr << a << ' ' << b << ' ' << get(a) << ' ' << get(b) << '\n';
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--;
cerr << "NEW Q \n";
u.add(a, b);
cout << u.res << '\n';
debug(u.e);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
27 ms |
536 KB |
Output is correct |
8 |
Correct |
36 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
540 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
352 KB |
Output is correct |
13 |
Correct |
2 ms |
344 KB |
Output is correct |
14 |
Correct |
3 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
3 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
344 KB |
Output is correct |
21 |
Correct |
41 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
13 ms |
348 KB |
Output is correct |
24 |
Correct |
7 ms |
348 KB |
Output is correct |
25 |
Correct |
32 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
600 KB |
Output is correct |
28 |
Correct |
2 ms |
544 KB |
Output is correct |
29 |
Correct |
3 ms |
344 KB |
Output is correct |
30 |
Correct |
2 ms |
344 KB |
Output is correct |
31 |
Correct |
23 ms |
500 KB |
Output is correct |
32 |
Correct |
3 ms |
344 KB |
Output is correct |
33 |
Correct |
23 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
27 ms |
536 KB |
Output is correct |
8 |
Correct |
36 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
540 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
352 KB |
Output is correct |
13 |
Correct |
2 ms |
344 KB |
Output is correct |
14 |
Correct |
3 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
3 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
344 KB |
Output is correct |
21 |
Correct |
41 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
13 ms |
348 KB |
Output is correct |
24 |
Correct |
7 ms |
348 KB |
Output is correct |
25 |
Correct |
32 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
600 KB |
Output is correct |
28 |
Correct |
2 ms |
544 KB |
Output is correct |
29 |
Correct |
3 ms |
344 KB |
Output is correct |
30 |
Correct |
2 ms |
344 KB |
Output is correct |
31 |
Correct |
23 ms |
500 KB |
Output is correct |
32 |
Correct |
3 ms |
344 KB |
Output is correct |
33 |
Correct |
23 ms |
344 KB |
Output is correct |
34 |
Correct |
127 ms |
748 KB |
Output is correct |
35 |
Correct |
3395 ms |
13268 KB |
Output is correct |
36 |
Correct |
3524 ms |
16940 KB |
Output is correct |
37 |
Correct |
3457 ms |
17508 KB |
Output is correct |
38 |
Correct |
3432 ms |
17152 KB |
Output is correct |
39 |
Correct |
56 ms |
1104 KB |
Output is correct |
40 |
Correct |
58 ms |
1364 KB |
Output is correct |
41 |
Correct |
57 ms |
1360 KB |
Output is correct |
42 |
Correct |
61 ms |
1164 KB |
Output is correct |
43 |
Correct |
57 ms |
1200 KB |
Output is correct |
44 |
Correct |
60 ms |
1140 KB |
Output is correct |
45 |
Correct |
56 ms |
1104 KB |
Output is correct |
46 |
Correct |
58 ms |
1120 KB |
Output is correct |
47 |
Correct |
57 ms |
1360 KB |
Output is correct |
48 |
Correct |
57 ms |
1360 KB |
Output is correct |
49 |
Correct |
140 ms |
2524 KB |
Output is correct |
50 |
Correct |
3495 ms |
17576 KB |
Output is correct |
51 |
Correct |
125 ms |
1512 KB |
Output is correct |
52 |
Correct |
3488 ms |
15704 KB |
Output is correct |
53 |
Correct |
134 ms |
2140 KB |
Output is correct |
54 |
Correct |
3473 ms |
15928 KB |
Output is correct |
55 |
Correct |
58 ms |
1560 KB |
Output is correct |
56 |
Correct |
59 ms |
1416 KB |
Output is correct |
57 |
Correct |
59 ms |
1360 KB |
Output is correct |
58 |
Correct |
59 ms |
1372 KB |
Output is correct |
59 |
Correct |
66 ms |
1468 KB |
Output is correct |
60 |
Correct |
3322 ms |
14128 KB |
Output is correct |
61 |
Correct |
63 ms |
1344 KB |
Output is correct |
62 |
Correct |
3576 ms |
16412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
27 ms |
536 KB |
Output is correct |
8 |
Correct |
36 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
540 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
352 KB |
Output is correct |
13 |
Correct |
2 ms |
344 KB |
Output is correct |
14 |
Correct |
3 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
3 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
344 KB |
Output is correct |
21 |
Correct |
41 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
348 KB |
Output is correct |
23 |
Correct |
13 ms |
348 KB |
Output is correct |
24 |
Correct |
7 ms |
348 KB |
Output is correct |
25 |
Correct |
32 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
600 KB |
Output is correct |
28 |
Correct |
2 ms |
544 KB |
Output is correct |
29 |
Correct |
3 ms |
344 KB |
Output is correct |
30 |
Correct |
2 ms |
344 KB |
Output is correct |
31 |
Correct |
23 ms |
500 KB |
Output is correct |
32 |
Correct |
3 ms |
344 KB |
Output is correct |
33 |
Correct |
23 ms |
344 KB |
Output is correct |
34 |
Correct |
127 ms |
748 KB |
Output is correct |
35 |
Correct |
3395 ms |
13268 KB |
Output is correct |
36 |
Correct |
3524 ms |
16940 KB |
Output is correct |
37 |
Correct |
3457 ms |
17508 KB |
Output is correct |
38 |
Correct |
3432 ms |
17152 KB |
Output is correct |
39 |
Correct |
56 ms |
1104 KB |
Output is correct |
40 |
Correct |
58 ms |
1364 KB |
Output is correct |
41 |
Correct |
57 ms |
1360 KB |
Output is correct |
42 |
Correct |
61 ms |
1164 KB |
Output is correct |
43 |
Correct |
57 ms |
1200 KB |
Output is correct |
44 |
Correct |
60 ms |
1140 KB |
Output is correct |
45 |
Correct |
56 ms |
1104 KB |
Output is correct |
46 |
Correct |
58 ms |
1120 KB |
Output is correct |
47 |
Correct |
57 ms |
1360 KB |
Output is correct |
48 |
Correct |
57 ms |
1360 KB |
Output is correct |
49 |
Correct |
140 ms |
2524 KB |
Output is correct |
50 |
Correct |
3495 ms |
17576 KB |
Output is correct |
51 |
Correct |
125 ms |
1512 KB |
Output is correct |
52 |
Correct |
3488 ms |
15704 KB |
Output is correct |
53 |
Correct |
134 ms |
2140 KB |
Output is correct |
54 |
Correct |
3473 ms |
15928 KB |
Output is correct |
55 |
Correct |
58 ms |
1560 KB |
Output is correct |
56 |
Correct |
59 ms |
1416 KB |
Output is correct |
57 |
Correct |
59 ms |
1360 KB |
Output is correct |
58 |
Correct |
59 ms |
1372 KB |
Output is correct |
59 |
Correct |
66 ms |
1468 KB |
Output is correct |
60 |
Correct |
3322 ms |
14128 KB |
Output is correct |
61 |
Correct |
63 ms |
1344 KB |
Output is correct |
62 |
Correct |
3576 ms |
16412 KB |
Output is correct |
63 |
Correct |
3861 ms |
76412 KB |
Output is correct |
64 |
Correct |
3775 ms |
76452 KB |
Output is correct |
65 |
Correct |
3765 ms |
76556 KB |
Output is correct |
66 |
Correct |
2897 ms |
41340 KB |
Output is correct |
67 |
Correct |
3250 ms |
68448 KB |
Output is correct |
68 |
Correct |
2974 ms |
41552 KB |
Output is correct |
69 |
Correct |
3362 ms |
42552 KB |
Output is correct |
70 |
Correct |
3531 ms |
41508 KB |
Output is correct |
71 |
Correct |
3569 ms |
41380 KB |
Output is correct |
72 |
Correct |
3489 ms |
52328 KB |
Output is correct |
73 |
Correct |
3741 ms |
54012 KB |
Output is correct |
74 |
Execution timed out |
5026 ms |
79316 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |