답안 #919671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
919671 2024-02-01T11:24:54 Z 12345678 Mergers (JOI19_mergers) C++17
100 / 100
1066 ms 112976 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=5e5+5, kx=19;
int lvl[nx], pa[nx][kx], n, k, u, v, s[nx], l[nx], cnt, dp[nx], ed[nx], ans, t[nx];
vector<int> d[nx];

void dfs(int u, int p)
{
    lvl[u]=lvl[p]+1; pa[u][0]=p;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    for (auto v:d[u]) if (v!=p) dfs(v, u);
}

int lca(int u, int v)
{
    if (u==0) return v;
    if (lvl[u]>lvl[v]) swap(u, v);
    for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
    if (u==v) return u;
    for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
    return pa[u][0];
}

void dfs2(int u, int p)
{
    for (auto v:d[u]) if (v!=p) dfs2(v, u), dp[u]+=dp[v];
    if (dp[u]==0) ed[u]=1;
}

void dfs3(int u, int p, int c)
{
    for (auto v:d[u])
    {
        if (v==p) continue;
        if (ed[v]) t[c]++, dfs3(v, u, v), ans+=(t[v]==0);
        else dfs3(v, u, c);
    }
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>k;
    for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    for (int i=1; i<=n; i++) cin>>s[i];
    dfs(1, 1);
    for (int i=1; i<=n; i++) l[s[i]]=lca(l[s[i]], i);
    for (int i=1; i<=n; i++) dp[l[s[i]]]--, dp[i]++;
    dfs2(1, 1);
    dfs3(1, 1, 1);
    ans+=(t[1]==1);
    cout<<(ans+1)/2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 4 ms 21080 KB Output is correct
3 Correct 5 ms 21080 KB Output is correct
4 Correct 5 ms 21336 KB Output is correct
5 Correct 5 ms 21080 KB Output is correct
6 Correct 5 ms 21080 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 5 ms 21080 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 5 ms 21080 KB Output is correct
11 Correct 5 ms 21080 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21084 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 5 ms 21080 KB Output is correct
17 Correct 5 ms 21080 KB Output is correct
18 Correct 5 ms 21084 KB Output is correct
19 Correct 5 ms 21084 KB Output is correct
20 Correct 4 ms 21080 KB Output is correct
21 Correct 5 ms 21080 KB Output is correct
22 Correct 5 ms 21084 KB Output is correct
23 Correct 6 ms 21140 KB Output is correct
24 Correct 5 ms 21336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 4 ms 21080 KB Output is correct
3 Correct 5 ms 21080 KB Output is correct
4 Correct 5 ms 21336 KB Output is correct
5 Correct 5 ms 21080 KB Output is correct
6 Correct 5 ms 21080 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 5 ms 21080 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 5 ms 21080 KB Output is correct
11 Correct 5 ms 21080 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21084 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 5 ms 21080 KB Output is correct
17 Correct 5 ms 21080 KB Output is correct
18 Correct 5 ms 21084 KB Output is correct
19 Correct 5 ms 21084 KB Output is correct
20 Correct 4 ms 21080 KB Output is correct
21 Correct 5 ms 21080 KB Output is correct
22 Correct 5 ms 21084 KB Output is correct
23 Correct 6 ms 21140 KB Output is correct
24 Correct 5 ms 21336 KB Output is correct
25 Correct 5 ms 21080 KB Output is correct
26 Correct 6 ms 21336 KB Output is correct
27 Correct 6 ms 21080 KB Output is correct
28 Correct 6 ms 21340 KB Output is correct
29 Correct 6 ms 21336 KB Output is correct
30 Correct 7 ms 21080 KB Output is correct
31 Correct 4 ms 21080 KB Output is correct
32 Correct 6 ms 21336 KB Output is correct
33 Correct 5 ms 21080 KB Output is correct
34 Correct 6 ms 21080 KB Output is correct
35 Correct 6 ms 21080 KB Output is correct
36 Correct 6 ms 21156 KB Output is correct
37 Correct 8 ms 21328 KB Output is correct
38 Correct 5 ms 21080 KB Output is correct
39 Correct 6 ms 21080 KB Output is correct
40 Correct 6 ms 21080 KB Output is correct
41 Correct 6 ms 21080 KB Output is correct
42 Correct 6 ms 21336 KB Output is correct
43 Correct 6 ms 21340 KB Output is correct
44 Correct 5 ms 21080 KB Output is correct
45 Correct 6 ms 21336 KB Output is correct
46 Correct 6 ms 21080 KB Output is correct
47 Correct 6 ms 21080 KB Output is correct
48 Correct 6 ms 21080 KB Output is correct
49 Correct 7 ms 21336 KB Output is correct
50 Correct 7 ms 21336 KB Output is correct
51 Correct 6 ms 21080 KB Output is correct
52 Correct 6 ms 21336 KB Output is correct
53 Correct 6 ms 21336 KB Output is correct
54 Correct 7 ms 21080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 4 ms 21080 KB Output is correct
3 Correct 5 ms 21080 KB Output is correct
4 Correct 5 ms 21336 KB Output is correct
5 Correct 5 ms 21080 KB Output is correct
6 Correct 5 ms 21080 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 5 ms 21080 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 5 ms 21080 KB Output is correct
11 Correct 5 ms 21080 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21084 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 5 ms 21080 KB Output is correct
17 Correct 5 ms 21080 KB Output is correct
18 Correct 5 ms 21084 KB Output is correct
19 Correct 5 ms 21084 KB Output is correct
20 Correct 4 ms 21080 KB Output is correct
21 Correct 5 ms 21080 KB Output is correct
22 Correct 5 ms 21084 KB Output is correct
23 Correct 6 ms 21140 KB Output is correct
24 Correct 5 ms 21336 KB Output is correct
25 Correct 5 ms 21080 KB Output is correct
26 Correct 54 ms 34500 KB Output is correct
27 Correct 74 ms 34384 KB Output is correct
28 Correct 7 ms 21080 KB Output is correct
29 Correct 5 ms 21080 KB Output is correct
30 Correct 5 ms 21084 KB Output is correct
31 Correct 71 ms 34384 KB Output is correct
32 Correct 6 ms 21080 KB Output is correct
33 Correct 95 ms 41428 KB Output is correct
34 Correct 75 ms 34188 KB Output is correct
35 Correct 7 ms 21080 KB Output is correct
36 Correct 86 ms 35156 KB Output is correct
37 Correct 6 ms 21080 KB Output is correct
38 Correct 6 ms 21080 KB Output is correct
39 Correct 51 ms 34632 KB Output is correct
40 Correct 6 ms 21336 KB Output is correct
41 Correct 61 ms 34128 KB Output is correct
42 Correct 88 ms 36688 KB Output is correct
43 Correct 5 ms 21080 KB Output is correct
44 Correct 85 ms 41812 KB Output is correct
45 Correct 84 ms 38132 KB Output is correct
46 Correct 6 ms 21336 KB Output is correct
47 Correct 8 ms 21084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 34500 KB Output is correct
2 Correct 54 ms 35268 KB Output is correct
3 Correct 7 ms 21080 KB Output is correct
4 Correct 7 ms 21080 KB Output is correct
5 Correct 6 ms 21084 KB Output is correct
6 Correct 6 ms 21080 KB Output is correct
7 Correct 7 ms 21084 KB Output is correct
8 Correct 84 ms 34896 KB Output is correct
9 Correct 6 ms 21080 KB Output is correct
10 Correct 71 ms 34384 KB Output is correct
11 Correct 6 ms 21080 KB Output is correct
12 Correct 74 ms 34388 KB Output is correct
13 Correct 77 ms 34640 KB Output is correct
14 Correct 60 ms 34896 KB Output is correct
15 Correct 52 ms 34668 KB Output is correct
16 Correct 6 ms 21340 KB Output is correct
17 Correct 5 ms 21080 KB Output is correct
18 Correct 53 ms 35148 KB Output is correct
19 Correct 73 ms 38992 KB Output is correct
20 Correct 6 ms 21080 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 56 ms 35016 KB Output is correct
23 Correct 6 ms 21080 KB Output is correct
24 Correct 75 ms 34500 KB Output is correct
25 Correct 67 ms 37456 KB Output is correct
26 Correct 6 ms 21336 KB Output is correct
27 Correct 7 ms 21336 KB Output is correct
28 Correct 7 ms 21080 KB Output is correct
29 Correct 8 ms 21336 KB Output is correct
30 Correct 6 ms 21336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 4 ms 21080 KB Output is correct
3 Correct 5 ms 21080 KB Output is correct
4 Correct 5 ms 21336 KB Output is correct
5 Correct 5 ms 21080 KB Output is correct
6 Correct 5 ms 21080 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 5 ms 21080 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 5 ms 21080 KB Output is correct
11 Correct 5 ms 21080 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21084 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 5 ms 21080 KB Output is correct
17 Correct 5 ms 21080 KB Output is correct
18 Correct 5 ms 21084 KB Output is correct
19 Correct 5 ms 21084 KB Output is correct
20 Correct 4 ms 21080 KB Output is correct
21 Correct 5 ms 21080 KB Output is correct
22 Correct 5 ms 21084 KB Output is correct
23 Correct 6 ms 21140 KB Output is correct
24 Correct 5 ms 21336 KB Output is correct
25 Correct 5 ms 21080 KB Output is correct
26 Correct 6 ms 21336 KB Output is correct
27 Correct 6 ms 21080 KB Output is correct
28 Correct 6 ms 21340 KB Output is correct
29 Correct 6 ms 21336 KB Output is correct
30 Correct 7 ms 21080 KB Output is correct
31 Correct 4 ms 21080 KB Output is correct
32 Correct 6 ms 21336 KB Output is correct
33 Correct 5 ms 21080 KB Output is correct
34 Correct 6 ms 21080 KB Output is correct
35 Correct 6 ms 21080 KB Output is correct
36 Correct 6 ms 21156 KB Output is correct
37 Correct 8 ms 21328 KB Output is correct
38 Correct 5 ms 21080 KB Output is correct
39 Correct 6 ms 21080 KB Output is correct
40 Correct 6 ms 21080 KB Output is correct
41 Correct 6 ms 21080 KB Output is correct
42 Correct 6 ms 21336 KB Output is correct
43 Correct 6 ms 21340 KB Output is correct
44 Correct 5 ms 21080 KB Output is correct
45 Correct 6 ms 21336 KB Output is correct
46 Correct 6 ms 21080 KB Output is correct
47 Correct 6 ms 21080 KB Output is correct
48 Correct 6 ms 21080 KB Output is correct
49 Correct 7 ms 21336 KB Output is correct
50 Correct 7 ms 21336 KB Output is correct
51 Correct 6 ms 21080 KB Output is correct
52 Correct 6 ms 21336 KB Output is correct
53 Correct 6 ms 21336 KB Output is correct
54 Correct 7 ms 21080 KB Output is correct
55 Correct 5 ms 21080 KB Output is correct
56 Correct 54 ms 34500 KB Output is correct
57 Correct 74 ms 34384 KB Output is correct
58 Correct 7 ms 21080 KB Output is correct
59 Correct 5 ms 21080 KB Output is correct
60 Correct 5 ms 21084 KB Output is correct
61 Correct 71 ms 34384 KB Output is correct
62 Correct 6 ms 21080 KB Output is correct
63 Correct 95 ms 41428 KB Output is correct
64 Correct 75 ms 34188 KB Output is correct
65 Correct 7 ms 21080 KB Output is correct
66 Correct 86 ms 35156 KB Output is correct
67 Correct 6 ms 21080 KB Output is correct
68 Correct 6 ms 21080 KB Output is correct
69 Correct 51 ms 34632 KB Output is correct
70 Correct 6 ms 21336 KB Output is correct
71 Correct 61 ms 34128 KB Output is correct
72 Correct 88 ms 36688 KB Output is correct
73 Correct 5 ms 21080 KB Output is correct
74 Correct 85 ms 41812 KB Output is correct
75 Correct 84 ms 38132 KB Output is correct
76 Correct 6 ms 21336 KB Output is correct
77 Correct 8 ms 21084 KB Output is correct
78 Correct 54 ms 34500 KB Output is correct
79 Correct 54 ms 35268 KB Output is correct
80 Correct 7 ms 21080 KB Output is correct
81 Correct 7 ms 21080 KB Output is correct
82 Correct 6 ms 21084 KB Output is correct
83 Correct 6 ms 21080 KB Output is correct
84 Correct 7 ms 21084 KB Output is correct
85 Correct 84 ms 34896 KB Output is correct
86 Correct 6 ms 21080 KB Output is correct
87 Correct 71 ms 34384 KB Output is correct
88 Correct 6 ms 21080 KB Output is correct
89 Correct 74 ms 34388 KB Output is correct
90 Correct 77 ms 34640 KB Output is correct
91 Correct 60 ms 34896 KB Output is correct
92 Correct 52 ms 34668 KB Output is correct
93 Correct 6 ms 21340 KB Output is correct
94 Correct 5 ms 21080 KB Output is correct
95 Correct 53 ms 35148 KB Output is correct
96 Correct 73 ms 38992 KB Output is correct
97 Correct 6 ms 21080 KB Output is correct
98 Correct 4 ms 21084 KB Output is correct
99 Correct 56 ms 35016 KB Output is correct
100 Correct 6 ms 21080 KB Output is correct
101 Correct 75 ms 34500 KB Output is correct
102 Correct 67 ms 37456 KB Output is correct
103 Correct 6 ms 21336 KB Output is correct
104 Correct 7 ms 21336 KB Output is correct
105 Correct 7 ms 21080 KB Output is correct
106 Correct 8 ms 21336 KB Output is correct
107 Correct 6 ms 21336 KB Output is correct
108 Correct 449 ms 88508 KB Output is correct
109 Correct 1036 ms 101660 KB Output is correct
110 Correct 1066 ms 112976 KB Output is correct
111 Correct 950 ms 110276 KB Output is correct
112 Correct 834 ms 100432 KB Output is correct
113 Correct 410 ms 88624 KB Output is correct
114 Correct 674 ms 83868 KB Output is correct
115 Correct 629 ms 83968 KB Output is correct
116 Correct 707 ms 86352 KB Output is correct
117 Correct 637 ms 87376 KB Output is correct
118 Correct 726 ms 83536 KB Output is correct
119 Correct 678 ms 87376 KB Output is correct
120 Correct 870 ms 101416 KB Output is correct
121 Correct 690 ms 87348 KB Output is correct
122 Correct 757 ms 87164 KB Output is correct
123 Correct 367 ms 88792 KB Output is correct
124 Correct 895 ms 85840 KB Output is correct