답안 #211897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211897 2020-03-21T15:52:10 Z Akashi 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1349 ms 79352 KB
#include <bits/stdc++.h>
using namespace std;

int n, m;
int TT[100001], SZ[100001];
set <int> in[100001], out[100001], bridge[100001], elem[100001];

long long Sol = 0;
inline int find(int x){
    if(x != TT[x]) return (TT[x] = find(TT[x]));
    return x;
}

inline void unite(int x, int y){
    if(x == y) return ;
    if(elem[x].size() + in[x].size() + out[x].size() + bridge[x].size() < in[y].size() + out[y].size() + bridge[y].size() + elem[y].size()) swap(x, y);

    Sol += 2LL * SZ[x] * SZ[y];

    for(auto it : bridge[y]) if(elem[x].count(it)) Sol = Sol - SZ[y];
    for(auto it : elem[y]) if(bridge[x].count(it)) Sol = Sol - SZ[x];

    for(auto it : bridge[y]) if(!bridge[x].count(it) && !elem[x].count(it)) Sol = Sol + SZ[x];

    int nr = bridge[x].size();
    for(auto it : bridge[y]) if(bridge[x].count(it)) --nr;
    for(auto it : elem[y]) if(bridge[x].count(it)) --nr;
    Sol = Sol + 1LL * SZ[y] * nr;

    for(auto it : elem[y]){
        elem[x].insert(it);
        if(bridge[x].count(it)) bridge[x].erase(it);
    }

    for(auto it : bridge[y]) if(!elem[x].count(it)) bridge[x].insert(it);

    if(in[x].count(y)) in[x].erase(y);
    if(out[x].count(y)) out[x].erase(y);

    for(auto it : in[y]){
        if(it != x){
            in[x].insert(it);
            out[it].erase(y);
            out[it].insert(x);
        }
    }
    for(auto it : out[y]){
        if(it != x){
            out[x].insert(it);
            in[it].erase(y);
            in[it].insert(x);
        }
    }

    SZ[x] += SZ[y];
    TT[y] = x;

    for(auto it : in[y]){
        if(out[x].count(it)){
            unite(x, find(it));
            x = find(x);
        }
    }

    for(auto it : out[y]){
        if(in[x].count(it)){
            unite(x, find(it));
            x = find(x);
        }
    }
}

int main()
{
//    freopen("1.in", "r", stdin);
//    freopen("1.out", "w", stdout);

    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n ; ++i){
        TT[i] = i, SZ[i] = 1;
        elem[i].insert(i);
    }

    int x, y, fx, fy;
    for(int i = 1; i <= m ; ++i){
        scanf("%d%d", &x, &y);
        fx = x; fy = y;
        x = find(x); y = find(y);
        if(x == y){
            printf("%lld\n", Sol);
            continue ;
        }

        if(!bridge[y].count(fx)){
            out[x].insert(y);
            in[y].insert(x);
            bridge[y].insert(fx);

            Sol += SZ[y];
            if(in[x].count(y)) unite(x, y);
        }
        printf("%lld\n", Sol);
    }

    return 0;
}









Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:84:19: warning: variable 'fy' set but not used [-Wunused-but-set-variable]
     int x, y, fx, fy;
                   ^~
joitter2.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19072 KB Output is correct
2 Correct 14 ms 19072 KB Output is correct
3 Correct 15 ms 19200 KB Output is correct
4 Correct 16 ms 19072 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 14 ms 19072 KB Output is correct
7 Correct 15 ms 19200 KB Output is correct
8 Correct 16 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 14 ms 19072 KB Output is correct
11 Correct 15 ms 19072 KB Output is correct
12 Correct 15 ms 19200 KB Output is correct
13 Correct 17 ms 19200 KB Output is correct
14 Correct 14 ms 19072 KB Output is correct
15 Correct 14 ms 19072 KB Output is correct
16 Correct 14 ms 19200 KB Output is correct
17 Correct 14 ms 19072 KB Output is correct
18 Correct 14 ms 19072 KB Output is correct
19 Correct 14 ms 19072 KB Output is correct
20 Correct 16 ms 19072 KB Output is correct
21 Correct 17 ms 19200 KB Output is correct
22 Correct 14 ms 19200 KB Output is correct
23 Correct 17 ms 19200 KB Output is correct
24 Correct 17 ms 19072 KB Output is correct
25 Correct 15 ms 19200 KB Output is correct
26 Correct 15 ms 19072 KB Output is correct
27 Correct 14 ms 19072 KB Output is correct
28 Correct 14 ms 19072 KB Output is correct
29 Correct 14 ms 19200 KB Output is correct
30 Correct 14 ms 19072 KB Output is correct
31 Correct 15 ms 19072 KB Output is correct
32 Correct 16 ms 19072 KB Output is correct
33 Correct 15 ms 19200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19072 KB Output is correct
2 Correct 14 ms 19072 KB Output is correct
3 Correct 15 ms 19200 KB Output is correct
4 Correct 16 ms 19072 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 14 ms 19072 KB Output is correct
7 Correct 15 ms 19200 KB Output is correct
8 Correct 16 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 14 ms 19072 KB Output is correct
11 Correct 15 ms 19072 KB Output is correct
12 Correct 15 ms 19200 KB Output is correct
13 Correct 17 ms 19200 KB Output is correct
14 Correct 14 ms 19072 KB Output is correct
15 Correct 14 ms 19072 KB Output is correct
16 Correct 14 ms 19200 KB Output is correct
17 Correct 14 ms 19072 KB Output is correct
18 Correct 14 ms 19072 KB Output is correct
19 Correct 14 ms 19072 KB Output is correct
20 Correct 16 ms 19072 KB Output is correct
21 Correct 17 ms 19200 KB Output is correct
22 Correct 14 ms 19200 KB Output is correct
23 Correct 17 ms 19200 KB Output is correct
24 Correct 17 ms 19072 KB Output is correct
25 Correct 15 ms 19200 KB Output is correct
26 Correct 15 ms 19072 KB Output is correct
27 Correct 14 ms 19072 KB Output is correct
28 Correct 14 ms 19072 KB Output is correct
29 Correct 14 ms 19200 KB Output is correct
30 Correct 14 ms 19072 KB Output is correct
31 Correct 15 ms 19072 KB Output is correct
32 Correct 16 ms 19072 KB Output is correct
33 Correct 15 ms 19200 KB Output is correct
34 Correct 18 ms 19200 KB Output is correct
35 Correct 151 ms 22872 KB Output is correct
36 Correct 177 ms 25340 KB Output is correct
37 Correct 162 ms 25464 KB Output is correct
38 Correct 160 ms 25084 KB Output is correct
39 Correct 19 ms 19584 KB Output is correct
40 Correct 20 ms 19968 KB Output is correct
41 Correct 20 ms 19968 KB Output is correct
42 Correct 18 ms 19584 KB Output is correct
43 Correct 20 ms 19712 KB Output is correct
44 Correct 21 ms 19584 KB Output is correct
45 Correct 19 ms 19712 KB Output is correct
46 Correct 19 ms 19584 KB Output is correct
47 Correct 21 ms 19840 KB Output is correct
48 Correct 22 ms 19840 KB Output is correct
49 Correct 30 ms 20460 KB Output is correct
50 Correct 166 ms 25592 KB Output is correct
51 Correct 26 ms 19840 KB Output is correct
52 Correct 154 ms 23928 KB Output is correct
53 Correct 30 ms 20344 KB Output is correct
54 Correct 152 ms 24672 KB Output is correct
55 Correct 23 ms 19968 KB Output is correct
56 Correct 23 ms 19968 KB Output is correct
57 Correct 26 ms 20224 KB Output is correct
58 Correct 23 ms 20224 KB Output is correct
59 Correct 20 ms 20096 KB Output is correct
60 Correct 127 ms 22392 KB Output is correct
61 Correct 22 ms 19840 KB Output is correct
62 Correct 165 ms 24952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19072 KB Output is correct
2 Correct 14 ms 19072 KB Output is correct
3 Correct 15 ms 19200 KB Output is correct
4 Correct 16 ms 19072 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 14 ms 19072 KB Output is correct
7 Correct 15 ms 19200 KB Output is correct
8 Correct 16 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 14 ms 19072 KB Output is correct
11 Correct 15 ms 19072 KB Output is correct
12 Correct 15 ms 19200 KB Output is correct
13 Correct 17 ms 19200 KB Output is correct
14 Correct 14 ms 19072 KB Output is correct
15 Correct 14 ms 19072 KB Output is correct
16 Correct 14 ms 19200 KB Output is correct
17 Correct 14 ms 19072 KB Output is correct
18 Correct 14 ms 19072 KB Output is correct
19 Correct 14 ms 19072 KB Output is correct
20 Correct 16 ms 19072 KB Output is correct
21 Correct 17 ms 19200 KB Output is correct
22 Correct 14 ms 19200 KB Output is correct
23 Correct 17 ms 19200 KB Output is correct
24 Correct 17 ms 19072 KB Output is correct
25 Correct 15 ms 19200 KB Output is correct
26 Correct 15 ms 19072 KB Output is correct
27 Correct 14 ms 19072 KB Output is correct
28 Correct 14 ms 19072 KB Output is correct
29 Correct 14 ms 19200 KB Output is correct
30 Correct 14 ms 19072 KB Output is correct
31 Correct 15 ms 19072 KB Output is correct
32 Correct 16 ms 19072 KB Output is correct
33 Correct 15 ms 19200 KB Output is correct
34 Correct 18 ms 19200 KB Output is correct
35 Correct 151 ms 22872 KB Output is correct
36 Correct 177 ms 25340 KB Output is correct
37 Correct 162 ms 25464 KB Output is correct
38 Correct 160 ms 25084 KB Output is correct
39 Correct 19 ms 19584 KB Output is correct
40 Correct 20 ms 19968 KB Output is correct
41 Correct 20 ms 19968 KB Output is correct
42 Correct 18 ms 19584 KB Output is correct
43 Correct 20 ms 19712 KB Output is correct
44 Correct 21 ms 19584 KB Output is correct
45 Correct 19 ms 19712 KB Output is correct
46 Correct 19 ms 19584 KB Output is correct
47 Correct 21 ms 19840 KB Output is correct
48 Correct 22 ms 19840 KB Output is correct
49 Correct 30 ms 20460 KB Output is correct
50 Correct 166 ms 25592 KB Output is correct
51 Correct 26 ms 19840 KB Output is correct
52 Correct 154 ms 23928 KB Output is correct
53 Correct 30 ms 20344 KB Output is correct
54 Correct 152 ms 24672 KB Output is correct
55 Correct 23 ms 19968 KB Output is correct
56 Correct 23 ms 19968 KB Output is correct
57 Correct 26 ms 20224 KB Output is correct
58 Correct 23 ms 20224 KB Output is correct
59 Correct 20 ms 20096 KB Output is correct
60 Correct 127 ms 22392 KB Output is correct
61 Correct 22 ms 19840 KB Output is correct
62 Correct 165 ms 24952 KB Output is correct
63 Correct 540 ms 68856 KB Output is correct
64 Correct 530 ms 68856 KB Output is correct
65 Correct 551 ms 68936 KB Output is correct
66 Correct 364 ms 45432 KB Output is correct
67 Correct 528 ms 72440 KB Output is correct
68 Correct 350 ms 45688 KB Output is correct
69 Correct 644 ms 45432 KB Output is correct
70 Correct 378 ms 45432 KB Output is correct
71 Correct 386 ms 45560 KB Output is correct
72 Correct 630 ms 56440 KB Output is correct
73 Correct 628 ms 58104 KB Output is correct
74 Correct 1349 ms 79032 KB Output is correct
75 Correct 955 ms 50644 KB Output is correct
76 Correct 1089 ms 63144 KB Output is correct
77 Correct 1069 ms 63288 KB Output is correct
78 Correct 316 ms 45048 KB Output is correct
79 Correct 582 ms 51192 KB Output is correct
80 Correct 369 ms 43900 KB Output is correct
81 Correct 687 ms 48280 KB Output is correct
82 Correct 958 ms 59176 KB Output is correct
83 Correct 982 ms 59256 KB Output is correct
84 Correct 904 ms 76536 KB Output is correct
85 Correct 913 ms 76280 KB Output is correct
86 Correct 432 ms 78328 KB Output is correct
87 Correct 507 ms 79352 KB Output is correct
88 Correct 657 ms 57468 KB Output is correct
89 Correct 1041 ms 61304 KB Output is correct