답안 #134561

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134561 2019-07-23T03:59:33 Z sealnot123 Mergers (JOI19_mergers) C++14
70 / 100
234 ms 33572 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 = fin(it);
			while(level[a] > level[target[i]]){
				p[a] = fin(parent[a]);
				a = p[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;
   ~~~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 8 ms 7544 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7544 KB Output is correct
5 Correct 7 ms 7544 KB Output is correct
6 Correct 8 ms 7548 KB Output is correct
7 Correct 9 ms 7544 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 8 ms 7416 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7516 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7544 KB Output is correct
14 Correct 8 ms 7544 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7448 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 9 ms 7548 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 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 8 ms 7544 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7544 KB Output is correct
5 Correct 7 ms 7544 KB Output is correct
6 Correct 8 ms 7548 KB Output is correct
7 Correct 9 ms 7544 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 8 ms 7416 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7516 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7544 KB Output is correct
14 Correct 8 ms 7544 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7448 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 9 ms 7548 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 7544 KB Output is correct
25 Correct 8 ms 7544 KB Output is correct
26 Correct 11 ms 8056 KB Output is correct
27 Correct 11 ms 7928 KB Output is correct
28 Correct 12 ms 8184 KB Output is correct
29 Correct 12 ms 8032 KB Output is correct
30 Correct 12 ms 7928 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 7800 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 12 ms 7928 KB Output is correct
40 Correct 13 ms 7800 KB Output is correct
41 Correct 11 ms 7924 KB Output is correct
42 Correct 11 ms 7928 KB Output is correct
43 Correct 11 ms 7928 KB Output is correct
44 Correct 8 ms 7544 KB Output is correct
45 Correct 11 ms 8184 KB Output is correct
46 Correct 11 ms 7928 KB Output is correct
47 Correct 8 ms 7544 KB Output is correct
48 Correct 12 ms 7900 KB Output is correct
49 Correct 11 ms 8056 KB Output is correct
50 Correct 12 ms 8184 KB Output is correct
51 Correct 11 ms 7928 KB Output is correct
52 Correct 10 ms 7932 KB Output is correct
53 Correct 11 ms 7928 KB Output is correct
54 Correct 11 ms 7804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 8 ms 7544 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7544 KB Output is correct
5 Correct 7 ms 7544 KB Output is correct
6 Correct 8 ms 7548 KB Output is correct
7 Correct 9 ms 7544 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 8 ms 7416 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7516 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7544 KB Output is correct
14 Correct 8 ms 7544 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7448 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 9 ms 7548 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 7544 KB Output is correct
25 Correct 8 ms 7480 KB Output is correct
26 Correct 112 ms 20708 KB Output is correct
27 Correct 207 ms 20540 KB Output is correct
28 Correct 11 ms 7928 KB Output is correct
29 Correct 8 ms 7416 KB Output is correct
30 Correct 8 ms 7544 KB Output is correct
31 Correct 146 ms 20468 KB Output is correct
32 Correct 10 ms 7800 KB Output is correct
33 Correct 176 ms 24568 KB Output is correct
34 Correct 161 ms 20384 KB Output is correct
35 Correct 11 ms 7916 KB Output is correct
36 Correct 198 ms 20864 KB Output is correct
37 Correct 12 ms 7928 KB Output is correct
38 Correct 13 ms 7928 KB Output is correct
39 Correct 157 ms 20716 KB Output is correct
40 Correct 11 ms 8024 KB Output is correct
41 Correct 123 ms 20336 KB Output is correct
42 Correct 186 ms 21788 KB Output is correct
43 Correct 8 ms 7432 KB Output is correct
44 Correct 168 ms 24816 KB Output is correct
45 Correct 170 ms 22520 KB Output is correct
46 Correct 11 ms 7928 KB Output is correct
47 Correct 11 ms 7848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 19356 KB Output is correct
2 Correct 143 ms 28788 KB Output is correct
3 Correct 14 ms 8028 KB Output is correct
4 Correct 12 ms 7928 KB Output is correct
5 Correct 10 ms 7416 KB Output is correct
6 Correct 10 ms 7440 KB Output is correct
7 Correct 13 ms 7800 KB Output is correct
8 Correct 214 ms 23544 KB Output is correct
9 Correct 11 ms 7840 KB Output is correct
10 Correct 207 ms 21636 KB Output is correct
11 Correct 8 ms 7416 KB Output is correct
12 Correct 233 ms 21596 KB Output is correct
13 Correct 205 ms 23708 KB Output is correct
14 Correct 178 ms 28044 KB Output is correct
15 Correct 124 ms 20568 KB Output is correct
16 Correct 11 ms 7928 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 156 ms 27632 KB Output is correct
19 Correct 203 ms 33572 KB Output is correct
20 Correct 11 ms 7928 KB Output is correct
21 Correct 8 ms 7416 KB Output is correct
22 Correct 177 ms 24000 KB Output is correct
23 Correct 12 ms 7928 KB Output is correct
24 Correct 234 ms 22352 KB Output is correct
25 Correct 182 ms 31668 KB Output is correct
26 Correct 11 ms 8056 KB Output is correct
27 Correct 12 ms 8156 KB Output is correct
28 Correct 11 ms 7900 KB Output is correct
29 Correct 10 ms 7800 KB Output is correct
30 Correct 12 ms 7928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 8 ms 7544 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7544 KB Output is correct
5 Correct 7 ms 7544 KB Output is correct
6 Correct 8 ms 7548 KB Output is correct
7 Correct 9 ms 7544 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 8 ms 7416 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7516 KB Output is correct
12 Correct 8 ms 7544 KB Output is correct
13 Correct 8 ms 7544 KB Output is correct
14 Correct 8 ms 7544 KB Output is correct
15 Correct 8 ms 7544 KB Output is correct
16 Correct 8 ms 7448 KB Output is correct
17 Correct 8 ms 7544 KB Output is correct
18 Correct 9 ms 7548 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 7544 KB Output is correct
25 Correct 8 ms 7544 KB Output is correct
26 Correct 11 ms 8056 KB Output is correct
27 Correct 11 ms 7928 KB Output is correct
28 Correct 12 ms 8184 KB Output is correct
29 Correct 12 ms 8032 KB Output is correct
30 Correct 12 ms 7928 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 7800 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 12 ms 7928 KB Output is correct
40 Correct 13 ms 7800 KB Output is correct
41 Correct 11 ms 7924 KB Output is correct
42 Correct 11 ms 7928 KB Output is correct
43 Correct 11 ms 7928 KB Output is correct
44 Correct 8 ms 7544 KB Output is correct
45 Correct 11 ms 8184 KB Output is correct
46 Correct 11 ms 7928 KB Output is correct
47 Correct 8 ms 7544 KB Output is correct
48 Correct 12 ms 7900 KB Output is correct
49 Correct 11 ms 8056 KB Output is correct
50 Correct 12 ms 8184 KB Output is correct
51 Correct 11 ms 7928 KB Output is correct
52 Correct 10 ms 7932 KB Output is correct
53 Correct 11 ms 7928 KB Output is correct
54 Correct 11 ms 7804 KB Output is correct
55 Correct 8 ms 7480 KB Output is correct
56 Correct 112 ms 20708 KB Output is correct
57 Correct 207 ms 20540 KB Output is correct
58 Correct 11 ms 7928 KB Output is correct
59 Correct 8 ms 7416 KB Output is correct
60 Correct 8 ms 7544 KB Output is correct
61 Correct 146 ms 20468 KB Output is correct
62 Correct 10 ms 7800 KB Output is correct
63 Correct 176 ms 24568 KB Output is correct
64 Correct 161 ms 20384 KB Output is correct
65 Correct 11 ms 7916 KB Output is correct
66 Correct 198 ms 20864 KB Output is correct
67 Correct 12 ms 7928 KB Output is correct
68 Correct 13 ms 7928 KB Output is correct
69 Correct 157 ms 20716 KB Output is correct
70 Correct 11 ms 8024 KB Output is correct
71 Correct 123 ms 20336 KB Output is correct
72 Correct 186 ms 21788 KB Output is correct
73 Correct 8 ms 7432 KB Output is correct
74 Correct 168 ms 24816 KB Output is correct
75 Correct 170 ms 22520 KB Output is correct
76 Correct 11 ms 7928 KB Output is correct
77 Correct 11 ms 7848 KB Output is correct
78 Correct 113 ms 19356 KB Output is correct
79 Correct 143 ms 28788 KB Output is correct
80 Correct 14 ms 8028 KB Output is correct
81 Correct 12 ms 7928 KB Output is correct
82 Correct 10 ms 7416 KB Output is correct
83 Correct 10 ms 7440 KB Output is correct
84 Correct 13 ms 7800 KB Output is correct
85 Correct 214 ms 23544 KB Output is correct
86 Correct 11 ms 7840 KB Output is correct
87 Correct 207 ms 21636 KB Output is correct
88 Correct 8 ms 7416 KB Output is correct
89 Correct 233 ms 21596 KB Output is correct
90 Correct 205 ms 23708 KB Output is correct
91 Correct 178 ms 28044 KB Output is correct
92 Correct 124 ms 20568 KB Output is correct
93 Correct 11 ms 7928 KB Output is correct
94 Correct 8 ms 7544 KB Output is correct
95 Correct 156 ms 27632 KB Output is correct
96 Correct 203 ms 33572 KB Output is correct
97 Correct 11 ms 7928 KB Output is correct
98 Correct 8 ms 7416 KB Output is correct
99 Correct 177 ms 24000 KB Output is correct
100 Correct 12 ms 7928 KB Output is correct
101 Correct 234 ms 22352 KB Output is correct
102 Correct 182 ms 31668 KB Output is correct
103 Correct 11 ms 8056 KB Output is correct
104 Correct 12 ms 8156 KB Output is correct
105 Correct 11 ms 7900 KB Output is correct
106 Correct 10 ms 7800 KB Output is correct
107 Correct 12 ms 7928 KB Output is correct
108 Runtime error 18 ms 14712 KB Execution killed with signal 11 (could be triggered by violating memory limits)
109 Halted 0 ms 0 KB -