# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
389760 | 2021-04-14T13:00:24 Z | ogibogi2004 | 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) | C++14 | 1491 ms | 73796 KB |
#include<bits/stdc++.h> using namespace std; #define ll long long const int MAXN=1e5+6; int n,m; int par[MAXN]; ll sz[MAXN]; set<int>edges_in[MAXN]; set<int>edges_out[MAXN]; set<int>comp[MAXN]; ll ans=0; int getRoot(int u) { if(u==par[u])return u; return par[u]=getRoot(par[u]); } ll cnt(int u) { return sz[u]*(sz[u]-1ll)+edges_in[u].size()*sz[u]; } stack<pair<int,int> >merges; void merge(int a,int b) { a=getRoot(a);b=getRoot(b); if(a==b)return; if(comp[b].size()+edges_in[b].size()+edges_out[b].size()>comp[a].size()+edges_in[a].size()+edges_out[a].size())swap(a,b); for(auto xd:edges_in[b]) { int t=getRoot(xd); if(t==a) { continue; } edges_out[t].erase(b); edges_out[t].insert(a); edges_in[a].insert(xd); if(edges_out[a].find(t)!=edges_out[a].end()) { merges.push({xd,a}); } } for(auto xd:edges_out[b]) { int t=getRoot(xd); if(xd==a) { continue; } edges_out[a].insert(xd); //edges_in[t].erase(b); //edges_in[t].insert(a); if(edges_out[xd].find(a)!=edges_out[xd].end()) { merges.push({xd,a}); } } edges_in[a].erase(b); edges_out[a].erase(b); for(auto xd:comp[b]) { edges_in[a].erase(xd); comp[a].insert(xd); } par[b]=a; sz[a]+=sz[b]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n>>m; for(int i=1;i<=n;i++) { par[i]=i;comp[i].insert(i); sz[i]=1; } for(int i=0;i<m;i++) { int a,b,a1,b1; cin>>a>>b;a1=a;b1=b; a=getRoot(a); b=getRoot(b); if(a==b) { cout<<ans<<endl; continue; } else { if(edges_out[b].find(a)!=edges_out[b].end()) { merges.push({a,b}); ans-=cnt(a);ans-=cnt(b); edges_in[b].insert(a1); edges_out[a].insert(b); ans+=cnt(a);ans+=cnt(b); int x,y; while(!merges.empty()) { tie(x,y)=merges.top();merges.pop(); x=getRoot(x);y=getRoot(y); if(x!=y) { ans-=cnt(x); ans-=cnt(y); merge(x,y); ans+=cnt(getRoot(x)); } //ans+=cnt(getRoot(y)); } } else if(edges_out[b].find(a)==edges_out[b].end()) { ans-=cnt(b); edges_in[b].insert(a1); edges_out[a].insert(b); ans+=cnt(b); } /*for(int j=1;j<=n;j++) { cout<<getRoot(j)<<" "; } cout<<endl; for(int j=1;j<=n;j++) { cout<<cnt(getRoot(j))<<" "; } cout<<endl;*/ cout<<ans<<endl; } /*for(int j=1;j<=n;j++)cout<<getRoot(j)<<" "; cout<<endl; for(int j=1;j<=n;j++)cout<<cnt(getRoot(j))<<" "; cout<<endl;*/ /*cout<<"**\n"; for(int j=1;j<=n;j++) { cout<<getRoot(j)<<":\n"; for(auto xd:edges_in[getRoot(j)]) { cout<<xd<<" "; } cout<<endl; }*/ } return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 14412 KB | Output is correct |
2 | Correct | 10 ms | 14392 KB | Output is correct |
3 | Correct | 9 ms | 14308 KB | Output is correct |
4 | Correct | 9 ms | 14412 KB | Output is correct |
5 | Correct | 9 ms | 14412 KB | Output is correct |
6 | Correct | 10 ms | 14412 KB | Output is correct |
7 | Correct | 18 ms | 14412 KB | Output is correct |
8 | Correct | 14 ms | 14448 KB | Output is correct |
9 | Correct | 14 ms | 14412 KB | Output is correct |
10 | Correct | 10 ms | 14412 KB | Output is correct |
11 | Correct | 10 ms | 14412 KB | Output is correct |
12 | Correct | 9 ms | 14412 KB | Output is correct |
13 | Correct | 9 ms | 14412 KB | Output is correct |
14 | Correct | 10 ms | 14340 KB | Output is correct |
15 | Correct | 9 ms | 14416 KB | Output is correct |
16 | Correct | 10 ms | 14428 KB | Output is correct |
17 | Correct | 9 ms | 14412 KB | Output is correct |
18 | Correct | 10 ms | 14412 KB | Output is correct |
19 | Correct | 10 ms | 14412 KB | Output is correct |
20 | Correct | 10 ms | 14412 KB | Output is correct |
21 | Correct | 14 ms | 14412 KB | Output is correct |
22 | Correct | 10 ms | 14412 KB | Output is correct |
23 | Correct | 11 ms | 14444 KB | Output is correct |
24 | Correct | 10 ms | 14412 KB | Output is correct |
25 | Correct | 13 ms | 14412 KB | Output is correct |
26 | Correct | 10 ms | 14412 KB | Output is correct |
27 | Correct | 9 ms | 14416 KB | Output is correct |
28 | Correct | 10 ms | 14412 KB | Output is correct |
29 | Correct | 10 ms | 14412 KB | Output is correct |
30 | Correct | 10 ms | 14416 KB | Output is correct |
31 | Correct | 13 ms | 14432 KB | Output is correct |
32 | Correct | 11 ms | 14388 KB | Output is correct |
33 | Correct | 13 ms | 14440 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 14412 KB | Output is correct |
2 | Correct | 10 ms | 14392 KB | Output is correct |
3 | Correct | 9 ms | 14308 KB | Output is correct |
4 | Correct | 9 ms | 14412 KB | Output is correct |
5 | Correct | 9 ms | 14412 KB | Output is correct |
6 | Correct | 10 ms | 14412 KB | Output is correct |
7 | Correct | 18 ms | 14412 KB | Output is correct |
8 | Correct | 14 ms | 14448 KB | Output is correct |
9 | Correct | 14 ms | 14412 KB | Output is correct |
10 | Correct | 10 ms | 14412 KB | Output is correct |
11 | Correct | 10 ms | 14412 KB | Output is correct |
12 | Correct | 9 ms | 14412 KB | Output is correct |
13 | Correct | 9 ms | 14412 KB | Output is correct |
14 | Correct | 10 ms | 14340 KB | Output is correct |
15 | Correct | 9 ms | 14416 KB | Output is correct |
16 | Correct | 10 ms | 14428 KB | Output is correct |
17 | Correct | 9 ms | 14412 KB | Output is correct |
18 | Correct | 10 ms | 14412 KB | Output is correct |
19 | Correct | 10 ms | 14412 KB | Output is correct |
20 | Correct | 10 ms | 14412 KB | Output is correct |
21 | Correct | 14 ms | 14412 KB | Output is correct |
22 | Correct | 10 ms | 14412 KB | Output is correct |
23 | Correct | 11 ms | 14444 KB | Output is correct |
24 | Correct | 10 ms | 14412 KB | Output is correct |
25 | Correct | 13 ms | 14412 KB | Output is correct |
26 | Correct | 10 ms | 14412 KB | Output is correct |
27 | Correct | 9 ms | 14416 KB | Output is correct |
28 | Correct | 10 ms | 14412 KB | Output is correct |
29 | Correct | 10 ms | 14412 KB | Output is correct |
30 | Correct | 10 ms | 14416 KB | Output is correct |
31 | Correct | 13 ms | 14432 KB | Output is correct |
32 | Correct | 11 ms | 14388 KB | Output is correct |
33 | Correct | 13 ms | 14440 KB | Output is correct |
34 | Correct | 27 ms | 14412 KB | Output is correct |
35 | Correct | 543 ms | 17540 KB | Output is correct |
36 | Correct | 581 ms | 19328 KB | Output is correct |
37 | Correct | 614 ms | 19460 KB | Output is correct |
38 | Correct | 576 ms | 19220 KB | Output is correct |
39 | Correct | 19 ms | 14800 KB | Output is correct |
40 | Correct | 20 ms | 15180 KB | Output is correct |
41 | Correct | 20 ms | 15104 KB | Output is correct |
42 | Correct | 19 ms | 14788 KB | Output is correct |
43 | Correct | 21 ms | 14780 KB | Output is correct |
44 | Correct | 21 ms | 14796 KB | Output is correct |
45 | Correct | 18 ms | 14828 KB | Output is correct |
46 | Correct | 19 ms | 14796 KB | Output is correct |
47 | Correct | 20 ms | 14940 KB | Output is correct |
48 | Correct | 21 ms | 14924 KB | Output is correct |
49 | Correct | 35 ms | 15308 KB | Output is correct |
50 | Correct | 578 ms | 19488 KB | Output is correct |
51 | Correct | 33 ms | 14948 KB | Output is correct |
52 | Correct | 559 ms | 18208 KB | Output is correct |
53 | Correct | 35 ms | 15272 KB | Output is correct |
54 | Correct | 590 ms | 18832 KB | Output is correct |
55 | Correct | 20 ms | 14992 KB | Output is correct |
56 | Correct | 21 ms | 14924 KB | Output is correct |
57 | Correct | 24 ms | 14980 KB | Output is correct |
58 | Correct | 21 ms | 14972 KB | Output is correct |
59 | Correct | 21 ms | 15180 KB | Output is correct |
60 | Correct | 540 ms | 17576 KB | Output is correct |
61 | Correct | 22 ms | 14912 KB | Output is correct |
62 | Correct | 598 ms | 19152 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 14412 KB | Output is correct |
2 | Correct | 10 ms | 14392 KB | Output is correct |
3 | Correct | 9 ms | 14308 KB | Output is correct |
4 | Correct | 9 ms | 14412 KB | Output is correct |
5 | Correct | 9 ms | 14412 KB | Output is correct |
6 | Correct | 10 ms | 14412 KB | Output is correct |
7 | Correct | 18 ms | 14412 KB | Output is correct |
8 | Correct | 14 ms | 14448 KB | Output is correct |
9 | Correct | 14 ms | 14412 KB | Output is correct |
10 | Correct | 10 ms | 14412 KB | Output is correct |
11 | Correct | 10 ms | 14412 KB | Output is correct |
12 | Correct | 9 ms | 14412 KB | Output is correct |
13 | Correct | 9 ms | 14412 KB | Output is correct |
14 | Correct | 10 ms | 14340 KB | Output is correct |
15 | Correct | 9 ms | 14416 KB | Output is correct |
16 | Correct | 10 ms | 14428 KB | Output is correct |
17 | Correct | 9 ms | 14412 KB | Output is correct |
18 | Correct | 10 ms | 14412 KB | Output is correct |
19 | Correct | 10 ms | 14412 KB | Output is correct |
20 | Correct | 10 ms | 14412 KB | Output is correct |
21 | Correct | 14 ms | 14412 KB | Output is correct |
22 | Correct | 10 ms | 14412 KB | Output is correct |
23 | Correct | 11 ms | 14444 KB | Output is correct |
24 | Correct | 10 ms | 14412 KB | Output is correct |
25 | Correct | 13 ms | 14412 KB | Output is correct |
26 | Correct | 10 ms | 14412 KB | Output is correct |
27 | Correct | 9 ms | 14416 KB | Output is correct |
28 | Correct | 10 ms | 14412 KB | Output is correct |
29 | Correct | 10 ms | 14412 KB | Output is correct |
30 | Correct | 10 ms | 14416 KB | Output is correct |
31 | Correct | 13 ms | 14432 KB | Output is correct |
32 | Correct | 11 ms | 14388 KB | Output is correct |
33 | Correct | 13 ms | 14440 KB | Output is correct |
34 | Correct | 27 ms | 14412 KB | Output is correct |
35 | Correct | 543 ms | 17540 KB | Output is correct |
36 | Correct | 581 ms | 19328 KB | Output is correct |
37 | Correct | 614 ms | 19460 KB | Output is correct |
38 | Correct | 576 ms | 19220 KB | Output is correct |
39 | Correct | 19 ms | 14800 KB | Output is correct |
40 | Correct | 20 ms | 15180 KB | Output is correct |
41 | Correct | 20 ms | 15104 KB | Output is correct |
42 | Correct | 19 ms | 14788 KB | Output is correct |
43 | Correct | 21 ms | 14780 KB | Output is correct |
44 | Correct | 21 ms | 14796 KB | Output is correct |
45 | Correct | 18 ms | 14828 KB | Output is correct |
46 | Correct | 19 ms | 14796 KB | Output is correct |
47 | Correct | 20 ms | 14940 KB | Output is correct |
48 | Correct | 21 ms | 14924 KB | Output is correct |
49 | Correct | 35 ms | 15308 KB | Output is correct |
50 | Correct | 578 ms | 19488 KB | Output is correct |
51 | Correct | 33 ms | 14948 KB | Output is correct |
52 | Correct | 559 ms | 18208 KB | Output is correct |
53 | Correct | 35 ms | 15272 KB | Output is correct |
54 | Correct | 590 ms | 18832 KB | Output is correct |
55 | Correct | 20 ms | 14992 KB | Output is correct |
56 | Correct | 21 ms | 14924 KB | Output is correct |
57 | Correct | 24 ms | 14980 KB | Output is correct |
58 | Correct | 21 ms | 14972 KB | Output is correct |
59 | Correct | 21 ms | 15180 KB | Output is correct |
60 | Correct | 540 ms | 17576 KB | Output is correct |
61 | Correct | 22 ms | 14912 KB | Output is correct |
62 | Correct | 598 ms | 19152 KB | Output is correct |
63 | Correct | 931 ms | 53380 KB | Output is correct |
64 | Correct | 949 ms | 53852 KB | Output is correct |
65 | Correct | 942 ms | 54064 KB | Output is correct |
66 | Correct | 670 ms | 38848 KB | Output is correct |
67 | Correct | 808 ms | 64252 KB | Output is correct |
68 | Correct | 606 ms | 38732 KB | Output is correct |
69 | Correct | 933 ms | 38660 KB | Output is correct |
70 | Correct | 609 ms | 38780 KB | Output is correct |
71 | Correct | 612 ms | 38800 KB | Output is correct |
72 | Correct | 842 ms | 48324 KB | Output is correct |
73 | Correct | 850 ms | 49980 KB | Output is correct |
74 | Correct | 1491 ms | 64660 KB | Output is correct |
75 | Correct | 1286 ms | 43868 KB | Output is correct |
76 | Correct | 1186 ms | 52540 KB | Output is correct |
77 | Correct | 1235 ms | 52764 KB | Output is correct |
78 | Correct | 527 ms | 36548 KB | Output is correct |
79 | Correct | 993 ms | 43852 KB | Output is correct |
80 | Correct | 519 ms | 36492 KB | Output is correct |
81 | Correct | 991 ms | 42316 KB | Output is correct |
82 | Correct | 942 ms | 47260 KB | Output is correct |
83 | Correct | 923 ms | 47252 KB | Output is correct |
84 | Correct | 841 ms | 47424 KB | Output is correct |
85 | Correct | 857 ms | 47276 KB | Output is correct |
86 | Correct | 688 ms | 71504 KB | Output is correct |
87 | Correct | 926 ms | 73796 KB | Output is correct |
88 | Correct | 801 ms | 49212 KB | Output is correct |
89 | Correct | 1216 ms | 51700 KB | Output is correct |