QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#220753 | #6307. Chase Game 2 | smallzhuzi# | WA | 9ms | 7732kb | C++14 | 1.8kb | 2023-10-20 19:39:17 | 2023-10-20 19:39:18 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int maxn = 1e5 + 5;
const int maxm = 2e5 + 5;
int n;
int head[maxn], to[maxm], ne[maxm], idx;
void add(int x, int y) {
ne[++idx] = head[x], head[x] = idx, to[idx] = y;
ne[++idx] = head[y], head[y] = idx, to[idx] = x;
}
int in[maxn];
int dep[maxn];
void dfs(int fa, int i) {
dep[i] = dep[fa] + 1;
for (int e = head[i], id = to[e]; e; e = ne[e], id = to[e]) {
if (id != fa)dfs(i, id);
}
}
void solve() {
cin >> n;
idx = 0;
for (int i = 1; i <= n; i++)head[i] = in[i] = 0;
for (int i = 1, x, y; i < n; i++) {
cin >> x >> y;
add(x, y);
in[x]++, in[y]++;
}
int rt;
for (int i = 1; i <= n; i++) {
if (in[i] == 1) {
rt = i;
break;
}
}
dep[0] = -1;
dfs(0, rt);
vector<int> v;
for (int i = 1; i <= n; i++) {
if (in[i] == 1) {
v.push_back(i);
}
}
sort(v.begin(), v.end(), [](int x, int y) {
return dep[x] < dep[y];
});
int cur = v.size() - 1, ans = 0;
// cout<<rt<<endl;
// cout<<v[0]<<" "<<v[cur]<<endl;
// for(int i=1;i<=n;i++)cout<<dep[i]<<" ";
// cout<<endl;
// for(auto x:v)cout<<x<<","<<dep[x]<<endl;
// cout<<endl;
if (dep[v[0]] + dep[v[cur]] < 3) {
cout << -1 << endl;
return;
}
for (int i = 0, ed = cur; i <= cur; i++) {
if (dep[v[i]] + dep[v[cur]] >= 3)ans++, cur--;
else ans++;
}
cout << ans << endl;
}
signed main() {
// freopen("../input.txt", "r", stdin);
cin.tie(nullptr), cout.tie(nullptr);
ios::sync_with_stdio(false);
int test = 1;
cin >> test;
while (test--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7680kb
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: 2ms
memory: 7732kb
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 1 -1 2 1 2 2 2 1 2 -1 2 2 1 2 2 1 1 1 -1 2 2 2 1 -1 1 1 2 1 1 -1 1 2 1 1 1 -1 1 1 2 2 2 1 1 1 -1 1 2 1 1 2 1 2 1 1 2 -1 -1 -1 2 2 2 1 1 1 2 2 2 -1 1 2 -1 1 1 -1 2 -1 -1 1 2 2 2 1 1 1 1 1 1 1 1 1 2 -1 1 1 2 -1 2 1 1 1 -1 2 -1 1 -1 -1 2 -1 2 1 2 2 1 1 1 1 2 1 1 1 1 -1 2 1 2 1 1 1 1 1 1 1 2 -1...
result:
ok 10000 numbers
Test #3:
score: -100
Wrong Answer
time: 9ms
memory: 7728kb
input:
10000 9 1 2 2 3 3 4 4 5 1 6 6 7 5 8 7 9 9 1 2 2 3 2 4 1 5 2 6 4 7 6 8 1 9 9 1 2 2 3 1 4 4 5 5 6 4 7 2 8 1 9 10 1 2 2 3 1 4 3 5 3 6 2 7 6 8 6 9 6 10 10 1 2 1 3 3 4 2 5 1 6 5 7 4 8 2 9 7 10 10 1 2 2 3 2 4 1 5 3 6 6 7 5 8 4 9 9 10 9 1 2 2 3 2 4 1 5 3 6 2 7 1 8 2 9 9 1 2 1 3 2 4 1 5 3 6 3 7 7 8 8 9 10 1...
output:
1 3 3 3 2 2 3 2 3 2 3 2 3 2 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 3 2 3 3 3 3 3 3 3 2 3 3 3 3 3 2 3 3 3 3 3 3 2 3 2 3 2 2 3 3 4 3 3 3 3 2 2 3 2 2 2 3 3 2 3 3 2 3 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 3 3 3 2 2 3 2 3 4 2 4 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:
wrong answer 37th numbers differ - expected: '4', found: '3'