QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#346661#7936. Eliminate TreeDelay_for_five_minutes#WA 178ms32296kbC++111.7kb2024-03-08 20:51:472024-03-08 20:51:47

Judging History

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

  • [2024-03-08 20:51:47]
  • 评测
  • 测评结果:WA
  • 用时:178ms
  • 内存:32296kb
  • [2024-03-08 20:51:47]
  • 提交

answer

#include<bits/stdc++.h>
#define maxn 200005
std::set<int> G[maxn];
std::queue<int> q1,q2;
bool vis[maxn];
bool deg2(int x) {
    if (vis[x]) return 0;
    if (G[x].size() > 2) return 0;
    for(auto i : G[x]) {
        if (G[i].size() == 1) {
            return 1;
        }
    }
    return 0;
}
void rmv(int u) {
    vis[u] = 1;
    for(auto i : G[u]) {
        G[i].erase(u);
        if (G[i].size() <= 1) {
            q1.push(i);
            if (G[i].size() > 0) {
                int v = *G[i].begin();
                if (deg2(v)) {
                    q2.push(v);
                }
            }
        }
        if (deg2(i)) {
            q2.push(i);
        }
    }
}
int main() {
    // freopen("in.txt","r",stdin);
    int n;
    std::cin >> n;
    for(int i = 1; i < n; i++) {
        int x,y;
        std::cin >> x >> y;
        G[x].insert(y);
        G[y].insert(x);
    }
    for(int i = 1; i <= n; i++) {
        if (G[i].size() <= 1) {
            q1.push(i);
        }
        if (deg2(i)){
            q2.push(i);
        }
    }
    int ans = 0;
    for(int cnt = 0;cnt < n;) {
        while(!q2.empty()) {
            int u = q2.front();q2.pop();
            if (!deg2(u)) continue;
            int v = -1;
            for(auto i : G[u]) {
                if (G[i].size() == 1) {
                    v = i;
                    break;
                }
            }
            rmv(v);
            rmv(u);
            cnt += 2;
            ans ++;
        }
        if (!q1.empty()) {
            int u = q1.front();
            q1.pop();
            if (vis[u]) continue;
            rmv(u);
            ans += 2;
            cnt++;
        }
    }
    std::cout << ans << std::endl;
}

詳細信息

Test #1:

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

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

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 178ms
memory: 32296kb

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:

152825

result:

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