# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
212001 | 2020-03-21T23:11:52 Z | sealnot123 | 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) | C++14 | 703 ms | 46328 KB |
#include<bits/stdc++.h> #define x first #define y second #define pb push_back #define eb emplace_back #define all(a) (a).begin(),(a).end() #define SZ(a) (int)(a).size() using namespace std; typedef pair<int,int> PII; typedef long long LL; typedef double DD; typedef long double LD; typedef pair<LL,LL> PLL; typedef pair<DD,DD> PDD; typedef vector<int> VI; typedef vector<LL> VL; const int N=100007; LL ans = 0; set< PII > II[N], OO[N]; set< PII >::iterator iit; int p[N], s[N]; int fin(int a){ if(a == p[a]) return a; return p[a] = fin(p[a]); } set<int> wait; LL val(int a){ return (LL)s[a]*(s[a]-1) + (LL)SZ(II[a])*s[a]; } void merge(int a, int b){ /*printf("kurwa\n");*/ /*printf("(%d,%d)\n",a,b);*/ /*printf("asdfdas %d\n",ans);*/ ans -= val(a)+val(b); if(SZ(II[a])+SZ(OO[a]) < SZ(OO[b])+SZ(II[b])) swap(a,b); for(PII e : II[b]){ if(e.x == a){ iit = OO[a].find(PII(b, e.y)); OO[a].erase(iit); continue; } iit = II[e.x].lower_bound(PII(a, 0)); if(iit != II[e.x].end() && iit->x == a){ wait.insert(e.x); } OO[e.x].insert(PII(a, e.y)); iit = OO[e.x].find(PII(b, e.y)); OO[e.x].erase(iit); II[a].insert(e); } /* printf("jazco\n");*/ for(PII e : OO[b]){ if(e.x == a){ iit = II[a].find(PII(b, e.y)); II[a].erase(iit); continue; } iit = OO[e.x].lower_bound(PII(a, 0)); if(iit != OO[e.x].end() && iit->x == a){ wait.insert(e.x); } II[e.x].insert(PII(a, e.y)); iit = II[e.x].find(PII(b, e.y)); II[e.x].erase(iit); OO[a].insert(e); } II[b].clear(); OO[b].clear(); /*printf("result %d(%d) :\n",a,s[a]+s[b]); printf("in : "); for(PII e : II[a]) printf("(%d,%d) ",e.x,e.y); printf("\nout : "); for(PII e : OO[a]) printf("(%d,%d) ",e.x,e.y); printf("\n");*/ p[b] = a; s[a] += s[b]; ans += val(a); if(wait.empty()) return; int x = *wait.begin(); wait.erase(wait.begin()); merge(a, x); } int main(){ int i,j,k,l,a,b,c,d; int n, m; scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ s[i] = 1; p[i] = i; } while(m--){ scanf("%d%d",&a,&b); c = a; a = fin(a); b = fin(b); if(a != b && OO[a].find(PII(b, c)) == OO[a].end()){ iit = II[a].lower_bound(PII(b, 0)); if(iit != II[a].end() && iit->x == b){ /*printf("must merge\n");*/ merge(a, b); }else{ OO[a].insert(PII(b, c)); II[b].insert(PII(a, c)); ans += s[b]; } } /*printf("XXXXXXXXXXXXXXXXXXXx");*/ printf("%lld\n",ans); } return 0; } /* 3 4 1 2 2 3 3 1 1 3 4 6 1 2 2 3 3 2 1 3 3 4 4 3 6 10 1 2 2 3 3 4 4 5 5 6 6 5 5 4 4 3 3 2 2 1 */
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 9728 KB | Output is correct |
2 | Correct | 9 ms | 9600 KB | Output is correct |
3 | Correct | 9 ms | 9728 KB | Output is correct |
4 | Correct | 9 ms | 9728 KB | Output is correct |
5 | Correct | 9 ms | 9728 KB | Output is correct |
6 | Correct | 9 ms | 9728 KB | Output is correct |
7 | Correct | 10 ms | 9728 KB | Output is correct |
8 | Correct | 10 ms | 9728 KB | Output is correct |
9 | Correct | 10 ms | 9728 KB | Output is correct |
10 | Correct | 9 ms | 9728 KB | Output is correct |
11 | Correct | 10 ms | 9728 KB | Output is correct |
12 | Correct | 9 ms | 9728 KB | Output is correct |
13 | Correct | 11 ms | 9728 KB | Output is correct |
14 | Correct | 9 ms | 9728 KB | Output is correct |
15 | Correct | 9 ms | 9728 KB | Output is correct |
16 | Correct | 10 ms | 9728 KB | Output is correct |
17 | Correct | 10 ms | 9728 KB | Output is correct |
18 | Correct | 10 ms | 9728 KB | Output is correct |
19 | Correct | 9 ms | 9728 KB | Output is correct |
20 | Correct | 10 ms | 9728 KB | Output is correct |
21 | Correct | 11 ms | 9728 KB | Output is correct |
22 | Correct | 10 ms | 9728 KB | Output is correct |
23 | Correct | 10 ms | 9728 KB | Output is correct |
24 | Correct | 10 ms | 9728 KB | Output is correct |
25 | Correct | 10 ms | 9728 KB | Output is correct |
26 | Correct | 10 ms | 9728 KB | Output is correct |
27 | Correct | 10 ms | 9728 KB | Output is correct |
28 | Correct | 9 ms | 9728 KB | Output is correct |
29 | Correct | 10 ms | 9728 KB | Output is correct |
30 | Correct | 9 ms | 9728 KB | Output is correct |
31 | Correct | 10 ms | 9728 KB | Output is correct |
32 | Correct | 10 ms | 9760 KB | Output is correct |
33 | Correct | 11 ms | 9728 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 9728 KB | Output is correct |
2 | Correct | 9 ms | 9600 KB | Output is correct |
3 | Correct | 9 ms | 9728 KB | Output is correct |
4 | Correct | 9 ms | 9728 KB | Output is correct |
5 | Correct | 9 ms | 9728 KB | Output is correct |
6 | Correct | 9 ms | 9728 KB | Output is correct |
7 | Correct | 10 ms | 9728 KB | Output is correct |
8 | Correct | 10 ms | 9728 KB | Output is correct |
9 | Correct | 10 ms | 9728 KB | Output is correct |
10 | Correct | 9 ms | 9728 KB | Output is correct |
11 | Correct | 10 ms | 9728 KB | Output is correct |
12 | Correct | 9 ms | 9728 KB | Output is correct |
13 | Correct | 11 ms | 9728 KB | Output is correct |
14 | Correct | 9 ms | 9728 KB | Output is correct |
15 | Correct | 9 ms | 9728 KB | Output is correct |
16 | Correct | 10 ms | 9728 KB | Output is correct |
17 | Correct | 10 ms | 9728 KB | Output is correct |
18 | Correct | 10 ms | 9728 KB | Output is correct |
19 | Correct | 9 ms | 9728 KB | Output is correct |
20 | Correct | 10 ms | 9728 KB | Output is correct |
21 | Correct | 11 ms | 9728 KB | Output is correct |
22 | Correct | 10 ms | 9728 KB | Output is correct |
23 | Correct | 10 ms | 9728 KB | Output is correct |
24 | Correct | 10 ms | 9728 KB | Output is correct |
25 | Correct | 10 ms | 9728 KB | Output is correct |
26 | Correct | 10 ms | 9728 KB | Output is correct |
27 | Correct | 10 ms | 9728 KB | Output is correct |
28 | Correct | 9 ms | 9728 KB | Output is correct |
29 | Correct | 10 ms | 9728 KB | Output is correct |
30 | Correct | 9 ms | 9728 KB | Output is correct |
31 | Correct | 10 ms | 9728 KB | Output is correct |
32 | Correct | 10 ms | 9760 KB | Output is correct |
33 | Correct | 11 ms | 9728 KB | Output is correct |
34 | Correct | 13 ms | 9856 KB | Output is correct |
35 | Correct | 129 ms | 12920 KB | Output is correct |
36 | Correct | 155 ms | 14688 KB | Output is correct |
37 | Correct | 162 ms | 14584 KB | Output is correct |
38 | Correct | 145 ms | 14456 KB | Output is correct |
39 | Correct | 12 ms | 9728 KB | Output is correct |
40 | Correct | 12 ms | 9856 KB | Output is correct |
41 | Correct | 12 ms | 9856 KB | Output is correct |
42 | Correct | 12 ms | 9728 KB | Output is correct |
43 | Correct | 12 ms | 9856 KB | Output is correct |
44 | Correct | 12 ms | 9856 KB | Output is correct |
45 | Correct | 11 ms | 9728 KB | Output is correct |
46 | Correct | 11 ms | 9728 KB | Output is correct |
47 | Correct | 12 ms | 9856 KB | Output is correct |
48 | Correct | 12 ms | 9856 KB | Output is correct |
49 | Correct | 20 ms | 10368 KB | Output is correct |
50 | Correct | 151 ms | 14712 KB | Output is correct |
51 | Correct | 18 ms | 9984 KB | Output is correct |
52 | Correct | 141 ms | 13688 KB | Output is correct |
53 | Correct | 20 ms | 10368 KB | Output is correct |
54 | Correct | 153 ms | 14200 KB | Output is correct |
55 | Correct | 15 ms | 10368 KB | Output is correct |
56 | Correct | 16 ms | 10368 KB | Output is correct |
57 | Correct | 14 ms | 10496 KB | Output is correct |
58 | Correct | 14 ms | 10496 KB | Output is correct |
59 | Correct | 12 ms | 9728 KB | Output is correct |
60 | Correct | 124 ms | 12152 KB | Output is correct |
61 | Correct | 17 ms | 9856 KB | Output is correct |
62 | Correct | 142 ms | 14328 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 9728 KB | Output is correct |
2 | Correct | 9 ms | 9600 KB | Output is correct |
3 | Correct | 9 ms | 9728 KB | Output is correct |
4 | Correct | 9 ms | 9728 KB | Output is correct |
5 | Correct | 9 ms | 9728 KB | Output is correct |
6 | Correct | 9 ms | 9728 KB | Output is correct |
7 | Correct | 10 ms | 9728 KB | Output is correct |
8 | Correct | 10 ms | 9728 KB | Output is correct |
9 | Correct | 10 ms | 9728 KB | Output is correct |
10 | Correct | 9 ms | 9728 KB | Output is correct |
11 | Correct | 10 ms | 9728 KB | Output is correct |
12 | Correct | 9 ms | 9728 KB | Output is correct |
13 | Correct | 11 ms | 9728 KB | Output is correct |
14 | Correct | 9 ms | 9728 KB | Output is correct |
15 | Correct | 9 ms | 9728 KB | Output is correct |
16 | Correct | 10 ms | 9728 KB | Output is correct |
17 | Correct | 10 ms | 9728 KB | Output is correct |
18 | Correct | 10 ms | 9728 KB | Output is correct |
19 | Correct | 9 ms | 9728 KB | Output is correct |
20 | Correct | 10 ms | 9728 KB | Output is correct |
21 | Correct | 11 ms | 9728 KB | Output is correct |
22 | Correct | 10 ms | 9728 KB | Output is correct |
23 | Correct | 10 ms | 9728 KB | Output is correct |
24 | Correct | 10 ms | 9728 KB | Output is correct |
25 | Correct | 10 ms | 9728 KB | Output is correct |
26 | Correct | 10 ms | 9728 KB | Output is correct |
27 | Correct | 10 ms | 9728 KB | Output is correct |
28 | Correct | 9 ms | 9728 KB | Output is correct |
29 | Correct | 10 ms | 9728 KB | Output is correct |
30 | Correct | 9 ms | 9728 KB | Output is correct |
31 | Correct | 10 ms | 9728 KB | Output is correct |
32 | Correct | 10 ms | 9760 KB | Output is correct |
33 | Correct | 11 ms | 9728 KB | Output is correct |
34 | Correct | 13 ms | 9856 KB | Output is correct |
35 | Correct | 129 ms | 12920 KB | Output is correct |
36 | Correct | 155 ms | 14688 KB | Output is correct |
37 | Correct | 162 ms | 14584 KB | Output is correct |
38 | Correct | 145 ms | 14456 KB | Output is correct |
39 | Correct | 12 ms | 9728 KB | Output is correct |
40 | Correct | 12 ms | 9856 KB | Output is correct |
41 | Correct | 12 ms | 9856 KB | Output is correct |
42 | Correct | 12 ms | 9728 KB | Output is correct |
43 | Correct | 12 ms | 9856 KB | Output is correct |
44 | Correct | 12 ms | 9856 KB | Output is correct |
45 | Correct | 11 ms | 9728 KB | Output is correct |
46 | Correct | 11 ms | 9728 KB | Output is correct |
47 | Correct | 12 ms | 9856 KB | Output is correct |
48 | Correct | 12 ms | 9856 KB | Output is correct |
49 | Correct | 20 ms | 10368 KB | Output is correct |
50 | Correct | 151 ms | 14712 KB | Output is correct |
51 | Correct | 18 ms | 9984 KB | Output is correct |
52 | Correct | 141 ms | 13688 KB | Output is correct |
53 | Correct | 20 ms | 10368 KB | Output is correct |
54 | Correct | 153 ms | 14200 KB | Output is correct |
55 | Correct | 15 ms | 10368 KB | Output is correct |
56 | Correct | 16 ms | 10368 KB | Output is correct |
57 | Correct | 14 ms | 10496 KB | Output is correct |
58 | Correct | 14 ms | 10496 KB | Output is correct |
59 | Correct | 12 ms | 9728 KB | Output is correct |
60 | Correct | 124 ms | 12152 KB | Output is correct |
61 | Correct | 17 ms | 9856 KB | Output is correct |
62 | Correct | 142 ms | 14328 KB | Output is correct |
63 | Correct | 466 ms | 40824 KB | Output is correct |
64 | Correct | 454 ms | 40696 KB | Output is correct |
65 | Correct | 449 ms | 40828 KB | Output is correct |
66 | Correct | 136 ms | 12664 KB | Output is correct |
67 | Correct | 210 ms | 16632 KB | Output is correct |
68 | Correct | 128 ms | 12664 KB | Output is correct |
69 | Correct | 251 ms | 17272 KB | Output is correct |
70 | Correct | 150 ms | 12664 KB | Output is correct |
71 | Correct | 150 ms | 12664 KB | Output is correct |
72 | Correct | 210 ms | 16632 KB | Output is correct |
73 | Correct | 211 ms | 16632 KB | Output is correct |
74 | Correct | 703 ms | 25080 KB | Output is correct |
75 | Correct | 402 ms | 20984 KB | Output is correct |
76 | Correct | 509 ms | 26268 KB | Output is correct |
77 | Correct | 511 ms | 26488 KB | Output is correct |
78 | Correct | 197 ms | 16632 KB | Output is correct |
79 | Correct | 323 ms | 18808 KB | Output is correct |
80 | Correct | 172 ms | 16632 KB | Output is correct |
81 | Correct | 265 ms | 18296 KB | Output is correct |
82 | Correct | 525 ms | 46200 KB | Output is correct |
83 | Correct | 543 ms | 46328 KB | Output is correct |
84 | Correct | 458 ms | 46328 KB | Output is correct |
85 | Correct | 450 ms | 46200 KB | Output is correct |
86 | Correct | 136 ms | 11868 KB | Output is correct |
87 | Correct | 177 ms | 12920 KB | Output is correct |
88 | Correct | 210 ms | 16632 KB | Output is correct |
89 | Correct | 511 ms | 23672 KB | Output is correct |