답안 #121002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
121002 2019-06-25T22:58:12 Z songc Mergers (JOI19_mergers) C++14
100 / 100
2478 ms 129356 KB
#include <bits/stdc++.h>
using namespace std;

int N, K, ans;
vector<int> adj[505050];
vector<int> rst[505050];
int S[505050], E[505050], num=1;
int st[505050], T[505050];
int P[22][505050], dep[505050];
int deg[505050], pa[505050];

void update(int t, int v){
    while (t <= N){
        T[t] += v;
        t += t & -t;
    }
}

int read(int t){
    int ret=0;
    while (t){
        ret += T[t];
        t ^= t & -t;
    }
    return ret;
}

int Find(int u){
    if (u == pa[u]) return u;
    return pa[u] = Find(pa[u]);
}

int dfs(int u, int p, int depth){
    P[0][u] = p;
    S[u] = num;
    E[u] = num++;
    dep[u] = depth;
    for (int v : adj[u]){
        if (v == p) continue;
        E[u] = dfs(v, u, depth+1);
    }
    return E[u];
}

void Union(int u, int p){
    if (read(E[u]) - read(S[u]-1) != 0) pa[Find(u)] = Find(p);
    for (int v : adj[u]){
        if (v == p) continue;
        Union(v, u);
    }
}

int LCA(int u, int v){
    if (dep[u] < dep[v]) swap(u, v);
    for (int i=20; i>=0; i--) if (dep[P[i][u]] >= dep[v]) u = P[i][u];
    if (u == v) return u;
    for (int i=20; i>=0; i--) if (P[i][u] != P[i][v]) u = P[i][u], v = P[i][v];
    return P[0][u];
}

int main(){
    int u, v;
    scanf("%d %d", &N, &K);
    for (int i=1; i<N; i++){
        scanf("%d %d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i=1; i<=N; i++){
        scanf("%d", &st[i]);
        rst[st[i]].push_back(i);
    }

    dfs(1, 0, 1);
    for (int i=1; i<=N; i++) update(i, 1);
    for (int i=1; i<=20; i++) for (int j=1; j<=N; j++) P[i][j] = P[i-1][P[i-1][j]];
    for (int i=1; i<=N; i++) pa[i] = i;
    for (int i=1; i<=K; i++){
        int lca = rst[i][0];
        for (int j=1; j<(int)rst[i].size(); j++) lca = LCA(lca, rst[i][j]);
        update(S[lca], -rst[i].size());
    }
    Union(1, 0);
    for (int i=1; i<=N; i++) for (int j : adj[i]) if (Find(i) != Find(j)) deg[Find(j)]++;
    for (int i=1; i<=N; i++) if (deg[i] == 1) ans++;
    printf("%d", (ans+1)/2);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &N, &K);
     ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &st[i]);
         ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24192 KB Output is correct
2 Correct 22 ms 24312 KB Output is correct
3 Correct 20 ms 24192 KB Output is correct
4 Correct 22 ms 24320 KB Output is correct
5 Correct 22 ms 24320 KB Output is correct
6 Correct 21 ms 24192 KB Output is correct
7 Correct 21 ms 24192 KB Output is correct
8 Correct 24 ms 24312 KB Output is correct
9 Correct 22 ms 24268 KB Output is correct
10 Correct 22 ms 24312 KB Output is correct
11 Correct 22 ms 24320 KB Output is correct
12 Correct 22 ms 24192 KB Output is correct
13 Correct 21 ms 24192 KB Output is correct
14 Correct 22 ms 24192 KB Output is correct
15 Correct 22 ms 24192 KB Output is correct
16 Correct 22 ms 24064 KB Output is correct
17 Correct 21 ms 24344 KB Output is correct
18 Correct 22 ms 24320 KB Output is correct
19 Correct 22 ms 24240 KB Output is correct
20 Correct 20 ms 24192 KB Output is correct
21 Correct 22 ms 24320 KB Output is correct
22 Correct 22 ms 24196 KB Output is correct
23 Correct 24 ms 24320 KB Output is correct
24 Correct 21 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24192 KB Output is correct
2 Correct 22 ms 24312 KB Output is correct
3 Correct 20 ms 24192 KB Output is correct
4 Correct 22 ms 24320 KB Output is correct
5 Correct 22 ms 24320 KB Output is correct
6 Correct 21 ms 24192 KB Output is correct
7 Correct 21 ms 24192 KB Output is correct
8 Correct 24 ms 24312 KB Output is correct
9 Correct 22 ms 24268 KB Output is correct
10 Correct 22 ms 24312 KB Output is correct
11 Correct 22 ms 24320 KB Output is correct
12 Correct 22 ms 24192 KB Output is correct
13 Correct 21 ms 24192 KB Output is correct
14 Correct 22 ms 24192 KB Output is correct
15 Correct 22 ms 24192 KB Output is correct
16 Correct 22 ms 24064 KB Output is correct
17 Correct 21 ms 24344 KB Output is correct
18 Correct 22 ms 24320 KB Output is correct
19 Correct 22 ms 24240 KB Output is correct
20 Correct 20 ms 24192 KB Output is correct
21 Correct 22 ms 24320 KB Output is correct
22 Correct 22 ms 24196 KB Output is correct
23 Correct 24 ms 24320 KB Output is correct
24 Correct 21 ms 24320 KB Output is correct
25 Correct 22 ms 24320 KB Output is correct
26 Correct 24 ms 24780 KB Output is correct
27 Correct 24 ms 24704 KB Output is correct
28 Correct 24 ms 24832 KB Output is correct
29 Correct 24 ms 24832 KB Output is correct
30 Correct 25 ms 24704 KB Output is correct
31 Correct 21 ms 24192 KB Output is correct
32 Correct 24 ms 24960 KB Output is correct
33 Correct 21 ms 24192 KB Output is correct
34 Correct 24 ms 24704 KB Output is correct
35 Correct 23 ms 24696 KB Output is correct
36 Correct 24 ms 24704 KB Output is correct
37 Correct 24 ms 24704 KB Output is correct
38 Correct 21 ms 24320 KB Output is correct
39 Correct 23 ms 24704 KB Output is correct
40 Correct 25 ms 24704 KB Output is correct
41 Correct 24 ms 24704 KB Output is correct
42 Correct 25 ms 24696 KB Output is correct
43 Correct 24 ms 24832 KB Output is correct
44 Correct 21 ms 24320 KB Output is correct
45 Correct 23 ms 24832 KB Output is correct
46 Correct 25 ms 24700 KB Output is correct
47 Correct 21 ms 24192 KB Output is correct
48 Correct 24 ms 24704 KB Output is correct
49 Correct 23 ms 24824 KB Output is correct
50 Correct 24 ms 24832 KB Output is correct
51 Correct 24 ms 24704 KB Output is correct
52 Correct 24 ms 24696 KB Output is correct
53 Correct 24 ms 24704 KB Output is correct
54 Correct 23 ms 24704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24192 KB Output is correct
2 Correct 22 ms 24312 KB Output is correct
3 Correct 20 ms 24192 KB Output is correct
4 Correct 22 ms 24320 KB Output is correct
5 Correct 22 ms 24320 KB Output is correct
6 Correct 21 ms 24192 KB Output is correct
7 Correct 21 ms 24192 KB Output is correct
8 Correct 24 ms 24312 KB Output is correct
9 Correct 22 ms 24268 KB Output is correct
10 Correct 22 ms 24312 KB Output is correct
11 Correct 22 ms 24320 KB Output is correct
12 Correct 22 ms 24192 KB Output is correct
13 Correct 21 ms 24192 KB Output is correct
14 Correct 22 ms 24192 KB Output is correct
15 Correct 22 ms 24192 KB Output is correct
16 Correct 22 ms 24064 KB Output is correct
17 Correct 21 ms 24344 KB Output is correct
18 Correct 22 ms 24320 KB Output is correct
19 Correct 22 ms 24240 KB Output is correct
20 Correct 20 ms 24192 KB Output is correct
21 Correct 22 ms 24320 KB Output is correct
22 Correct 22 ms 24196 KB Output is correct
23 Correct 24 ms 24320 KB Output is correct
24 Correct 21 ms 24320 KB Output is correct
25 Correct 22 ms 24192 KB Output is correct
26 Correct 132 ms 38900 KB Output is correct
27 Correct 151 ms 38520 KB Output is correct
28 Correct 23 ms 24704 KB Output is correct
29 Correct 22 ms 24320 KB Output is correct
30 Correct 21 ms 24184 KB Output is correct
31 Correct 165 ms 38624 KB Output is correct
32 Correct 25 ms 24704 KB Output is correct
33 Correct 228 ms 44252 KB Output is correct
34 Correct 200 ms 38648 KB Output is correct
35 Correct 24 ms 24696 KB Output is correct
36 Correct 211 ms 39264 KB Output is correct
37 Correct 23 ms 24704 KB Output is correct
38 Correct 25 ms 24704 KB Output is correct
39 Correct 216 ms 38892 KB Output is correct
40 Correct 24 ms 24824 KB Output is correct
41 Correct 140 ms 38616 KB Output is correct
42 Correct 197 ms 40312 KB Output is correct
43 Correct 22 ms 24192 KB Output is correct
44 Correct 180 ms 44632 KB Output is correct
45 Correct 176 ms 41464 KB Output is correct
46 Correct 23 ms 24732 KB Output is correct
47 Correct 23 ms 24704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 38900 KB Output is correct
2 Correct 147 ms 41840 KB Output is correct
3 Correct 26 ms 24832 KB Output is correct
4 Correct 24 ms 24704 KB Output is correct
5 Correct 21 ms 24192 KB Output is correct
6 Correct 21 ms 24192 KB Output is correct
7 Correct 25 ms 24704 KB Output is correct
8 Correct 264 ms 40028 KB Output is correct
9 Correct 25 ms 24832 KB Output is correct
10 Correct 324 ms 39004 KB Output is correct
11 Correct 21 ms 24236 KB Output is correct
12 Correct 322 ms 39112 KB Output is correct
13 Correct 261 ms 39928 KB Output is correct
14 Correct 155 ms 41592 KB Output is correct
15 Correct 143 ms 38952 KB Output is correct
16 Correct 24 ms 24820 KB Output is correct
17 Correct 21 ms 24320 KB Output is correct
18 Correct 167 ms 41584 KB Output is correct
19 Correct 184 ms 44700 KB Output is correct
20 Correct 25 ms 24704 KB Output is correct
21 Correct 21 ms 24312 KB Output is correct
22 Correct 218 ms 40304 KB Output is correct
23 Correct 25 ms 24696 KB Output is correct
24 Correct 288 ms 39288 KB Output is correct
25 Correct 187 ms 43668 KB Output is correct
26 Correct 24 ms 24824 KB Output is correct
27 Correct 24 ms 24832 KB Output is correct
28 Correct 25 ms 24704 KB Output is correct
29 Correct 24 ms 24704 KB Output is correct
30 Correct 23 ms 24704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24192 KB Output is correct
2 Correct 22 ms 24312 KB Output is correct
3 Correct 20 ms 24192 KB Output is correct
4 Correct 22 ms 24320 KB Output is correct
5 Correct 22 ms 24320 KB Output is correct
6 Correct 21 ms 24192 KB Output is correct
7 Correct 21 ms 24192 KB Output is correct
8 Correct 24 ms 24312 KB Output is correct
9 Correct 22 ms 24268 KB Output is correct
10 Correct 22 ms 24312 KB Output is correct
11 Correct 22 ms 24320 KB Output is correct
12 Correct 22 ms 24192 KB Output is correct
13 Correct 21 ms 24192 KB Output is correct
14 Correct 22 ms 24192 KB Output is correct
15 Correct 22 ms 24192 KB Output is correct
16 Correct 22 ms 24064 KB Output is correct
17 Correct 21 ms 24344 KB Output is correct
18 Correct 22 ms 24320 KB Output is correct
19 Correct 22 ms 24240 KB Output is correct
20 Correct 20 ms 24192 KB Output is correct
21 Correct 22 ms 24320 KB Output is correct
22 Correct 22 ms 24196 KB Output is correct
23 Correct 24 ms 24320 KB Output is correct
24 Correct 21 ms 24320 KB Output is correct
25 Correct 22 ms 24320 KB Output is correct
26 Correct 24 ms 24780 KB Output is correct
27 Correct 24 ms 24704 KB Output is correct
28 Correct 24 ms 24832 KB Output is correct
29 Correct 24 ms 24832 KB Output is correct
30 Correct 25 ms 24704 KB Output is correct
31 Correct 21 ms 24192 KB Output is correct
32 Correct 24 ms 24960 KB Output is correct
33 Correct 21 ms 24192 KB Output is correct
34 Correct 24 ms 24704 KB Output is correct
35 Correct 23 ms 24696 KB Output is correct
36 Correct 24 ms 24704 KB Output is correct
37 Correct 24 ms 24704 KB Output is correct
38 Correct 21 ms 24320 KB Output is correct
39 Correct 23 ms 24704 KB Output is correct
40 Correct 25 ms 24704 KB Output is correct
41 Correct 24 ms 24704 KB Output is correct
42 Correct 25 ms 24696 KB Output is correct
43 Correct 24 ms 24832 KB Output is correct
44 Correct 21 ms 24320 KB Output is correct
45 Correct 23 ms 24832 KB Output is correct
46 Correct 25 ms 24700 KB Output is correct
47 Correct 21 ms 24192 KB Output is correct
48 Correct 24 ms 24704 KB Output is correct
49 Correct 23 ms 24824 KB Output is correct
50 Correct 24 ms 24832 KB Output is correct
51 Correct 24 ms 24704 KB Output is correct
52 Correct 24 ms 24696 KB Output is correct
53 Correct 24 ms 24704 KB Output is correct
54 Correct 23 ms 24704 KB Output is correct
55 Correct 22 ms 24192 KB Output is correct
56 Correct 132 ms 38900 KB Output is correct
57 Correct 151 ms 38520 KB Output is correct
58 Correct 23 ms 24704 KB Output is correct
59 Correct 22 ms 24320 KB Output is correct
60 Correct 21 ms 24184 KB Output is correct
61 Correct 165 ms 38624 KB Output is correct
62 Correct 25 ms 24704 KB Output is correct
63 Correct 228 ms 44252 KB Output is correct
64 Correct 200 ms 38648 KB Output is correct
65 Correct 24 ms 24696 KB Output is correct
66 Correct 211 ms 39264 KB Output is correct
67 Correct 23 ms 24704 KB Output is correct
68 Correct 25 ms 24704 KB Output is correct
69 Correct 216 ms 38892 KB Output is correct
70 Correct 24 ms 24824 KB Output is correct
71 Correct 140 ms 38616 KB Output is correct
72 Correct 197 ms 40312 KB Output is correct
73 Correct 22 ms 24192 KB Output is correct
74 Correct 180 ms 44632 KB Output is correct
75 Correct 176 ms 41464 KB Output is correct
76 Correct 23 ms 24732 KB Output is correct
77 Correct 23 ms 24704 KB Output is correct
78 Correct 205 ms 38900 KB Output is correct
79 Correct 147 ms 41840 KB Output is correct
80 Correct 26 ms 24832 KB Output is correct
81 Correct 24 ms 24704 KB Output is correct
82 Correct 21 ms 24192 KB Output is correct
83 Correct 21 ms 24192 KB Output is correct
84 Correct 25 ms 24704 KB Output is correct
85 Correct 264 ms 40028 KB Output is correct
86 Correct 25 ms 24832 KB Output is correct
87 Correct 324 ms 39004 KB Output is correct
88 Correct 21 ms 24236 KB Output is correct
89 Correct 322 ms 39112 KB Output is correct
90 Correct 261 ms 39928 KB Output is correct
91 Correct 155 ms 41592 KB Output is correct
92 Correct 143 ms 38952 KB Output is correct
93 Correct 24 ms 24820 KB Output is correct
94 Correct 21 ms 24320 KB Output is correct
95 Correct 167 ms 41584 KB Output is correct
96 Correct 184 ms 44700 KB Output is correct
97 Correct 25 ms 24704 KB Output is correct
98 Correct 21 ms 24312 KB Output is correct
99 Correct 218 ms 40304 KB Output is correct
100 Correct 25 ms 24696 KB Output is correct
101 Correct 288 ms 39288 KB Output is correct
102 Correct 187 ms 43668 KB Output is correct
103 Correct 24 ms 24824 KB Output is correct
104 Correct 24 ms 24832 KB Output is correct
105 Correct 25 ms 24704 KB Output is correct
106 Correct 24 ms 24704 KB Output is correct
107 Correct 23 ms 24704 KB Output is correct
108 Correct 1787 ms 103188 KB Output is correct
109 Correct 1781 ms 109904 KB Output is correct
110 Correct 1732 ms 118632 KB Output is correct
111 Correct 1466 ms 129356 KB Output is correct
112 Correct 1566 ms 121600 KB Output is correct
113 Correct 1083 ms 110684 KB Output is correct
114 Correct 994 ms 95584 KB Output is correct
115 Correct 1046 ms 95328 KB Output is correct
116 Correct 2478 ms 99868 KB Output is correct
117 Correct 1293 ms 111000 KB Output is correct
118 Correct 2133 ms 96468 KB Output is correct
119 Correct 1299 ms 110844 KB Output is correct
120 Correct 1338 ms 122016 KB Output is correct
121 Correct 1316 ms 111032 KB Output is correct
122 Correct 1899 ms 104608 KB Output is correct
123 Correct 864 ms 112344 KB Output is correct
124 Correct 1370 ms 108896 KB Output is correct