# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
534327 |
2022-03-08T04:51:25 Z |
79brue |
Mergers (JOI19_mergers) |
C++14 |
|
121 ms |
41172 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct UnionFind{
int par[500002];
void init(int n){
for(int i=1; i<=n; i++) par[i] = i;
}
int find(int x){
return x==par[x] ? x : (par[x] = find(par[x]));
}
void merge(int x, int y){
x = find(x), y = find(y);
par[x] = y;
}
} dsu;
int n, k;
int arr[500002];
vector<int> link[500002];
int deg[500002], leafCnt;
int in[500002], inCnt;
int par[500002], depth[500002];
int LCADB[500002][20];
vector<int> vec[500002];
int sum[500002];
void dfs(int x, int p=-1){
in[x] = ++inCnt;
vec[arr[x]].push_back(x);
for(auto y: link[x]){
if(y == p) continue;
par[y] = LCADB[y][0] = x;
depth[y] = depth[x]+1;
dfs(y, x);
}
}
void dfs2(int x, int p=-1){
for(auto y: link[x]){
if(y == p) continue;
dfs2(y, x);
sum[x] += sum[y];
}
if(sum[x]){
dsu.merge(x, p);
}
}
int getLCA(int x, int y){
if(depth[x] < depth[y]) swap(x, y);
for(int d=0; d<20; d++) if((depth[x]-depth[y])&(1<<d)) x=LCADB[x][d];
if(x==y) return x;
for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
return par[x];
}
int main(){
scanf("%d %d", &n, &k);
dsu.init(n);
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y);
link[y].push_back(x);
}
for(int i=1; i<=n; i++) scanf("%d", &arr[i]);
dfs(1);
for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];
for(int i=1; i<=k; i++){
sort(vec[i].begin(), vec[i].end(), [&](int x, int y){
return in[x] < in[y];
});
if(!vec[i].empty()) vec[i].push_back(vec[i][0]);
for(int j=0; j<(int)vec[i].size()-1; j++){
int x = vec[i][j], y = vec[i][j+1], z = getLCA(x, y);
sum[in[x]]++, sum[in[y]]++, sum[in[z]]-=2;
}
}
dfs2(1);
for(int i=1; i<=n; i++){
for(auto j: link[i]){
if(dsu.find(i) != dsu.find(j)) deg[dsu.find(i)]++;
}
}
for(int i=1; i<=n; i++) if(deg[i] == 1) leafCnt++;
printf("%d\n", (leafCnt + 1) / 2);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:75:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | for(int i=1; i<=n; i++) scanf("%d", &arr[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Incorrect |
12 ms |
23804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Incorrect |
12 ms |
23804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Incorrect |
12 ms |
23804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
38232 KB |
Output is correct |
2 |
Correct |
121 ms |
41172 KB |
Output is correct |
3 |
Incorrect |
16 ms |
24396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Incorrect |
12 ms |
23804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |