Submission #134495

# Submission time Handle Problem Language Result Execution time Memory
134495 2019-07-22T21:07:34 Z sealnot123 Mergers (JOI19_mergers) C++14
56 / 100
3000 ms 33740 KB
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
typedef double D;
typedef long double LD;
const int N = 100005;
vector<int> g[N], G[N], state[N];
int p[N], parent[N], target[N], level[N], LCA[20][N], dp[N][3];
int n, ans, tmp[3];

int fin(int i){
	if(p[i] == i) return i;
	return p[i] = fin(p[i]);
}

void dfs(int u, int v){
	LCA[0][u] = v;
	level[u] = level[v] + 1;
	parent[u] = v;
	for(int e : g[u]){
		if(e == v) continue;
		dfs(e, u);
	}
}

void DFS(int u, int v){
	/*printf("==%d\n",u);*/
	int leaf = 1;
	dp[u][0] = 0;
	dp[u][1] = dp[u][2] = N;
	int i,j,k;
	for(int e : G[u]){
		if(e == v) continue;
		leaf = 0;
		DFS(e, u);
		for(i=0;i<3;i++) tmp[i] = N;
		for(i=0;i<3;i++){
			for(j=0;j<3;j++){
				for(k=1;k<3;k++){
					if(j+k<i) continue;
					tmp[i] = min(tmp[i], dp[u][j]+dp[e][k]-min((j+k-i)/2,min(j,k)));
				}
			}
		}
		/*printf("**##%d : %d %d %d\n",u,tmp[0],tmp[1],tmp[2]);*/
		for(i=0;i<3;i++) dp[u][i] = tmp[i];
	}
	if(leaf){
		dp[u][0] = 0;
		dp[u][1] = 1;
		dp[u][2] = N;
	}
	/*printf("#%d : %d %d %d\n",u,dp[u][0],dp[u][1],dp[u][2]);*/
	return ;
}

int getLCA(int a, int b){
	if(level[a] < level[b]) swap(a, b);
	int i;
	for(i = 16; i >= 0; i--){
		if(level[LCA[i][a]] >= level[b]) a = LCA[i][a];  
	}
	/*printf("sddsas %d %d\n",a,b);*/
	if(a == b) return a;
	for(i = 16; i >= 0; i--){
		if(LCA[i][a] != LCA[i][b]) a = LCA[i][a], b = LCA[i][b];
	}
	return LCA[0][a];
}

int main(){
	int i,j,k,l,a,b,c,d;
	scanf("%d%d",&n,&k);
	for(i=1;i<n;i++){
		scanf("%d%d",&a,&b);
		g[a].pb(b);
		g[b].pb(a);
	}
	for(i=1;i<=n;i++){
		scanf("%d",&a), p[i]=i;
		state[a].pb(i);
	}
	dfs(1, 0);
	for(i = 1; i < 17; i++){
		for(j = 1; j <= n; j++){
			LCA[i][j] = LCA[i-1][LCA[i-1][j]];
		}
	}
	/*for(i=1;i<=n;i++) printf("%d : %d %d %d\n",i,level[i],LCA[0][i],LCA[1][i]);*/
	for(i = 1; i <= k; i++){
		target[i] = state[i][0];
		for(j = 1; j < SZ(state[i]); j++){
			/*printf("%d %d",target[i],state[i][j]);*/
			target[i] = getLCA(target[i], state[i][j]);
			/*printf(": %d\n",target[i]);*/
		}
		/*printf("#%d : %d\n",i,target[i]);*/
		for(int it : state[i]){
			a = it;
			while(a != target[i]){
				if(fin(a) != fin(parent[a])) p[fin(a)] = fin(parent[a]);
				a = parent[a];
			}
		}
	}
	for(i = 1; i <= n; i++){
		for(int e : g[i]){
			if(fin(i) != fin(e)){
				G[fin(i)].pb(fin(e));
			}
		}
	}
	for(i = 1; i <= n; i++){
		sort(all(G[i]));
		G[i].erase(unique(all(G[i])), G[i].end());
	}
	DFS(1, 1);
	printf("%d",dp[1][0]);
    return 0;
}
/*
4 4
1 2
1 3
1 4
1 2 3 4
(2)

6 6
1 2
1 3
1 4
4 5
5 6
1 2 3 4 5 6
(2)

7 7
1 2
1 3
2 4
2 5
3 6
3 7
1 2 3 4 5 6 7
(2)

10 10
1 2
1 3
1 4
4 5
4 6
5 7
5 8
6 9
6 10
1 2 3 4 5 6 7 8 9 10
(3)

9 9
1 2
2 3
2 4
1 5
5 6
5 7
7 8
7 9
1 2 3 4 5 6 7 8 9
(3)

5 4
1 2
2 3
3 4
3 5
1
2
1
3
4

5 4
1 2
2 3
3 4
4 5
1
2
3
4
1

2 2
1 2
1
2
*/

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:80:12: warning: unused variable 'l' [-Wunused-variable]
  int i,j,k,l,a,b,c,d;
            ^
mergers.cpp:80:18: warning: unused variable 'c' [-Wunused-variable]
  int i,j,k,l,a,b,c,d;
                  ^
mergers.cpp:80:20: warning: unused variable 'd' [-Wunused-variable]
  int i,j,k,l,a,b,c,d;
                    ^
mergers.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&k);
  ~~~~~^~~~~~~~~~~~~~
mergers.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&a,&b);
   ~~~~~^~~~~~~~~~~~~~
mergers.cpp:88:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&a), p[i]=i;
   ~~~~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7544 KB Output is correct
4 Correct 8 ms 7416 KB Output is correct
5 Correct 8 ms 7416 KB Output is correct
6 Correct 8 ms 7420 KB Output is correct
7 Correct 8 ms 7416 KB Output is correct
8 Correct 8 ms 7544 KB Output is correct
9 Correct 8 ms 7544 KB Output is correct
10 Correct 8 ms 7416 KB Output is correct
11 Correct 8 ms 7544 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7516 KB Output is correct
14 Correct 8 ms 7416 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7544 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 8 ms 7544 KB Output is correct
19 Correct 8 ms 7544 KB Output is correct
20 Correct 8 ms 7544 KB Output is correct
21 Correct 8 ms 7544 KB Output is correct
22 Correct 8 ms 7544 KB Output is correct
23 Correct 8 ms 7416 KB Output is correct
24 Correct 8 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7544 KB Output is correct
4 Correct 8 ms 7416 KB Output is correct
5 Correct 8 ms 7416 KB Output is correct
6 Correct 8 ms 7420 KB Output is correct
7 Correct 8 ms 7416 KB Output is correct
8 Correct 8 ms 7544 KB Output is correct
9 Correct 8 ms 7544 KB Output is correct
10 Correct 8 ms 7416 KB Output is correct
11 Correct 8 ms 7544 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7516 KB Output is correct
14 Correct 8 ms 7416 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7544 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 8 ms 7544 KB Output is correct
19 Correct 8 ms 7544 KB Output is correct
20 Correct 8 ms 7544 KB Output is correct
21 Correct 8 ms 7544 KB Output is correct
22 Correct 8 ms 7544 KB Output is correct
23 Correct 8 ms 7416 KB Output is correct
24 Correct 8 ms 7416 KB Output is correct
25 Correct 8 ms 7544 KB Output is correct
26 Correct 11 ms 7928 KB Output is correct
27 Correct 11 ms 7928 KB Output is correct
28 Correct 11 ms 8104 KB Output is correct
29 Correct 11 ms 8056 KB Output is correct
30 Correct 11 ms 7800 KB Output is correct
31 Correct 8 ms 7544 KB Output is correct
32 Correct 11 ms 8056 KB Output is correct
33 Correct 8 ms 7416 KB Output is correct
34 Correct 10 ms 7932 KB Output is correct
35 Correct 11 ms 8056 KB Output is correct
36 Correct 11 ms 7928 KB Output is correct
37 Correct 11 ms 7928 KB Output is correct
38 Correct 8 ms 7544 KB Output is correct
39 Correct 11 ms 7928 KB Output is correct
40 Correct 11 ms 7928 KB Output is correct
41 Correct 10 ms 7956 KB Output is correct
42 Correct 10 ms 7928 KB Output is correct
43 Correct 54 ms 7928 KB Output is correct
44 Correct 8 ms 7416 KB Output is correct
45 Correct 11 ms 7928 KB Output is correct
46 Correct 11 ms 7928 KB Output is correct
47 Correct 8 ms 7544 KB Output is correct
48 Correct 11 ms 7928 KB Output is correct
49 Correct 11 ms 8184 KB Output is correct
50 Correct 11 ms 8184 KB Output is correct
51 Correct 13 ms 7928 KB Output is correct
52 Correct 10 ms 7800 KB Output is correct
53 Correct 11 ms 7932 KB Output is correct
54 Correct 11 ms 7800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7544 KB Output is correct
4 Correct 8 ms 7416 KB Output is correct
5 Correct 8 ms 7416 KB Output is correct
6 Correct 8 ms 7420 KB Output is correct
7 Correct 8 ms 7416 KB Output is correct
8 Correct 8 ms 7544 KB Output is correct
9 Correct 8 ms 7544 KB Output is correct
10 Correct 8 ms 7416 KB Output is correct
11 Correct 8 ms 7544 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7516 KB Output is correct
14 Correct 8 ms 7416 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7544 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 8 ms 7544 KB Output is correct
19 Correct 8 ms 7544 KB Output is correct
20 Correct 8 ms 7544 KB Output is correct
21 Correct 8 ms 7544 KB Output is correct
22 Correct 8 ms 7544 KB Output is correct
23 Correct 8 ms 7416 KB Output is correct
24 Correct 8 ms 7416 KB Output is correct
25 Correct 8 ms 7544 KB Output is correct
26 Correct 106 ms 20768 KB Output is correct
27 Correct 195 ms 20520 KB Output is correct
28 Correct 11 ms 7928 KB Output is correct
29 Correct 8 ms 7544 KB Output is correct
30 Correct 8 ms 7544 KB Output is correct
31 Correct 192 ms 20592 KB Output is correct
32 Correct 12 ms 7928 KB Output is correct
33 Execution timed out 3058 ms 24696 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 105 ms 19312 KB Output is correct
2 Correct 129 ms 26964 KB Output is correct
3 Correct 11 ms 8056 KB Output is correct
4 Correct 11 ms 7928 KB Output is correct
5 Correct 8 ms 7416 KB Output is correct
6 Correct 8 ms 7416 KB Output is correct
7 Correct 10 ms 7800 KB Output is correct
8 Correct 175 ms 23564 KB Output is correct
9 Correct 11 ms 7928 KB Output is correct
10 Correct 152 ms 21724 KB Output is correct
11 Correct 8 ms 7416 KB Output is correct
12 Correct 210 ms 21624 KB Output is correct
13 Correct 162 ms 23640 KB Output is correct
14 Correct 148 ms 28152 KB Output is correct
15 Correct 121 ms 20692 KB Output is correct
16 Correct 12 ms 7928 KB Output is correct
17 Correct 9 ms 7544 KB Output is correct
18 Correct 137 ms 27620 KB Output is correct
19 Correct 166 ms 33740 KB Output is correct
20 Correct 12 ms 7928 KB Output is correct
21 Correct 8 ms 7488 KB Output is correct
22 Correct 133 ms 23924 KB Output is correct
23 Correct 11 ms 7928 KB Output is correct
24 Correct 171 ms 22392 KB Output is correct
25 Correct 169 ms 31856 KB Output is correct
26 Correct 11 ms 8056 KB Output is correct
27 Correct 11 ms 8184 KB Output is correct
28 Correct 11 ms 7872 KB Output is correct
29 Correct 10 ms 7800 KB Output is correct
30 Correct 10 ms 7928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7544 KB Output is correct
4 Correct 8 ms 7416 KB Output is correct
5 Correct 8 ms 7416 KB Output is correct
6 Correct 8 ms 7420 KB Output is correct
7 Correct 8 ms 7416 KB Output is correct
8 Correct 8 ms 7544 KB Output is correct
9 Correct 8 ms 7544 KB Output is correct
10 Correct 8 ms 7416 KB Output is correct
11 Correct 8 ms 7544 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7516 KB Output is correct
14 Correct 8 ms 7416 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7544 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 8 ms 7544 KB Output is correct
19 Correct 8 ms 7544 KB Output is correct
20 Correct 8 ms 7544 KB Output is correct
21 Correct 8 ms 7544 KB Output is correct
22 Correct 8 ms 7544 KB Output is correct
23 Correct 8 ms 7416 KB Output is correct
24 Correct 8 ms 7416 KB Output is correct
25 Correct 8 ms 7544 KB Output is correct
26 Correct 11 ms 7928 KB Output is correct
27 Correct 11 ms 7928 KB Output is correct
28 Correct 11 ms 8104 KB Output is correct
29 Correct 11 ms 8056 KB Output is correct
30 Correct 11 ms 7800 KB Output is correct
31 Correct 8 ms 7544 KB Output is correct
32 Correct 11 ms 8056 KB Output is correct
33 Correct 8 ms 7416 KB Output is correct
34 Correct 10 ms 7932 KB Output is correct
35 Correct 11 ms 8056 KB Output is correct
36 Correct 11 ms 7928 KB Output is correct
37 Correct 11 ms 7928 KB Output is correct
38 Correct 8 ms 7544 KB Output is correct
39 Correct 11 ms 7928 KB Output is correct
40 Correct 11 ms 7928 KB Output is correct
41 Correct 10 ms 7956 KB Output is correct
42 Correct 10 ms 7928 KB Output is correct
43 Correct 54 ms 7928 KB Output is correct
44 Correct 8 ms 7416 KB Output is correct
45 Correct 11 ms 7928 KB Output is correct
46 Correct 11 ms 7928 KB Output is correct
47 Correct 8 ms 7544 KB Output is correct
48 Correct 11 ms 7928 KB Output is correct
49 Correct 11 ms 8184 KB Output is correct
50 Correct 11 ms 8184 KB Output is correct
51 Correct 13 ms 7928 KB Output is correct
52 Correct 10 ms 7800 KB Output is correct
53 Correct 11 ms 7932 KB Output is correct
54 Correct 11 ms 7800 KB Output is correct
55 Correct 8 ms 7544 KB Output is correct
56 Correct 106 ms 20768 KB Output is correct
57 Correct 195 ms 20520 KB Output is correct
58 Correct 11 ms 7928 KB Output is correct
59 Correct 8 ms 7544 KB Output is correct
60 Correct 8 ms 7544 KB Output is correct
61 Correct 192 ms 20592 KB Output is correct
62 Correct 12 ms 7928 KB Output is correct
63 Execution timed out 3058 ms 24696 KB Time limit exceeded
64 Halted 0 ms 0 KB -