Submission #295220

# Submission time Handle Problem Language Result Execution time Memory
295220 2020-09-09T14:27:24 Z arnold518 Mergers (JOI19_mergers) C++14
100 / 100
1645 ms 156408 KB
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 5e5;

int N, K;
vector<int> adj[MAXN+10];
vector<int> V[MAXN+10];

int L[MAXN+10], R[MAXN+10], cnt, dep[MAXN+10];
int par[MAXN+10][30], A[MAXN+10];
int dp[MAXN+10], P[MAXN+10], deg[MAXN+10];

void dfs(int now, int bef, int d)
{
	L[now]=++cnt;
	par[now][0]=bef;
	dep[now]=d;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now, d+1);
	}
	R[now]=cnt;
}

int lca(int u, int v)
{
	if(dep[u]>dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) v=par[v][i];
	if(u==v) return u;
	for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
	return par[u][0];
}

void dfs2(int now, int bef)
{
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs2(nxt, now);
		dp[now]+=dp[nxt];
	}
}

void dfs3(int now, int bef)
{
	if(dp[now]==0)
	{
		P[now]=now;
		if(now!=bef) deg[now]++, deg[P[bef]]++;
	}
	else P[now]=P[bef];
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs3(nxt, now);
	}
}

int main()
{
	scanf("%d%d", &N, &K);
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1; i<=N; i++) scanf("%d", &A[i]), V[A[i]].push_back(i);

	dfs(1, 1, 1);
	for(int i=1; i<=20; i++) for(int j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];

	for(int i=1; i<=N; i++) dp[i]++;
	for(int i=1; i<=K; i++)
	{
		int w=V[i][0];
		for(auto it : V[i]) w=lca(w, it);
		dp[w]-=V[i].size();
	}

	dfs2(1, 1);
	dfs3(1, 1);

	int ans=0;
	for(int i=1; i<=N; i++) if(deg[i]==1) ans++;
	printf("%d\n", (ans+1)/2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |  scanf("%d%d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   71 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:75:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   75 |  for(int i=1; i<=N; i++) scanf("%d", &A[i]), V[A[i]].push_back(i);
      |                          ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23828 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 19 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23892 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 16 ms 23872 KB Output is correct
10 Correct 20 ms 23880 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 17 ms 23936 KB Output is correct
13 Correct 21 ms 23816 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 19 ms 23808 KB Output is correct
16 Correct 17 ms 23880 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23856 KB Output is correct
21 Correct 16 ms 23828 KB Output is correct
22 Correct 20 ms 23880 KB Output is correct
23 Correct 16 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23828 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 19 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23892 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 16 ms 23872 KB Output is correct
10 Correct 20 ms 23880 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 17 ms 23936 KB Output is correct
13 Correct 21 ms 23816 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 19 ms 23808 KB Output is correct
16 Correct 17 ms 23880 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23856 KB Output is correct
21 Correct 16 ms 23828 KB Output is correct
22 Correct 20 ms 23880 KB Output is correct
23 Correct 16 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 15 ms 23824 KB Output is correct
26 Correct 23 ms 24448 KB Output is correct
27 Correct 18 ms 24448 KB Output is correct
28 Correct 20 ms 24576 KB Output is correct
29 Correct 21 ms 24576 KB Output is correct
30 Correct 18 ms 24448 KB Output is correct
31 Correct 16 ms 23808 KB Output is correct
32 Correct 20 ms 24704 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 20 ms 24380 KB Output is correct
35 Correct 25 ms 24576 KB Output is correct
36 Correct 19 ms 24384 KB Output is correct
37 Correct 19 ms 24448 KB Output is correct
38 Correct 15 ms 23860 KB Output is correct
39 Correct 19 ms 24448 KB Output is correct
40 Correct 19 ms 24472 KB Output is correct
41 Correct 19 ms 24448 KB Output is correct
42 Correct 18 ms 24504 KB Output is correct
43 Correct 22 ms 24576 KB Output is correct
44 Correct 15 ms 23840 KB Output is correct
45 Correct 18 ms 24576 KB Output is correct
46 Correct 24 ms 24448 KB Output is correct
47 Correct 16 ms 23840 KB Output is correct
48 Correct 21 ms 24448 KB Output is correct
49 Correct 19 ms 24576 KB Output is correct
50 Correct 20 ms 24576 KB Output is correct
51 Correct 20 ms 24448 KB Output is correct
52 Correct 18 ms 24448 KB Output is correct
53 Correct 20 ms 24448 KB Output is correct
54 Correct 18 ms 24448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23828 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 19 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23892 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 16 ms 23872 KB Output is correct
10 Correct 20 ms 23880 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 17 ms 23936 KB Output is correct
13 Correct 21 ms 23816 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 19 ms 23808 KB Output is correct
16 Correct 17 ms 23880 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23856 KB Output is correct
21 Correct 16 ms 23828 KB Output is correct
22 Correct 20 ms 23880 KB Output is correct
23 Correct 16 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 128 ms 43504 KB Output is correct
27 Correct 173 ms 43128 KB Output is correct
28 Correct 18 ms 24448 KB Output is correct
29 Correct 15 ms 23808 KB Output is correct
30 Correct 19 ms 23808 KB Output is correct
31 Correct 183 ms 43256 KB Output is correct
32 Correct 18 ms 24448 KB Output is correct
33 Correct 208 ms 48868 KB Output is correct
34 Correct 194 ms 43144 KB Output is correct
35 Correct 22 ms 24448 KB Output is correct
36 Correct 236 ms 43768 KB Output is correct
37 Correct 22 ms 24448 KB Output is correct
38 Correct 21 ms 24448 KB Output is correct
39 Correct 157 ms 43376 KB Output is correct
40 Correct 21 ms 24576 KB Output is correct
41 Correct 183 ms 43120 KB Output is correct
42 Correct 246 ms 44896 KB Output is correct
43 Correct 20 ms 23808 KB Output is correct
44 Correct 216 ms 48964 KB Output is correct
45 Correct 220 ms 46072 KB Output is correct
46 Correct 18 ms 24448 KB Output is correct
47 Correct 21 ms 24576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 43460 KB Output is correct
2 Correct 164 ms 46812 KB Output is correct
3 Correct 19 ms 24440 KB Output is correct
4 Correct 19 ms 24448 KB Output is correct
5 Correct 28 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 19 ms 24448 KB Output is correct
8 Correct 230 ms 44896 KB Output is correct
9 Correct 19 ms 24448 KB Output is correct
10 Correct 211 ms 43728 KB Output is correct
11 Correct 18 ms 23980 KB Output is correct
12 Correct 232 ms 43572 KB Output is correct
13 Correct 232 ms 44792 KB Output is correct
14 Correct 222 ms 46456 KB Output is correct
15 Correct 136 ms 43380 KB Output is correct
16 Correct 20 ms 24576 KB Output is correct
17 Correct 16 ms 23936 KB Output is correct
18 Correct 162 ms 46448 KB Output is correct
19 Correct 244 ms 49580 KB Output is correct
20 Correct 20 ms 24512 KB Output is correct
21 Correct 18 ms 23936 KB Output is correct
22 Correct 163 ms 45040 KB Output is correct
23 Correct 20 ms 24448 KB Output is correct
24 Correct 211 ms 44024 KB Output is correct
25 Correct 230 ms 48732 KB Output is correct
26 Correct 20 ms 24592 KB Output is correct
27 Correct 21 ms 24576 KB Output is correct
28 Correct 21 ms 24448 KB Output is correct
29 Correct 18 ms 24448 KB Output is correct
30 Correct 19 ms 24448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23828 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 19 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23892 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 16 ms 23872 KB Output is correct
10 Correct 20 ms 23880 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 17 ms 23936 KB Output is correct
13 Correct 21 ms 23816 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 19 ms 23808 KB Output is correct
16 Correct 17 ms 23880 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 16 ms 23856 KB Output is correct
21 Correct 16 ms 23828 KB Output is correct
22 Correct 20 ms 23880 KB Output is correct
23 Correct 16 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 15 ms 23824 KB Output is correct
26 Correct 23 ms 24448 KB Output is correct
27 Correct 18 ms 24448 KB Output is correct
28 Correct 20 ms 24576 KB Output is correct
29 Correct 21 ms 24576 KB Output is correct
30 Correct 18 ms 24448 KB Output is correct
31 Correct 16 ms 23808 KB Output is correct
32 Correct 20 ms 24704 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 20 ms 24380 KB Output is correct
35 Correct 25 ms 24576 KB Output is correct
36 Correct 19 ms 24384 KB Output is correct
37 Correct 19 ms 24448 KB Output is correct
38 Correct 15 ms 23860 KB Output is correct
39 Correct 19 ms 24448 KB Output is correct
40 Correct 19 ms 24472 KB Output is correct
41 Correct 19 ms 24448 KB Output is correct
42 Correct 18 ms 24504 KB Output is correct
43 Correct 22 ms 24576 KB Output is correct
44 Correct 15 ms 23840 KB Output is correct
45 Correct 18 ms 24576 KB Output is correct
46 Correct 24 ms 24448 KB Output is correct
47 Correct 16 ms 23840 KB Output is correct
48 Correct 21 ms 24448 KB Output is correct
49 Correct 19 ms 24576 KB Output is correct
50 Correct 20 ms 24576 KB Output is correct
51 Correct 20 ms 24448 KB Output is correct
52 Correct 18 ms 24448 KB Output is correct
53 Correct 20 ms 24448 KB Output is correct
54 Correct 18 ms 24448 KB Output is correct
55 Correct 17 ms 23808 KB Output is correct
56 Correct 128 ms 43504 KB Output is correct
57 Correct 173 ms 43128 KB Output is correct
58 Correct 18 ms 24448 KB Output is correct
59 Correct 15 ms 23808 KB Output is correct
60 Correct 19 ms 23808 KB Output is correct
61 Correct 183 ms 43256 KB Output is correct
62 Correct 18 ms 24448 KB Output is correct
63 Correct 208 ms 48868 KB Output is correct
64 Correct 194 ms 43144 KB Output is correct
65 Correct 22 ms 24448 KB Output is correct
66 Correct 236 ms 43768 KB Output is correct
67 Correct 22 ms 24448 KB Output is correct
68 Correct 21 ms 24448 KB Output is correct
69 Correct 157 ms 43376 KB Output is correct
70 Correct 21 ms 24576 KB Output is correct
71 Correct 183 ms 43120 KB Output is correct
72 Correct 246 ms 44896 KB Output is correct
73 Correct 20 ms 23808 KB Output is correct
74 Correct 216 ms 48964 KB Output is correct
75 Correct 220 ms 46072 KB Output is correct
76 Correct 18 ms 24448 KB Output is correct
77 Correct 21 ms 24576 KB Output is correct
78 Correct 141 ms 43460 KB Output is correct
79 Correct 164 ms 46812 KB Output is correct
80 Correct 19 ms 24440 KB Output is correct
81 Correct 19 ms 24448 KB Output is correct
82 Correct 28 ms 23808 KB Output is correct
83 Correct 16 ms 23808 KB Output is correct
84 Correct 19 ms 24448 KB Output is correct
85 Correct 230 ms 44896 KB Output is correct
86 Correct 19 ms 24448 KB Output is correct
87 Correct 211 ms 43728 KB Output is correct
88 Correct 18 ms 23980 KB Output is correct
89 Correct 232 ms 43572 KB Output is correct
90 Correct 232 ms 44792 KB Output is correct
91 Correct 222 ms 46456 KB Output is correct
92 Correct 136 ms 43380 KB Output is correct
93 Correct 20 ms 24576 KB Output is correct
94 Correct 16 ms 23936 KB Output is correct
95 Correct 162 ms 46448 KB Output is correct
96 Correct 244 ms 49580 KB Output is correct
97 Correct 20 ms 24512 KB Output is correct
98 Correct 18 ms 23936 KB Output is correct
99 Correct 163 ms 45040 KB Output is correct
100 Correct 20 ms 24448 KB Output is correct
101 Correct 211 ms 44024 KB Output is correct
102 Correct 230 ms 48732 KB Output is correct
103 Correct 20 ms 24592 KB Output is correct
104 Correct 21 ms 24576 KB Output is correct
105 Correct 21 ms 24448 KB Output is correct
106 Correct 18 ms 24448 KB Output is correct
107 Correct 19 ms 24448 KB Output is correct
108 Correct 940 ms 130308 KB Output is correct
109 Correct 1474 ms 134616 KB Output is correct
110 Correct 1244 ms 143464 KB Output is correct
111 Correct 1645 ms 156408 KB Output is correct
112 Correct 1501 ms 148532 KB Output is correct
113 Correct 973 ms 138080 KB Output is correct
114 Correct 983 ms 120292 KB Output is correct
115 Correct 976 ms 120348 KB Output is correct
116 Correct 1320 ms 126712 KB Output is correct
117 Correct 1301 ms 138120 KB Output is correct
118 Correct 1031 ms 121592 KB Output is correct
119 Correct 1274 ms 138360 KB Output is correct
120 Correct 1470 ms 149368 KB Output is correct
121 Correct 1292 ms 138156 KB Output is correct
122 Correct 1311 ms 131832 KB Output is correct
123 Correct 963 ms 139736 KB Output is correct
124 Correct 1440 ms 135776 KB Output is correct