QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#626924 | #6307. Chase Game 2 | TokaiZaopen# | WA | 6ms | 3844kb | C++23 | 1.7kb | 2024-10-10 14:01:10 | 2024-10-10 14:01:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
// #define int long long
int solve()
{
int n;
cin >> n;
vector<int> son(n + 1);
vector<vector<int>> g(n + 1);
vector<int> cnt(n + 1);
for (int i = 0; i < n - 1; i++)
{
int x, y;
cin >> x >> y;
cnt[x]++;
cnt[y]++;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
int fa = 0;
for (int i = 1; i <= n; i++)
{
if (cnt[i] != 1)
{
fa = i;
break;
}
}
if (fa == 0)
{
cout << -1 << "\n";
return 0;
}
auto dfs = [&](int x, int fa, auto self) -> void {
for (auto it : g[x])
{
if (it == fa)
{
continue;
}
self(it, x, self);
if (son[it] == 0)
{
son[x]++;
}
}
};
dfs(fa, 0, dfs);
vector<int> nums;
for (auto it : son)
{
if (it > 0)
{
nums.emplace_back(it);
}
}
sort(all(nums), greater<int>());
if (nums.size() == 1)
{
cout << -1 << "\n";
return 0;
}
if (nums[0] >= accumulate(nums.begin() + 1, nums.end(), 0))
{
cout << nums[0] << "\n";
}
else
{
cout << (accumulate(all(nums), 0) + 1) / 2 << "\n";
}
return 0;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
cin >> test;
while (test--)
{
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3844kb
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: 6ms
memory: 3564kb
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 1 1 2 -1 2 1 2 2 2 1 2 -1 2 2 1 2 2 1 1 1 -1 2 2 2 1 -1 1 2 2 2 1 -1 1 2 1 1 1 -1 1 1 2 2 2 2 1 1 -1 1 2 1 1 2 1 2 1 1 2 -1 -1 -1 2 2 2 1 2 1 2 2 2 -1 1 2 -1 2 1 -1 2 -1 -1 2 2 2 2 2 1 1 1 1 1 1 1 1 2 -1 2 1 2 -1 2 1 1 2 -1 2 -1 1 -1 -1 2 -1 2 1 2 2 1 1 1 1 2 1 1 1 2 -1 2 1 2 1 1 2 2 2 1 1 2 -1...
result:
wrong answer 5th numbers differ - expected: '1', found: '2'