답안 #134564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134564 2019-07-23T04:00:40 Z sealnot123 Mergers (JOI19_mergers) C++14
100 / 100
1961 ms 171248 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 = 500005;
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 = 18; 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 = 18; 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 < 19; 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 34 ms 35752 KB Output is correct
2 Correct 35 ms 35804 KB Output is correct
3 Correct 38 ms 35796 KB Output is correct
4 Correct 35 ms 35680 KB Output is correct
5 Correct 38 ms 35656 KB Output is correct
6 Correct 41 ms 35700 KB Output is correct
7 Correct 34 ms 35704 KB Output is correct
8 Correct 35 ms 35696 KB Output is correct
9 Correct 34 ms 35732 KB Output is correct
10 Correct 34 ms 35740 KB Output is correct
11 Correct 34 ms 35708 KB Output is correct
12 Correct 34 ms 35788 KB Output is correct
13 Correct 34 ms 35708 KB Output is correct
14 Correct 36 ms 35692 KB Output is correct
15 Correct 34 ms 35704 KB Output is correct
16 Correct 34 ms 35704 KB Output is correct
17 Correct 34 ms 35704 KB Output is correct
18 Correct 35 ms 35704 KB Output is correct
19 Correct 34 ms 35708 KB Output is correct
20 Correct 34 ms 35704 KB Output is correct
21 Correct 34 ms 35676 KB Output is correct
22 Correct 35 ms 35736 KB Output is correct
23 Correct 35 ms 35704 KB Output is correct
24 Correct 35 ms 35704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 35752 KB Output is correct
2 Correct 35 ms 35804 KB Output is correct
3 Correct 38 ms 35796 KB Output is correct
4 Correct 35 ms 35680 KB Output is correct
5 Correct 38 ms 35656 KB Output is correct
6 Correct 41 ms 35700 KB Output is correct
7 Correct 34 ms 35704 KB Output is correct
8 Correct 35 ms 35696 KB Output is correct
9 Correct 34 ms 35732 KB Output is correct
10 Correct 34 ms 35740 KB Output is correct
11 Correct 34 ms 35708 KB Output is correct
12 Correct 34 ms 35788 KB Output is correct
13 Correct 34 ms 35708 KB Output is correct
14 Correct 36 ms 35692 KB Output is correct
15 Correct 34 ms 35704 KB Output is correct
16 Correct 34 ms 35704 KB Output is correct
17 Correct 34 ms 35704 KB Output is correct
18 Correct 35 ms 35704 KB Output is correct
19 Correct 34 ms 35708 KB Output is correct
20 Correct 34 ms 35704 KB Output is correct
21 Correct 34 ms 35676 KB Output is correct
22 Correct 35 ms 35736 KB Output is correct
23 Correct 35 ms 35704 KB Output is correct
24 Correct 35 ms 35704 KB Output is correct
25 Correct 34 ms 35704 KB Output is correct
26 Correct 37 ms 36188 KB Output is correct
27 Correct 37 ms 36088 KB Output is correct
28 Correct 37 ms 36344 KB Output is correct
29 Correct 38 ms 36324 KB Output is correct
30 Correct 44 ms 36092 KB Output is correct
31 Correct 34 ms 35704 KB Output is correct
32 Correct 37 ms 36344 KB Output is correct
33 Correct 34 ms 35832 KB Output is correct
34 Correct 36 ms 36200 KB Output is correct
35 Correct 37 ms 36344 KB Output is correct
36 Correct 37 ms 36088 KB Output is correct
37 Correct 37 ms 36216 KB Output is correct
38 Correct 34 ms 35832 KB Output is correct
39 Correct 39 ms 36236 KB Output is correct
40 Correct 36 ms 36088 KB Output is correct
41 Correct 38 ms 36216 KB Output is correct
42 Correct 37 ms 36216 KB Output is correct
43 Correct 36 ms 36216 KB Output is correct
44 Correct 34 ms 35704 KB Output is correct
45 Correct 45 ms 36216 KB Output is correct
46 Correct 46 ms 36244 KB Output is correct
47 Correct 34 ms 35704 KB Output is correct
48 Correct 38 ms 36344 KB Output is correct
49 Correct 37 ms 36344 KB Output is correct
50 Correct 37 ms 36472 KB Output is correct
51 Correct 37 ms 36216 KB Output is correct
52 Correct 37 ms 36088 KB Output is correct
53 Correct 37 ms 36216 KB Output is correct
54 Correct 37 ms 36088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 35752 KB Output is correct
2 Correct 35 ms 35804 KB Output is correct
3 Correct 38 ms 35796 KB Output is correct
4 Correct 35 ms 35680 KB Output is correct
5 Correct 38 ms 35656 KB Output is correct
6 Correct 41 ms 35700 KB Output is correct
7 Correct 34 ms 35704 KB Output is correct
8 Correct 35 ms 35696 KB Output is correct
9 Correct 34 ms 35732 KB Output is correct
10 Correct 34 ms 35740 KB Output is correct
11 Correct 34 ms 35708 KB Output is correct
12 Correct 34 ms 35788 KB Output is correct
13 Correct 34 ms 35708 KB Output is correct
14 Correct 36 ms 35692 KB Output is correct
15 Correct 34 ms 35704 KB Output is correct
16 Correct 34 ms 35704 KB Output is correct
17 Correct 34 ms 35704 KB Output is correct
18 Correct 35 ms 35704 KB Output is correct
19 Correct 34 ms 35708 KB Output is correct
20 Correct 34 ms 35704 KB Output is correct
21 Correct 34 ms 35676 KB Output is correct
22 Correct 35 ms 35736 KB Output is correct
23 Correct 35 ms 35704 KB Output is correct
24 Correct 35 ms 35704 KB Output is correct
25 Correct 43 ms 35704 KB Output is correct
26 Correct 191 ms 48392 KB Output is correct
27 Correct 186 ms 48120 KB Output is correct
28 Correct 36 ms 36088 KB Output is correct
29 Correct 36 ms 35792 KB Output is correct
30 Correct 41 ms 35704 KB Output is correct
31 Correct 196 ms 48132 KB Output is correct
32 Correct 38 ms 36088 KB Output is correct
33 Correct 216 ms 52496 KB Output is correct
34 Correct 215 ms 48252 KB Output is correct
35 Correct 46 ms 36216 KB Output is correct
36 Correct 291 ms 48632 KB Output is correct
37 Correct 37 ms 36164 KB Output is correct
38 Correct 37 ms 36088 KB Output is correct
39 Correct 145 ms 48496 KB Output is correct
40 Correct 36 ms 36200 KB Output is correct
41 Correct 142 ms 48116 KB Output is correct
42 Correct 232 ms 49440 KB Output is correct
43 Correct 34 ms 35832 KB Output is correct
44 Correct 191 ms 52568 KB Output is correct
45 Correct 205 ms 50248 KB Output is correct
46 Correct 38 ms 36188 KB Output is correct
47 Correct 37 ms 36216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 228 ms 48368 KB Output is correct
2 Correct 167 ms 56148 KB Output is correct
3 Correct 44 ms 36184 KB Output is correct
4 Correct 37 ms 36216 KB Output is correct
5 Correct 37 ms 35832 KB Output is correct
6 Correct 38 ms 35804 KB Output is correct
7 Correct 46 ms 36088 KB Output is correct
8 Correct 249 ms 50940 KB Output is correct
9 Correct 37 ms 36216 KB Output is correct
10 Correct 265 ms 49272 KB Output is correct
11 Correct 34 ms 35832 KB Output is correct
12 Correct 265 ms 49272 KB Output is correct
13 Correct 258 ms 51036 KB Output is correct
14 Correct 199 ms 55544 KB Output is correct
15 Correct 156 ms 48368 KB Output is correct
16 Correct 37 ms 36216 KB Output is correct
17 Correct 34 ms 35708 KB Output is correct
18 Correct 179 ms 55096 KB Output is correct
19 Correct 228 ms 60920 KB Output is correct
20 Correct 38 ms 36216 KB Output is correct
21 Correct 33 ms 35704 KB Output is correct
22 Correct 184 ms 51564 KB Output is correct
23 Correct 38 ms 36216 KB Output is correct
24 Correct 266 ms 49912 KB Output is correct
25 Correct 200 ms 59128 KB Output is correct
26 Correct 38 ms 36344 KB Output is correct
27 Correct 37 ms 36344 KB Output is correct
28 Correct 38 ms 36100 KB Output is correct
29 Correct 36 ms 36088 KB Output is correct
30 Correct 37 ms 36216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 35752 KB Output is correct
2 Correct 35 ms 35804 KB Output is correct
3 Correct 38 ms 35796 KB Output is correct
4 Correct 35 ms 35680 KB Output is correct
5 Correct 38 ms 35656 KB Output is correct
6 Correct 41 ms 35700 KB Output is correct
7 Correct 34 ms 35704 KB Output is correct
8 Correct 35 ms 35696 KB Output is correct
9 Correct 34 ms 35732 KB Output is correct
10 Correct 34 ms 35740 KB Output is correct
11 Correct 34 ms 35708 KB Output is correct
12 Correct 34 ms 35788 KB Output is correct
13 Correct 34 ms 35708 KB Output is correct
14 Correct 36 ms 35692 KB Output is correct
15 Correct 34 ms 35704 KB Output is correct
16 Correct 34 ms 35704 KB Output is correct
17 Correct 34 ms 35704 KB Output is correct
18 Correct 35 ms 35704 KB Output is correct
19 Correct 34 ms 35708 KB Output is correct
20 Correct 34 ms 35704 KB Output is correct
21 Correct 34 ms 35676 KB Output is correct
22 Correct 35 ms 35736 KB Output is correct
23 Correct 35 ms 35704 KB Output is correct
24 Correct 35 ms 35704 KB Output is correct
25 Correct 34 ms 35704 KB Output is correct
26 Correct 37 ms 36188 KB Output is correct
27 Correct 37 ms 36088 KB Output is correct
28 Correct 37 ms 36344 KB Output is correct
29 Correct 38 ms 36324 KB Output is correct
30 Correct 44 ms 36092 KB Output is correct
31 Correct 34 ms 35704 KB Output is correct
32 Correct 37 ms 36344 KB Output is correct
33 Correct 34 ms 35832 KB Output is correct
34 Correct 36 ms 36200 KB Output is correct
35 Correct 37 ms 36344 KB Output is correct
36 Correct 37 ms 36088 KB Output is correct
37 Correct 37 ms 36216 KB Output is correct
38 Correct 34 ms 35832 KB Output is correct
39 Correct 39 ms 36236 KB Output is correct
40 Correct 36 ms 36088 KB Output is correct
41 Correct 38 ms 36216 KB Output is correct
42 Correct 37 ms 36216 KB Output is correct
43 Correct 36 ms 36216 KB Output is correct
44 Correct 34 ms 35704 KB Output is correct
45 Correct 45 ms 36216 KB Output is correct
46 Correct 46 ms 36244 KB Output is correct
47 Correct 34 ms 35704 KB Output is correct
48 Correct 38 ms 36344 KB Output is correct
49 Correct 37 ms 36344 KB Output is correct
50 Correct 37 ms 36472 KB Output is correct
51 Correct 37 ms 36216 KB Output is correct
52 Correct 37 ms 36088 KB Output is correct
53 Correct 37 ms 36216 KB Output is correct
54 Correct 37 ms 36088 KB Output is correct
55 Correct 43 ms 35704 KB Output is correct
56 Correct 191 ms 48392 KB Output is correct
57 Correct 186 ms 48120 KB Output is correct
58 Correct 36 ms 36088 KB Output is correct
59 Correct 36 ms 35792 KB Output is correct
60 Correct 41 ms 35704 KB Output is correct
61 Correct 196 ms 48132 KB Output is correct
62 Correct 38 ms 36088 KB Output is correct
63 Correct 216 ms 52496 KB Output is correct
64 Correct 215 ms 48252 KB Output is correct
65 Correct 46 ms 36216 KB Output is correct
66 Correct 291 ms 48632 KB Output is correct
67 Correct 37 ms 36164 KB Output is correct
68 Correct 37 ms 36088 KB Output is correct
69 Correct 145 ms 48496 KB Output is correct
70 Correct 36 ms 36200 KB Output is correct
71 Correct 142 ms 48116 KB Output is correct
72 Correct 232 ms 49440 KB Output is correct
73 Correct 34 ms 35832 KB Output is correct
74 Correct 191 ms 52568 KB Output is correct
75 Correct 205 ms 50248 KB Output is correct
76 Correct 38 ms 36188 KB Output is correct
77 Correct 37 ms 36216 KB Output is correct
78 Correct 228 ms 48368 KB Output is correct
79 Correct 167 ms 56148 KB Output is correct
80 Correct 44 ms 36184 KB Output is correct
81 Correct 37 ms 36216 KB Output is correct
82 Correct 37 ms 35832 KB Output is correct
83 Correct 38 ms 35804 KB Output is correct
84 Correct 46 ms 36088 KB Output is correct
85 Correct 249 ms 50940 KB Output is correct
86 Correct 37 ms 36216 KB Output is correct
87 Correct 265 ms 49272 KB Output is correct
88 Correct 34 ms 35832 KB Output is correct
89 Correct 265 ms 49272 KB Output is correct
90 Correct 258 ms 51036 KB Output is correct
91 Correct 199 ms 55544 KB Output is correct
92 Correct 156 ms 48368 KB Output is correct
93 Correct 37 ms 36216 KB Output is correct
94 Correct 34 ms 35708 KB Output is correct
95 Correct 179 ms 55096 KB Output is correct
96 Correct 228 ms 60920 KB Output is correct
97 Correct 38 ms 36216 KB Output is correct
98 Correct 33 ms 35704 KB Output is correct
99 Correct 184 ms 51564 KB Output is correct
100 Correct 38 ms 36216 KB Output is correct
101 Correct 266 ms 49912 KB Output is correct
102 Correct 200 ms 59128 KB Output is correct
103 Correct 38 ms 36344 KB Output is correct
104 Correct 37 ms 36344 KB Output is correct
105 Correct 38 ms 36100 KB Output is correct
106 Correct 36 ms 36088 KB Output is correct
107 Correct 37 ms 36216 KB Output is correct
108 Correct 1444 ms 120888 KB Output is correct
109 Correct 1533 ms 115120 KB Output is correct
110 Correct 1524 ms 121940 KB Output is correct
111 Correct 1333 ms 171248 KB Output is correct
112 Correct 1119 ms 145520 KB Output is correct
113 Correct 962 ms 142324 KB Output is correct
114 Correct 798 ms 104676 KB Output is correct
115 Correct 797 ms 104636 KB Output is correct
116 Correct 1961 ms 112728 KB Output is correct
117 Correct 1156 ms 144468 KB Output is correct
118 Correct 1949 ms 107408 KB Output is correct
119 Correct 1145 ms 144636 KB Output is correct
120 Correct 1270 ms 163660 KB Output is correct
121 Correct 1166 ms 144632 KB Output is correct
122 Correct 1570 ms 124412 KB Output is correct
123 Correct 838 ms 147556 KB Output is correct
124 Correct 1301 ms 136556 KB Output is correct