Submission #128926

# Submission time Handle Problem Language Result Execution time Memory
128926 2019-07-11T10:58:54 Z kuroni Mergers (JOI19_mergers) C++17
100 / 100
1565 ms 64004 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 500005, K = 500005;

int n, k, u, v, ans = 0, a[N], sub[N], f[N];
int cur = 0, sum[K], cnt[K];
vector<int> adj[N];

void add(int u, int p, int val, int av = 0)
{
    cur -= (cnt[a[u]] == sum[a[u]] || cnt[a[u]] == 0);
    cnt[a[u]] += val;
    cur += (cnt[a[u]] == sum[a[u]] || cnt[a[u]] == 0);
    for (int &v : adj[u])
        if (v != p && v != av)
            add(v, u, val, av);
}

int DFS_1(int u, int p = 0)
{
    sub[u] = 1;
    for (int &v : adj[u])
        if (v != p)
            sub[u] += DFS_1(v, u);
    return sub[u];
}

bool DFS_2(int u, int p, bool big)
{
    int mc = 0;
    for (int &v : adj[u])
        if (v != p && sub[v] > sub[mc])
            mc = v;
    for (int &v : adj[u])
        if (v != p && v != mc)
        {
            if (DFS_2(v, u, false))
                f[u]++;
            else
                f[u] += f[v];
        }
    if (mc != 0)
    {
        if (DFS_2(mc, u, true))
            f[u]++;
        else
            f[u] += f[mc];
    }
    add(u, p, 1, mc);
    bool chk = (cur == 0);
    if (cur == 0)
        ans += (f[u] == (u == 1));
    if (!big)
        add(u, p, -1);
    return chk;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> k;
    for (int i = 1; i < n; i++)
    {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum[a[i]]++;
    }
    DFS_1(1);
    DFS_2(1, 0, true);
    cout << (ans + 1) / 2;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12124 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 13 ms 12144 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 15 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 16 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12128 KB Output is correct
24 Correct 15 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12124 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 13 ms 12144 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 15 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 16 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12128 KB Output is correct
24 Correct 15 ms 12152 KB Output is correct
25 Correct 12 ms 12152 KB Output is correct
26 Correct 15 ms 12280 KB Output is correct
27 Correct 15 ms 12280 KB Output is correct
28 Correct 15 ms 12408 KB Output is correct
29 Correct 15 ms 12312 KB Output is correct
30 Correct 15 ms 12280 KB Output is correct
31 Correct 12 ms 12152 KB Output is correct
32 Correct 15 ms 12408 KB Output is correct
33 Correct 12 ms 12152 KB Output is correct
34 Correct 13 ms 12280 KB Output is correct
35 Correct 15 ms 12280 KB Output is correct
36 Correct 15 ms 12280 KB Output is correct
37 Correct 15 ms 12328 KB Output is correct
38 Correct 12 ms 12152 KB Output is correct
39 Correct 14 ms 12280 KB Output is correct
40 Correct 14 ms 12280 KB Output is correct
41 Correct 17 ms 12280 KB Output is correct
42 Correct 14 ms 12280 KB Output is correct
43 Correct 15 ms 12508 KB Output is correct
44 Correct 14 ms 12124 KB Output is correct
45 Correct 14 ms 12408 KB Output is correct
46 Correct 15 ms 12284 KB Output is correct
47 Correct 12 ms 12152 KB Output is correct
48 Correct 15 ms 12252 KB Output is correct
49 Correct 14 ms 12380 KB Output is correct
50 Correct 14 ms 12408 KB Output is correct
51 Correct 15 ms 12280 KB Output is correct
52 Correct 14 ms 12280 KB Output is correct
53 Correct 14 ms 12280 KB Output is correct
54 Correct 16 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12124 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 13 ms 12144 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 15 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 16 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12128 KB Output is correct
24 Correct 15 ms 12152 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 72 ms 16836 KB Output is correct
27 Correct 125 ms 17004 KB Output is correct
28 Correct 14 ms 12280 KB Output is correct
29 Correct 13 ms 12152 KB Output is correct
30 Correct 13 ms 12152 KB Output is correct
31 Correct 128 ms 17016 KB Output is correct
32 Correct 17 ms 12284 KB Output is correct
33 Correct 130 ms 24136 KB Output is correct
34 Correct 131 ms 17012 KB Output is correct
35 Correct 15 ms 12408 KB Output is correct
36 Correct 143 ms 17720 KB Output is correct
37 Correct 14 ms 12280 KB Output is correct
38 Correct 15 ms 12308 KB Output is correct
39 Correct 73 ms 16960 KB Output is correct
40 Correct 15 ms 12540 KB Output is correct
41 Correct 160 ms 17016 KB Output is correct
42 Correct 137 ms 19376 KB Output is correct
43 Correct 13 ms 12124 KB Output is correct
44 Correct 113 ms 24548 KB Output is correct
45 Correct 103 ms 20600 KB Output is correct
46 Correct 14 ms 12280 KB Output is correct
47 Correct 15 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 16368 KB Output is correct
2 Correct 83 ms 17516 KB Output is correct
3 Correct 15 ms 12268 KB Output is correct
4 Correct 15 ms 12280 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 14 ms 12280 KB Output is correct
8 Correct 133 ms 17272 KB Output is correct
9 Correct 15 ms 12280 KB Output is correct
10 Correct 141 ms 16888 KB Output is correct
11 Correct 16 ms 12152 KB Output is correct
12 Correct 153 ms 16844 KB Output is correct
13 Correct 152 ms 17144 KB Output is correct
14 Correct 151 ms 17680 KB Output is correct
15 Correct 71 ms 16752 KB Output is correct
16 Correct 14 ms 12252 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 83 ms 17516 KB Output is correct
19 Correct 153 ms 21488 KB Output is correct
20 Correct 15 ms 12280 KB Output is correct
21 Correct 12 ms 12152 KB Output is correct
22 Correct 90 ms 17236 KB Output is correct
23 Correct 15 ms 12280 KB Output is correct
24 Correct 167 ms 16964 KB Output is correct
25 Correct 112 ms 20216 KB Output is correct
26 Correct 17 ms 12280 KB Output is correct
27 Correct 15 ms 12408 KB Output is correct
28 Correct 15 ms 12280 KB Output is correct
29 Correct 14 ms 12280 KB Output is correct
30 Correct 14 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12124 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 14 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 13 ms 12144 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 15 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 16 ms 12152 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 12 ms 12152 KB Output is correct
23 Correct 13 ms 12128 KB Output is correct
24 Correct 15 ms 12152 KB Output is correct
25 Correct 12 ms 12152 KB Output is correct
26 Correct 15 ms 12280 KB Output is correct
27 Correct 15 ms 12280 KB Output is correct
28 Correct 15 ms 12408 KB Output is correct
29 Correct 15 ms 12312 KB Output is correct
30 Correct 15 ms 12280 KB Output is correct
31 Correct 12 ms 12152 KB Output is correct
32 Correct 15 ms 12408 KB Output is correct
33 Correct 12 ms 12152 KB Output is correct
34 Correct 13 ms 12280 KB Output is correct
35 Correct 15 ms 12280 KB Output is correct
36 Correct 15 ms 12280 KB Output is correct
37 Correct 15 ms 12328 KB Output is correct
38 Correct 12 ms 12152 KB Output is correct
39 Correct 14 ms 12280 KB Output is correct
40 Correct 14 ms 12280 KB Output is correct
41 Correct 17 ms 12280 KB Output is correct
42 Correct 14 ms 12280 KB Output is correct
43 Correct 15 ms 12508 KB Output is correct
44 Correct 14 ms 12124 KB Output is correct
45 Correct 14 ms 12408 KB Output is correct
46 Correct 15 ms 12284 KB Output is correct
47 Correct 12 ms 12152 KB Output is correct
48 Correct 15 ms 12252 KB Output is correct
49 Correct 14 ms 12380 KB Output is correct
50 Correct 14 ms 12408 KB Output is correct
51 Correct 15 ms 12280 KB Output is correct
52 Correct 14 ms 12280 KB Output is correct
53 Correct 14 ms 12280 KB Output is correct
54 Correct 16 ms 12280 KB Output is correct
55 Correct 13 ms 12152 KB Output is correct
56 Correct 72 ms 16836 KB Output is correct
57 Correct 125 ms 17004 KB Output is correct
58 Correct 14 ms 12280 KB Output is correct
59 Correct 13 ms 12152 KB Output is correct
60 Correct 13 ms 12152 KB Output is correct
61 Correct 128 ms 17016 KB Output is correct
62 Correct 17 ms 12284 KB Output is correct
63 Correct 130 ms 24136 KB Output is correct
64 Correct 131 ms 17012 KB Output is correct
65 Correct 15 ms 12408 KB Output is correct
66 Correct 143 ms 17720 KB Output is correct
67 Correct 14 ms 12280 KB Output is correct
68 Correct 15 ms 12308 KB Output is correct
69 Correct 73 ms 16960 KB Output is correct
70 Correct 15 ms 12540 KB Output is correct
71 Correct 160 ms 17016 KB Output is correct
72 Correct 137 ms 19376 KB Output is correct
73 Correct 13 ms 12124 KB Output is correct
74 Correct 113 ms 24548 KB Output is correct
75 Correct 103 ms 20600 KB Output is correct
76 Correct 14 ms 12280 KB Output is correct
77 Correct 15 ms 12280 KB Output is correct
78 Correct 71 ms 16368 KB Output is correct
79 Correct 83 ms 17516 KB Output is correct
80 Correct 15 ms 12268 KB Output is correct
81 Correct 15 ms 12280 KB Output is correct
82 Correct 13 ms 12152 KB Output is correct
83 Correct 13 ms 12152 KB Output is correct
84 Correct 14 ms 12280 KB Output is correct
85 Correct 133 ms 17272 KB Output is correct
86 Correct 15 ms 12280 KB Output is correct
87 Correct 141 ms 16888 KB Output is correct
88 Correct 16 ms 12152 KB Output is correct
89 Correct 153 ms 16844 KB Output is correct
90 Correct 152 ms 17144 KB Output is correct
91 Correct 151 ms 17680 KB Output is correct
92 Correct 71 ms 16752 KB Output is correct
93 Correct 14 ms 12252 KB Output is correct
94 Correct 12 ms 12152 KB Output is correct
95 Correct 83 ms 17516 KB Output is correct
96 Correct 153 ms 21488 KB Output is correct
97 Correct 15 ms 12280 KB Output is correct
98 Correct 12 ms 12152 KB Output is correct
99 Correct 90 ms 17236 KB Output is correct
100 Correct 15 ms 12280 KB Output is correct
101 Correct 167 ms 16964 KB Output is correct
102 Correct 112 ms 20216 KB Output is correct
103 Correct 17 ms 12280 KB Output is correct
104 Correct 15 ms 12408 KB Output is correct
105 Correct 15 ms 12280 KB Output is correct
106 Correct 14 ms 12280 KB Output is correct
107 Correct 14 ms 12280 KB Output is correct
108 Correct 477 ms 35588 KB Output is correct
109 Correct 1039 ms 52580 KB Output is correct
110 Correct 922 ms 64004 KB Output is correct
111 Correct 1085 ms 61424 KB Output is correct
112 Correct 1006 ms 51808 KB Output is correct
113 Correct 536 ms 37724 KB Output is correct
114 Correct 1061 ms 34560 KB Output is correct
115 Correct 1051 ms 34596 KB Output is correct
116 Correct 1186 ms 35488 KB Output is correct
117 Correct 1307 ms 38496 KB Output is correct
118 Correct 1148 ms 34188 KB Output is correct
119 Correct 1377 ms 38496 KB Output is correct
120 Correct 971 ms 52448 KB Output is correct
121 Correct 1287 ms 38628 KB Output is correct
122 Correct 1180 ms 37100 KB Output is correct
123 Correct 496 ms 38080 KB Output is correct
124 Correct 1565 ms 37884 KB Output is correct