QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562782#6307. Chase Game 2HTensor#WA 24ms8360kbC++172.1kb2024-09-13 20:49:402024-09-13 20:49:41

Judging History

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

  • [2024-09-13 20:49:41]
  • 评测
  • 测评结果:WA
  • 用时:24ms
  • 内存:8360kb
  • [2024-09-13 20:49:40]
  • 提交

answer

#include <bits/stdc++.h>
#define dd(x) cout << #x << "\n"
#define d(x) cout << #x  << ": " << x << "\n"
using namespace std;
#define int long long
using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vii = vector<vector<int>>;
using a3 = array<int, 3>;
const int inf = 0x3f3f3f3f3f3f3f3fLL;

const int N = 1e5 + 5;
vector<int> adj[N];
vector<int> leaf, dep, rd;
vector<int> leaves[N];

void dfs1(int u, int d) {
    int cnt = 0;
    for(auto v : adj[u]) {
        if(v == d) continue;
        dep[v] = dep[u] + 1;
        dfs1(v, u);
        ++cnt;
    }
    if(!cnt) leaf[u] = 1;
}

void dfs2(int u, int d) {
    for(auto v : adj[u]) {
        if(v == d) continue;
        if(leaf[v]) {
            leaves[u].push_back(v);
            continue;
        } else {
            dfs2(v, u);
        }
    }
}

void solve() {
    leaf.clear(); dep.clear(); rd.clear();
    int n; cin >> n;
    leaf.resize(n + 1); dep.resize(n + 1); rd.resize(n + 1);
    for(int i = 1; i <= n; i++) adj[i].clear(), leaves[i].clear();
    for(int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        rd[u]++, rd[v]++;
    }

    if(n == 2) {
        cout << "-1\n";
        return ;
    }

    int st = 1;
    for(int i = 1; i <= n; i++) if(rd[i] > 1) st = i;

    dfs1(st, 0);
    dfs2(st, 0);
    int tot = 0, maxx = 0;
    for(int i = 1; i <= n; i++) {
        if((int) leaves[i].size()) {
            int x = leaves[i].size();
            maxx = max(maxx, x); tot += x;
        }
    }

    int ans = 0;
    int pp = tot - maxx;
    if(!pp) {
        cout << "-1\n";
        return ;
    } else if(maxx > pp) {
        ans = maxx;
    } else {
        ans = tot / 2;
    }

    cout << ans << "\n";
}

signed main() {
    // ios::sync_with_stdio(false); cin.tie(0);
    int T; cin >> T;
    while(T--) solve();
    return 0;
}

/*
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



1
6
1 2
2 3
3 4
4 5
2 6


1
6
1 2
1 3
1 4
1 5
1 6

Ans: -1

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8360kb

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

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: 24ms
memory: 8232kb

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
2
2
3
2
1
3
2
2
2
3
2
3
2
2
2
2
2
2
2
3
2
3
2
2
2
2
3
2
3
2
2
2
3
2
3
4
2
3
2
2
2
2
2
2
3
2
3
2
2
2
3
2
1
3
1
2
2
2
3
2
3
2
4
2
2
2
2
2
1
2
2
3
2
2
2
3
2
4
2
2
3
2
3
2
2
2
3
3
3
2
2
3
2
2
3
2
3
2
2
2
2
2
3
5
2
2
1
2
3
2
2
4
2
5
2
1
2
2
2
3
2
2
3
3
3
2
1
3
3
2
2
2
2
2
2
3
2
2
3
3
2
2
3
2
3
2
3
2
2
...

result:

wrong answer 2nd numbers differ - expected: '3', found: '2'