Submission #105865

# Submission time Handle Problem Language Result Execution time Memory
105865 2019-04-15T11:19:50 Z JustasZ Mergers (JOI19_mergers) C++14
56 / 100
3000 ms 57744 KB
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int>pii;
const int maxn = 5e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, k, anc[maxn][20], dep[maxn], par[maxn];
vector<int>adj[maxn], grp[maxn], g[maxn];
void predfs(int v)
{
    dep[v] = dep[anc[v][0]] + 1;
    for(int i = 1; i < 20; i++)
        anc[v][i] = anc[anc[v][i - 1]][i - 1];
    for(int to : adj[v])
    {
        if(to != anc[v][0])
        {
            anc[to][0] = v;
            predfs(to);
        }
    }
}
int lca(int a, int b)
{
    if(dep[a] > dep[b]) swap(a, b);
    for(int i = 19; i >= 0; i--)
        if(dep[anc[b][i]] >= dep[a])
            b = anc[b][i];
    if(a == b) return a;
    for(int i = 19; i >= 0; i--)
        if(anc[a][i] != anc[b][i])
            a = anc[a][i], b = anc[b][i];
    return anc[a][0];
}
int root(int v)
{
    return v == par[v] ? v : par[v] = root(par[v]);
}
void unite(int a, int b)
{
    a = root(a), b = root(b);
    if(a == b) return;
    par[b] = a;
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
        par[i] = i;
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    predfs(1);
    for(int i = 1; i <= n; i++)
    {
        int g; cin >> g;
        grp[g].pb(i);
    }
    for(int kk = 1; kk <= k; kk++)
    {
        int LCA = -1;
        for(int a : grp[kk])
        {
            if(LCA == -1)
                LCA = a;
            else
                LCA = lca(LCA, a);
        }
        for(int a : grp[kk])
        {
            int v = a;
            while(v != LCA)
            {
                unite(v, anc[v][0]);
                v = anc[v][0];
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int to : adj[i])
        {
            if(root(i) < root(to))
            {
                g[root(i)].pb(root(to));
                g[root(to)].pb(root(i));
            }
        }
    }
    int leaves = 0;
    for(int i = 1; i <= n; i++)
        if(sz(g[i]) == 1) ++leaves;
    cout << (leaves + 1) / 2 << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 40 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 38 ms 35652 KB Output is correct
4 Correct 38 ms 35576 KB Output is correct
5 Correct 41 ms 35584 KB Output is correct
6 Correct 37 ms 35576 KB Output is correct
7 Correct 37 ms 35576 KB Output is correct
8 Correct 42 ms 35576 KB Output is correct
9 Correct 39 ms 35672 KB Output is correct
10 Correct 36 ms 35520 KB Output is correct
11 Correct 37 ms 35556 KB Output is correct
12 Correct 38 ms 35576 KB Output is correct
13 Correct 37 ms 35704 KB Output is correct
14 Correct 36 ms 35628 KB Output is correct
15 Correct 36 ms 35548 KB Output is correct
16 Correct 43 ms 35676 KB Output is correct
17 Correct 36 ms 35548 KB Output is correct
18 Correct 38 ms 35548 KB Output is correct
19 Correct 39 ms 35704 KB Output is correct
20 Correct 41 ms 35704 KB Output is correct
21 Correct 37 ms 35704 KB Output is correct
22 Correct 44 ms 35576 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 37 ms 35712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 38 ms 35652 KB Output is correct
4 Correct 38 ms 35576 KB Output is correct
5 Correct 41 ms 35584 KB Output is correct
6 Correct 37 ms 35576 KB Output is correct
7 Correct 37 ms 35576 KB Output is correct
8 Correct 42 ms 35576 KB Output is correct
9 Correct 39 ms 35672 KB Output is correct
10 Correct 36 ms 35520 KB Output is correct
11 Correct 37 ms 35556 KB Output is correct
12 Correct 38 ms 35576 KB Output is correct
13 Correct 37 ms 35704 KB Output is correct
14 Correct 36 ms 35628 KB Output is correct
15 Correct 36 ms 35548 KB Output is correct
16 Correct 43 ms 35676 KB Output is correct
17 Correct 36 ms 35548 KB Output is correct
18 Correct 38 ms 35548 KB Output is correct
19 Correct 39 ms 35704 KB Output is correct
20 Correct 41 ms 35704 KB Output is correct
21 Correct 37 ms 35704 KB Output is correct
22 Correct 44 ms 35576 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 37 ms 35712 KB Output is correct
25 Correct 33 ms 35580 KB Output is correct
26 Correct 36 ms 36088 KB Output is correct
27 Correct 37 ms 35960 KB Output is correct
28 Correct 38 ms 36216 KB Output is correct
29 Correct 40 ms 36088 KB Output is correct
30 Correct 39 ms 35968 KB Output is correct
31 Correct 38 ms 35704 KB Output is correct
32 Correct 37 ms 36088 KB Output is correct
33 Correct 34 ms 35576 KB Output is correct
34 Correct 38 ms 36096 KB Output is correct
35 Correct 42 ms 36216 KB Output is correct
36 Correct 37 ms 35968 KB Output is correct
37 Correct 39 ms 36088 KB Output is correct
38 Correct 42 ms 35712 KB Output is correct
39 Correct 39 ms 36060 KB Output is correct
40 Correct 39 ms 36088 KB Output is correct
41 Correct 39 ms 35932 KB Output is correct
42 Correct 40 ms 36088 KB Output is correct
43 Correct 107 ms 36096 KB Output is correct
44 Correct 37 ms 35584 KB Output is correct
45 Correct 39 ms 36092 KB Output is correct
46 Correct 40 ms 36096 KB Output is correct
47 Correct 37 ms 35576 KB Output is correct
48 Correct 42 ms 35960 KB Output is correct
49 Correct 45 ms 36216 KB Output is correct
50 Correct 39 ms 36316 KB Output is correct
51 Correct 38 ms 35968 KB Output is correct
52 Correct 39 ms 35960 KB Output is correct
53 Correct 65 ms 36088 KB Output is correct
54 Correct 42 ms 36060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 38 ms 35652 KB Output is correct
4 Correct 38 ms 35576 KB Output is correct
5 Correct 41 ms 35584 KB Output is correct
6 Correct 37 ms 35576 KB Output is correct
7 Correct 37 ms 35576 KB Output is correct
8 Correct 42 ms 35576 KB Output is correct
9 Correct 39 ms 35672 KB Output is correct
10 Correct 36 ms 35520 KB Output is correct
11 Correct 37 ms 35556 KB Output is correct
12 Correct 38 ms 35576 KB Output is correct
13 Correct 37 ms 35704 KB Output is correct
14 Correct 36 ms 35628 KB Output is correct
15 Correct 36 ms 35548 KB Output is correct
16 Correct 43 ms 35676 KB Output is correct
17 Correct 36 ms 35548 KB Output is correct
18 Correct 38 ms 35548 KB Output is correct
19 Correct 39 ms 35704 KB Output is correct
20 Correct 41 ms 35704 KB Output is correct
21 Correct 37 ms 35704 KB Output is correct
22 Correct 44 ms 35576 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 37 ms 35712 KB Output is correct
25 Correct 37 ms 35552 KB Output is correct
26 Correct 184 ms 50248 KB Output is correct
27 Correct 344 ms 49392 KB Output is correct
28 Correct 42 ms 35968 KB Output is correct
29 Correct 41 ms 35704 KB Output is correct
30 Correct 45 ms 35576 KB Output is correct
31 Correct 305 ms 49276 KB Output is correct
32 Correct 46 ms 36088 KB Output is correct
33 Execution timed out 3053 ms 53496 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 207 ms 50232 KB Output is correct
2 Correct 172 ms 56044 KB Output is correct
3 Correct 41 ms 36092 KB Output is correct
4 Correct 44 ms 35960 KB Output is correct
5 Correct 42 ms 35576 KB Output is correct
6 Correct 36 ms 35576 KB Output is correct
7 Correct 39 ms 35960 KB Output is correct
8 Correct 295 ms 51164 KB Output is correct
9 Correct 38 ms 35968 KB Output is correct
10 Correct 223 ms 49628 KB Output is correct
11 Correct 39 ms 35704 KB Output is correct
12 Correct 247 ms 49440 KB Output is correct
13 Correct 215 ms 51320 KB Output is correct
14 Correct 255 ms 55504 KB Output is correct
15 Correct 190 ms 50032 KB Output is correct
16 Correct 42 ms 36096 KB Output is correct
17 Correct 41 ms 35576 KB Output is correct
18 Correct 188 ms 55144 KB Output is correct
19 Correct 256 ms 57744 KB Output is correct
20 Correct 41 ms 35960 KB Output is correct
21 Correct 36 ms 35584 KB Output is correct
22 Correct 171 ms 52152 KB Output is correct
23 Correct 41 ms 36088 KB Output is correct
24 Correct 221 ms 50100 KB Output is correct
25 Correct 263 ms 56912 KB Output is correct
26 Correct 40 ms 36216 KB Output is correct
27 Correct 42 ms 36176 KB Output is correct
28 Correct 39 ms 36088 KB Output is correct
29 Correct 37 ms 35960 KB Output is correct
30 Correct 40 ms 36088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 38 ms 35652 KB Output is correct
4 Correct 38 ms 35576 KB Output is correct
5 Correct 41 ms 35584 KB Output is correct
6 Correct 37 ms 35576 KB Output is correct
7 Correct 37 ms 35576 KB Output is correct
8 Correct 42 ms 35576 KB Output is correct
9 Correct 39 ms 35672 KB Output is correct
10 Correct 36 ms 35520 KB Output is correct
11 Correct 37 ms 35556 KB Output is correct
12 Correct 38 ms 35576 KB Output is correct
13 Correct 37 ms 35704 KB Output is correct
14 Correct 36 ms 35628 KB Output is correct
15 Correct 36 ms 35548 KB Output is correct
16 Correct 43 ms 35676 KB Output is correct
17 Correct 36 ms 35548 KB Output is correct
18 Correct 38 ms 35548 KB Output is correct
19 Correct 39 ms 35704 KB Output is correct
20 Correct 41 ms 35704 KB Output is correct
21 Correct 37 ms 35704 KB Output is correct
22 Correct 44 ms 35576 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 37 ms 35712 KB Output is correct
25 Correct 33 ms 35580 KB Output is correct
26 Correct 36 ms 36088 KB Output is correct
27 Correct 37 ms 35960 KB Output is correct
28 Correct 38 ms 36216 KB Output is correct
29 Correct 40 ms 36088 KB Output is correct
30 Correct 39 ms 35968 KB Output is correct
31 Correct 38 ms 35704 KB Output is correct
32 Correct 37 ms 36088 KB Output is correct
33 Correct 34 ms 35576 KB Output is correct
34 Correct 38 ms 36096 KB Output is correct
35 Correct 42 ms 36216 KB Output is correct
36 Correct 37 ms 35968 KB Output is correct
37 Correct 39 ms 36088 KB Output is correct
38 Correct 42 ms 35712 KB Output is correct
39 Correct 39 ms 36060 KB Output is correct
40 Correct 39 ms 36088 KB Output is correct
41 Correct 39 ms 35932 KB Output is correct
42 Correct 40 ms 36088 KB Output is correct
43 Correct 107 ms 36096 KB Output is correct
44 Correct 37 ms 35584 KB Output is correct
45 Correct 39 ms 36092 KB Output is correct
46 Correct 40 ms 36096 KB Output is correct
47 Correct 37 ms 35576 KB Output is correct
48 Correct 42 ms 35960 KB Output is correct
49 Correct 45 ms 36216 KB Output is correct
50 Correct 39 ms 36316 KB Output is correct
51 Correct 38 ms 35968 KB Output is correct
52 Correct 39 ms 35960 KB Output is correct
53 Correct 65 ms 36088 KB Output is correct
54 Correct 42 ms 36060 KB Output is correct
55 Correct 37 ms 35552 KB Output is correct
56 Correct 184 ms 50248 KB Output is correct
57 Correct 344 ms 49392 KB Output is correct
58 Correct 42 ms 35968 KB Output is correct
59 Correct 41 ms 35704 KB Output is correct
60 Correct 45 ms 35576 KB Output is correct
61 Correct 305 ms 49276 KB Output is correct
62 Correct 46 ms 36088 KB Output is correct
63 Execution timed out 3053 ms 53496 KB Time limit exceeded
64 Halted 0 ms 0 KB -