# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
377181 | 2021-03-13T08:40:00 Z | nafis_shifat | 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) | C++14 | 1635 ms | 125812 KB |
#include<bits/stdc++.h> #define ll long long #define pii pair<int,int> #define f first #define s second using namespace std; const int mxn=1e5+5; const int inf=1e9; int parent[mxn]; int sz[mxn]; set<int> adj[mxn]; set<int> in[mxn]; vector<int> nodes[mxn]; int sz2[mxn][2]; set<int> eds[mxn][2]; set<int> reps[mxn]; set<int> ireps[mxn]; ll ans = 0; int findrep(int u) { return parent[u] == u ? u : parent[u] = findrep(parent[u]); } void unite(int u,int v) { int U = findrep(u); int V = findrep(v); if(U == V) return; if(adj[V].count(U)) { ans += 2 * sz[U] * sz[V]; if(sz[U] + sz2[U][0] + sz2[U][1] > sz[V] + sz2[V][0] + sz2[V][1]) swap(U,V); vector<int> nod1,nod2; for(int i : nodes[U]) { auto it1 = eds[i][0].begin(); while(it1 != eds[i][0].end()) { int x = *it1; int X = findrep(x); if(X == V) { ans -= sz[V]; in[V].erase(U); adj[U].erase(V); eds[x][1].erase(i); sz2[X][1]--; it1 = eds[i][0].erase(it1); sz2[U][0]--; } else if(in[V].count(X)) { nod1.push_back(X); in[X].erase(U); eds[x][1].erase(i); sz2[X][1]--; ans -= sz[X]; it1 = eds[i][0].erase(it1); sz2[U][0]--; } else it1++; } auto it2 = eds[i][1].begin(); while(it2 != eds[i][1].end()) { int x = *it2; int X = findrep(x); if(X == V) { ans -= sz[U]; adj[V].erase(U); in[U].erase(V); eds[x][0].erase(i); sz2[X][0]--; it2 = eds[i][1].erase(it2); sz2[U][1]--; } else if(adj[V].count(X)) { nod2.push_back(X); adj[X].erase(U); eds[x][0].erase(i); sz2[X][0]--; ans -= sz[U]; it2 = eds[i][1].erase(it2); sz2[U][1]--; } else if(ireps[V].count(x)) { sz2[U][1]--; reps[x].erase(U); eds[x][0].erase(i); sz2[X][0]--; it2 = eds[i][1].erase(it2); ans -= sz[U]; } else it2++; } } while(!ireps[U].empty()) { auto it = ireps[U].begin(); ireps[V].insert(*it); reps[*it].erase(U); reps[*it].insert(V); ireps[U].erase(it); } ans += sz2[U][1] * sz[V]; ans += sz2[V][1] * sz[U]; parent[U] = V; sz[V] += sz[U]; sz2[V][0] += sz2[U][0]; sz2[V][1] += sz2[U][1]; while(!nodes[U].empty()) { int x = nodes[U].back(); nodes[U].pop_back(); nodes[V].push_back(x); } while(!adj[U].empty()) { int x = *adj[U].begin(); in[x].erase(U); adj[U].erase(x); adj[V].insert(x); in[x].insert(V); } while(!in[U].empty()) { int x = *in[U].begin(); in[U].erase(x); adj[x].erase(U); in[V].insert(x); adj[x].insert(V); } for(int x : nod1) { unite(V,x); } for(int x : nod2) { unite(x,V); } } else { if(reps[u].count(V)) return; ans += sz[V]; eds[u][0].insert(v); eds[v][1].insert(u); adj[U].insert(V); in[V].insert(U); reps[u].insert(V); ireps[V].insert(u); sz2[U][0]++; sz2[V][1]++; } } int main() { int n,m; cin >> n >> m; for(int i = 1; i <= n; i++) { parent[i] = i; sz[i] = 1; sz2[i][0] = sz2[i][1] = 0; nodes[i].push_back(i); } for(int i = 1; i <= m; i++) { int u,v; scanf("%d%d",&u,&v); unite(u,v); cout<<ans<<endl; } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 30828 KB | Output is correct |
2 | Correct | 24 ms | 30828 KB | Output is correct |
3 | Correct | 20 ms | 30828 KB | Output is correct |
4 | Correct | 22 ms | 30956 KB | Output is correct |
5 | Correct | 21 ms | 30828 KB | Output is correct |
6 | Correct | 22 ms | 31104 KB | Output is correct |
7 | Correct | 31 ms | 30956 KB | Output is correct |
8 | Correct | 30 ms | 30956 KB | Output is correct |
9 | Correct | 28 ms | 30956 KB | Output is correct |
10 | Correct | 21 ms | 30828 KB | Output is correct |
11 | Correct | 21 ms | 30828 KB | Output is correct |
12 | Correct | 21 ms | 30956 KB | Output is correct |
13 | Correct | 24 ms | 30828 KB | Output is correct |
14 | Correct | 21 ms | 30828 KB | Output is correct |
15 | Correct | 22 ms | 30924 KB | Output is correct |
16 | Correct | 24 ms | 30828 KB | Output is correct |
17 | Correct | 20 ms | 30828 KB | Output is correct |
18 | Correct | 21 ms | 30828 KB | Output is correct |
19 | Correct | 22 ms | 30828 KB | Output is correct |
20 | Correct | 21 ms | 30956 KB | Output is correct |
21 | Correct | 27 ms | 30956 KB | Output is correct |
22 | Correct | 22 ms | 30828 KB | Output is correct |
23 | Correct | 27 ms | 30956 KB | Output is correct |
24 | Correct | 25 ms | 30956 KB | Output is correct |
25 | Correct | 30 ms | 30956 KB | Output is correct |
26 | Correct | 21 ms | 30956 KB | Output is correct |
27 | Correct | 21 ms | 30956 KB | Output is correct |
28 | Correct | 21 ms | 30956 KB | Output is correct |
29 | Correct | 20 ms | 30956 KB | Output is correct |
30 | Correct | 24 ms | 30828 KB | Output is correct |
31 | Correct | 30 ms | 30956 KB | Output is correct |
32 | Correct | 20 ms | 30976 KB | Output is correct |
33 | Correct | 26 ms | 30956 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 30828 KB | Output is correct |
2 | Correct | 24 ms | 30828 KB | Output is correct |
3 | Correct | 20 ms | 30828 KB | Output is correct |
4 | Correct | 22 ms | 30956 KB | Output is correct |
5 | Correct | 21 ms | 30828 KB | Output is correct |
6 | Correct | 22 ms | 31104 KB | Output is correct |
7 | Correct | 31 ms | 30956 KB | Output is correct |
8 | Correct | 30 ms | 30956 KB | Output is correct |
9 | Correct | 28 ms | 30956 KB | Output is correct |
10 | Correct | 21 ms | 30828 KB | Output is correct |
11 | Correct | 21 ms | 30828 KB | Output is correct |
12 | Correct | 21 ms | 30956 KB | Output is correct |
13 | Correct | 24 ms | 30828 KB | Output is correct |
14 | Correct | 21 ms | 30828 KB | Output is correct |
15 | Correct | 22 ms | 30924 KB | Output is correct |
16 | Correct | 24 ms | 30828 KB | Output is correct |
17 | Correct | 20 ms | 30828 KB | Output is correct |
18 | Correct | 21 ms | 30828 KB | Output is correct |
19 | Correct | 22 ms | 30828 KB | Output is correct |
20 | Correct | 21 ms | 30956 KB | Output is correct |
21 | Correct | 27 ms | 30956 KB | Output is correct |
22 | Correct | 22 ms | 30828 KB | Output is correct |
23 | Correct | 27 ms | 30956 KB | Output is correct |
24 | Correct | 25 ms | 30956 KB | Output is correct |
25 | Correct | 30 ms | 30956 KB | Output is correct |
26 | Correct | 21 ms | 30956 KB | Output is correct |
27 | Correct | 21 ms | 30956 KB | Output is correct |
28 | Correct | 21 ms | 30956 KB | Output is correct |
29 | Correct | 20 ms | 30956 KB | Output is correct |
30 | Correct | 24 ms | 30828 KB | Output is correct |
31 | Correct | 30 ms | 30956 KB | Output is correct |
32 | Correct | 20 ms | 30976 KB | Output is correct |
33 | Correct | 26 ms | 30956 KB | Output is correct |
34 | Correct | 47 ms | 31084 KB | Output is correct |
35 | Correct | 833 ms | 38112 KB | Output is correct |
36 | Correct | 874 ms | 42976 KB | Output is correct |
37 | Correct | 886 ms | 43116 KB | Output is correct |
38 | Correct | 896 ms | 42416 KB | Output is correct |
39 | Correct | 34 ms | 31212 KB | Output is correct |
40 | Correct | 35 ms | 31468 KB | Output is correct |
41 | Correct | 37 ms | 31468 KB | Output is correct |
42 | Correct | 35 ms | 31084 KB | Output is correct |
43 | Correct | 35 ms | 31468 KB | Output is correct |
44 | Correct | 36 ms | 31340 KB | Output is correct |
45 | Correct | 33 ms | 31084 KB | Output is correct |
46 | Correct | 35 ms | 31084 KB | Output is correct |
47 | Correct | 34 ms | 31340 KB | Output is correct |
48 | Correct | 37 ms | 31340 KB | Output is correct |
49 | Correct | 65 ms | 32748 KB | Output is correct |
50 | Correct | 909 ms | 43392 KB | Output is correct |
51 | Correct | 55 ms | 31852 KB | Output is correct |
52 | Correct | 830 ms | 40044 KB | Output is correct |
53 | Correct | 61 ms | 32620 KB | Output is correct |
54 | Correct | 859 ms | 41580 KB | Output is correct |
55 | Correct | 46 ms | 32236 KB | Output is correct |
56 | Correct | 43 ms | 32236 KB | Output is correct |
57 | Correct | 39 ms | 32492 KB | Output is correct |
58 | Correct | 39 ms | 32492 KB | Output is correct |
59 | Correct | 35 ms | 31212 KB | Output is correct |
60 | Correct | 837 ms | 36128 KB | Output is correct |
61 | Correct | 38 ms | 31616 KB | Output is correct |
62 | Correct | 885 ms | 42092 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 30828 KB | Output is correct |
2 | Correct | 24 ms | 30828 KB | Output is correct |
3 | Correct | 20 ms | 30828 KB | Output is correct |
4 | Correct | 22 ms | 30956 KB | Output is correct |
5 | Correct | 21 ms | 30828 KB | Output is correct |
6 | Correct | 22 ms | 31104 KB | Output is correct |
7 | Correct | 31 ms | 30956 KB | Output is correct |
8 | Correct | 30 ms | 30956 KB | Output is correct |
9 | Correct | 28 ms | 30956 KB | Output is correct |
10 | Correct | 21 ms | 30828 KB | Output is correct |
11 | Correct | 21 ms | 30828 KB | Output is correct |
12 | Correct | 21 ms | 30956 KB | Output is correct |
13 | Correct | 24 ms | 30828 KB | Output is correct |
14 | Correct | 21 ms | 30828 KB | Output is correct |
15 | Correct | 22 ms | 30924 KB | Output is correct |
16 | Correct | 24 ms | 30828 KB | Output is correct |
17 | Correct | 20 ms | 30828 KB | Output is correct |
18 | Correct | 21 ms | 30828 KB | Output is correct |
19 | Correct | 22 ms | 30828 KB | Output is correct |
20 | Correct | 21 ms | 30956 KB | Output is correct |
21 | Correct | 27 ms | 30956 KB | Output is correct |
22 | Correct | 22 ms | 30828 KB | Output is correct |
23 | Correct | 27 ms | 30956 KB | Output is correct |
24 | Correct | 25 ms | 30956 KB | Output is correct |
25 | Correct | 30 ms | 30956 KB | Output is correct |
26 | Correct | 21 ms | 30956 KB | Output is correct |
27 | Correct | 21 ms | 30956 KB | Output is correct |
28 | Correct | 21 ms | 30956 KB | Output is correct |
29 | Correct | 20 ms | 30956 KB | Output is correct |
30 | Correct | 24 ms | 30828 KB | Output is correct |
31 | Correct | 30 ms | 30956 KB | Output is correct |
32 | Correct | 20 ms | 30976 KB | Output is correct |
33 | Correct | 26 ms | 30956 KB | Output is correct |
34 | Correct | 47 ms | 31084 KB | Output is correct |
35 | Correct | 833 ms | 38112 KB | Output is correct |
36 | Correct | 874 ms | 42976 KB | Output is correct |
37 | Correct | 886 ms | 43116 KB | Output is correct |
38 | Correct | 896 ms | 42416 KB | Output is correct |
39 | Correct | 34 ms | 31212 KB | Output is correct |
40 | Correct | 35 ms | 31468 KB | Output is correct |
41 | Correct | 37 ms | 31468 KB | Output is correct |
42 | Correct | 35 ms | 31084 KB | Output is correct |
43 | Correct | 35 ms | 31468 KB | Output is correct |
44 | Correct | 36 ms | 31340 KB | Output is correct |
45 | Correct | 33 ms | 31084 KB | Output is correct |
46 | Correct | 35 ms | 31084 KB | Output is correct |
47 | Correct | 34 ms | 31340 KB | Output is correct |
48 | Correct | 37 ms | 31340 KB | Output is correct |
49 | Correct | 65 ms | 32748 KB | Output is correct |
50 | Correct | 909 ms | 43392 KB | Output is correct |
51 | Correct | 55 ms | 31852 KB | Output is correct |
52 | Correct | 830 ms | 40044 KB | Output is correct |
53 | Correct | 61 ms | 32620 KB | Output is correct |
54 | Correct | 859 ms | 41580 KB | Output is correct |
55 | Correct | 46 ms | 32236 KB | Output is correct |
56 | Correct | 43 ms | 32236 KB | Output is correct |
57 | Correct | 39 ms | 32492 KB | Output is correct |
58 | Correct | 39 ms | 32492 KB | Output is correct |
59 | Correct | 35 ms | 31212 KB | Output is correct |
60 | Correct | 837 ms | 36128 KB | Output is correct |
61 | Correct | 38 ms | 31616 KB | Output is correct |
62 | Correct | 885 ms | 42092 KB | Output is correct |
63 | Correct | 1635 ms | 125524 KB | Output is correct |
64 | Correct | 1602 ms | 125548 KB | Output is correct |
65 | Correct | 1602 ms | 125812 KB | Output is correct |
66 | Correct | 850 ms | 49920 KB | Output is correct |
67 | Incorrect | 1268 ms | 58596 KB | Output isn't correct |
68 | Halted | 0 ms | 0 KB | - |