답안 #391453

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391453 2021-04-18T19:11:38 Z qwerasdfzxcl Mergers (JOI19_mergers) C++14
100 / 100
1539 ms 164480 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[500500], joi[500500], v[500500];
int col[500500], cnt[500500], val=0, val2=0;
bool visited[500500], visited2[500500], chk[500500];
struct LCA{
    int spa[500500][21], dep[500500];
    void dfs(int s, int pa=0){
        spa[s][0]=pa;
        for (int i=1;i<21 && spa[s][i-1];i++) spa[s][i] = spa[spa[s][i-1]][i-1];
        dep[s] = dep[pa]+1;
        for (auto &v:adj[s]) if (v!=pa){
            dfs(v, s);
        }
    }
    int query(int v, int u){
        if (dep[v]>dep[u]) swap(v, u);
        for (int i=20;i>=0;i--) if (spa[u][i] && dep[spa[u][i]]>dep[v]) u = spa[u][i];
        if (dep[v]!=dep[u]) u = spa[u][0];
        if (v!=u){
            for (int i=20;i>=0;i--) if (spa[v][i] && spa[v][i]!=spa[u][i]) v = spa[v][i], u = spa[u][i];
            v = spa[v][0], u = spa[u][0];
        }
        return v;
    }
}lca;

pair<int, int> dfs1(int s){
    pair<int, int> ret;
    ret.second = 1;
    for (auto &x:v[s]){
        ret.first += cnt[x];
    }
    visited[s]=1;
    //printf("%d %d %d\n", s, ret.first, ret.second);
    //printf(" %d %d %d\n", col[s], lct[col[s]], cnt[s]);
    for (auto &v:adj[s]) if (!visited[v]){
        auto tmp = dfs1(v);
        ret.first += tmp.first;
        ret.second += tmp.second;
    }
    if (ret.first==ret.second) chk[s]=1;
    //printf("%d %d %d\n", s, ret.first, ret.second);
    return ret;
}

int dfs2(int s, bool test){
    int ret=0;
    if (chk[s]) ret++;
    if (chk[s] && s!=1 && !test) test=1, val2++;
    visited2[s] = 1;
    for (auto &v:adj[s]) if (!visited2[v]){
        ret += dfs2(v, test);
    }
    if (chk[s] && ret==1 && s!=1) val++;
    return ret;
}

int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i=0;i<n-1;i++){
        int a, b;
        scanf("%d %d", &a, &b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int i=1;i<=n;i++){
        scanf("%d", col+i);
        cnt[col[i]]++;
        joi[col[i]].push_back(i);
    }
    lca.dfs(1);
    for (int i=1;i<=k;i++){
        int tmp;
        tmp = joi[i][0];
        if (cnt[i]==1){
            v[tmp].push_back(i); continue;
        }
        for (int j=1;j<cnt[i];j++){
            tmp = lca.query(tmp, joi[i][j]);
        }
        v[tmp].push_back(i);
    }
    dfs1(1);
    dfs2(1, 0);
    /*for (int i=1;i<=k;i++) printf("%d ", lct[i]);
    printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", chk[i]);
    printf("\n");*/
    if (val2==1) val++;
    //printf("%d\n", val2);
    printf("%d\n", (val+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]
   63 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   71 |         scanf("%d", col+i);
      |         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 35532 KB Output is correct
2 Correct 23 ms 35560 KB Output is correct
3 Correct 24 ms 35624 KB Output is correct
4 Correct 23 ms 35556 KB Output is correct
5 Correct 23 ms 35568 KB Output is correct
6 Correct 23 ms 35532 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 23 ms 35532 KB Output is correct
9 Correct 25 ms 35540 KB Output is correct
10 Correct 26 ms 35484 KB Output is correct
11 Correct 23 ms 35552 KB Output is correct
12 Correct 23 ms 35584 KB Output is correct
13 Correct 23 ms 35560 KB Output is correct
14 Correct 23 ms 35532 KB Output is correct
15 Correct 23 ms 35580 KB Output is correct
16 Correct 23 ms 35496 KB Output is correct
17 Correct 22 ms 35532 KB Output is correct
18 Correct 23 ms 35556 KB Output is correct
19 Correct 25 ms 35552 KB Output is correct
20 Correct 22 ms 35532 KB Output is correct
21 Correct 23 ms 35536 KB Output is correct
22 Correct 23 ms 35512 KB Output is correct
23 Correct 23 ms 35540 KB Output is correct
24 Correct 23 ms 35540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 35532 KB Output is correct
2 Correct 23 ms 35560 KB Output is correct
3 Correct 24 ms 35624 KB Output is correct
4 Correct 23 ms 35556 KB Output is correct
5 Correct 23 ms 35568 KB Output is correct
6 Correct 23 ms 35532 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 23 ms 35532 KB Output is correct
9 Correct 25 ms 35540 KB Output is correct
10 Correct 26 ms 35484 KB Output is correct
11 Correct 23 ms 35552 KB Output is correct
12 Correct 23 ms 35584 KB Output is correct
13 Correct 23 ms 35560 KB Output is correct
14 Correct 23 ms 35532 KB Output is correct
15 Correct 23 ms 35580 KB Output is correct
16 Correct 23 ms 35496 KB Output is correct
17 Correct 22 ms 35532 KB Output is correct
18 Correct 23 ms 35556 KB Output is correct
19 Correct 25 ms 35552 KB Output is correct
20 Correct 22 ms 35532 KB Output is correct
21 Correct 23 ms 35536 KB Output is correct
22 Correct 23 ms 35512 KB Output is correct
23 Correct 23 ms 35540 KB Output is correct
24 Correct 23 ms 35540 KB Output is correct
25 Correct 23 ms 35532 KB Output is correct
26 Correct 25 ms 36128 KB Output is correct
27 Correct 25 ms 36044 KB Output is correct
28 Correct 25 ms 36300 KB Output is correct
29 Correct 26 ms 36216 KB Output is correct
30 Correct 25 ms 36044 KB Output is correct
31 Correct 23 ms 35512 KB Output is correct
32 Correct 27 ms 36412 KB Output is correct
33 Correct 24 ms 35532 KB Output is correct
34 Correct 25 ms 36008 KB Output is correct
35 Correct 25 ms 36192 KB Output is correct
36 Correct 25 ms 35952 KB Output is correct
37 Correct 25 ms 36052 KB Output is correct
38 Correct 26 ms 35620 KB Output is correct
39 Correct 25 ms 36044 KB Output is correct
40 Correct 25 ms 36044 KB Output is correct
41 Correct 25 ms 36052 KB Output is correct
42 Correct 25 ms 36088 KB Output is correct
43 Correct 25 ms 36172 KB Output is correct
44 Correct 23 ms 35628 KB Output is correct
45 Correct 25 ms 36268 KB Output is correct
46 Correct 25 ms 36044 KB Output is correct
47 Correct 23 ms 35556 KB Output is correct
48 Correct 26 ms 35960 KB Output is correct
49 Correct 25 ms 36220 KB Output is correct
50 Correct 25 ms 36164 KB Output is correct
51 Correct 25 ms 36044 KB Output is correct
52 Correct 27 ms 36008 KB Output is correct
53 Correct 28 ms 36096 KB Output is correct
54 Correct 25 ms 35908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 35532 KB Output is correct
2 Correct 23 ms 35560 KB Output is correct
3 Correct 24 ms 35624 KB Output is correct
4 Correct 23 ms 35556 KB Output is correct
5 Correct 23 ms 35568 KB Output is correct
6 Correct 23 ms 35532 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 23 ms 35532 KB Output is correct
9 Correct 25 ms 35540 KB Output is correct
10 Correct 26 ms 35484 KB Output is correct
11 Correct 23 ms 35552 KB Output is correct
12 Correct 23 ms 35584 KB Output is correct
13 Correct 23 ms 35560 KB Output is correct
14 Correct 23 ms 35532 KB Output is correct
15 Correct 23 ms 35580 KB Output is correct
16 Correct 23 ms 35496 KB Output is correct
17 Correct 22 ms 35532 KB Output is correct
18 Correct 23 ms 35556 KB Output is correct
19 Correct 25 ms 35552 KB Output is correct
20 Correct 22 ms 35532 KB Output is correct
21 Correct 23 ms 35536 KB Output is correct
22 Correct 23 ms 35512 KB Output is correct
23 Correct 23 ms 35540 KB Output is correct
24 Correct 23 ms 35540 KB Output is correct
25 Correct 23 ms 35532 KB Output is correct
26 Correct 96 ms 49724 KB Output is correct
27 Correct 129 ms 49432 KB Output is correct
28 Correct 25 ms 36044 KB Output is correct
29 Correct 23 ms 35540 KB Output is correct
30 Correct 23 ms 35616 KB Output is correct
31 Correct 136 ms 49552 KB Output is correct
32 Correct 24 ms 35976 KB Output is correct
33 Correct 202 ms 56528 KB Output is correct
34 Correct 131 ms 49524 KB Output is correct
35 Correct 25 ms 36044 KB Output is correct
36 Correct 173 ms 50144 KB Output is correct
37 Correct 24 ms 35972 KB Output is correct
38 Correct 25 ms 35960 KB Output is correct
39 Correct 96 ms 49760 KB Output is correct
40 Correct 25 ms 36216 KB Output is correct
41 Correct 124 ms 49436 KB Output is correct
42 Correct 186 ms 51840 KB Output is correct
43 Correct 23 ms 35564 KB Output is correct
44 Correct 199 ms 56944 KB Output is correct
45 Correct 201 ms 53284 KB Output is correct
46 Correct 25 ms 35956 KB Output is correct
47 Correct 25 ms 36044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 49336 KB Output is correct
2 Correct 126 ms 55504 KB Output is correct
3 Correct 25 ms 36144 KB Output is correct
4 Correct 25 ms 36044 KB Output is correct
5 Correct 22 ms 35532 KB Output is correct
6 Correct 23 ms 35556 KB Output is correct
7 Correct 25 ms 35964 KB Output is correct
8 Correct 173 ms 51648 KB Output is correct
9 Correct 25 ms 35908 KB Output is correct
10 Correct 138 ms 49652 KB Output is correct
11 Correct 23 ms 35532 KB Output is correct
12 Correct 154 ms 49452 KB Output is correct
13 Correct 161 ms 51944 KB Output is correct
14 Correct 157 ms 55756 KB Output is correct
15 Correct 102 ms 49764 KB Output is correct
16 Correct 25 ms 36088 KB Output is correct
17 Correct 23 ms 35604 KB Output is correct
18 Correct 128 ms 55012 KB Output is correct
19 Correct 192 ms 59592 KB Output is correct
20 Correct 29 ms 36148 KB Output is correct
21 Correct 22 ms 35588 KB Output is correct
22 Correct 129 ms 52440 KB Output is correct
23 Correct 26 ms 36028 KB Output is correct
24 Correct 147 ms 50792 KB Output is correct
25 Correct 189 ms 59124 KB Output is correct
26 Correct 25 ms 36184 KB Output is correct
27 Correct 30 ms 36200 KB Output is correct
28 Correct 25 ms 36036 KB Output is correct
29 Correct 25 ms 36008 KB Output is correct
30 Correct 25 ms 36080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 35532 KB Output is correct
2 Correct 23 ms 35560 KB Output is correct
3 Correct 24 ms 35624 KB Output is correct
4 Correct 23 ms 35556 KB Output is correct
5 Correct 23 ms 35568 KB Output is correct
6 Correct 23 ms 35532 KB Output is correct
7 Correct 23 ms 35532 KB Output is correct
8 Correct 23 ms 35532 KB Output is correct
9 Correct 25 ms 35540 KB Output is correct
10 Correct 26 ms 35484 KB Output is correct
11 Correct 23 ms 35552 KB Output is correct
12 Correct 23 ms 35584 KB Output is correct
13 Correct 23 ms 35560 KB Output is correct
14 Correct 23 ms 35532 KB Output is correct
15 Correct 23 ms 35580 KB Output is correct
16 Correct 23 ms 35496 KB Output is correct
17 Correct 22 ms 35532 KB Output is correct
18 Correct 23 ms 35556 KB Output is correct
19 Correct 25 ms 35552 KB Output is correct
20 Correct 22 ms 35532 KB Output is correct
21 Correct 23 ms 35536 KB Output is correct
22 Correct 23 ms 35512 KB Output is correct
23 Correct 23 ms 35540 KB Output is correct
24 Correct 23 ms 35540 KB Output is correct
25 Correct 23 ms 35532 KB Output is correct
26 Correct 25 ms 36128 KB Output is correct
27 Correct 25 ms 36044 KB Output is correct
28 Correct 25 ms 36300 KB Output is correct
29 Correct 26 ms 36216 KB Output is correct
30 Correct 25 ms 36044 KB Output is correct
31 Correct 23 ms 35512 KB Output is correct
32 Correct 27 ms 36412 KB Output is correct
33 Correct 24 ms 35532 KB Output is correct
34 Correct 25 ms 36008 KB Output is correct
35 Correct 25 ms 36192 KB Output is correct
36 Correct 25 ms 35952 KB Output is correct
37 Correct 25 ms 36052 KB Output is correct
38 Correct 26 ms 35620 KB Output is correct
39 Correct 25 ms 36044 KB Output is correct
40 Correct 25 ms 36044 KB Output is correct
41 Correct 25 ms 36052 KB Output is correct
42 Correct 25 ms 36088 KB Output is correct
43 Correct 25 ms 36172 KB Output is correct
44 Correct 23 ms 35628 KB Output is correct
45 Correct 25 ms 36268 KB Output is correct
46 Correct 25 ms 36044 KB Output is correct
47 Correct 23 ms 35556 KB Output is correct
48 Correct 26 ms 35960 KB Output is correct
49 Correct 25 ms 36220 KB Output is correct
50 Correct 25 ms 36164 KB Output is correct
51 Correct 25 ms 36044 KB Output is correct
52 Correct 27 ms 36008 KB Output is correct
53 Correct 28 ms 36096 KB Output is correct
54 Correct 25 ms 35908 KB Output is correct
55 Correct 23 ms 35532 KB Output is correct
56 Correct 96 ms 49724 KB Output is correct
57 Correct 129 ms 49432 KB Output is correct
58 Correct 25 ms 36044 KB Output is correct
59 Correct 23 ms 35540 KB Output is correct
60 Correct 23 ms 35616 KB Output is correct
61 Correct 136 ms 49552 KB Output is correct
62 Correct 24 ms 35976 KB Output is correct
63 Correct 202 ms 56528 KB Output is correct
64 Correct 131 ms 49524 KB Output is correct
65 Correct 25 ms 36044 KB Output is correct
66 Correct 173 ms 50144 KB Output is correct
67 Correct 24 ms 35972 KB Output is correct
68 Correct 25 ms 35960 KB Output is correct
69 Correct 96 ms 49760 KB Output is correct
70 Correct 25 ms 36216 KB Output is correct
71 Correct 124 ms 49436 KB Output is correct
72 Correct 186 ms 51840 KB Output is correct
73 Correct 23 ms 35564 KB Output is correct
74 Correct 199 ms 56944 KB Output is correct
75 Correct 201 ms 53284 KB Output is correct
76 Correct 25 ms 35956 KB Output is correct
77 Correct 25 ms 36044 KB Output is correct
78 Correct 116 ms 49336 KB Output is correct
79 Correct 126 ms 55504 KB Output is correct
80 Correct 25 ms 36144 KB Output is correct
81 Correct 25 ms 36044 KB Output is correct
82 Correct 22 ms 35532 KB Output is correct
83 Correct 23 ms 35556 KB Output is correct
84 Correct 25 ms 35964 KB Output is correct
85 Correct 173 ms 51648 KB Output is correct
86 Correct 25 ms 35908 KB Output is correct
87 Correct 138 ms 49652 KB Output is correct
88 Correct 23 ms 35532 KB Output is correct
89 Correct 154 ms 49452 KB Output is correct
90 Correct 161 ms 51944 KB Output is correct
91 Correct 157 ms 55756 KB Output is correct
92 Correct 102 ms 49764 KB Output is correct
93 Correct 25 ms 36088 KB Output is correct
94 Correct 23 ms 35604 KB Output is correct
95 Correct 128 ms 55012 KB Output is correct
96 Correct 192 ms 59592 KB Output is correct
97 Correct 29 ms 36148 KB Output is correct
98 Correct 22 ms 35588 KB Output is correct
99 Correct 129 ms 52440 KB Output is correct
100 Correct 26 ms 36028 KB Output is correct
101 Correct 147 ms 50792 KB Output is correct
102 Correct 189 ms 59124 KB Output is correct
103 Correct 25 ms 36184 KB Output is correct
104 Correct 30 ms 36200 KB Output is correct
105 Correct 25 ms 36036 KB Output is correct
106 Correct 25 ms 36008 KB Output is correct
107 Correct 25 ms 36080 KB Output is correct
108 Correct 711 ms 119112 KB Output is correct
109 Correct 1539 ms 125328 KB Output is correct
110 Correct 1535 ms 136612 KB Output is correct
111 Correct 1530 ms 164480 KB Output is correct
112 Correct 1415 ms 154628 KB Output is correct
113 Correct 916 ms 138336 KB Output is correct
114 Correct 803 ms 107452 KB Output is correct
115 Correct 796 ms 107388 KB Output is correct
116 Correct 1062 ms 113696 KB Output is correct
117 Correct 1177 ms 141416 KB Output is correct
118 Correct 854 ms 108492 KB Output is correct
119 Correct 1142 ms 141552 KB Output is correct
120 Correct 1409 ms 155596 KB Output is correct
121 Correct 1138 ms 141320 KB Output is correct
122 Correct 1127 ms 124040 KB Output is correct
123 Correct 850 ms 142864 KB Output is correct
124 Correct 1283 ms 139200 KB Output is correct