QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#762654 | #9751. 覆盖一棵树 | syhhh1 | WA | 52ms | 18448kb | C++17 | 1.5kb | 2024-11-19 16:02:09 | 2024-11-19 16:02:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int dep[N];
vector<int>f[N];
bool st[N];
int n;
set<int>cnt[N];
int vis[N];
vector<int>g;
void dfs(int u,int fa)
{
dep[u]=dep[fa]+1;
for(auto v:f[u])
{
if(v==fa)continue;
dfs(v,u);
}
}
void dfs2(int u,int fa,int x,int y)
{
if(dep[y]-dep[u]<=x)
{
cnt[u].insert(fa);
st[u]=true;
for(auto v:f[u]){
if(v==fa)continue;
dfs2(v,u,x,y);
}
}
}
bool check(int x)
{
for(int i=1;i<=n;i++){
st[i]=false;
cnt[i].clear();
}
int sz=g.size();
for(int i=0;i<sz;i++)
{
dfs2(g[i],g[i],x,g[i]);
}
bool fg=true;
for(int i=1;i<=n;i++){
if(vis[i])continue;
int c=f[i].size();
if(i==1&&cnt[1].size()<c)fg=false;
else if(cnt[i].size()<c-1)fg=false;
}
// if(x==2)cout<<cnt[1]<<" "<<f[1].size()<<endl;
// if(x==2)cout<<fg<<endl;
if(fg)return true;
return false;
}
signed main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
g.clear();
for(int i=1;i<=n;i++)
{
f[i].clear();
vis[i]=false;
dep[i]=0;
}
for(int i=2;i<=n;i++)
{
int x;
cin>>x;
f[i].push_back(x);
f[x].push_back(i);
}
for(int i=2;i<=n;i++)if(f[i].size()==1){
vis[i]=true;
g.push_back(i);
}
dfs(1,0);
// int mx=0;
int l=1,r=n-1;
while(l<r)
{
int mid=(l+r)/2;
if(check(mid))r=mid;
else l=mid+1;
}
cout<<l<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18448kb
input:
2 8 1 2 3 2 5 1 7 8 1 2 3 4 5 6 7
output:
3 7
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 52ms
memory: 18168kb
input:
33428 10 1 2 3 3 4 6 7 7 9 10 1 2 3 4 5 6 7 8 8 8 1 2 3 4 5 6 7 8 1 2 3 4 4 6 7 4 1 2 3 3 1 2 3 1 1 9 1 2 3 4 5 6 7 8 2 1 3 1 2 10 1 2 3 4 5 6 7 8 9 3 1 2 2 1 10 1 2 3 4 5 6 7 8 9 2 1 5 1 2 2 4 8 1 2 3 4 5 6 7 5 1 2 3 3 2 1 5 1 2 3 4 3 1 2 9 1 2 3 4 5 6 6 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 5 7 8 8 1 2 ...
output:
4 8 7 4 3 2 1 8 1 2 9 2 1 9 1 2 7 3 1 4 2 6 8 5 6 4 1 2 9 7 4 3 4 5 7 3 3 7 3 4 9 3 3 3 8 2 8 6 2 4 6 4 2 1 5 2 5 2 2 4 2 2 5 2 2 6 3 9 2 5 5 3 2 2 1 2 1 9 1 5 1 6 3 5 2 9 4 2 3 1 3 3 1 2 5 3 8 4 7 6 4 6 2 6 8 4 4 2 4 5 1 7 7 5 2 2 2 2 7 3 3 6 4 3 3 3 2 2 4 8 4 5 8 6 4 2 3 7 6 1 3 2 7 7 9 1 2 5 5 2 ...
result:
wrong answer 103rd lines differ - expected: '4', found: '2'