QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#419670#7936. Eliminate Treegabrielwu#WA 117ms18252kbC++202.4kb2024-05-24 08:51:192024-05-24 08:51:21

Judging History

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

  • [2024-05-24 08:51:21]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:18252kb
  • [2024-05-24 08:51:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define forn(i,n) for (int i=0; i<(n); i++)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define f first
#define s second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

template<typename A, typename B> ostream& operator<<(ostream & cout, pair<A,B> const &p) {
    return cout << "("<< p.f << ", "<< p.s << ")";
}
template<typename A> ostream& operator<<(ostream &cout, vector<A> const & v) {
    cout << "[";
    forn(i, v.size()) {
        if(i) cout << ", ";
        cout << v[i];
    }
    return cout << "]";
}

int n;
vector<vector<int>> adj;
vector<int> deg;
vector<bool> in;
set<int> leav, goodleav;
// void rem(u):
// in[u] false
// for nb: deg[nb]--
// if nb is node: add
// if deg[nb] <= 2: check neighbors of nb

// while true
// if goodleaves: u, nb, rem(u), rem(nb), ans++
// else: u from leaves, rem(u), ans += 2
void rem(int i) {
    leav.erase(i); goodleav.erase(i);
    in[i] = false;
    for (int nb : adj[i]) if (in[nb]) {
        deg[nb]--;
        if (deg[nb]==1) {
            int nbnb;
            for (int j : adj[nb]) if (in[j]) nbnb = j;
            if (deg[nbnb] <= 2) goodleav.insert(nb);
            else leav.insert(nb);
        }
        if (deg[nb] <= 2) {
            for (int j : adj[nb]) if (deg[j]==1) goodleav.insert(j);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n; adj.resize(n); deg.assign(n,0); in.assign(n,true);
    forn(i, n-1) {
        int u,v; cin >> u >> v; u--; v--;
        adj[u].pb(v); adj[v].pb(u);
    }
    forn(i, n) {
        deg[i] = adj[i].size();
        if (deg[i] == 1) {
            int nb = adj[i][0];
            if (deg[nb] <= 2) goodleav.insert(i);
            else leav.insert(i);
        }
    }

    int ans = 0, left = n;
    while (left > 1) {
        if (! goodleav.empty()) {
            int i = *goodleav.begin(); 
            rem(i);
            for (int nb : adj[i]) if (in[nb]) rem(nb);
            ans++;
            left -= 2;
        } else if (! leav.empty()) {
            int i = *leav.begin();
            rem(i);
            ans += 2;
            left--;
        } else {
            while (true);
        }
    }

    if (left > 0) ans += 2;
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 117ms
memory: 18252kb

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:

98333

result:

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