Submission #105867

# Submission time Handle Problem Language Result Execution time Memory
105867 2019-04-15T11:33:17 Z JustasZ Mergers (JOI19_mergers) C++14
100 / 100
1881 ms 152384 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], highest[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;
    if(dep[highest[b]] < dep[highest[a]])
        highest[a] = highest[b];
}
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])
        {
            highest[root(a)] = LCA;
        }
    }
    vector<int>ord;
    for(int i = 1; i <= n; i++)
        ord.pb(i);
    sort(all(ord), [&](int a, int b)
         {
             return dep[a] > dep[b];
         });
    for(int v : ord)
    {
        if(highest[root(v)] == v)
            continue;
        else
            unite(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 38 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 63 ms 35704 KB Output is correct
4 Correct 36 ms 35584 KB Output is correct
5 Correct 37 ms 35576 KB Output is correct
6 Correct 36 ms 35548 KB Output is correct
7 Correct 41 ms 35576 KB Output is correct
8 Correct 37 ms 35704 KB Output is correct
9 Correct 38 ms 35548 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 41 ms 35584 KB Output is correct
12 Correct 37 ms 35580 KB Output is correct
13 Correct 42 ms 35768 KB Output is correct
14 Correct 38 ms 35532 KB Output is correct
15 Correct 36 ms 35584 KB Output is correct
16 Correct 43 ms 35704 KB Output is correct
17 Correct 37 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 38 ms 35584 KB Output is correct
20 Correct 37 ms 35576 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 39 ms 35640 KB Output is correct
23 Correct 35 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 63 ms 35704 KB Output is correct
4 Correct 36 ms 35584 KB Output is correct
5 Correct 37 ms 35576 KB Output is correct
6 Correct 36 ms 35548 KB Output is correct
7 Correct 41 ms 35576 KB Output is correct
8 Correct 37 ms 35704 KB Output is correct
9 Correct 38 ms 35548 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 41 ms 35584 KB Output is correct
12 Correct 37 ms 35580 KB Output is correct
13 Correct 42 ms 35768 KB Output is correct
14 Correct 38 ms 35532 KB Output is correct
15 Correct 36 ms 35584 KB Output is correct
16 Correct 43 ms 35704 KB Output is correct
17 Correct 37 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 38 ms 35584 KB Output is correct
20 Correct 37 ms 35576 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 39 ms 35640 KB Output is correct
23 Correct 35 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 35 ms 35584 KB Output is correct
26 Correct 37 ms 36216 KB Output is correct
27 Correct 45 ms 35968 KB Output is correct
28 Correct 37 ms 36224 KB Output is correct
29 Correct 38 ms 36224 KB Output is correct
30 Correct 46 ms 35960 KB Output is correct
31 Correct 37 ms 35584 KB Output is correct
32 Correct 42 ms 36224 KB Output is correct
33 Correct 40 ms 35576 KB Output is correct
34 Correct 48 ms 36088 KB Output is correct
35 Correct 40 ms 36216 KB Output is correct
36 Correct 39 ms 36088 KB Output is correct
37 Correct 40 ms 36088 KB Output is correct
38 Correct 36 ms 35576 KB Output is correct
39 Correct 37 ms 35960 KB Output is correct
40 Correct 37 ms 35964 KB Output is correct
41 Correct 36 ms 36096 KB Output is correct
42 Correct 43 ms 36088 KB Output is correct
43 Correct 39 ms 36088 KB Output is correct
44 Correct 35 ms 35584 KB Output is correct
45 Correct 40 ms 36088 KB Output is correct
46 Correct 42 ms 35964 KB Output is correct
47 Correct 40 ms 35568 KB Output is correct
48 Correct 38 ms 36020 KB Output is correct
49 Correct 36 ms 36224 KB Output is correct
50 Correct 36 ms 36216 KB Output is correct
51 Correct 39 ms 36096 KB Output is correct
52 Correct 37 ms 36096 KB Output is correct
53 Correct 38 ms 36088 KB Output is correct
54 Correct 37 ms 36060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 63 ms 35704 KB Output is correct
4 Correct 36 ms 35584 KB Output is correct
5 Correct 37 ms 35576 KB Output is correct
6 Correct 36 ms 35548 KB Output is correct
7 Correct 41 ms 35576 KB Output is correct
8 Correct 37 ms 35704 KB Output is correct
9 Correct 38 ms 35548 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 41 ms 35584 KB Output is correct
12 Correct 37 ms 35580 KB Output is correct
13 Correct 42 ms 35768 KB Output is correct
14 Correct 38 ms 35532 KB Output is correct
15 Correct 36 ms 35584 KB Output is correct
16 Correct 43 ms 35704 KB Output is correct
17 Correct 37 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 38 ms 35584 KB Output is correct
20 Correct 37 ms 35576 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 39 ms 35640 KB Output is correct
23 Correct 35 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 35 ms 35584 KB Output is correct
26 Correct 157 ms 49756 KB Output is correct
27 Correct 197 ms 48964 KB Output is correct
28 Correct 42 ms 35960 KB Output is correct
29 Correct 36 ms 35576 KB Output is correct
30 Correct 34 ms 35584 KB Output is correct
31 Correct 208 ms 48884 KB Output is correct
32 Correct 41 ms 36088 KB Output is correct
33 Correct 253 ms 53236 KB Output is correct
34 Correct 235 ms 50424 KB Output is correct
35 Correct 38 ms 35960 KB Output is correct
36 Correct 225 ms 50740 KB Output is correct
37 Correct 42 ms 36088 KB Output is correct
38 Correct 37 ms 35972 KB Output is correct
39 Correct 156 ms 51068 KB Output is correct
40 Correct 43 ms 36188 KB Output is correct
41 Correct 168 ms 50288 KB Output is correct
42 Correct 246 ms 51824 KB Output is correct
43 Correct 41 ms 35576 KB Output is correct
44 Correct 223 ms 54772 KB Output is correct
45 Correct 242 ms 52724 KB Output is correct
46 Correct 37 ms 36088 KB Output is correct
47 Correct 40 ms 36088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 148 ms 49772 KB Output is correct
2 Correct 165 ms 55220 KB Output is correct
3 Correct 38 ms 36216 KB Output is correct
4 Correct 39 ms 36072 KB Output is correct
5 Correct 36 ms 35704 KB Output is correct
6 Correct 35 ms 35584 KB Output is correct
7 Correct 41 ms 36140 KB Output is correct
8 Correct 235 ms 50184 KB Output is correct
9 Correct 39 ms 36088 KB Output is correct
10 Correct 248 ms 49012 KB Output is correct
11 Correct 40 ms 35576 KB Output is correct
12 Correct 212 ms 48988 KB Output is correct
13 Correct 258 ms 50548 KB Output is correct
14 Correct 277 ms 54644 KB Output is correct
15 Correct 166 ms 49660 KB Output is correct
16 Correct 39 ms 36092 KB Output is correct
17 Correct 37 ms 35572 KB Output is correct
18 Correct 202 ms 54260 KB Output is correct
19 Correct 329 ms 56816 KB Output is correct
20 Correct 43 ms 36088 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 196 ms 51168 KB Output is correct
23 Correct 47 ms 35960 KB Output is correct
24 Correct 234 ms 49304 KB Output is correct
25 Correct 255 ms 56048 KB Output is correct
26 Correct 40 ms 36216 KB Output is correct
27 Correct 41 ms 36228 KB Output is correct
28 Correct 40 ms 36060 KB Output is correct
29 Correct 39 ms 36088 KB Output is correct
30 Correct 47 ms 36088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 35576 KB Output is correct
2 Correct 38 ms 35576 KB Output is correct
3 Correct 63 ms 35704 KB Output is correct
4 Correct 36 ms 35584 KB Output is correct
5 Correct 37 ms 35576 KB Output is correct
6 Correct 36 ms 35548 KB Output is correct
7 Correct 41 ms 35576 KB Output is correct
8 Correct 37 ms 35704 KB Output is correct
9 Correct 38 ms 35548 KB Output is correct
10 Correct 35 ms 35576 KB Output is correct
11 Correct 41 ms 35584 KB Output is correct
12 Correct 37 ms 35580 KB Output is correct
13 Correct 42 ms 35768 KB Output is correct
14 Correct 38 ms 35532 KB Output is correct
15 Correct 36 ms 35584 KB Output is correct
16 Correct 43 ms 35704 KB Output is correct
17 Correct 37 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 38 ms 35584 KB Output is correct
20 Correct 37 ms 35576 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 39 ms 35640 KB Output is correct
23 Correct 35 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 35 ms 35584 KB Output is correct
26 Correct 37 ms 36216 KB Output is correct
27 Correct 45 ms 35968 KB Output is correct
28 Correct 37 ms 36224 KB Output is correct
29 Correct 38 ms 36224 KB Output is correct
30 Correct 46 ms 35960 KB Output is correct
31 Correct 37 ms 35584 KB Output is correct
32 Correct 42 ms 36224 KB Output is correct
33 Correct 40 ms 35576 KB Output is correct
34 Correct 48 ms 36088 KB Output is correct
35 Correct 40 ms 36216 KB Output is correct
36 Correct 39 ms 36088 KB Output is correct
37 Correct 40 ms 36088 KB Output is correct
38 Correct 36 ms 35576 KB Output is correct
39 Correct 37 ms 35960 KB Output is correct
40 Correct 37 ms 35964 KB Output is correct
41 Correct 36 ms 36096 KB Output is correct
42 Correct 43 ms 36088 KB Output is correct
43 Correct 39 ms 36088 KB Output is correct
44 Correct 35 ms 35584 KB Output is correct
45 Correct 40 ms 36088 KB Output is correct
46 Correct 42 ms 35964 KB Output is correct
47 Correct 40 ms 35568 KB Output is correct
48 Correct 38 ms 36020 KB Output is correct
49 Correct 36 ms 36224 KB Output is correct
50 Correct 36 ms 36216 KB Output is correct
51 Correct 39 ms 36096 KB Output is correct
52 Correct 37 ms 36096 KB Output is correct
53 Correct 38 ms 36088 KB Output is correct
54 Correct 37 ms 36060 KB Output is correct
55 Correct 35 ms 35584 KB Output is correct
56 Correct 157 ms 49756 KB Output is correct
57 Correct 197 ms 48964 KB Output is correct
58 Correct 42 ms 35960 KB Output is correct
59 Correct 36 ms 35576 KB Output is correct
60 Correct 34 ms 35584 KB Output is correct
61 Correct 208 ms 48884 KB Output is correct
62 Correct 41 ms 36088 KB Output is correct
63 Correct 253 ms 53236 KB Output is correct
64 Correct 235 ms 50424 KB Output is correct
65 Correct 38 ms 35960 KB Output is correct
66 Correct 225 ms 50740 KB Output is correct
67 Correct 42 ms 36088 KB Output is correct
68 Correct 37 ms 35972 KB Output is correct
69 Correct 156 ms 51068 KB Output is correct
70 Correct 43 ms 36188 KB Output is correct
71 Correct 168 ms 50288 KB Output is correct
72 Correct 246 ms 51824 KB Output is correct
73 Correct 41 ms 35576 KB Output is correct
74 Correct 223 ms 54772 KB Output is correct
75 Correct 242 ms 52724 KB Output is correct
76 Correct 37 ms 36088 KB Output is correct
77 Correct 40 ms 36088 KB Output is correct
78 Correct 148 ms 49772 KB Output is correct
79 Correct 165 ms 55220 KB Output is correct
80 Correct 38 ms 36216 KB Output is correct
81 Correct 39 ms 36072 KB Output is correct
82 Correct 36 ms 35704 KB Output is correct
83 Correct 35 ms 35584 KB Output is correct
84 Correct 41 ms 36140 KB Output is correct
85 Correct 235 ms 50184 KB Output is correct
86 Correct 39 ms 36088 KB Output is correct
87 Correct 248 ms 49012 KB Output is correct
88 Correct 40 ms 35576 KB Output is correct
89 Correct 212 ms 48988 KB Output is correct
90 Correct 258 ms 50548 KB Output is correct
91 Correct 277 ms 54644 KB Output is correct
92 Correct 166 ms 49660 KB Output is correct
93 Correct 39 ms 36092 KB Output is correct
94 Correct 37 ms 35572 KB Output is correct
95 Correct 202 ms 54260 KB Output is correct
96 Correct 329 ms 56816 KB Output is correct
97 Correct 43 ms 36088 KB Output is correct
98 Correct 35 ms 35576 KB Output is correct
99 Correct 196 ms 51168 KB Output is correct
100 Correct 47 ms 35960 KB Output is correct
101 Correct 234 ms 49304 KB Output is correct
102 Correct 255 ms 56048 KB Output is correct
103 Correct 40 ms 36216 KB Output is correct
104 Correct 41 ms 36228 KB Output is correct
105 Correct 40 ms 36060 KB Output is correct
106 Correct 39 ms 36088 KB Output is correct
107 Correct 47 ms 36088 KB Output is correct
108 Correct 1016 ms 120484 KB Output is correct
109 Correct 1881 ms 119096 KB Output is correct
110 Correct 1843 ms 126756 KB Output is correct
111 Correct 1686 ms 152384 KB Output is correct
112 Correct 1602 ms 141408 KB Output is correct
113 Correct 1086 ms 139040 KB Output is correct
114 Correct 1049 ms 108564 KB Output is correct
115 Correct 1061 ms 109540 KB Output is correct
116 Correct 1406 ms 113392 KB Output is correct
117 Correct 1405 ms 140692 KB Output is correct
118 Correct 1133 ms 109432 KB Output is correct
119 Correct 1405 ms 140644 KB Output is correct
120 Correct 1651 ms 148240 KB Output is correct
121 Correct 1508 ms 140520 KB Output is correct
122 Correct 1562 ms 121072 KB Output is correct
123 Correct 1212 ms 143460 KB Output is correct
124 Correct 1527 ms 132200 KB Output is correct