QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#812976 | #72. Mergers | guleng2007# | 0 | 45ms | 30940kb | C++20 | 1.5kb | 2024-12-13 20:18:42 | 2024-12-13 20:18:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int fa[N][20], dep[N], cnt[N], sum[N];
vector <int> g[N], vec[N];
void dfs1(int x)
{
for(int i=1;i<20;i++)
fa[x][i]=fa[fa[x][i-1]][i-1];
for(auto to:g[x])
if(to!=fa[x][0])
{
dep[to]=dep[x]+1;
fa[to][0]=x;
dfs1(to);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
swap(x,y);
for(int i=19;i>=0;i--)
if(dep[fa[x][i]]>=dep[y])
x=fa[x][i];
if(x==y)
return x;
for(int i=19;i>=0;i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i], y=fa[y][i];
return fa[x][0];
}
int dfsn[N], sz[N], indx;
void dfs2(int x)
{
dfsn[x]=++indx, sz[x]=1;
for(auto to:g[x])
if(to!=fa[x][0])
dfs2(to), cnt[x] += cnt[to], sz[x] += sz[to];
}
int main()
{
int n,k;
cin >> n >> k;
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d %d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dep[1]=1, dfs1(1);
for(int i=1;i<=n;i++)
{
int typ;
scanf("%d",&typ);
vec[typ].push_back(i);
}
for(int i=1;i<=k;i++)
{
if(vec[i].size()==0)
continue;
for(int j=0;j<vec[i].size()-1;j++)
cnt[vec[i][j]]++, cnt[vec[i][j+1]]++, cnt[lca(vec[i][j],vec[i][j+1])] -= 2;
}
dfs2(1);
for(int i=2;i<=n;i++)
if(cnt[i]==0)
sum[dfsn[i]]++;
for(int i=1;i<=n;i++)
sum[i] += sum[i-1];
int ans=0;
for(int i=1;i<=n;i++)
if(cnt[i]==0)
{
if(sum[dfsn[i]+sz[i]-1]-sum[dfsn[i]-1]==0 || sum[dfsn[i]+sz[i]-1]-sum[dfsn[i]-1]==sum[n])
ans++;
}
printf("%d\n",(ans+1)/2);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 7972kb
input:
1 1 1
output:
1
result:
wrong answer 1st lines differ - expected: '0', found: '1'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #66:
score: 0
Wrong Answer
time: 45ms
memory: 30940kb
input:
100000 100000 14957 4585 67467 70858 61843 47396 50630 17382 61027 39858 94990 21698 10240 22940 23505 67581 91432 14182 22040 40125 24556 60351 75822 41519 82801 23601 90653 29138 85096 34582 99587 59109 8932 45189 18235 36632 43160 14939 67600 76675 60175 65542 99294 53955 46429 66931 16822 48854 ...
output:
1
result:
wrong answer 1st lines differ - expected: '25042', found: '1'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%