# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
756294 | 2023-06-11T12:30:43 Z | amin | Making Friends on Joitter is Fun (JOI20_joitter2) | C++14 | 1718 ms | 110068 KB |
#include <bits/stdc++.h> using namespace std; #define ll long long ll pa[100000]; ll ans=0; set<ll>in[100000],out[100000],pain[100000]; ll sz[100000]; ll par(ll x) { if(pa[x]==x) return x; pa[x]=par(pa[x]); return pa[x]; } void un(ll x,ll y) { x=par(x); y=par(y); if(x==y) { return ; } if(sz[x]<sz[y]) swap(x,y); ///dont forget the sz[x]+=sz[y];sz[y]=0; // pa[y]=x; ans-=sz[x]*in[x].size(); ans-=sz[y]*in[y].size(); for(auto i:in[y]) { /*if(in[x].find(i)!=in[x].end()) continue; */ in[x].insert(i); } for(auto i:pain[y]) pain[x].insert(par(i)); ans+=in[x].size()*(sz[x]+sz[y]); for(auto i:out[y]) { // if(out[x].find(i)!=out[x].end()) // continue; out[x].insert(i); pain[par(i)].erase(y); pain[par(i)].insert(x); } pa[y]=x; sz[x]+=sz[y]; sz[y]=0; for(auto i:in[y]) { if(pain[par(i)].find(x)!=pain[par(i)].end()) un(x,i); } for(auto i:out[y]) { if(pain[x].find(par(i))!=pain[x].end()) un(x,i); } } int main() { //os_base::sync_with_stdio(0); cout.tie(0); ll n; cin>>n; ll m; ans=n; for(ll i=0;i<n;i++) { sz[i]=1; pa[i]=i; in[i].insert(i); } cin>>m; while(m--) { ll x,y; cin>>x>>y; x--; y--; if(par(x)==par(y)) { // cout<<1<<' '; cout<<ans-n<<endl; continue; } if(pain[par(x)].find(par(y))!=pain[par(x)].end()) { //cout<<' '<<2<<' '; un(x,y); cout<<ans-n<<endl; continue; } if(in[par(y)].find(x)!=in[par(y)].end()) { //cout<<3<<' '; cout<<ans-n<<endl; continue; } in[par(y)].insert(x); out[par(x)].insert(y); pain[par(y)].insert(par(x)); ans+=sz[par(y)]; //cout<<4<<' '; cout<<ans-n<<endl; } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14320 KB | Output is correct |
2 | Correct | 10 ms | 14292 KB | Output is correct |
3 | Correct | 7 ms | 14292 KB | Output is correct |
4 | Correct | 7 ms | 14292 KB | Output is correct |
5 | Correct | 7 ms | 14292 KB | Output is correct |
6 | Correct | 9 ms | 14292 KB | Output is correct |
7 | Correct | 14 ms | 14420 KB | Output is correct |
8 | Correct | 13 ms | 14420 KB | Output is correct |
9 | Correct | 12 ms | 14380 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 9 ms | 14400 KB | Output is correct |
12 | Correct | 9 ms | 14420 KB | Output is correct |
13 | Correct | 10 ms | 14292 KB | Output is correct |
14 | Correct | 9 ms | 14420 KB | Output is correct |
15 | Correct | 8 ms | 14400 KB | Output is correct |
16 | Correct | 8 ms | 14372 KB | Output is correct |
17 | Correct | 8 ms | 14420 KB | Output is correct |
18 | Correct | 7 ms | 14332 KB | Output is correct |
19 | Correct | 7 ms | 14420 KB | Output is correct |
20 | Correct | 8 ms | 14420 KB | Output is correct |
21 | Correct | 12 ms | 14548 KB | Output is correct |
22 | Correct | 9 ms | 14396 KB | Output is correct |
23 | Correct | 9 ms | 14408 KB | Output is correct |
24 | Correct | 10 ms | 14400 KB | Output is correct |
25 | Correct | 10 ms | 14420 KB | Output is correct |
26 | Correct | 8 ms | 14420 KB | Output is correct |
27 | Correct | 9 ms | 14420 KB | Output is correct |
28 | Correct | 9 ms | 14420 KB | Output is correct |
29 | Correct | 8 ms | 14404 KB | Output is correct |
30 | Correct | 8 ms | 14408 KB | Output is correct |
31 | Correct | 12 ms | 14420 KB | Output is correct |
32 | Correct | 9 ms | 14420 KB | Output is correct |
33 | Correct | 12 ms | 14424 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14320 KB | Output is correct |
2 | Correct | 10 ms | 14292 KB | Output is correct |
3 | Correct | 7 ms | 14292 KB | Output is correct |
4 | Correct | 7 ms | 14292 KB | Output is correct |
5 | Correct | 7 ms | 14292 KB | Output is correct |
6 | Correct | 9 ms | 14292 KB | Output is correct |
7 | Correct | 14 ms | 14420 KB | Output is correct |
8 | Correct | 13 ms | 14420 KB | Output is correct |
9 | Correct | 12 ms | 14380 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 9 ms | 14400 KB | Output is correct |
12 | Correct | 9 ms | 14420 KB | Output is correct |
13 | Correct | 10 ms | 14292 KB | Output is correct |
14 | Correct | 9 ms | 14420 KB | Output is correct |
15 | Correct | 8 ms | 14400 KB | Output is correct |
16 | Correct | 8 ms | 14372 KB | Output is correct |
17 | Correct | 8 ms | 14420 KB | Output is correct |
18 | Correct | 7 ms | 14332 KB | Output is correct |
19 | Correct | 7 ms | 14420 KB | Output is correct |
20 | Correct | 8 ms | 14420 KB | Output is correct |
21 | Correct | 12 ms | 14548 KB | Output is correct |
22 | Correct | 9 ms | 14396 KB | Output is correct |
23 | Correct | 9 ms | 14408 KB | Output is correct |
24 | Correct | 10 ms | 14400 KB | Output is correct |
25 | Correct | 10 ms | 14420 KB | Output is correct |
26 | Correct | 8 ms | 14420 KB | Output is correct |
27 | Correct | 9 ms | 14420 KB | Output is correct |
28 | Correct | 9 ms | 14420 KB | Output is correct |
29 | Correct | 8 ms | 14404 KB | Output is correct |
30 | Correct | 8 ms | 14408 KB | Output is correct |
31 | Correct | 12 ms | 14420 KB | Output is correct |
32 | Correct | 9 ms | 14420 KB | Output is correct |
33 | Correct | 12 ms | 14424 KB | Output is correct |
34 | Correct | 23 ms | 14452 KB | Output is correct |
35 | Correct | 546 ms | 20324 KB | Output is correct |
36 | Correct | 575 ms | 23296 KB | Output is correct |
37 | Correct | 584 ms | 23456 KB | Output is correct |
38 | Correct | 559 ms | 23212 KB | Output is correct |
39 | Correct | 17 ms | 14932 KB | Output is correct |
40 | Correct | 20 ms | 15444 KB | Output is correct |
41 | Correct | 20 ms | 15444 KB | Output is correct |
42 | Correct | 17 ms | 14932 KB | Output is correct |
43 | Correct | 23 ms | 14876 KB | Output is correct |
44 | Correct | 16 ms | 14800 KB | Output is correct |
45 | Correct | 17 ms | 14932 KB | Output is correct |
46 | Correct | 16 ms | 14932 KB | Output is correct |
47 | Correct | 17 ms | 15096 KB | Output is correct |
48 | Correct | 18 ms | 15184 KB | Output is correct |
49 | Correct | 34 ms | 15952 KB | Output is correct |
50 | Correct | 580 ms | 23476 KB | Output is correct |
51 | Correct | 35 ms | 15180 KB | Output is correct |
52 | Correct | 545 ms | 21868 KB | Output is correct |
53 | Correct | 41 ms | 15828 KB | Output is correct |
54 | Correct | 530 ms | 22604 KB | Output is correct |
55 | Correct | 19 ms | 15280 KB | Output is correct |
56 | Correct | 18 ms | 15316 KB | Output is correct |
57 | Correct | 18 ms | 15568 KB | Output is correct |
58 | Correct | 18 ms | 15656 KB | Output is correct |
59 | Correct | 21 ms | 15660 KB | Output is correct |
60 | Correct | 563 ms | 20684 KB | Output is correct |
61 | Correct | 28 ms | 15184 KB | Output is correct |
62 | Correct | 539 ms | 22872 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14320 KB | Output is correct |
2 | Correct | 10 ms | 14292 KB | Output is correct |
3 | Correct | 7 ms | 14292 KB | Output is correct |
4 | Correct | 7 ms | 14292 KB | Output is correct |
5 | Correct | 7 ms | 14292 KB | Output is correct |
6 | Correct | 9 ms | 14292 KB | Output is correct |
7 | Correct | 14 ms | 14420 KB | Output is correct |
8 | Correct | 13 ms | 14420 KB | Output is correct |
9 | Correct | 12 ms | 14380 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 9 ms | 14400 KB | Output is correct |
12 | Correct | 9 ms | 14420 KB | Output is correct |
13 | Correct | 10 ms | 14292 KB | Output is correct |
14 | Correct | 9 ms | 14420 KB | Output is correct |
15 | Correct | 8 ms | 14400 KB | Output is correct |
16 | Correct | 8 ms | 14372 KB | Output is correct |
17 | Correct | 8 ms | 14420 KB | Output is correct |
18 | Correct | 7 ms | 14332 KB | Output is correct |
19 | Correct | 7 ms | 14420 KB | Output is correct |
20 | Correct | 8 ms | 14420 KB | Output is correct |
21 | Correct | 12 ms | 14548 KB | Output is correct |
22 | Correct | 9 ms | 14396 KB | Output is correct |
23 | Correct | 9 ms | 14408 KB | Output is correct |
24 | Correct | 10 ms | 14400 KB | Output is correct |
25 | Correct | 10 ms | 14420 KB | Output is correct |
26 | Correct | 8 ms | 14420 KB | Output is correct |
27 | Correct | 9 ms | 14420 KB | Output is correct |
28 | Correct | 9 ms | 14420 KB | Output is correct |
29 | Correct | 8 ms | 14404 KB | Output is correct |
30 | Correct | 8 ms | 14408 KB | Output is correct |
31 | Correct | 12 ms | 14420 KB | Output is correct |
32 | Correct | 9 ms | 14420 KB | Output is correct |
33 | Correct | 12 ms | 14424 KB | Output is correct |
34 | Correct | 23 ms | 14452 KB | Output is correct |
35 | Correct | 546 ms | 20324 KB | Output is correct |
36 | Correct | 575 ms | 23296 KB | Output is correct |
37 | Correct | 584 ms | 23456 KB | Output is correct |
38 | Correct | 559 ms | 23212 KB | Output is correct |
39 | Correct | 17 ms | 14932 KB | Output is correct |
40 | Correct | 20 ms | 15444 KB | Output is correct |
41 | Correct | 20 ms | 15444 KB | Output is correct |
42 | Correct | 17 ms | 14932 KB | Output is correct |
43 | Correct | 23 ms | 14876 KB | Output is correct |
44 | Correct | 16 ms | 14800 KB | Output is correct |
45 | Correct | 17 ms | 14932 KB | Output is correct |
46 | Correct | 16 ms | 14932 KB | Output is correct |
47 | Correct | 17 ms | 15096 KB | Output is correct |
48 | Correct | 18 ms | 15184 KB | Output is correct |
49 | Correct | 34 ms | 15952 KB | Output is correct |
50 | Correct | 580 ms | 23476 KB | Output is correct |
51 | Correct | 35 ms | 15180 KB | Output is correct |
52 | Correct | 545 ms | 21868 KB | Output is correct |
53 | Correct | 41 ms | 15828 KB | Output is correct |
54 | Correct | 530 ms | 22604 KB | Output is correct |
55 | Correct | 19 ms | 15280 KB | Output is correct |
56 | Correct | 18 ms | 15316 KB | Output is correct |
57 | Correct | 18 ms | 15568 KB | Output is correct |
58 | Correct | 18 ms | 15656 KB | Output is correct |
59 | Correct | 21 ms | 15660 KB | Output is correct |
60 | Correct | 563 ms | 20684 KB | Output is correct |
61 | Correct | 28 ms | 15184 KB | Output is correct |
62 | Correct | 539 ms | 22872 KB | Output is correct |
63 | Correct | 1048 ms | 68348 KB | Output is correct |
64 | Correct | 1037 ms | 68364 KB | Output is correct |
65 | Correct | 1048 ms | 68304 KB | Output is correct |
66 | Correct | 601 ms | 43836 KB | Output is correct |
67 | Correct | 922 ms | 86396 KB | Output is correct |
68 | Correct | 632 ms | 43808 KB | Output is correct |
69 | Correct | 802 ms | 39012 KB | Output is correct |
70 | Correct | 696 ms | 43852 KB | Output is correct |
71 | Correct | 815 ms | 43820 KB | Output is correct |
72 | Correct | 998 ms | 58344 KB | Output is correct |
73 | Correct | 978 ms | 60924 KB | Output is correct |
74 | Correct | 1718 ms | 85332 KB | Output is correct |
75 | Correct | 1250 ms | 47900 KB | Output is correct |
76 | Correct | 1453 ms | 65836 KB | Output is correct |
77 | Correct | 1384 ms | 66128 KB | Output is correct |
78 | Correct | 578 ms | 42928 KB | Output is correct |
79 | Correct | 1138 ms | 50824 KB | Output is correct |
80 | Correct | 544 ms | 40408 KB | Output is correct |
81 | Correct | 1004 ms | 44552 KB | Output is correct |
82 | Correct | 1094 ms | 61752 KB | Output is correct |
83 | Correct | 1175 ms | 61708 KB | Output is correct |
84 | Correct | 1047 ms | 77468 KB | Output is correct |
85 | Correct | 1012 ms | 77440 KB | Output is correct |
86 | Correct | 880 ms | 107872 KB | Output is correct |
87 | Correct | 1044 ms | 110068 KB | Output is correct |
88 | Correct | 908 ms | 59708 KB | Output is correct |
89 | Correct | 1348 ms | 63724 KB | Output is correct |