QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#322976#7936. Eliminate TreeAnwar_Gehad_Maghraby#WA 187ms35824kbC++201.9kb2024-02-08 02:32:032024-02-08 02:32:04

Judging History

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

  • [2024-02-08 02:32:04]
  • 评测
  • 测评结果:WA
  • 用时:187ms
  • 内存:35824kb
  • [2024-02-08 02:32:03]
  • 提交

answer

/**
 *    author:  MaGnsi0
 *    created: 07.02.2024 20:06:23
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n; cin >> n;
    vector<int> deg(n), ones(n);
    vector<set<int>> adj(n);
    for (int i = 0; i < n - 1; ++i) {
        int u; cin >> u; u--;
        int v; cin >> v; v--;
        adj[u].emplace(v);
        adj[v].emplace(u);
        deg[u]++, deg[v]++;
    }
    for (int v = 0; v < n; ++v) {
        for (int u : adj[v]) {
            ones[v] += deg[u] == 1;
        }

    }
    vector<bool> removed(n, false);
    set<pair<int, int>> can_remove;
    for (int i = 0; i < n; ++i) {
        if (deg[i] <= 1) {
            can_remove.emplace((deg[i] ? -ones[*adj[i].begin()] : 1), i);
        }
    }
    int ans = 0;
    while (!can_remove.empty()) {
        int v = can_remove.begin() -> second;
        can_remove.erase(can_remove.begin());
        if (removed[v]) { continue; }
        if (deg[v] == 0) {
            removed[v] = true;
            ans += 2;
        } else {
            int u = *adj[v].begin();
            if (deg[u] <= 2) {
                removed[v] = removed[u] = true;
                for (int x : adj[u]) {
                    if (removed[x]) { continue; }
                    deg[x]--; adj[x].erase(u);
                    if (deg[u] == 1) { ones[x]--; }
                    if (deg[x] <= 1) {
                        can_remove.emplace((deg[x] ? -ones[*adj[x].begin()] : 1), x);
                    }
                }
                ans++;
            } else {
                removed[v] = true;
                deg[u]--;
                adj[u].erase(v);
                if (deg[u] <= 1) {
                    can_remove.emplace((deg[u] ? -ones[*adj[u].begin()] : 1), u);
                }
                ans += 2;
            }
        }
    }
    cout << ans;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 187ms
memory: 35824kb

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:

169457

result:

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