답안 #362356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
362356 2021-02-02T18:49:05 Z Mahdi_Shokoufi Mergers (JOI19_mergers) C++17
100 / 100
1206 ms 137740 KB
//In the name of Allah
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define Sz(x) (int)(x.size())

const int N = 5e5 + 10;
const int L = 20;

int h[N], par[N][L], sum[N], P[N], sz[N], deg[N];
vector < int > adj[N], vec[N];

void DFS(int v, int p){
    par[v][0] = p;
    for (int i = 1; i < L; i ++)
        par[v][i] = par[par[v][i - 1]][i - 1];
    for (int u : adj[v])
        if (u != p)
            h[u] = h[v] + 1, DFS(u, v);
}

void calDFS(int v, int p){
    for (int u : adj[v])
        if (u != p)
            calDFS(u, v), sum[v] += sum[u];
    sum[v] ++;
}

int getPar(int v, int t){
    for (int i = 0; i < L; i ++)
        if (t >> i & 1)
            v = par[v][i];
    return v;
}

int getLca(int u, int v){
    if (h[v] < h[u])
        swap(u, v);
    v = getPar(v, h[v] - h[u]);
    if (u == v)
        return v;
    for (int i = L - 1; i >= 0; i --)
        if (par[u][i] != par[v][i])
            u = par[u][i], v = par[v][i];
    return par[v][0];
}

int get(int v){
    return (P[v] == v ? v : P[v] = get(P[v]));
}

void merge(int u, int v){
    u = get(u);
    v = get(v);
    if (u == v)
        return ;
    if (sz[v] < sz[u])
        swap(u, v);
    P[u] = v;
    sz[v] += sz[u];
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i < n; i ++){
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for (int i = 1; i <= n; i ++){
        int c; cin >> c;
        vec[c].pb(i);
    }
    DFS(1, 0);
    for (int i = 1; i <= k; i ++){
        int lca = vec[i][0];
        for (int j = 1; j < Sz(vec[i]); j ++)
            lca = getLca(lca, vec[i][j]);
        sum[lca] -= Sz(vec[i]);
    }
    calDFS(1, 0);
    for (int i = 0; i < N; i ++)
        P[i] = i, sz[i] = 1;
    for (int i = 2; i <= n; i ++)
        if (sum[i] != 0)
            merge(i, par[i][0]);
    for (int i = 2; i <= n; i ++)
        if (sum[i] == 0)
            deg[get(i)] ++, deg[get(par[i][0])] ++;
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
        if (deg[i] == 1)
            cnt ++;
    cout << (cnt + 1) / 2;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 27756 KB Output is correct
2 Correct 19 ms 27756 KB Output is correct
3 Correct 20 ms 27724 KB Output is correct
4 Correct 19 ms 27756 KB Output is correct
5 Correct 19 ms 27756 KB Output is correct
6 Correct 21 ms 27760 KB Output is correct
7 Correct 20 ms 27756 KB Output is correct
8 Correct 19 ms 27756 KB Output is correct
9 Correct 20 ms 27756 KB Output is correct
10 Correct 20 ms 27756 KB Output is correct
11 Correct 20 ms 27756 KB Output is correct
12 Correct 20 ms 27756 KB Output is correct
13 Correct 21 ms 27756 KB Output is correct
14 Correct 20 ms 27756 KB Output is correct
15 Correct 19 ms 27756 KB Output is correct
16 Correct 19 ms 27820 KB Output is correct
17 Correct 20 ms 27820 KB Output is correct
18 Correct 20 ms 27756 KB Output is correct
19 Correct 19 ms 27756 KB Output is correct
20 Correct 22 ms 27756 KB Output is correct
21 Correct 19 ms 27756 KB Output is correct
22 Correct 19 ms 27756 KB Output is correct
23 Correct 19 ms 27756 KB Output is correct
24 Correct 19 ms 27756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 27756 KB Output is correct
2 Correct 19 ms 27756 KB Output is correct
3 Correct 20 ms 27724 KB Output is correct
4 Correct 19 ms 27756 KB Output is correct
5 Correct 19 ms 27756 KB Output is correct
6 Correct 21 ms 27760 KB Output is correct
7 Correct 20 ms 27756 KB Output is correct
8 Correct 19 ms 27756 KB Output is correct
9 Correct 20 ms 27756 KB Output is correct
10 Correct 20 ms 27756 KB Output is correct
11 Correct 20 ms 27756 KB Output is correct
12 Correct 20 ms 27756 KB Output is correct
13 Correct 21 ms 27756 KB Output is correct
14 Correct 20 ms 27756 KB Output is correct
15 Correct 19 ms 27756 KB Output is correct
16 Correct 19 ms 27820 KB Output is correct
17 Correct 20 ms 27820 KB Output is correct
18 Correct 20 ms 27756 KB Output is correct
19 Correct 19 ms 27756 KB Output is correct
20 Correct 22 ms 27756 KB Output is correct
21 Correct 19 ms 27756 KB Output is correct
22 Correct 19 ms 27756 KB Output is correct
23 Correct 19 ms 27756 KB Output is correct
24 Correct 19 ms 27756 KB Output is correct
25 Correct 19 ms 27756 KB Output is correct
26 Correct 21 ms 28268 KB Output is correct
27 Correct 84 ms 28144 KB Output is correct
28 Correct 65 ms 28396 KB Output is correct
29 Correct 103 ms 28268 KB Output is correct
30 Correct 21 ms 28140 KB Output is correct
31 Correct 19 ms 27828 KB Output is correct
32 Correct 21 ms 28396 KB Output is correct
33 Correct 20 ms 27756 KB Output is correct
34 Correct 22 ms 28140 KB Output is correct
35 Correct 176 ms 28268 KB Output is correct
36 Correct 22 ms 28140 KB Output is correct
37 Correct 23 ms 28268 KB Output is correct
38 Correct 19 ms 27800 KB Output is correct
39 Correct 22 ms 28268 KB Output is correct
40 Correct 21 ms 28140 KB Output is correct
41 Correct 21 ms 28140 KB Output is correct
42 Correct 21 ms 28268 KB Output is correct
43 Correct 21 ms 28396 KB Output is correct
44 Correct 19 ms 27756 KB Output is correct
45 Correct 62 ms 28268 KB Output is correct
46 Correct 21 ms 28268 KB Output is correct
47 Correct 57 ms 27756 KB Output is correct
48 Correct 21 ms 28140 KB Output is correct
49 Correct 21 ms 28396 KB Output is correct
50 Correct 21 ms 28268 KB Output is correct
51 Correct 22 ms 28140 KB Output is correct
52 Correct 23 ms 28288 KB Output is correct
53 Correct 21 ms 28268 KB Output is correct
54 Correct 21 ms 28140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 27756 KB Output is correct
2 Correct 19 ms 27756 KB Output is correct
3 Correct 20 ms 27724 KB Output is correct
4 Correct 19 ms 27756 KB Output is correct
5 Correct 19 ms 27756 KB Output is correct
6 Correct 21 ms 27760 KB Output is correct
7 Correct 20 ms 27756 KB Output is correct
8 Correct 19 ms 27756 KB Output is correct
9 Correct 20 ms 27756 KB Output is correct
10 Correct 20 ms 27756 KB Output is correct
11 Correct 20 ms 27756 KB Output is correct
12 Correct 20 ms 27756 KB Output is correct
13 Correct 21 ms 27756 KB Output is correct
14 Correct 20 ms 27756 KB Output is correct
15 Correct 19 ms 27756 KB Output is correct
16 Correct 19 ms 27820 KB Output is correct
17 Correct 20 ms 27820 KB Output is correct
18 Correct 20 ms 27756 KB Output is correct
19 Correct 19 ms 27756 KB Output is correct
20 Correct 22 ms 27756 KB Output is correct
21 Correct 19 ms 27756 KB Output is correct
22 Correct 19 ms 27756 KB Output is correct
23 Correct 19 ms 27756 KB Output is correct
24 Correct 19 ms 27756 KB Output is correct
25 Correct 19 ms 27884 KB Output is correct
26 Correct 102 ms 41896 KB Output is correct
27 Correct 128 ms 41580 KB Output is correct
28 Correct 21 ms 28140 KB Output is correct
29 Correct 20 ms 27756 KB Output is correct
30 Correct 22 ms 27756 KB Output is correct
31 Correct 133 ms 41836 KB Output is correct
32 Correct 21 ms 28268 KB Output is correct
33 Correct 201 ms 48748 KB Output is correct
34 Correct 131 ms 41580 KB Output is correct
35 Correct 21 ms 28140 KB Output is correct
36 Correct 151 ms 42348 KB Output is correct
37 Correct 20 ms 28140 KB Output is correct
38 Correct 19 ms 28140 KB Output is correct
39 Correct 97 ms 41828 KB Output is correct
40 Correct 20 ms 28396 KB Output is correct
41 Correct 137 ms 41372 KB Output is correct
42 Correct 190 ms 44140 KB Output is correct
43 Correct 19 ms 27756 KB Output is correct
44 Correct 222 ms 49124 KB Output is correct
45 Correct 221 ms 45488 KB Output is correct
46 Correct 22 ms 28140 KB Output is correct
47 Correct 21 ms 28140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 41956 KB Output is correct
2 Correct 113 ms 45156 KB Output is correct
3 Correct 23 ms 28268 KB Output is correct
4 Correct 22 ms 28140 KB Output is correct
5 Correct 20 ms 27756 KB Output is correct
6 Correct 19 ms 27756 KB Output is correct
7 Correct 24 ms 28140 KB Output is correct
8 Correct 160 ms 43372 KB Output is correct
9 Correct 21 ms 28140 KB Output is correct
10 Correct 136 ms 42092 KB Output is correct
11 Correct 19 ms 27756 KB Output is correct
12 Correct 153 ms 42220 KB Output is correct
13 Correct 162 ms 43244 KB Output is correct
14 Correct 130 ms 44908 KB Output is correct
15 Correct 98 ms 41828 KB Output is correct
16 Correct 21 ms 28268 KB Output is correct
17 Correct 19 ms 27756 KB Output is correct
18 Correct 109 ms 44900 KB Output is correct
19 Correct 169 ms 48748 KB Output is correct
20 Correct 21 ms 28268 KB Output is correct
21 Correct 19 ms 27756 KB Output is correct
22 Correct 110 ms 43492 KB Output is correct
23 Correct 21 ms 28140 KB Output is correct
24 Correct 143 ms 42476 KB Output is correct
25 Correct 164 ms 47596 KB Output is correct
26 Correct 21 ms 28268 KB Output is correct
27 Correct 21 ms 28268 KB Output is correct
28 Correct 21 ms 28140 KB Output is correct
29 Correct 21 ms 28140 KB Output is correct
30 Correct 21 ms 28268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 27756 KB Output is correct
2 Correct 19 ms 27756 KB Output is correct
3 Correct 20 ms 27724 KB Output is correct
4 Correct 19 ms 27756 KB Output is correct
5 Correct 19 ms 27756 KB Output is correct
6 Correct 21 ms 27760 KB Output is correct
7 Correct 20 ms 27756 KB Output is correct
8 Correct 19 ms 27756 KB Output is correct
9 Correct 20 ms 27756 KB Output is correct
10 Correct 20 ms 27756 KB Output is correct
11 Correct 20 ms 27756 KB Output is correct
12 Correct 20 ms 27756 KB Output is correct
13 Correct 21 ms 27756 KB Output is correct
14 Correct 20 ms 27756 KB Output is correct
15 Correct 19 ms 27756 KB Output is correct
16 Correct 19 ms 27820 KB Output is correct
17 Correct 20 ms 27820 KB Output is correct
18 Correct 20 ms 27756 KB Output is correct
19 Correct 19 ms 27756 KB Output is correct
20 Correct 22 ms 27756 KB Output is correct
21 Correct 19 ms 27756 KB Output is correct
22 Correct 19 ms 27756 KB Output is correct
23 Correct 19 ms 27756 KB Output is correct
24 Correct 19 ms 27756 KB Output is correct
25 Correct 19 ms 27756 KB Output is correct
26 Correct 21 ms 28268 KB Output is correct
27 Correct 84 ms 28144 KB Output is correct
28 Correct 65 ms 28396 KB Output is correct
29 Correct 103 ms 28268 KB Output is correct
30 Correct 21 ms 28140 KB Output is correct
31 Correct 19 ms 27828 KB Output is correct
32 Correct 21 ms 28396 KB Output is correct
33 Correct 20 ms 27756 KB Output is correct
34 Correct 22 ms 28140 KB Output is correct
35 Correct 176 ms 28268 KB Output is correct
36 Correct 22 ms 28140 KB Output is correct
37 Correct 23 ms 28268 KB Output is correct
38 Correct 19 ms 27800 KB Output is correct
39 Correct 22 ms 28268 KB Output is correct
40 Correct 21 ms 28140 KB Output is correct
41 Correct 21 ms 28140 KB Output is correct
42 Correct 21 ms 28268 KB Output is correct
43 Correct 21 ms 28396 KB Output is correct
44 Correct 19 ms 27756 KB Output is correct
45 Correct 62 ms 28268 KB Output is correct
46 Correct 21 ms 28268 KB Output is correct
47 Correct 57 ms 27756 KB Output is correct
48 Correct 21 ms 28140 KB Output is correct
49 Correct 21 ms 28396 KB Output is correct
50 Correct 21 ms 28268 KB Output is correct
51 Correct 22 ms 28140 KB Output is correct
52 Correct 23 ms 28288 KB Output is correct
53 Correct 21 ms 28268 KB Output is correct
54 Correct 21 ms 28140 KB Output is correct
55 Correct 19 ms 27884 KB Output is correct
56 Correct 102 ms 41896 KB Output is correct
57 Correct 128 ms 41580 KB Output is correct
58 Correct 21 ms 28140 KB Output is correct
59 Correct 20 ms 27756 KB Output is correct
60 Correct 22 ms 27756 KB Output is correct
61 Correct 133 ms 41836 KB Output is correct
62 Correct 21 ms 28268 KB Output is correct
63 Correct 201 ms 48748 KB Output is correct
64 Correct 131 ms 41580 KB Output is correct
65 Correct 21 ms 28140 KB Output is correct
66 Correct 151 ms 42348 KB Output is correct
67 Correct 20 ms 28140 KB Output is correct
68 Correct 19 ms 28140 KB Output is correct
69 Correct 97 ms 41828 KB Output is correct
70 Correct 20 ms 28396 KB Output is correct
71 Correct 137 ms 41372 KB Output is correct
72 Correct 190 ms 44140 KB Output is correct
73 Correct 19 ms 27756 KB Output is correct
74 Correct 222 ms 49124 KB Output is correct
75 Correct 221 ms 45488 KB Output is correct
76 Correct 22 ms 28140 KB Output is correct
77 Correct 21 ms 28140 KB Output is correct
78 Correct 99 ms 41956 KB Output is correct
79 Correct 113 ms 45156 KB Output is correct
80 Correct 23 ms 28268 KB Output is correct
81 Correct 22 ms 28140 KB Output is correct
82 Correct 20 ms 27756 KB Output is correct
83 Correct 19 ms 27756 KB Output is correct
84 Correct 24 ms 28140 KB Output is correct
85 Correct 160 ms 43372 KB Output is correct
86 Correct 21 ms 28140 KB Output is correct
87 Correct 136 ms 42092 KB Output is correct
88 Correct 19 ms 27756 KB Output is correct
89 Correct 153 ms 42220 KB Output is correct
90 Correct 162 ms 43244 KB Output is correct
91 Correct 130 ms 44908 KB Output is correct
92 Correct 98 ms 41828 KB Output is correct
93 Correct 21 ms 28268 KB Output is correct
94 Correct 19 ms 27756 KB Output is correct
95 Correct 109 ms 44900 KB Output is correct
96 Correct 169 ms 48748 KB Output is correct
97 Correct 21 ms 28268 KB Output is correct
98 Correct 19 ms 27756 KB Output is correct
99 Correct 110 ms 43492 KB Output is correct
100 Correct 21 ms 28140 KB Output is correct
101 Correct 143 ms 42476 KB Output is correct
102 Correct 164 ms 47596 KB Output is correct
103 Correct 21 ms 28268 KB Output is correct
104 Correct 21 ms 28268 KB Output is correct
105 Correct 21 ms 28140 KB Output is correct
106 Correct 21 ms 28140 KB Output is correct
107 Correct 21 ms 28268 KB Output is correct
108 Correct 679 ms 106848 KB Output is correct
109 Correct 1164 ms 114640 KB Output is correct
110 Correct 1170 ms 125936 KB Output is correct
111 Correct 1206 ms 137740 KB Output is correct
112 Correct 1134 ms 127816 KB Output is correct
113 Correct 654 ms 114644 KB Output is correct
114 Correct 765 ms 96776 KB Output is correct
115 Correct 779 ms 96920 KB Output is correct
116 Correct 934 ms 103324 KB Output is correct
117 Correct 915 ms 114668 KB Output is correct
118 Correct 791 ms 98668 KB Output is correct
119 Correct 922 ms 114696 KB Output is correct
120 Correct 1097 ms 128492 KB Output is correct
121 Correct 901 ms 114520 KB Output is correct
122 Correct 995 ms 108256 KB Output is correct
123 Correct 644 ms 116188 KB Output is correct
124 Correct 1020 ms 112876 KB Output is correct