답안 #212160

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212160 2020-03-22T11:30:34 Z DodgeBallMan 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1081 ms 69880 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 1e5 + 10;
set<int> in[N], out[N], com[N], e[N];
long long ans;
long long par[N], sz[N];
int n, m;

int find( int u ) { return par[u] == u ? u : par[u] = find( par[u] ); }

long long cal( int u ) { return 1LL * ( ( ( int )e[u].size() - sz[u] ) * sz[u] ) + 1LL * sz[u] * ( sz[u]-1 ); }

void merge( int a, int b ) {
    vector<pii> v;
    a = find( a ), b = find( b );
    if( a == b ) return ;
    if( in[a].size() + out[a].size() + e[a].size() < in[b].size() + out[b].size() + e[b].size() ) swap( a, b );
    ans -= cal( a ), ans -= cal( b );
    in[a].erase( b ), in[b].erase( a ), out[a].erase( b ), out[b].erase( a );
    for( int x : in[b] ) {
        if( out[a].count( x ) ) v.emplace_back( x, a );
        out[x].erase( b ), out[x].emplace( a );
    }
    for( int x : out[b] ) {
        if( in[a].count( x ) ) v.emplace_back( x, a );
        in[x].erase( b ), in[x].emplace( a );
    }
    for( int x : in[b] ) in[a].emplace( x );
    for( int x : out[b] ) out[a].emplace( x );
    for( int x : e[b] ) e[a].emplace( x );
    in[b].clear(), out[b].clear(), e[b].clear();
    sz[a] += sz[b], par[b] = a;
    ans += cal( a );
    for( pii x : v ) merge( x.x, x.y ); 
}

int main()
{
    scanf("%d %d",&n,&m);
    iota( par, par + N, 0 ), fill( sz, sz + N, 1 );
    for( int i = 1 ; i <= n ; i++ ) e[i].emplace( i );
    while( m-- ) {
        int u, v;
        scanf("%d %d",&u,&v);
        int a = find( u ), b = find( v );
        if( out[b].count( a ) ) merge( u, v );
        else {
            ans -= cal( b );
            in[b].emplace( a ), out[a].emplace( b );
            e[b].emplace( u );
            ans += cal( b );
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:44: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:49:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&u,&v);
         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 20608 KB Output is correct
2 Correct 16 ms 20608 KB Output is correct
3 Correct 16 ms 20736 KB Output is correct
4 Correct 16 ms 20608 KB Output is correct
5 Correct 16 ms 20736 KB Output is correct
6 Correct 15 ms 20608 KB Output is correct
7 Correct 17 ms 20736 KB Output is correct
8 Correct 16 ms 20736 KB Output is correct
9 Correct 18 ms 20736 KB Output is correct
10 Correct 17 ms 20608 KB Output is correct
11 Correct 16 ms 20640 KB Output is correct
12 Correct 16 ms 20608 KB Output is correct
13 Correct 15 ms 20608 KB Output is correct
14 Correct 16 ms 20640 KB Output is correct
15 Correct 17 ms 20608 KB Output is correct
16 Correct 15 ms 20608 KB Output is correct
17 Correct 17 ms 20736 KB Output is correct
18 Correct 16 ms 20608 KB Output is correct
19 Correct 16 ms 20736 KB Output is correct
20 Correct 16 ms 20736 KB Output is correct
21 Correct 16 ms 20736 KB Output is correct
22 Correct 17 ms 20608 KB Output is correct
23 Correct 16 ms 20736 KB Output is correct
24 Correct 16 ms 20736 KB Output is correct
25 Correct 17 ms 20736 KB Output is correct
26 Correct 16 ms 20736 KB Output is correct
27 Correct 16 ms 20736 KB Output is correct
28 Correct 16 ms 20736 KB Output is correct
29 Correct 16 ms 20736 KB Output is correct
30 Correct 15 ms 20608 KB Output is correct
31 Correct 16 ms 20736 KB Output is correct
32 Correct 16 ms 20608 KB Output is correct
33 Correct 16 ms 20736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 20608 KB Output is correct
2 Correct 16 ms 20608 KB Output is correct
3 Correct 16 ms 20736 KB Output is correct
4 Correct 16 ms 20608 KB Output is correct
5 Correct 16 ms 20736 KB Output is correct
6 Correct 15 ms 20608 KB Output is correct
7 Correct 17 ms 20736 KB Output is correct
8 Correct 16 ms 20736 KB Output is correct
9 Correct 18 ms 20736 KB Output is correct
10 Correct 17 ms 20608 KB Output is correct
11 Correct 16 ms 20640 KB Output is correct
12 Correct 16 ms 20608 KB Output is correct
13 Correct 15 ms 20608 KB Output is correct
14 Correct 16 ms 20640 KB Output is correct
15 Correct 17 ms 20608 KB Output is correct
16 Correct 15 ms 20608 KB Output is correct
17 Correct 17 ms 20736 KB Output is correct
18 Correct 16 ms 20608 KB Output is correct
19 Correct 16 ms 20736 KB Output is correct
20 Correct 16 ms 20736 KB Output is correct
21 Correct 16 ms 20736 KB Output is correct
22 Correct 17 ms 20608 KB Output is correct
23 Correct 16 ms 20736 KB Output is correct
24 Correct 16 ms 20736 KB Output is correct
25 Correct 17 ms 20736 KB Output is correct
26 Correct 16 ms 20736 KB Output is correct
27 Correct 16 ms 20736 KB Output is correct
28 Correct 16 ms 20736 KB Output is correct
29 Correct 16 ms 20736 KB Output is correct
30 Correct 15 ms 20608 KB Output is correct
31 Correct 16 ms 20736 KB Output is correct
32 Correct 16 ms 20608 KB Output is correct
33 Correct 16 ms 20736 KB Output is correct
34 Correct 19 ms 20736 KB Output is correct
35 Correct 152 ms 24412 KB Output is correct
36 Correct 165 ms 26616 KB Output is correct
37 Correct 170 ms 26744 KB Output is correct
38 Correct 163 ms 26360 KB Output is correct
39 Correct 19 ms 20736 KB Output is correct
40 Correct 20 ms 20864 KB Output is correct
41 Correct 19 ms 20992 KB Output is correct
42 Correct 19 ms 20864 KB Output is correct
43 Correct 21 ms 20992 KB Output is correct
44 Correct 20 ms 20992 KB Output is correct
45 Correct 19 ms 20736 KB Output is correct
46 Correct 20 ms 20864 KB Output is correct
47 Correct 19 ms 20864 KB Output is correct
48 Correct 20 ms 20864 KB Output is correct
49 Correct 29 ms 21672 KB Output is correct
50 Correct 170 ms 26872 KB Output is correct
51 Correct 25 ms 21120 KB Output is correct
52 Correct 152 ms 25184 KB Output is correct
53 Correct 27 ms 21504 KB Output is correct
54 Correct 161 ms 26108 KB Output is correct
55 Correct 23 ms 21376 KB Output is correct
56 Correct 22 ms 21376 KB Output is correct
57 Correct 23 ms 21632 KB Output is correct
58 Correct 22 ms 21632 KB Output is correct
59 Correct 19 ms 20864 KB Output is correct
60 Correct 141 ms 23160 KB Output is correct
61 Correct 21 ms 20992 KB Output is correct
62 Correct 169 ms 26232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 20608 KB Output is correct
2 Correct 16 ms 20608 KB Output is correct
3 Correct 16 ms 20736 KB Output is correct
4 Correct 16 ms 20608 KB Output is correct
5 Correct 16 ms 20736 KB Output is correct
6 Correct 15 ms 20608 KB Output is correct
7 Correct 17 ms 20736 KB Output is correct
8 Correct 16 ms 20736 KB Output is correct
9 Correct 18 ms 20736 KB Output is correct
10 Correct 17 ms 20608 KB Output is correct
11 Correct 16 ms 20640 KB Output is correct
12 Correct 16 ms 20608 KB Output is correct
13 Correct 15 ms 20608 KB Output is correct
14 Correct 16 ms 20640 KB Output is correct
15 Correct 17 ms 20608 KB Output is correct
16 Correct 15 ms 20608 KB Output is correct
17 Correct 17 ms 20736 KB Output is correct
18 Correct 16 ms 20608 KB Output is correct
19 Correct 16 ms 20736 KB Output is correct
20 Correct 16 ms 20736 KB Output is correct
21 Correct 16 ms 20736 KB Output is correct
22 Correct 17 ms 20608 KB Output is correct
23 Correct 16 ms 20736 KB Output is correct
24 Correct 16 ms 20736 KB Output is correct
25 Correct 17 ms 20736 KB Output is correct
26 Correct 16 ms 20736 KB Output is correct
27 Correct 16 ms 20736 KB Output is correct
28 Correct 16 ms 20736 KB Output is correct
29 Correct 16 ms 20736 KB Output is correct
30 Correct 15 ms 20608 KB Output is correct
31 Correct 16 ms 20736 KB Output is correct
32 Correct 16 ms 20608 KB Output is correct
33 Correct 16 ms 20736 KB Output is correct
34 Correct 19 ms 20736 KB Output is correct
35 Correct 152 ms 24412 KB Output is correct
36 Correct 165 ms 26616 KB Output is correct
37 Correct 170 ms 26744 KB Output is correct
38 Correct 163 ms 26360 KB Output is correct
39 Correct 19 ms 20736 KB Output is correct
40 Correct 20 ms 20864 KB Output is correct
41 Correct 19 ms 20992 KB Output is correct
42 Correct 19 ms 20864 KB Output is correct
43 Correct 21 ms 20992 KB Output is correct
44 Correct 20 ms 20992 KB Output is correct
45 Correct 19 ms 20736 KB Output is correct
46 Correct 20 ms 20864 KB Output is correct
47 Correct 19 ms 20864 KB Output is correct
48 Correct 20 ms 20864 KB Output is correct
49 Correct 29 ms 21672 KB Output is correct
50 Correct 170 ms 26872 KB Output is correct
51 Correct 25 ms 21120 KB Output is correct
52 Correct 152 ms 25184 KB Output is correct
53 Correct 27 ms 21504 KB Output is correct
54 Correct 161 ms 26108 KB Output is correct
55 Correct 23 ms 21376 KB Output is correct
56 Correct 22 ms 21376 KB Output is correct
57 Correct 23 ms 21632 KB Output is correct
58 Correct 22 ms 21632 KB Output is correct
59 Correct 19 ms 20864 KB Output is correct
60 Correct 141 ms 23160 KB Output is correct
61 Correct 21 ms 20992 KB Output is correct
62 Correct 169 ms 26232 KB Output is correct
63 Correct 670 ms 69880 KB Output is correct
64 Correct 633 ms 69752 KB Output is correct
65 Correct 649 ms 69752 KB Output is correct
66 Correct 271 ms 27512 KB Output is correct
67 Correct 472 ms 33912 KB Output is correct
68 Correct 217 ms 27516 KB Output is correct
69 Correct 454 ms 34680 KB Output is correct
70 Correct 256 ms 27512 KB Output is correct
71 Correct 277 ms 27640 KB Output is correct
72 Correct 448 ms 33912 KB Output is correct
73 Correct 492 ms 34032 KB Output is correct
74 Correct 1081 ms 44444 KB Output is correct
75 Correct 685 ms 39288 KB Output is correct
76 Correct 832 ms 44036 KB Output is correct
77 Correct 843 ms 44024 KB Output is correct
78 Correct 290 ms 34040 KB Output is correct
79 Correct 509 ms 35408 KB Output is correct
80 Correct 249 ms 33888 KB Output is correct
81 Correct 418 ms 35448 KB Output is correct
82 Correct 872 ms 54904 KB Output is correct
83 Correct 858 ms 54904 KB Output is correct
84 Correct 767 ms 64476 KB Output is correct
85 Correct 761 ms 64376 KB Output is correct
86 Correct 314 ms 28280 KB Output is correct
87 Correct 370 ms 29432 KB Output is correct
88 Correct 459 ms 33912 KB Output is correct
89 Correct 814 ms 43328 KB Output is correct