//#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse4")
#include <complex>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define N ((ll)5e5+5)
#define M ((ll)1e9+7)
#define fi first
#define se second
#define bs (0b10000000000000000000)
#define inf ((ll)1e16)
struct dsu{
queue<pair<ll, ll>> e;
vector<ll> p, sz;
ll ans;
vector<set<ll>> in;
vector<set<pair<ll, ll>>> out;
void init(ll lsz) {p.resize(lsz+5), sz.resize(lsz+5), in.resize(lsz+5), out.resize(lsz+5);for(int i=1; i<=lsz; i++) p[i] = i, sz[i] = 1;}
ll gpar(ll x) {return x==p[x] ? p[x]:p[x] = gpar(p[x]);}
void merge(ll x, ll y){
x=gpar(x), y=gpar(y);if(sz[x] < sz[y]) swap(x, y);
for(auto z:out[y]){
ans -= sz[z.fi];in[z.fi].erase(z.se);
e.push({z.se, z.fi});
}
ans += sz[y]*sz[x]*2;
ans += sz[y]*in[x].size();
ll wef;
for(auto z:in[y]){
wef = gpar(z);out[wef].erase({y, z});ans-=sz[y];e.push({z, y});
}
in[y].clear(), out[y].clear();
sz[x] += sz[y], p[y] = x;
}
ll add(ll x, ll y){
e.push({x, y});while(e.size()){
x=e.front().fi, y=e.front().se, e.pop();ll rx = gpar(x), ry = gpar(y);
if(rx == ry || in[ry].find(x) != in[ry].end()) continue;
auto f = out[ry].lower_bound({rx, 0});
if(f==out[ry].end() || f->fi!=rx){
ans += sz[ry];out[rx].insert({ry, x});in[ry].insert({x});
}else{
merge(rx, ry);
}
}
return ans;
}
};
ll n, m;
dsu ds;
int32_t main() {
#define int ll
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll a, b;cin >> n >> m;ds.init(n);
for(int i=1; i<=m; i++) cin >> a >> b, cout << ds.add(a, b) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
452 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 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 |
448 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
452 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 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 |
448 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
3 ms |
600 KB |
Output is correct |
35 |
Correct |
64 ms |
6228 KB |
Output is correct |
36 |
Correct |
72 ms |
8276 KB |
Output is correct |
37 |
Correct |
74 ms |
8276 KB |
Output is correct |
38 |
Correct |
79 ms |
8268 KB |
Output is correct |
39 |
Correct |
2 ms |
756 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
736 KB |
Output is correct |
47 |
Correct |
2 ms |
836 KB |
Output is correct |
48 |
Correct |
2 ms |
604 KB |
Output is correct |
49 |
Correct |
6 ms |
1368 KB |
Output is correct |
50 |
Correct |
78 ms |
8532 KB |
Output is correct |
51 |
Correct |
4 ms |
860 KB |
Output is correct |
52 |
Correct |
66 ms |
7008 KB |
Output is correct |
53 |
Correct |
6 ms |
1112 KB |
Output is correct |
54 |
Correct |
68 ms |
7544 KB |
Output is correct |
55 |
Correct |
5 ms |
1176 KB |
Output is correct |
56 |
Correct |
4 ms |
1116 KB |
Output is correct |
57 |
Correct |
3 ms |
1116 KB |
Output is correct |
58 |
Correct |
5 ms |
912 KB |
Output is correct |
59 |
Correct |
2 ms |
604 KB |
Output is correct |
60 |
Correct |
60 ms |
5660 KB |
Output is correct |
61 |
Correct |
3 ms |
860 KB |
Output is correct |
62 |
Correct |
70 ms |
8028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
452 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 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 |
448 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
3 ms |
600 KB |
Output is correct |
35 |
Correct |
64 ms |
6228 KB |
Output is correct |
36 |
Correct |
72 ms |
8276 KB |
Output is correct |
37 |
Correct |
74 ms |
8276 KB |
Output is correct |
38 |
Correct |
79 ms |
8268 KB |
Output is correct |
39 |
Correct |
2 ms |
756 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
736 KB |
Output is correct |
47 |
Correct |
2 ms |
836 KB |
Output is correct |
48 |
Correct |
2 ms |
604 KB |
Output is correct |
49 |
Correct |
6 ms |
1368 KB |
Output is correct |
50 |
Correct |
78 ms |
8532 KB |
Output is correct |
51 |
Correct |
4 ms |
860 KB |
Output is correct |
52 |
Correct |
66 ms |
7008 KB |
Output is correct |
53 |
Correct |
6 ms |
1112 KB |
Output is correct |
54 |
Correct |
68 ms |
7544 KB |
Output is correct |
55 |
Correct |
5 ms |
1176 KB |
Output is correct |
56 |
Correct |
4 ms |
1116 KB |
Output is correct |
57 |
Correct |
3 ms |
1116 KB |
Output is correct |
58 |
Correct |
5 ms |
912 KB |
Output is correct |
59 |
Correct |
2 ms |
604 KB |
Output is correct |
60 |
Correct |
60 ms |
5660 KB |
Output is correct |
61 |
Correct |
3 ms |
860 KB |
Output is correct |
62 |
Correct |
70 ms |
8028 KB |
Output is correct |
63 |
Correct |
289 ms |
49740 KB |
Output is correct |
64 |
Correct |
266 ms |
49700 KB |
Output is correct |
65 |
Correct |
275 ms |
49680 KB |
Output is correct |
66 |
Correct |
67 ms |
15988 KB |
Output is correct |
67 |
Correct |
102 ms |
20460 KB |
Output is correct |
68 |
Correct |
64 ms |
15700 KB |
Output is correct |
69 |
Correct |
146 ms |
21208 KB |
Output is correct |
70 |
Correct |
77 ms |
15700 KB |
Output is correct |
71 |
Correct |
64 ms |
15704 KB |
Output is correct |
72 |
Correct |
150 ms |
20500 KB |
Output is correct |
73 |
Correct |
117 ms |
20480 KB |
Output is correct |
74 |
Correct |
457 ms |
30044 KB |
Output is correct |
75 |
Correct |
213 ms |
26236 KB |
Output is correct |
76 |
Correct |
284 ms |
29680 KB |
Output is correct |
77 |
Correct |
280 ms |
29760 KB |
Output is correct |
78 |
Correct |
81 ms |
20048 KB |
Output is correct |
79 |
Correct |
156 ms |
23816 KB |
Output is correct |
80 |
Correct |
90 ms |
20048 KB |
Output is correct |
81 |
Correct |
155 ms |
23336 KB |
Output is correct |
82 |
Correct |
381 ms |
37336 KB |
Output is correct |
83 |
Correct |
330 ms |
37200 KB |
Output is correct |
84 |
Correct |
281 ms |
36928 KB |
Output is correct |
85 |
Correct |
259 ms |
36936 KB |
Output is correct |
86 |
Correct |
87 ms |
14888 KB |
Output is correct |
87 |
Correct |
101 ms |
17204 KB |
Output is correct |
88 |
Correct |
128 ms |
20500 KB |
Output is correct |
89 |
Correct |
315 ms |
29268 KB |
Output is correct |