QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#302919 | #6307. Chase Game 2 | ZYF_B# | WA | 2ms | 3808kb | C++14 | 1.5kb | 2024-01-11 15:20:23 | 2024-01-11 15:20:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
const int MAXN=1e5+10;
int n,cnt,rt;
int v[MAXN*2],first[MAXN],net[MAXN*2];
int num[MAXN],f[MAXN],h[MAXN];
void add(int x,int y)
{
++cnt;
v[cnt]=y;
net[cnt]=first[x];
first[x]=cnt;
num[y]++;
}
void dfs(int x,int y)
{
for(int i=first[x];i;i=net[i])
{
if(v[i]==y)continue;
dfs(v[i],x);
}
int tot=0,p=1;
for(int i=first[x];i;i=net[i])
{
if(v[i]==y)continue;
if(num[v[i]]==1)
{
if(h[x]>0)h[x]--,f[x]++;
else tot++;
p=0;
continue;
}
if(h[v[i]]>0)p=0;
f[x]+=f[v[i]];
if(h[v[i]]<tot)f[x]+=h[v[i]],tot-=h[v[i]];
else f[x]+=tot,h[v[i]]-=tot;
int t=0;
t=min(h[x],h[v[i]]);
f[x]+=t;
h[x]=abs(h[x]-h[v[i]]);
}
h[x]+=tot;
if(p)h[x]=1;
return ;
}
bool tp()
{
for(int i=1;i<=n;i++)if(num[i]==(n-1))return 1;
return 0;
}
int main()
{
int T=read();
while(T--)
{
n=read();
for(int i=1;i<n;i++)
{
int x,y;
x=read();y=read();
add(x,y);
add(y,x);
}
if(tp())puts("-1");
else
{
dfs(1,0);printf("%d\n",f[1]+h[1]);
}
for(int i=1;i<=n;i++)f[i]=h[i]=num[i]=first[i]=0;
cnt=0;
}
return 0;
}
/*
4
6
1 2
2 3
3 4
4 5
4 6
6
1 2
1 3
1 4
1 5
5 6
7
1 2
2 3
2 4
1 5
5 6
6 7
5
1 2
2 3
3 4
3 5
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3808kb
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
output:
-1 1 -1 2
result:
ok 4 number(s): "-1 1 -1 2"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3744kb
input:
10000 4 1 2 1 3 3 4 4 1 2 1 3 1 4 4 1 2 2 3 1 4 5 1 2 2 3 1 4 4 5 5 1 2 2 3 3 4 4 5 4 1 2 2 3 2 4 5 1 2 1 3 2 4 2 5 4 1 2 2 3 1 4 5 1 2 1 3 2 4 1 5 5 1 2 2 3 3 4 2 5 5 1 2 1 3 2 4 2 5 4 1 2 1 3 3 4 5 1 2 1 3 3 4 1 5 4 1 2 1 3 1 4 5 1 2 1 3 3 4 3 5 5 1 2 2 3 3 4 3 5 4 1 2 1 3 2 4 5 1 2 2 3 2 4 3 5 5 ...
output:
1 -1 2 1 1 -1 3 2 3 2 3 1 3 -1 2 2 2 2 3 1 1 2 -1 3 2 2 1 -1 2 2 3 1 2 -1 1 2 2 1 2 -1 2 2 2 3 3 2 1 1 -1 2 2 1 1 2 1 3 1 1 3 -1 -1 -1 2 3 3 1 2 2 3 3 3 -1 2 3 -1 2 1 -1 2 -1 -1 1 3 3 2 2 2 1 1 1 1 1 2 2 3 -1 1 2 2 -1 3 1 2 1 -1 3 -1 2 -1 -1 3 -1 3 1 2 3 1 1 2 1 2 1 1 1 1 -1 2 1 3 1 1 1 2 2 2 2 3 -1...
result:
wrong answer 3rd numbers differ - expected: '1', found: '2'