답안 #313894

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
313894 2020-10-17T08:46:17 Z georgerapeanu 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++11
100 / 100
1125 ms 73268 KB
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

const int NMAX = 1e5;

int n,m;

int father[NMAX + 5];
set<int> in[NMAX + 5];
set<int> out[NMAX + 5];
set<int> nodes[NMAX + 5];

int find_root(int nod){
    if(father[nod] == 0){
        return nod;
    }
    return father[nod] = find_root(father[nod]);
}

long long unite(int x,int y){
    x = find_root(x);
    y = find_root(y);

    if(x == y){
        return false;
    }

    if(nodes[x].size() + in[x].size() + out[x].size() > nodes[y].size() + in[y].size() + out[y].size()){
        swap(x,y);
    }

    if((out[x].count(y) && out[y].count(x)) == 0){
        return 0;
    }

    long long delta = 0;

    delta -= 1LL * nodes[x].size() * in[x].size();
    delta -= 1LL * nodes[x].size() * nodes[x].size();
    delta -= 1LL * nodes[y].size() * in[y].size();
    delta -= 1LL * nodes[y].size() * nodes[y].size();

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

    for(auto it:in[x]){
        if(nodes[y].count(it)){
            continue;
        }
        in[y].insert(it);
        if(out[find_root(it)].count(x)){
            out[find_root(it)].erase(x);
            out[find_root(it)].insert(y);
        }
    }

    for(auto it:out[x]){
        if(nodes[y].count(it)){
            continue;
        }
        out[y].insert(it);
    }
    
    delta += 1LL * nodes[y].size() * in[y].size();
    delta += 1LL * nodes[y].size() * nodes[y].size();

    for(auto it:out[x]){
        delta += unite(x,it);
    }
    
    for(auto it:in[x]){
        delta += unite(x,it);
    }
    
    return delta;
}

long long add_edge(int x,int y){
    if(find_root(x) == find_root(y)){
        return 0;
    }


    long long delta = 0;

    delta -= 1LL * nodes[find_root(y)].size() * in[find_root(y)].size();
    delta -= 1LL * nodes[find_root(y)].size() * nodes[find_root(y)].size();

    out[find_root(x)].insert(find_root(y));
    in[find_root(y)].insert(x);

    delta += 1LL * nodes[find_root(y)].size() * in[find_root(y)].size();
    delta += 1LL * nodes[find_root(y)].size() * nodes[find_root(y)].size();
    delta += unite(x,y);

    return delta;
}

void afis(){
    printf("\n");
    for(int i = 1;i <= n;i++)printf("%d ",find_root(i));printf("\n");
    for(int i = 1;i <= n;i++){
        if(find_root(i) == i){
            printf("root %d\n",i);
            for(auto it:nodes[i])printf("%d ",it);printf("\n");
            for(auto it:in[i])printf("%d ",it);printf("\n");
            for(auto it:out[i])printf("%d ",it);printf("\n");
        }
    }
    printf("\n");
}

int main(){

    scanf("%d %d",&n,&m);

    for(int i = 1;i <= n;i++){
        nodes[i].insert(i);
    }

    long long ans = 0;

    for(int i = 1;i <= m;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        ans += add_edge(x,y);
        printf("%lld\n",ans);
    //    afis();
    }

    return 0;
}

Compilation message

joitter2.cpp: In function 'void afis()':
joitter2.cpp:112:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  112 |     for(int i = 1;i <= n;i++)printf("%d ",find_root(i));printf("\n");
      |     ^~~
joitter2.cpp:112:57: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  112 |     for(int i = 1;i <= n;i++)printf("%d ",find_root(i));printf("\n");
      |                                                         ^~~~~~
joitter2.cpp:116:13: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  116 |             for(auto it:nodes[i])printf("%d ",it);printf("\n");
      |             ^~~
joitter2.cpp:116:51: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  116 |             for(auto it:nodes[i])printf("%d ",it);printf("\n");
      |                                                   ^~~~~~
joitter2.cpp:117:13: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  117 |             for(auto it:in[i])printf("%d ",it);printf("\n");
      |             ^~~
joitter2.cpp:117:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  117 |             for(auto it:in[i])printf("%d ",it);printf("\n");
      |                                                ^~~~~~
joitter2.cpp:118:13: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  118 |             for(auto it:out[i])printf("%d ",it);printf("\n");
      |             ^~~
joitter2.cpp:118:49: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  118 |             for(auto it:out[i])printf("%d ",it);printf("\n");
      |                                                 ^~~~~~
joitter2.cpp: In function 'int main()':
joitter2.cpp:126:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  126 |     scanf("%d %d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  136 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14336 KB Output is correct
2 Correct 10 ms 14336 KB Output is correct
3 Correct 10 ms 14336 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14336 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 10 ms 14336 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14336 KB Output is correct
14 Correct 10 ms 14336 KB Output is correct
15 Correct 10 ms 14336 KB Output is correct
16 Correct 10 ms 14336 KB Output is correct
17 Correct 10 ms 14336 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 10 ms 14464 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14464 KB Output is correct
25 Correct 11 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14336 KB Output is correct
28 Correct 10 ms 14336 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 10 ms 14464 KB Output is correct
31 Correct 10 ms 14464 KB Output is correct
32 Correct 10 ms 14464 KB Output is correct
33 Correct 10 ms 14464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14336 KB Output is correct
2 Correct 10 ms 14336 KB Output is correct
3 Correct 10 ms 14336 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14336 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 10 ms 14336 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14336 KB Output is correct
14 Correct 10 ms 14336 KB Output is correct
15 Correct 10 ms 14336 KB Output is correct
16 Correct 10 ms 14336 KB Output is correct
17 Correct 10 ms 14336 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 10 ms 14464 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14464 KB Output is correct
25 Correct 11 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14336 KB Output is correct
28 Correct 10 ms 14336 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 10 ms 14464 KB Output is correct
31 Correct 10 ms 14464 KB Output is correct
32 Correct 10 ms 14464 KB Output is correct
33 Correct 10 ms 14464 KB Output is correct
34 Correct 15 ms 14464 KB Output is correct
35 Correct 134 ms 17536 KB Output is correct
36 Correct 171 ms 19320 KB Output is correct
37 Correct 158 ms 19448 KB Output is correct
38 Correct 156 ms 19192 KB Output is correct
39 Correct 13 ms 14848 KB Output is correct
40 Correct 16 ms 15232 KB Output is correct
41 Correct 15 ms 15232 KB Output is correct
42 Correct 13 ms 14848 KB Output is correct
43 Correct 14 ms 14848 KB Output is correct
44 Correct 14 ms 14848 KB Output is correct
45 Correct 13 ms 14848 KB Output is correct
46 Correct 15 ms 14848 KB Output is correct
47 Correct 15 ms 14976 KB Output is correct
48 Correct 15 ms 14976 KB Output is correct
49 Correct 22 ms 15360 KB Output is correct
50 Correct 157 ms 19448 KB Output is correct
51 Correct 20 ms 14976 KB Output is correct
52 Correct 148 ms 18428 KB Output is correct
53 Correct 22 ms 15360 KB Output is correct
54 Correct 149 ms 18972 KB Output is correct
55 Correct 16 ms 14976 KB Output is correct
56 Correct 17 ms 14976 KB Output is correct
57 Correct 16 ms 15232 KB Output is correct
58 Correct 16 ms 15232 KB Output is correct
59 Correct 15 ms 15232 KB Output is correct
60 Correct 134 ms 17528 KB Output is correct
61 Correct 16 ms 14976 KB Output is correct
62 Correct 154 ms 19324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14336 KB Output is correct
2 Correct 10 ms 14336 KB Output is correct
3 Correct 10 ms 14336 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14336 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 10 ms 14336 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14336 KB Output is correct
14 Correct 10 ms 14336 KB Output is correct
15 Correct 10 ms 14336 KB Output is correct
16 Correct 10 ms 14336 KB Output is correct
17 Correct 10 ms 14336 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 10 ms 14464 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 10 ms 14464 KB Output is correct
25 Correct 11 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14336 KB Output is correct
28 Correct 10 ms 14336 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 10 ms 14464 KB Output is correct
31 Correct 10 ms 14464 KB Output is correct
32 Correct 10 ms 14464 KB Output is correct
33 Correct 10 ms 14464 KB Output is correct
34 Correct 15 ms 14464 KB Output is correct
35 Correct 134 ms 17536 KB Output is correct
36 Correct 171 ms 19320 KB Output is correct
37 Correct 158 ms 19448 KB Output is correct
38 Correct 156 ms 19192 KB Output is correct
39 Correct 13 ms 14848 KB Output is correct
40 Correct 16 ms 15232 KB Output is correct
41 Correct 15 ms 15232 KB Output is correct
42 Correct 13 ms 14848 KB Output is correct
43 Correct 14 ms 14848 KB Output is correct
44 Correct 14 ms 14848 KB Output is correct
45 Correct 13 ms 14848 KB Output is correct
46 Correct 15 ms 14848 KB Output is correct
47 Correct 15 ms 14976 KB Output is correct
48 Correct 15 ms 14976 KB Output is correct
49 Correct 22 ms 15360 KB Output is correct
50 Correct 157 ms 19448 KB Output is correct
51 Correct 20 ms 14976 KB Output is correct
52 Correct 148 ms 18428 KB Output is correct
53 Correct 22 ms 15360 KB Output is correct
54 Correct 149 ms 18972 KB Output is correct
55 Correct 16 ms 14976 KB Output is correct
56 Correct 17 ms 14976 KB Output is correct
57 Correct 16 ms 15232 KB Output is correct
58 Correct 16 ms 15232 KB Output is correct
59 Correct 15 ms 15232 KB Output is correct
60 Correct 134 ms 17528 KB Output is correct
61 Correct 16 ms 14976 KB Output is correct
62 Correct 154 ms 19324 KB Output is correct
63 Correct 503 ms 49428 KB Output is correct
64 Correct 515 ms 49272 KB Output is correct
65 Correct 530 ms 49400 KB Output is correct
66 Correct 330 ms 35832 KB Output is correct
67 Correct 518 ms 61304 KB Output is correct
68 Correct 317 ms 38136 KB Output is correct
69 Correct 578 ms 37880 KB Output is correct
70 Correct 361 ms 38008 KB Output is correct
71 Correct 376 ms 38136 KB Output is correct
72 Correct 582 ms 47608 KB Output is correct
73 Correct 572 ms 49248 KB Output is correct
74 Correct 1125 ms 64200 KB Output is correct
75 Correct 827 ms 43256 KB Output is correct
76 Correct 946 ms 52344 KB Output is correct
77 Correct 929 ms 52600 KB Output is correct
78 Correct 326 ms 35832 KB Output is correct
79 Correct 588 ms 43256 KB Output is correct
80 Correct 313 ms 35832 KB Output is correct
81 Correct 576 ms 41604 KB Output is correct
82 Correct 765 ms 46684 KB Output is correct
83 Correct 808 ms 46736 KB Output is correct
84 Correct 711 ms 57592 KB Output is correct
85 Correct 708 ms 57464 KB Output is correct
86 Correct 395 ms 70776 KB Output is correct
87 Correct 443 ms 73268 KB Output is correct
88 Correct 607 ms 48560 KB Output is correct
89 Correct 961 ms 51044 KB Output is correct