QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#346626#7936. Eliminate TreeDelay_for_five_minutes#WA 95ms15788kbC++111.5kb2024-03-08 19:40:172024-03-08 19:40:17

Judging History

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

  • [2024-03-08 19:40:17]
  • 评测
  • 测评结果:WA
  • 用时:95ms
  • 内存:15788kb
  • [2024-03-08 19:40:17]
  • 提交

answer

#include<bits/stdc++.h>
int d[200005];
std::vector<int> G[200005];
bool vis[200005];
std::queue<int> que;
int getfa(int u) {
    for(auto t : G[u]) {
        if (!vis[t]) {
            return t;
        }
    }
    assert(0);
    return -1;
}
void rmv(int now) {
    // std::cout << "remove" << now << std::endl;
    for(auto i : G[now]) {
        d[i]--;
        if (d[i] == 1 && !vis[i]) {
            que.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].push_back(y);
        G[y].push_back(x);
        d[x]++,d[y]++;
    }
    for(int i = 1; i <= n; i++) {
        if (d[i] == 1) {
            que.push(i);
        }
    }
    int ans = 0;
    int cnt = 0;
    while(!que.empty() && cnt < n - 1) {
        auto top = que.front();
        // std::cout << top.second << std::endl;
        que.pop();
        if (vis[top]) continue;
        if (d[getfa(top)] <= 2) {
            // std::cout << "A" ;
            int u = top;
            int v = getfa(u);
            vis[u] = 1; vis[v] = 1;
            rmv(u),rmv(v);
            ans ++;
            cnt += 2;
        }
        else {
            // std::cout << "B" ;
            vis[top] = 1;
            rmv(top);
            ans += 2;
            cnt++;
        }
    }
    if (cnt == n - 1) {
        ans += 2;
    }
    std::cout << ans << std::endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
1 2
2 3
3 4
3 5

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 8316kb

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 95ms
memory: 15788kb

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'