답안 #563776

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563776 2022-05-18T06:15:34 Z tqbfjotld Mergers (JOI19_mergers) C++14
100 / 100
1137 ms 143332 KB
#include <bits/stdc++.h>
using namespace std;

int p[500005][20];
int dep[500005];

vector<int> adjl[500005];

void dfs(int node, int pa){
    p[node][0] = pa;
    for (auto x : adjl[node]){
        if (x==pa) continue;
        dep[x] = dep[node]+1;
        dfs(x,node);
    }
}

int lca(int a, int b){
    if (dep[a]>dep[b]){
        swap(a,b);
    }
    int t = dep[b]-dep[a];
    for (int x = 0; x<20; x++){
        if (t&(1<<x)){
            b = p[b][x];
        }
    }
    if (a==b) return a;
    for (int x = 19; x>=0; x--){
        if (p[a][x]!=p[b][x]){
            a = p[a][x];
            b = p[b][x];
        }
    }
    return p[a][0];
}

int arr[500005];
vector<int> states[500005];

int pa[500005];
int rt(int a){return pa[a]==a?a:pa[a]=rt(pa[a]);}
void mer(int a, int b){
    a = rt(a);
    b = rt(b);
    if (a==b) return;
    pa[a] = b;
}

int dfs2(int node, int np){
    int num = arr[node];
    for (auto x : adjl[node]){
        if (x==np) continue;
        num += dfs2(x,node);
    }
    if (num>0){
        mer(node,np);
    }
    return num;
}

vector<int> adjl2[500005];

int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for (int x = 0; x<n-1; x++){
        int a,b;
        scanf("%d%d",&a,&b);
        adjl[a].push_back(b);
        adjl[b].push_back(a);
    }
    dfs(1,0);
    for (int x = 1; x<20; x++){
        for (int y = 0; y<=n; y++){
            p[y][x] = p[p[y][x-1]][x-1];
        }
    }
    for (int x = 1; x<=n; x++){
        int a;
        scanf("%d",&a);
        states[a].push_back(x);
    }
    for (int x = 1; x<=k; x++){
        if (states[x].empty()) continue;
        int cur = states[x][0];
        for (int y = 1; y<states[x].size(); y++){
            arr[cur]++;
            arr[states[x][y]]++;
            int t = lca(cur,states[x][y]);
            //printf("lca = %d\n",t);
            arr[t]-=2;
            cur = states[x][y];
        }
    }
    for (int x = 0; x<=n; x++){
        pa[x] = x;
    }
    dfs2(1,0);
    int z = 0;
    for (int x = 2; x<=n; x++){
        if (rt(p[x][0])!=rt(x)){
            adjl2[rt(x)].push_back(rt(p[x][0]));
            adjl2[rt(p[x][0])].push_back(rt(x));
            z++;
            //printf("z %d %d\n",rt(x),rt(p[x][0]));
        }
    }
    if (z==0){
        printf("0");
        return 0;
    }
    int t2 = 0;
    for (int x = 1; x<=n; x++){
        if (adjl2[x].size()==1){
            t2++;
        }
    }
    printf("%d",(t2+1)/2);

}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:87:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         for (int y = 1; y<states[x].size(); y++){
      |                         ~^~~~~~~~~~~~~~~~~
mergers.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     scanf("%d%d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~
mergers.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         scanf("%d%d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~
mergers.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         scanf("%d",&a);
      |         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 35540 KB Output is correct
2 Correct 18 ms 35568 KB Output is correct
3 Correct 18 ms 35480 KB Output is correct
4 Correct 18 ms 35536 KB Output is correct
5 Correct 20 ms 35540 KB Output is correct
6 Correct 19 ms 35568 KB Output is correct
7 Correct 19 ms 35540 KB Output is correct
8 Correct 18 ms 35540 KB Output is correct
9 Correct 19 ms 35460 KB Output is correct
10 Correct 19 ms 35464 KB Output is correct
11 Correct 19 ms 35536 KB Output is correct
12 Correct 18 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 18 ms 35540 KB Output is correct
15 Correct 18 ms 35572 KB Output is correct
16 Correct 18 ms 35540 KB Output is correct
17 Correct 18 ms 35456 KB Output is correct
18 Correct 19 ms 35560 KB Output is correct
19 Correct 18 ms 35560 KB Output is correct
20 Correct 20 ms 35540 KB Output is correct
21 Correct 20 ms 35496 KB Output is correct
22 Correct 18 ms 35508 KB Output is correct
23 Correct 20 ms 35540 KB Output is correct
24 Correct 20 ms 35512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 35540 KB Output is correct
2 Correct 18 ms 35568 KB Output is correct
3 Correct 18 ms 35480 KB Output is correct
4 Correct 18 ms 35536 KB Output is correct
5 Correct 20 ms 35540 KB Output is correct
6 Correct 19 ms 35568 KB Output is correct
7 Correct 19 ms 35540 KB Output is correct
8 Correct 18 ms 35540 KB Output is correct
9 Correct 19 ms 35460 KB Output is correct
10 Correct 19 ms 35464 KB Output is correct
11 Correct 19 ms 35536 KB Output is correct
12 Correct 18 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 18 ms 35540 KB Output is correct
15 Correct 18 ms 35572 KB Output is correct
16 Correct 18 ms 35540 KB Output is correct
17 Correct 18 ms 35456 KB Output is correct
18 Correct 19 ms 35560 KB Output is correct
19 Correct 18 ms 35560 KB Output is correct
20 Correct 20 ms 35540 KB Output is correct
21 Correct 20 ms 35496 KB Output is correct
22 Correct 18 ms 35508 KB Output is correct
23 Correct 20 ms 35540 KB Output is correct
24 Correct 20 ms 35512 KB Output is correct
25 Correct 18 ms 35540 KB Output is correct
26 Correct 21 ms 35924 KB Output is correct
27 Correct 20 ms 35960 KB Output is correct
28 Correct 20 ms 36052 KB Output is correct
29 Correct 20 ms 36112 KB Output is correct
30 Correct 20 ms 35868 KB Output is correct
31 Correct 19 ms 35540 KB Output is correct
32 Correct 19 ms 36052 KB Output is correct
33 Correct 19 ms 35560 KB Output is correct
34 Correct 20 ms 35924 KB Output is correct
35 Correct 20 ms 35992 KB Output is correct
36 Correct 20 ms 35876 KB Output is correct
37 Correct 20 ms 35924 KB Output is correct
38 Correct 18 ms 35468 KB Output is correct
39 Correct 20 ms 35924 KB Output is correct
40 Correct 21 ms 35840 KB Output is correct
41 Correct 20 ms 35924 KB Output is correct
42 Correct 20 ms 35924 KB Output is correct
43 Correct 19 ms 36016 KB Output is correct
44 Correct 18 ms 35576 KB Output is correct
45 Correct 21 ms 36064 KB Output is correct
46 Correct 21 ms 35872 KB Output is correct
47 Correct 17 ms 35540 KB Output is correct
48 Correct 21 ms 35904 KB Output is correct
49 Correct 21 ms 36020 KB Output is correct
50 Correct 22 ms 36052 KB Output is correct
51 Correct 21 ms 35960 KB Output is correct
52 Correct 21 ms 35924 KB Output is correct
53 Correct 20 ms 35924 KB Output is correct
54 Correct 19 ms 35924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 35540 KB Output is correct
2 Correct 18 ms 35568 KB Output is correct
3 Correct 18 ms 35480 KB Output is correct
4 Correct 18 ms 35536 KB Output is correct
5 Correct 20 ms 35540 KB Output is correct
6 Correct 19 ms 35568 KB Output is correct
7 Correct 19 ms 35540 KB Output is correct
8 Correct 18 ms 35540 KB Output is correct
9 Correct 19 ms 35460 KB Output is correct
10 Correct 19 ms 35464 KB Output is correct
11 Correct 19 ms 35536 KB Output is correct
12 Correct 18 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 18 ms 35540 KB Output is correct
15 Correct 18 ms 35572 KB Output is correct
16 Correct 18 ms 35540 KB Output is correct
17 Correct 18 ms 35456 KB Output is correct
18 Correct 19 ms 35560 KB Output is correct
19 Correct 18 ms 35560 KB Output is correct
20 Correct 20 ms 35540 KB Output is correct
21 Correct 20 ms 35496 KB Output is correct
22 Correct 18 ms 35508 KB Output is correct
23 Correct 20 ms 35540 KB Output is correct
24 Correct 20 ms 35512 KB Output is correct
25 Correct 18 ms 35572 KB Output is correct
26 Correct 86 ms 48516 KB Output is correct
27 Correct 133 ms 48416 KB Output is correct
28 Correct 20 ms 35908 KB Output is correct
29 Correct 17 ms 35520 KB Output is correct
30 Correct 18 ms 35564 KB Output is correct
31 Correct 103 ms 48164 KB Output is correct
32 Correct 20 ms 35924 KB Output is correct
33 Correct 130 ms 54008 KB Output is correct
34 Correct 112 ms 48340 KB Output is correct
35 Correct 20 ms 35916 KB Output is correct
36 Correct 123 ms 48768 KB Output is correct
37 Correct 20 ms 35924 KB Output is correct
38 Correct 20 ms 35924 KB Output is correct
39 Correct 86 ms 48580 KB Output is correct
40 Correct 20 ms 36052 KB Output is correct
41 Correct 108 ms 48228 KB Output is correct
42 Correct 148 ms 50148 KB Output is correct
43 Correct 17 ms 35548 KB Output is correct
44 Correct 130 ms 54144 KB Output is correct
45 Correct 122 ms 51276 KB Output is correct
46 Correct 19 ms 35896 KB Output is correct
47 Correct 20 ms 35860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 48560 KB Output is correct
2 Correct 113 ms 54288 KB Output is correct
3 Correct 20 ms 36036 KB Output is correct
4 Correct 21 ms 35912 KB Output is correct
5 Correct 18 ms 35472 KB Output is correct
6 Correct 19 ms 35540 KB Output is correct
7 Correct 20 ms 35888 KB Output is correct
8 Correct 125 ms 49808 KB Output is correct
9 Correct 22 ms 35924 KB Output is correct
10 Correct 109 ms 48272 KB Output is correct
11 Correct 17 ms 35468 KB Output is correct
12 Correct 117 ms 48176 KB Output is correct
13 Correct 120 ms 49808 KB Output is correct
14 Correct 120 ms 53684 KB Output is correct
15 Correct 93 ms 48544 KB Output is correct
16 Correct 20 ms 35924 KB Output is correct
17 Correct 19 ms 35492 KB Output is correct
18 Correct 93 ms 53616 KB Output is correct
19 Correct 131 ms 56704 KB Output is correct
20 Correct 21 ms 35940 KB Output is correct
21 Correct 18 ms 35572 KB Output is correct
22 Correct 106 ms 50256 KB Output is correct
23 Correct 20 ms 35844 KB Output is correct
24 Correct 120 ms 48812 KB Output is correct
25 Correct 130 ms 55628 KB Output is correct
26 Correct 19 ms 36128 KB Output is correct
27 Correct 21 ms 36056 KB Output is correct
28 Correct 22 ms 36052 KB Output is correct
29 Correct 22 ms 35924 KB Output is correct
30 Correct 24 ms 35944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 35540 KB Output is correct
2 Correct 18 ms 35568 KB Output is correct
3 Correct 18 ms 35480 KB Output is correct
4 Correct 18 ms 35536 KB Output is correct
5 Correct 20 ms 35540 KB Output is correct
6 Correct 19 ms 35568 KB Output is correct
7 Correct 19 ms 35540 KB Output is correct
8 Correct 18 ms 35540 KB Output is correct
9 Correct 19 ms 35460 KB Output is correct
10 Correct 19 ms 35464 KB Output is correct
11 Correct 19 ms 35536 KB Output is correct
12 Correct 18 ms 35540 KB Output is correct
13 Correct 18 ms 35540 KB Output is correct
14 Correct 18 ms 35540 KB Output is correct
15 Correct 18 ms 35572 KB Output is correct
16 Correct 18 ms 35540 KB Output is correct
17 Correct 18 ms 35456 KB Output is correct
18 Correct 19 ms 35560 KB Output is correct
19 Correct 18 ms 35560 KB Output is correct
20 Correct 20 ms 35540 KB Output is correct
21 Correct 20 ms 35496 KB Output is correct
22 Correct 18 ms 35508 KB Output is correct
23 Correct 20 ms 35540 KB Output is correct
24 Correct 20 ms 35512 KB Output is correct
25 Correct 18 ms 35540 KB Output is correct
26 Correct 21 ms 35924 KB Output is correct
27 Correct 20 ms 35960 KB Output is correct
28 Correct 20 ms 36052 KB Output is correct
29 Correct 20 ms 36112 KB Output is correct
30 Correct 20 ms 35868 KB Output is correct
31 Correct 19 ms 35540 KB Output is correct
32 Correct 19 ms 36052 KB Output is correct
33 Correct 19 ms 35560 KB Output is correct
34 Correct 20 ms 35924 KB Output is correct
35 Correct 20 ms 35992 KB Output is correct
36 Correct 20 ms 35876 KB Output is correct
37 Correct 20 ms 35924 KB Output is correct
38 Correct 18 ms 35468 KB Output is correct
39 Correct 20 ms 35924 KB Output is correct
40 Correct 21 ms 35840 KB Output is correct
41 Correct 20 ms 35924 KB Output is correct
42 Correct 20 ms 35924 KB Output is correct
43 Correct 19 ms 36016 KB Output is correct
44 Correct 18 ms 35576 KB Output is correct
45 Correct 21 ms 36064 KB Output is correct
46 Correct 21 ms 35872 KB Output is correct
47 Correct 17 ms 35540 KB Output is correct
48 Correct 21 ms 35904 KB Output is correct
49 Correct 21 ms 36020 KB Output is correct
50 Correct 22 ms 36052 KB Output is correct
51 Correct 21 ms 35960 KB Output is correct
52 Correct 21 ms 35924 KB Output is correct
53 Correct 20 ms 35924 KB Output is correct
54 Correct 19 ms 35924 KB Output is correct
55 Correct 18 ms 35572 KB Output is correct
56 Correct 86 ms 48516 KB Output is correct
57 Correct 133 ms 48416 KB Output is correct
58 Correct 20 ms 35908 KB Output is correct
59 Correct 17 ms 35520 KB Output is correct
60 Correct 18 ms 35564 KB Output is correct
61 Correct 103 ms 48164 KB Output is correct
62 Correct 20 ms 35924 KB Output is correct
63 Correct 130 ms 54008 KB Output is correct
64 Correct 112 ms 48340 KB Output is correct
65 Correct 20 ms 35916 KB Output is correct
66 Correct 123 ms 48768 KB Output is correct
67 Correct 20 ms 35924 KB Output is correct
68 Correct 20 ms 35924 KB Output is correct
69 Correct 86 ms 48580 KB Output is correct
70 Correct 20 ms 36052 KB Output is correct
71 Correct 108 ms 48228 KB Output is correct
72 Correct 148 ms 50148 KB Output is correct
73 Correct 17 ms 35548 KB Output is correct
74 Correct 130 ms 54144 KB Output is correct
75 Correct 122 ms 51276 KB Output is correct
76 Correct 19 ms 35896 KB Output is correct
77 Correct 20 ms 35860 KB Output is correct
78 Correct 113 ms 48560 KB Output is correct
79 Correct 113 ms 54288 KB Output is correct
80 Correct 20 ms 36036 KB Output is correct
81 Correct 21 ms 35912 KB Output is correct
82 Correct 18 ms 35472 KB Output is correct
83 Correct 19 ms 35540 KB Output is correct
84 Correct 20 ms 35888 KB Output is correct
85 Correct 125 ms 49808 KB Output is correct
86 Correct 22 ms 35924 KB Output is correct
87 Correct 109 ms 48272 KB Output is correct
88 Correct 17 ms 35468 KB Output is correct
89 Correct 117 ms 48176 KB Output is correct
90 Correct 120 ms 49808 KB Output is correct
91 Correct 120 ms 53684 KB Output is correct
92 Correct 93 ms 48544 KB Output is correct
93 Correct 20 ms 35924 KB Output is correct
94 Correct 19 ms 35492 KB Output is correct
95 Correct 93 ms 53616 KB Output is correct
96 Correct 131 ms 56704 KB Output is correct
97 Correct 21 ms 35940 KB Output is correct
98 Correct 18 ms 35572 KB Output is correct
99 Correct 106 ms 50256 KB Output is correct
100 Correct 20 ms 35844 KB Output is correct
101 Correct 120 ms 48812 KB Output is correct
102 Correct 130 ms 55628 KB Output is correct
103 Correct 19 ms 36128 KB Output is correct
104 Correct 21 ms 36056 KB Output is correct
105 Correct 22 ms 36052 KB Output is correct
106 Correct 22 ms 35924 KB Output is correct
107 Correct 24 ms 35944 KB Output is correct
108 Correct 678 ms 106136 KB Output is correct
109 Correct 1100 ms 113312 KB Output is correct
110 Correct 1137 ms 122264 KB Output is correct
111 Correct 1061 ms 143332 KB Output is correct
112 Correct 886 ms 130316 KB Output is correct
113 Correct 568 ms 126428 KB Output is correct
114 Correct 735 ms 98800 KB Output is correct
115 Correct 724 ms 98776 KB Output is correct
116 Correct 853 ms 101668 KB Output is correct
117 Correct 896 ms 126652 KB Output is correct
118 Correct 724 ms 98636 KB Output is correct
119 Correct 887 ms 126768 KB Output is correct
120 Correct 1024 ms 137008 KB Output is correct
121 Correct 945 ms 127308 KB Output is correct
122 Correct 829 ms 109192 KB Output is correct
123 Correct 541 ms 129368 KB Output is correct
124 Correct 962 ms 120528 KB Output is correct