답안 #129773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129773 2019-07-13T08:02:47 Z dndhk Mergers (JOI19_mergers) C++14
100 / 100
1551 ms 134004 KB
#include <bits/stdc++.h>

#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL


using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_N = 500000;
const int MAX_T = 20;

int N, K;
vector<int> gp[MAX_N+1];
int p[MAX_N+1][20];
int par[MAX_N+1];
int in[MAX_N+1], out[MAX_N+1], cnt = 0;
int lv[MAX_N+1];

void dfs(int x){
	in[x] = ++cnt;
	par[x] = p[x][0];
	for(int j=1; j<MAX_T; j++){
		p[x][j] = p[p[x][j-1]][j-1];
	}
	for(int i=0; i<gp[x].size(); i++){
		if(gp[x][i]==p[x][0])	continue;
		p[gp[x][i]][0] = x;
		lv[gp[x][i]] = lv[x]+1;
		dfs(gp[x][i]);
	}
	out[x] = cnt;
}
vector<int> st[MAX_N+1];

bool sf(int x, int y){
	return in[x]<in[y];
}

int lca(int x, int y){
	for(int i=MAX_T-1; i>=0; i--){
		if(lv[p[x][i]]>=lv[y]){
			x = p[x][i];
		}if(lv[p[y][i]]>=lv[x]){
			y = p[y][i];
		}
	}
	for(int i=MAX_T-1; i>=0; i--){
		if(p[x][i]!=p[y][i]){
			x = p[x][i]; y = p[y][i];
		}
	}
	if(x!=y){
		x = p[x][0];
	}
	return x;
}

vector<pii> upd;

int g[MAX_N+1];

void init_g(int x){
	for(int i=1; i<=x; i++){
		g[i] = i;
	}
}

int find_g(int x){
	return (x==g[x]) ? x : g[x] = find_g(g[x]);
}

void union_g(int x, int y){
	x = find_g(x); y = find_g(y);
	g[x] = y;
}

void merge_g(int x, int y){
	//cout<<x<<" "<<y<<endl;
	if(lv[par[x]]<lv[y])	return;
	merge_g(par[x], y); par[x] = y;
	union_g(x, y);
}

vector<pii> edge;
int degree[MAX_N+1];

int main(){
	scanf("%d%d", &N, &K);
	init_g(N);
	for(int i=0; i<N-1; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		gp[a].pb(b); gp[b].pb(a);
		edge.pb({a, b});
	}
	lv[1] = 1;
	dfs(1);
	for(int i=1; i<=N; i++){
		int x;
		scanf("%d", &x);
		st[x].pb(i);
	}
	for(int i=1; i<=K; i++){
		sort(st[i].begin(), st[i].end(), sf);
		for(int j=1; j<st[i].size(); j++){
			int x = st[i][j-1]; int y = st[i][j];
			upd.pb({x, lca(x, y)}); upd.pb({y, lca(x, y)});
		}
	}
	while(!upd.empty()){
		int x = upd.back().first; int y = upd.back().second; upd.pop_back();
		merge_g(x, y);
	}
	for(int i=0; i<edge.size(); i++){
		if(find_g(edge[i].first) != find_g(edge[i].second)){
			degree[find_g(edge[i].first)]++; degree[find_g(edge[i].second)]++;
		}
	}
	int ans = 0;
	for(int i=1; i<=N; i++){
		if(degree[i]==1)	ans++;
	}
	printf("%d", (ans+1)/2);
	return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int)':
mergers.cpp:40:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:120:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=1; j<st[i].size(); j++){
                ~^~~~~~~~~~~~~
mergers.cpp:129:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<edge.size(); i++){
               ~^~~~~~~~~~~~
mergers.cpp:103: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:107: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:115:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &x);
   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23932 KB Output is correct
2 Correct 29 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23804 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 23 ms 23932 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 28 ms 23800 KB Output is correct
13 Correct 24 ms 23800 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 23 ms 23800 KB Output is correct
16 Correct 28 ms 23928 KB Output is correct
17 Correct 24 ms 23780 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 24 ms 23928 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23932 KB Output is correct
2 Correct 29 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23804 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 23 ms 23932 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 28 ms 23800 KB Output is correct
13 Correct 24 ms 23800 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 23 ms 23800 KB Output is correct
16 Correct 28 ms 23928 KB Output is correct
17 Correct 24 ms 23780 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 24 ms 23928 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23932 KB Output is correct
25 Correct 23 ms 23928 KB Output is correct
26 Correct 27 ms 24440 KB Output is correct
27 Correct 27 ms 24568 KB Output is correct
28 Correct 26 ms 24412 KB Output is correct
29 Correct 26 ms 24440 KB Output is correct
30 Correct 28 ms 24440 KB Output is correct
31 Correct 23 ms 23800 KB Output is correct
32 Correct 26 ms 24440 KB Output is correct
33 Correct 28 ms 23928 KB Output is correct
34 Correct 28 ms 24412 KB Output is correct
35 Correct 27 ms 24440 KB Output is correct
36 Correct 28 ms 24440 KB Output is correct
37 Correct 27 ms 24440 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 28 ms 24440 KB Output is correct
40 Correct 28 ms 24456 KB Output is correct
41 Correct 27 ms 24440 KB Output is correct
42 Correct 27 ms 24440 KB Output is correct
43 Correct 33 ms 24568 KB Output is correct
44 Correct 28 ms 23928 KB Output is correct
45 Correct 26 ms 24412 KB Output is correct
46 Correct 27 ms 24440 KB Output is correct
47 Correct 23 ms 23800 KB Output is correct
48 Correct 28 ms 24440 KB Output is correct
49 Correct 26 ms 24440 KB Output is correct
50 Correct 27 ms 24440 KB Output is correct
51 Correct 28 ms 24568 KB Output is correct
52 Correct 33 ms 24568 KB Output is correct
53 Correct 32 ms 24440 KB Output is correct
54 Correct 28 ms 24440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23932 KB Output is correct
2 Correct 29 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23804 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 23 ms 23932 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 28 ms 23800 KB Output is correct
13 Correct 24 ms 23800 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 23 ms 23800 KB Output is correct
16 Correct 28 ms 23928 KB Output is correct
17 Correct 24 ms 23780 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 24 ms 23928 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23932 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Correct 176 ms 42116 KB Output is correct
27 Correct 220 ms 41784 KB Output is correct
28 Correct 27 ms 24444 KB Output is correct
29 Correct 24 ms 23928 KB Output is correct
30 Correct 29 ms 23928 KB Output is correct
31 Correct 211 ms 41940 KB Output is correct
32 Correct 27 ms 24440 KB Output is correct
33 Correct 256 ms 46044 KB Output is correct
34 Correct 230 ms 41912 KB Output is correct
35 Correct 27 ms 24440 KB Output is correct
36 Correct 216 ms 42072 KB Output is correct
37 Correct 29 ms 24564 KB Output is correct
38 Correct 28 ms 24440 KB Output is correct
39 Correct 182 ms 42044 KB Output is correct
40 Correct 28 ms 24612 KB Output is correct
41 Correct 216 ms 42536 KB Output is correct
42 Correct 244 ms 43152 KB Output is correct
43 Correct 24 ms 23800 KB Output is correct
44 Correct 236 ms 46924 KB Output is correct
45 Correct 259 ms 44016 KB Output is correct
46 Correct 28 ms 24444 KB Output is correct
47 Correct 27 ms 24436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 40676 KB Output is correct
2 Correct 126 ms 41444 KB Output is correct
3 Correct 32 ms 24312 KB Output is correct
4 Correct 28 ms 24440 KB Output is correct
5 Correct 24 ms 23928 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 27 ms 24440 KB Output is correct
8 Correct 194 ms 42600 KB Output is correct
9 Correct 28 ms 24440 KB Output is correct
10 Correct 204 ms 41936 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 213 ms 41808 KB Output is correct
13 Correct 199 ms 42504 KB Output is correct
14 Correct 159 ms 42960 KB Output is correct
15 Correct 190 ms 41972 KB Output is correct
16 Correct 26 ms 24440 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 154 ms 43372 KB Output is correct
19 Correct 170 ms 45164 KB Output is correct
20 Correct 27 ms 24440 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 162 ms 42720 KB Output is correct
23 Correct 28 ms 24440 KB Output is correct
24 Correct 265 ms 42348 KB Output is correct
25 Correct 168 ms 44368 KB Output is correct
26 Correct 32 ms 24440 KB Output is correct
27 Correct 37 ms 24440 KB Output is correct
28 Correct 33 ms 24440 KB Output is correct
29 Correct 27 ms 24440 KB Output is correct
30 Correct 32 ms 24440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23932 KB Output is correct
2 Correct 29 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23804 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 23 ms 23932 KB Output is correct
8 Correct 24 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23928 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 28 ms 23800 KB Output is correct
13 Correct 24 ms 23800 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 23 ms 23800 KB Output is correct
16 Correct 28 ms 23928 KB Output is correct
17 Correct 24 ms 23780 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 24 ms 23928 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23932 KB Output is correct
25 Correct 23 ms 23928 KB Output is correct
26 Correct 27 ms 24440 KB Output is correct
27 Correct 27 ms 24568 KB Output is correct
28 Correct 26 ms 24412 KB Output is correct
29 Correct 26 ms 24440 KB Output is correct
30 Correct 28 ms 24440 KB Output is correct
31 Correct 23 ms 23800 KB Output is correct
32 Correct 26 ms 24440 KB Output is correct
33 Correct 28 ms 23928 KB Output is correct
34 Correct 28 ms 24412 KB Output is correct
35 Correct 27 ms 24440 KB Output is correct
36 Correct 28 ms 24440 KB Output is correct
37 Correct 27 ms 24440 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 28 ms 24440 KB Output is correct
40 Correct 28 ms 24456 KB Output is correct
41 Correct 27 ms 24440 KB Output is correct
42 Correct 27 ms 24440 KB Output is correct
43 Correct 33 ms 24568 KB Output is correct
44 Correct 28 ms 23928 KB Output is correct
45 Correct 26 ms 24412 KB Output is correct
46 Correct 27 ms 24440 KB Output is correct
47 Correct 23 ms 23800 KB Output is correct
48 Correct 28 ms 24440 KB Output is correct
49 Correct 26 ms 24440 KB Output is correct
50 Correct 27 ms 24440 KB Output is correct
51 Correct 28 ms 24568 KB Output is correct
52 Correct 33 ms 24568 KB Output is correct
53 Correct 32 ms 24440 KB Output is correct
54 Correct 28 ms 24440 KB Output is correct
55 Correct 24 ms 23928 KB Output is correct
56 Correct 176 ms 42116 KB Output is correct
57 Correct 220 ms 41784 KB Output is correct
58 Correct 27 ms 24444 KB Output is correct
59 Correct 24 ms 23928 KB Output is correct
60 Correct 29 ms 23928 KB Output is correct
61 Correct 211 ms 41940 KB Output is correct
62 Correct 27 ms 24440 KB Output is correct
63 Correct 256 ms 46044 KB Output is correct
64 Correct 230 ms 41912 KB Output is correct
65 Correct 27 ms 24440 KB Output is correct
66 Correct 216 ms 42072 KB Output is correct
67 Correct 29 ms 24564 KB Output is correct
68 Correct 28 ms 24440 KB Output is correct
69 Correct 182 ms 42044 KB Output is correct
70 Correct 28 ms 24612 KB Output is correct
71 Correct 216 ms 42536 KB Output is correct
72 Correct 244 ms 43152 KB Output is correct
73 Correct 24 ms 23800 KB Output is correct
74 Correct 236 ms 46924 KB Output is correct
75 Correct 259 ms 44016 KB Output is correct
76 Correct 28 ms 24444 KB Output is correct
77 Correct 27 ms 24436 KB Output is correct
78 Correct 177 ms 40676 KB Output is correct
79 Correct 126 ms 41444 KB Output is correct
80 Correct 32 ms 24312 KB Output is correct
81 Correct 28 ms 24440 KB Output is correct
82 Correct 24 ms 23928 KB Output is correct
83 Correct 23 ms 23800 KB Output is correct
84 Correct 27 ms 24440 KB Output is correct
85 Correct 194 ms 42600 KB Output is correct
86 Correct 28 ms 24440 KB Output is correct
87 Correct 204 ms 41936 KB Output is correct
88 Correct 24 ms 23928 KB Output is correct
89 Correct 213 ms 41808 KB Output is correct
90 Correct 199 ms 42504 KB Output is correct
91 Correct 159 ms 42960 KB Output is correct
92 Correct 190 ms 41972 KB Output is correct
93 Correct 26 ms 24440 KB Output is correct
94 Correct 24 ms 23928 KB Output is correct
95 Correct 154 ms 43372 KB Output is correct
96 Correct 170 ms 45164 KB Output is correct
97 Correct 27 ms 24440 KB Output is correct
98 Correct 24 ms 23800 KB Output is correct
99 Correct 162 ms 42720 KB Output is correct
100 Correct 28 ms 24440 KB Output is correct
101 Correct 265 ms 42348 KB Output is correct
102 Correct 168 ms 44368 KB Output is correct
103 Correct 32 ms 24440 KB Output is correct
104 Correct 37 ms 24440 KB Output is correct
105 Correct 33 ms 24440 KB Output is correct
106 Correct 27 ms 24440 KB Output is correct
107 Correct 32 ms 24440 KB Output is correct
108 Correct 956 ms 119212 KB Output is correct
109 Correct 1551 ms 126136 KB Output is correct
110 Correct 1475 ms 131164 KB Output is correct
111 Correct 1090 ms 134004 KB Output is correct
112 Correct 1101 ms 128400 KB Output is correct
113 Correct 907 ms 122108 KB Output is correct
114 Correct 1211 ms 115128 KB Output is correct
115 Correct 1207 ms 114780 KB Output is correct
116 Correct 1383 ms 115708 KB Output is correct
117 Correct 924 ms 120432 KB Output is correct
118 Correct 1152 ms 112216 KB Output is correct
119 Correct 920 ms 120544 KB Output is correct
120 Correct 1095 ms 128536 KB Output is correct
121 Correct 925 ms 120540 KB Output is correct
122 Correct 1191 ms 118380 KB Output is correct
123 Correct 755 ms 122040 KB Output is correct
124 Correct 948 ms 118364 KB Output is correct