Submission #391434

# Submission time Handle Problem Language Result Execution time Memory
391434 2021-04-18T18:09:18 Z qwerasdfzxcl Mergers (JOI19_mergers) C++14
48 / 100
3000 ms 36896 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[500500];
int col[500500], cnt[500500], val=0, val2=0;
bool visited[500500], visited2[500500], chk[500500];

unordered_map<int, int> dfs1(int s){
    unordered_map<int, int> ret;
    ret[col[s]] = 1;
    visited[s]=1;
    for (auto &v:adj[s]) if (!visited[v]){
        auto tmp = dfs1(v);
        for (auto &p:tmp){
            ret[p.first] += p.second;
        }
    }
    chk[s] = 1;
    for (auto &p:ret){
        if (p.second!=cnt[p.first]){
            chk[s] = 0; break;
        }
    }
    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]]++;
    }
    dfs1(1);
    dfs2(1, 0);
    /*for (int i=1;i<=n;i++) printf("%d ", col[i]);
    printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", cnt[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:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   42 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |         scanf("%d", col+i);
      |         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12036 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 11984 KB Output is correct
6 Correct 8 ms 12052 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 9 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 12000 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 9 ms 11980 KB Output is correct
15 Correct 9 ms 11980 KB Output is correct
16 Correct 9 ms 11980 KB Output is correct
17 Correct 10 ms 12032 KB Output is correct
18 Correct 8 ms 12080 KB Output is correct
19 Correct 8 ms 11984 KB Output is correct
20 Correct 10 ms 11980 KB Output is correct
21 Correct 8 ms 11980 KB Output is correct
22 Correct 8 ms 12092 KB Output is correct
23 Correct 8 ms 11980 KB Output is correct
24 Correct 8 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12036 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 11984 KB Output is correct
6 Correct 8 ms 12052 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 9 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 12000 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 9 ms 11980 KB Output is correct
15 Correct 9 ms 11980 KB Output is correct
16 Correct 9 ms 11980 KB Output is correct
17 Correct 10 ms 12032 KB Output is correct
18 Correct 8 ms 12080 KB Output is correct
19 Correct 8 ms 11984 KB Output is correct
20 Correct 10 ms 11980 KB Output is correct
21 Correct 8 ms 11980 KB Output is correct
22 Correct 8 ms 12092 KB Output is correct
23 Correct 8 ms 11980 KB Output is correct
24 Correct 8 ms 11980 KB Output is correct
25 Correct 8 ms 12156 KB Output is correct
26 Correct 15 ms 12364 KB Output is correct
27 Correct 11 ms 12208 KB Output is correct
28 Correct 230 ms 12696 KB Output is correct
29 Correct 13 ms 12492 KB Output is correct
30 Correct 11 ms 12200 KB Output is correct
31 Correct 8 ms 11980 KB Output is correct
32 Correct 281 ms 12712 KB Output is correct
33 Correct 8 ms 11980 KB Output is correct
34 Correct 10 ms 12236 KB Output is correct
35 Correct 15 ms 12488 KB Output is correct
36 Correct 11 ms 12236 KB Output is correct
37 Correct 15 ms 12324 KB Output is correct
38 Correct 9 ms 11980 KB Output is correct
39 Correct 14 ms 12364 KB Output is correct
40 Correct 10 ms 12236 KB Output is correct
41 Correct 10 ms 12236 KB Output is correct
42 Correct 10 ms 12364 KB Output is correct
43 Correct 10 ms 12772 KB Output is correct
44 Correct 8 ms 11980 KB Output is correct
45 Correct 122 ms 12576 KB Output is correct
46 Correct 17 ms 12364 KB Output is correct
47 Correct 8 ms 11980 KB Output is correct
48 Correct 14 ms 12292 KB Output is correct
49 Correct 11 ms 12504 KB Output is correct
50 Correct 127 ms 12560 KB Output is correct
51 Correct 11 ms 12236 KB Output is correct
52 Correct 10 ms 12236 KB Output is correct
53 Correct 10 ms 12364 KB Output is correct
54 Correct 12 ms 12244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12036 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 11984 KB Output is correct
6 Correct 8 ms 12052 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 9 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 12000 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 9 ms 11980 KB Output is correct
15 Correct 9 ms 11980 KB Output is correct
16 Correct 9 ms 11980 KB Output is correct
17 Correct 10 ms 12032 KB Output is correct
18 Correct 8 ms 12080 KB Output is correct
19 Correct 8 ms 11984 KB Output is correct
20 Correct 10 ms 11980 KB Output is correct
21 Correct 8 ms 11980 KB Output is correct
22 Correct 8 ms 12092 KB Output is correct
23 Correct 8 ms 11980 KB Output is correct
24 Correct 8 ms 11980 KB Output is correct
25 Correct 8 ms 11980 KB Output is correct
26 Correct 76 ms 17680 KB Output is correct
27 Correct 126 ms 17468 KB Output is correct
28 Correct 11 ms 12236 KB Output is correct
29 Correct 8 ms 11980 KB Output is correct
30 Correct 8 ms 12040 KB Output is correct
31 Correct 94 ms 17452 KB Output is correct
32 Correct 10 ms 12236 KB Output is correct
33 Correct 494 ms 36164 KB Output is correct
34 Correct 117 ms 17352 KB Output is correct
35 Correct 10 ms 12260 KB Output is correct
36 Correct 127 ms 19396 KB Output is correct
37 Correct 10 ms 12196 KB Output is correct
38 Correct 12 ms 12236 KB Output is correct
39 Correct 72 ms 17676 KB Output is correct
40 Correct 10 ms 12748 KB Output is correct
41 Correct 109 ms 17352 KB Output is correct
42 Correct 319 ms 23620 KB Output is correct
43 Correct 8 ms 12004 KB Output is correct
44 Correct 123 ms 36896 KB Output is correct
45 Correct 290 ms 27912 KB Output is correct
46 Correct 10 ms 12228 KB Output is correct
47 Correct 11 ms 12236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 16348 KB Output is correct
2 Correct 111 ms 24892 KB Output is correct
3 Correct 14 ms 12468 KB Output is correct
4 Correct 10 ms 12236 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 11 ms 12164 KB Output is correct
8 Correct 665 ms 22468 KB Output is correct
9 Correct 10 ms 12228 KB Output is correct
10 Correct 116 ms 17580 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 137 ms 17640 KB Output is correct
13 Correct 388 ms 22284 KB Output is correct
14 Correct 357 ms 26300 KB Output is correct
15 Correct 72 ms 17708 KB Output is correct
16 Correct 12 ms 12364 KB Output is correct
17 Correct 9 ms 11988 KB Output is correct
18 Correct 129 ms 25800 KB Output is correct
19 Execution timed out 3068 ms 27864 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12036 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 11984 KB Output is correct
6 Correct 8 ms 12052 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 9 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 12000 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 9 ms 11980 KB Output is correct
15 Correct 9 ms 11980 KB Output is correct
16 Correct 9 ms 11980 KB Output is correct
17 Correct 10 ms 12032 KB Output is correct
18 Correct 8 ms 12080 KB Output is correct
19 Correct 8 ms 11984 KB Output is correct
20 Correct 10 ms 11980 KB Output is correct
21 Correct 8 ms 11980 KB Output is correct
22 Correct 8 ms 12092 KB Output is correct
23 Correct 8 ms 11980 KB Output is correct
24 Correct 8 ms 11980 KB Output is correct
25 Correct 8 ms 12156 KB Output is correct
26 Correct 15 ms 12364 KB Output is correct
27 Correct 11 ms 12208 KB Output is correct
28 Correct 230 ms 12696 KB Output is correct
29 Correct 13 ms 12492 KB Output is correct
30 Correct 11 ms 12200 KB Output is correct
31 Correct 8 ms 11980 KB Output is correct
32 Correct 281 ms 12712 KB Output is correct
33 Correct 8 ms 11980 KB Output is correct
34 Correct 10 ms 12236 KB Output is correct
35 Correct 15 ms 12488 KB Output is correct
36 Correct 11 ms 12236 KB Output is correct
37 Correct 15 ms 12324 KB Output is correct
38 Correct 9 ms 11980 KB Output is correct
39 Correct 14 ms 12364 KB Output is correct
40 Correct 10 ms 12236 KB Output is correct
41 Correct 10 ms 12236 KB Output is correct
42 Correct 10 ms 12364 KB Output is correct
43 Correct 10 ms 12772 KB Output is correct
44 Correct 8 ms 11980 KB Output is correct
45 Correct 122 ms 12576 KB Output is correct
46 Correct 17 ms 12364 KB Output is correct
47 Correct 8 ms 11980 KB Output is correct
48 Correct 14 ms 12292 KB Output is correct
49 Correct 11 ms 12504 KB Output is correct
50 Correct 127 ms 12560 KB Output is correct
51 Correct 11 ms 12236 KB Output is correct
52 Correct 10 ms 12236 KB Output is correct
53 Correct 10 ms 12364 KB Output is correct
54 Correct 12 ms 12244 KB Output is correct
55 Correct 8 ms 11980 KB Output is correct
56 Correct 76 ms 17680 KB Output is correct
57 Correct 126 ms 17468 KB Output is correct
58 Correct 11 ms 12236 KB Output is correct
59 Correct 8 ms 11980 KB Output is correct
60 Correct 8 ms 12040 KB Output is correct
61 Correct 94 ms 17452 KB Output is correct
62 Correct 10 ms 12236 KB Output is correct
63 Correct 494 ms 36164 KB Output is correct
64 Correct 117 ms 17352 KB Output is correct
65 Correct 10 ms 12260 KB Output is correct
66 Correct 127 ms 19396 KB Output is correct
67 Correct 10 ms 12196 KB Output is correct
68 Correct 12 ms 12236 KB Output is correct
69 Correct 72 ms 17676 KB Output is correct
70 Correct 10 ms 12748 KB Output is correct
71 Correct 109 ms 17352 KB Output is correct
72 Correct 319 ms 23620 KB Output is correct
73 Correct 8 ms 12004 KB Output is correct
74 Correct 123 ms 36896 KB Output is correct
75 Correct 290 ms 27912 KB Output is correct
76 Correct 10 ms 12228 KB Output is correct
77 Correct 11 ms 12236 KB Output is correct
78 Correct 74 ms 16348 KB Output is correct
79 Correct 111 ms 24892 KB Output is correct
80 Correct 14 ms 12468 KB Output is correct
81 Correct 10 ms 12236 KB Output is correct
82 Correct 8 ms 11980 KB Output is correct
83 Correct 8 ms 11980 KB Output is correct
84 Correct 11 ms 12164 KB Output is correct
85 Correct 665 ms 22468 KB Output is correct
86 Correct 10 ms 12228 KB Output is correct
87 Correct 116 ms 17580 KB Output is correct
88 Correct 8 ms 11980 KB Output is correct
89 Correct 137 ms 17640 KB Output is correct
90 Correct 388 ms 22284 KB Output is correct
91 Correct 357 ms 26300 KB Output is correct
92 Correct 72 ms 17708 KB Output is correct
93 Correct 12 ms 12364 KB Output is correct
94 Correct 9 ms 11988 KB Output is correct
95 Correct 129 ms 25800 KB Output is correct
96 Execution timed out 3068 ms 27864 KB Time limit exceeded
97 Halted 0 ms 0 KB -