QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#292148 | #6307. Chase Game 2 | luhanning | RE | 7ms | 6072kb | C++20 | 1.1kb | 2023-12-27 19:44:04 | 2023-12-27 19:44:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int e[N],ne[N],idx,h[N];
int d[N];
vector<int> ans;
void add(int a,int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >>T;
while(T --)
{
int n;
cin >>n;
ans.clear();
for(int i = 1;i <=n;i ++)
{
d[i] = 0;
h[i] = -1;
}
for(int i = 1;i < n;i ++)
{
int a,b;
cin >>a>>b;
add(a,b);
add(b,a);
d[a] ++;
d[b] ++;
}
int cnt = 0;
int max1 = 0;
// for(int i = 1;i <= n;i ++)
// {
// cout<<d[i]<<' ';
// }
// cout<<'\n';
for(int i = 1;i <= n;i ++)
{
if(d[i] == 1)
{
cnt ++;
continue;
}
int cnt1 = 0;
for(int k = h[i];k != -1;k = ne[k])
{
int j = e[k];
if(d[j] == 1)cnt1 ++;
}
if(cnt1 > 0)ans.push_back(cnt1),max1 = max(max1,cnt1);
}
if(ans.size() <= 1)cout<<"-1"<<'\n';
else
{
if(max1 >= ((cnt + 1) / 2))cout<<max1<<'\n';
else cout<<(cnt + 1) / 2<<'\n';
}
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
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: 0
Accepted
time: 4ms
memory: 5564kb
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 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 4 3 3 3 2 3 2 2 3 3 3 3 2 3 3 2 3 3 3 3 3 2 3 2 2 3 5 3 3 2 2 3 2 3 4 2 5 3 2 3 3 2 3 2 3 3 3 3 2 2 3 3 3 2 3 3 3 3 3 3 2 3 3 2 2 4 3 3 3 3 2 3 ...
result:
ok 10000 numbers
Test #4:
score: 0
Accepted
time: 3ms
memory: 5988kb
input:
10000 9 1 2 2 3 1 4 2 5 3 6 5 7 2 8 2 9 9 1 2 2 3 1 4 2 5 5 6 5 7 1 8 7 9 9 1 2 1 3 1 4 1 5 4 6 5 7 1 8 6 9 9 1 2 1 3 3 4 2 5 2 6 6 7 6 8 6 9 10 1 2 1 3 2 4 1 5 2 6 5 7 4 8 1 9 9 10 10 1 2 1 3 3 4 4 5 5 6 2 7 7 8 7 9 1 10 10 1 2 1 3 3 4 3 5 1 6 2 7 3 8 3 9 6 10 10 1 2 2 3 1 4 1 5 3 6 2 7 6 8 4 9 5 1...
output:
3 3 3 3 3 2 4 2 2 3 3 3 3 3 3 3 3 3 2 3 3 3 2 2 2 3 3 2 3 2 3 3 3 2 3 2 3 3 3 3 4 2 2 3 2 2 3 4 3 4 3 3 3 3 3 3 3 2 3 3 2 2 3 4 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 2 3 2 2 3 3 2 3 3 2 2 3 2 3 2 3 3 2 2 3 3 2 3 3 3 2 3 3 2 2 3 3 3 2 3 3 5 4 3 2 3 2 3 3 3 3 2 2 3 3 3 3 3 3 2 3 2 3 3 3 4 3 2 3 2 3 3 3 2 ...
result:
ok 10000 numbers
Test #5:
score: -100
Runtime Error
input:
10000 20 1 2 1 3 1 4 2 5 4 6 6 7 2 8 4 9 7 10 6 11 4 12 6 13 13 14 10 15 8 16 11 17 5 18 15 19 10 20 19 1 2 1 3 2 4 3 5 4 6 1 7 1 8 2 9 4 10 10 11 8 12 2 13 4 14 1 15 4 16 4 17 14 18 3 19 20 1 2 1 3 1 4 1 5 1 6 5 7 3 8 5 9 1 10 8 11 8 12 2 13 7 14 4 15 2 16 12 17 14 18 18 19 1 20 19 1 2 1 3 3 4 2 5 ...
output:
5 6 5 5 5 4 5 6 3 6 5 4 5 6 5 6 5 5 5 5 5 4 5 5 5 6 6 5 6 4 5 5 6 4 5 5 4 5 3 6 5 5 6 5 5 4 5 3 6 6 5 5 6 4 6 5 5 5 6 5 5 5 4 6 4 5 5 5 4 5 5 5 6 7 5 5 6 6 4 6 5 5 5 5 6 6 5 6 6 6 4 5 6 4 5 4 4 5 5 6 5 5 5 7 5 5 5 5 4 4 6 4 6 4 5 4 4 6 4 5 5 5 4 5 5 5 6 5 5 6 5 5 3 4 6 4 4 5 5 5 4 4 6 5 5 5 5 6 5 6 ...