QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#695538#6307. Chase Game 2blue_skyWA 6ms3824kbC++201.7kb2024-10-31 20:14:312024-10-31 20:14:54

Judging History

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

  • [2024-10-31 20:14:54]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:3824kb
  • [2024-10-31 20:14:31]
  • 提交

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;
    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);
        }
    }

    int d_1 = 0, d_2 = 0;
    map<int, int> root; // has leaf of the deep
    for (int i = 2; i <= n; i++)
        if (in[i] == 1)
        {
            leaf++;
            root[e[i][0]] = d[i];
            if (d[i] == 1)
                d_1++;
        }

    d_2 = leaf - d_1;
    int f = 1;
    if (!d_2)
        f = 0;
    if (root.size() == 1 && (*root.begin()).y < 3)
        f = 0;
    int res = leaf;
    if (root.size() > 1)
        res = d_2 >= d_1 ? d_1 + (d_2 - d_1 + 1) / 2 : d_1;
    cout << (f ? res : -1) << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

result:

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