제출 #209056

#제출 시각아이디문제언어결과실행 시간메모리
209056bensonlzlMergers (JOI19_mergers)C++14
0 / 100
126 ms55676 KiB
#include <bits/stdc++.h> using namespace std; int N, K, X, Y, state[500005], par[500005], sz[500005], anc[500005]; int lca[500005][20], depth[500005]; vector<int> AdjList[500005], AdjList2[500005], st[500005]; void dfs_init(int x, int p){ anc[x] = p; lca[x][0] = p; for (int i = 1; i < 20; ++i){ lca[x][i] = lca[lca[x][i-1]][i-1]; } for (auto it : AdjList[x]){ if (it == p) continue; depth[it] = depth[x] + 1; dfs_init(it,x); } } int get_lca(int x, int y){ if (depth[x] > depth[y]) swap(x,y); int d = depth[y] - depth[x]; for (int i = 0; i < 20; ++i){ if (d & (1 << i)){ y = lca[y][i]; } } if (x == y) return x; for (int i = 19; i >= 0; --i){ if (lca[x][i] != lca[y][i]){ x = lca[x][i]; y = lca[y][i]; } } return lca[x][0]; } int find_par(int x){ if (x != par[x]) par[x] = find_par(par[x]); return par[x]; } void merg(int x, int y){ int X = find_par(x), Y = find_par(y); if (X == Y) return; if (sz[X] < sz[Y]) swap(X,Y); par[Y] = X; sz[X] += sz[Y]; if (depth[anc[Y]] < depth[anc[X]]){ anc[X] = anc[Y]; } } void combine(int x, int t){ int X = find_par(x), T = find_par(t); if (anc[X] == anc[T]) return; combine(anc[X],T); merg(X,anc[X]); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> K; for (int i = 1; i < N; ++i){ cin >> X >> Y; AdjList[X].push_back(Y); AdjList[Y].push_back(X); } for (int i = 1; i <= N; ++i){ par[i] = i; sz[i] = 1; cin >> state[i]; st[state[i]].push_back(i); } dfs_init(1,0); for (int i = 1; i <= K; ++i){ int L = st[i][0]; for (int j = 1; j < st[i].size(); ++j){ L = get_lca(L,st[i][j]); } //cout << i << ' ' << L << '\n'; for (auto it : st[i]){ combine(it,L); } } for (int i = 1; i <= N; ++i){ for (auto it : AdjList[i]){ if (find_par(i) != find_par(it)){ AdjList2[find_par(i)].push_back(find_par(it)); } } } int leaves = 0; for (int i = 1; i <= N; ++i){ if (AdjList2[i].size() == 1) leaves++; } cout << (leaves+1)/2 << '\n'; }

컴파일 시 표준 에러 (stderr) 메시지

mergers.cpp: In function 'int main()':
mergers.cpp:81:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 1; j < st[i].size(); ++j){
                   ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...