QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#439506#7936. Eliminate Treemonkey8WA 125ms32532kbC++141.5kb2024-06-12 06:29:432024-06-12 06:29:45

Judging History

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

  • [2024-06-12 06:29:45]
  • 评测
  • 测评结果:WA
  • 用时:125ms
  • 内存:32532kb
  • [2024-06-12 06:29:43]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cstdio>
#include <utility> 
#include <queue>
#include <math.h>
#include <set>
#include <bitset>
#include <cmath>
#include <bitset>
#include <stack>
#include <cstring>
#include <math.h>
#include <iomanip>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 200010;
set<int> graph[MAXN];
int rem[MAXN];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int n; cin >> n;
    for(int i = 0; i < n - 1; i++) {
        int x, y; cin >> x >> y;
        graph[x].insert(y);
        graph[y].insert(x);
    }
    int ans = 0;
    queue<int> q;
    for(int i = 1; i <= n; i++) {
        if(graph[i].size() <= 1) q.push(i);
    }
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        if(rem[x]) continue;
        rem[x] = 1;
        if(graph[x].size() == 0) ans += 2;
        else {
            int y = *(graph[x].begin());
            if(graph[y].size() <= 2) {
                rem[y] = 1;
                for(int z : graph[y]) {
                    if(rem[z]) continue;
                    graph[z].erase(y);
                    if(graph[z].size() <= 1) q.push(z);
                }
                ans++;
            }
            else {
                graph[y].erase(x);
                if(graph[y].size() <= 1) q.push(y);
                ans += 2;
            }
        }
    }
    cout << ans << '\n';
}

詳細信息

Test #1:

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

input:

5
1 2
2 3
3 4
3 5

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 12928kb

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 125ms
memory: 32532kb

input:

196666
32025 108673
181779 17115
162570 134230
93003 39673
89144 1269
185919 154408
34896 65369
35860 44720
55698 1390
45520 189805
147867 124511
135459 132555
87303 18031
176314 59695
33352 130640
87272 39716
35749 108807
143493 94486
126512 116598
40212 70895
132216 80855
22241 188737
150354 17346...

output:

169517

result:

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