QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#689420#6307. Chase Game 2blue_sky#WA 5ms3780kbC++201.6kb2024-10-30 16:56:322024-10-30 16:56:38

Judging History

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

  • [2024-10-30 16:56:38]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3780kb
  • [2024-10-30 16:56:32]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define bug(X) cout << "bug:# " << X << endl
#define bug2(f, X) cout << "bug:# " << f << " " << X << endl
#define bug3(i, j, G) cout << "bug:# " << i << ' ' << j << ' ' << G << endl
#define endl '\n'
using namespace std;
const int mod = 998244353;
const int N = 10 + 5e5;
void _();
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    vector<int> in(n + 1);
    vector<vector<int>> e(n + 1);
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        e[u].push_back(v);
        e[v].push_back(u);
        in[u]++;
        in[v]++;
    }

    int leaf = 0;
    int has_3 = 0, has_2 = 0;
    vector<int> vis(n + 1), d(n + 1, 1e18);
    queue<int> q;
    d[1] = 0;
    q.push(1);
    while (q.size())
    {
        int u = q.front();
        q.pop();
        if (vis[u])
            continue;
        vis[u] = 1;
        for (auto v : e[u])
        {
            d[v] = min(d[v], d[u] + 1);
            q.push(v);
        }
    }

    for (int i = 2; i <= n; i++)
        if (in[i] == 1)
        {
            leaf++;
            if (d[i] >= 3)
                has_3 = 1;
            if (d[i] < 3)
                has_2 = 1;
        }
    // for (int i = 1; i <= n; i++)
    //     bug2(i, d[i]);
    int res = leaf;
    if (!has_3 && has_2)
        res = -1;
    // bug2(has_2,has_3);
    cout << res << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3608kb

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: 3780kb

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
-1
-1
-1
2
-1
-1
-1
-1
-1
2
-1
2
-1
1
-1
-1
-1
-1
2
2
1
-1
-1
2
-1
1
-1
-1
-1
-1
-1
1
-1
-1
-1
-1
2
-1
-1
2
1
1
-1
-1
2
1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
2
-1
-1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
2
-1
-1
2
2
-1
-1
1
1
1
-1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
-1...

result:

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