QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#292877#6307. Chase Game 2YcfhnndWA 5ms3456kbC++20991b2023-12-28 15:53:432023-12-28 15:53:43

Judging History

你现在查看的是最新测评结果

  • [2023-12-28 15:53:43]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3456kb
  • [2023-12-28 15:53:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

void solve(){
    int n;
    cin >> n;
    vector<vector<int>>adj(n + 1);
    for (int i = 0;i < n - 1;i ++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1;i <= n;i ++){
        if (adj[i].size() == n - 1){
            cout << -1 << '\n';
            return;
        }
    }

    vector<int>dep(n + 1);
    int cnt = 0;
    auto dfs = [&](auto self, int u, int p) -> void{
        dep[u] = dep[p] + 1;
        for (auto v : adj[u]){
            if (v == p) continue;
            self(self, v, u);        
        }
        if (adj[u].size() == 1 && u != 1){
            cnt ++;
        }
    };dfs(dfs, 1, 0);

    cout << cnt << '\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    cin >> T;
    while (T --){
        solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3436kb

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: 5ms
memory: 3456kb

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:

2
-1
2
2
1
-1
3
2
3
2
3
2
3
-1
3
2
2
2
3
1
2
2
-1
3
2
2
1
-1
2
2
3
1
2
-1
2
3
2
1
2
-1
2
2
2
3
3
2
1
1
-1
2
2
1
2
2
2
3
2
2
3
-1
-1
-1
2
3
3
2
2
2
3
3
3
-1
2
3
-1
2
2
-1
3
-1
-1
2
3
3
2
2
2
2
1
1
1
2
2
2
3
-1
2
2
3
-1
3
2
2
2
-1
3
-1
2
-1
-1
3
-1
3
1
2
3
2
2
2
2
2
2
2
1
2
-1
2
1
3
1
1
2
2
2
2
2
3
-1...

result:

wrong answer 1st numbers differ - expected: '1', found: '2'