QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562727#6307. Chase Game 2HTensor#WA 4ms6036kbC++171.9kb2024-09-13 20:18:512024-09-13 20:18:51

Judging History

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

  • [2024-09-13 20:18:51]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:6036kb
  • [2024-09-13 20:18:51]
  • 提交

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> cnt1, cnt2, leaf, dep;
int ans;

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]) {
            cnt2[u]++;
            continue;
        } else {
            dfs2(v, u);
            cnt1[u] += cnt2[v];
            cnt1[u] += cnt1[v];
        }
    }
    int maxx = min(cnt1[u], cnt2[u]);
    cnt1[u] -= maxx, cnt2[u] -= maxx, ans += maxx;
}

void solve() {
    ans = 0;
    cnt1.clear(), cnt2.clear(), leaf.clear(); dep.clear();
    int n; cin >> n;
    cnt1.resize(n + 1), cnt2.resize(n + 1), leaf.resize(n + 1); dep.resize(n + 1);
    for(int i = 1; i <= n; i++) adj[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);
    }

    dfs1(1, 0);
    int maxdep = 0;
    for(int i = 1; i <= n; i++) {
        maxdep = max(maxdep, dep[i]);
    }

    dfs2(1, 0);

    if(!ans && (cnt1[1] || cnt2[1]) && maxdep <= 2) {
        cout << "-1\n";
        return ;
    }

    ans += max(cnt1[1], cnt2[1]);
    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

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

result:

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