QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#252153#7709. Rikka with Tree GameSolitaryDream#AC ✓272ms13672kbC++17994b2023-11-15 16:01:092023-11-15 16:01:09

Judging History

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

  • [2023-11-15 16:01:09]
  • 评测
  • 测评结果:AC
  • 用时:272ms
  • 内存:13672kb
  • [2023-11-15 16:01:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int de[N], f[N];
vector<int> g[N];
inline void Dfs(int x, int fa) {
    de[x] = de[fa] + 1;
    int tot = 0;
    for (auto y : g[x]) if (y != fa) {
        Dfs(y, x);
        ++tot;
    }
    if (!tot) { f[x] = 1; return; }
    if (de[x] & 1) {
        f[x] = 1e9;
        for (auto y : g[x]) if (y != fa) {
            f[x] = min(f[x], f[y]);
        }
    } else {
        f[x] = 0;
        for (auto y : g[x]) if (y != fa) {
            f[x] += f[y];
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int Case;
    cin >> Case;
    while (Case--) {
        int n;
        cin >> n;
        for (int i = 1, x, y; i < n; ++i) {
            cin >> x >> y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        Dfs(1, 0);
        printf("%d\n", f[1]);
        for (int i = 1; i <= n; ++i) g[i].clear();
    }
    return 0;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
8
1 2
2 3
2 4
4 5
4 8
5 6
5 7

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 272ms
memory: 13672kb

input:

1000
99764
2 30715
2 19164
28252 54917
2 22227
69989 72917
2 25881
71664 73507
2 4043
2 32442
2 21200
2 21295
2 26649
2 34517
20442 49141
2 45126
2 35202
28501 46600
1786 70039
2 22315
2 33705
84715 85920
14461 96733
53917 72854
24406 65536
2 429
2 39950
2 28944
2 31689
2 2213
2 24414
6488 82695
2 2...

output:

47571
69285
28334
28408
1
8
3
1
10
1
2
1
3
2
3
3
3
5
4
1
1
2
2
5
1
6
1
5
5
2
1
1
1
1
1
1
1
4
3
1
9
1
3
3
1
3
4
1
1
5
2
3
1
4
1
1
1
1
5
6
1
3
1
1
1
2
1
1
1
2
2
4
3
9
1
2
2
3
4
1
1
6
1
1
1
1
1
1
1
4
3
1
1
6
6
2
4
1
1
6
2
3
1
1
11
2
2
3
1
7
1
1
3
3
1
5
1
2
1
1
2
1
1
2
1
1
1
3
2
5
2
2
1
1
1
3
1
2
10
2
3...

result:

ok 1000 numbers