QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#110385 | #6307. Chase Game 2 | Tx_Lcy | RE | 0ms | 0kb | C++14 | 1.8kb | 2023-06-01 20:12:38 | 2023-06-01 20:12:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int const N=1e5+10;
vector<int>a[N],s[N];
int ans,laf[N],g[N],dep[N];
void dfs(int x,int fa){
int flg=0;
for (auto v:a[x]){
if (v==fa) continue;
flg=1,dep[v]=dep[x]+1,dfs(v,x);
}
for (auto v:a[x]) if (laf[v]) ++g[x],s[x].push_back(v);
if (!flg) laf[x]=1;
}
signed main(){
freopen("hunter.in","r",stdin);
freopen("hunter.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;cin>>t;
while (t--){
int n;cin>>n;
for (int i=1;i<=n;++i) a[i].clear(),laf[i]=g[i]=0,s[i].clear();
ans=0;
for (int i=1;i<n;++i){
int u,v;cin>>u>>v;
a[u].push_back(v);
a[v].push_back(u);
}
int x=1;
for (int i=1;i<=n;++i)
if (a[i].size()>1){x=i;break;}
dep[x]=0,dfs(x,0);
int j=0;
for (int i=1;i<=n;++i) if (g[i]>g[j]) j=i;
sort(g+1,g+n+1);
if (!g[n-1]){
int flg=0;
for (auto v:s[j])
if (dep[v]>2){
cout<<s[j].size()<<'\n';
flg=1;break;
}
if (!flg) cout<<"-1\n";
continue;
}
int mx=0,sm=0;
for (int i=1;i<=n;++i) mx=max(mx,g[i]),sm+=g[i];
if (2*mx>=sm){
int r=sm-g[n];
g[n]-=r,sm-=r;
cout<<sm<<'\n';
}else{
cout<<(sm+1)/2<<'\n';
}
/*int m=0;
for (int i=1;i<=n;++i) m+=g[i];
int l=1;
for (int i=n;i>=1;--i){
while (l+1<=i && !g[l]) ++l;
if (l==i) break;
int t=min(g[i],g[l]);
g[i]-=t,g[l]-=t,m-=t;
if (g[i]) ++i;
}
cout<<m<<'\n';*/
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
4 2 1 2 4 1 2 2 3 3 4 4 1 2 2 3 2 4 5 1 2 2 3 3 4 3 5