QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#286144#7936. Eliminate Treeucup-team2894#WA 73ms36200kbC++201.8kb2023-12-17 04:05:532023-12-17 04:05:55

Judging History

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

  • [2023-12-17 04:05:55]
  • 评测
  • 测评结果:WA
  • 用时:73ms
  • 内存:36200kb
  • [2023-12-17 04:05:53]
  • 提交

answer

// UW BLACK
#include <bits/stdc++.h>

#define fr first
#define sc second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)((x).size()))
#define rep(i,a,b) for(int i=(a);i<(b);++i)

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int,int>;

mt19937 rnd(223);

clock_t TIME0;

#define TIME ((clock() - TIME0) * 1.0 / CLOCKS_PER_SEC)

constexpr int maxn = 1e6 + 100, inf = 1e9 + 100;
using T = long double;
constexpr T EPS = 1e-9;

const ll mod = 1e9+7;

bool rem[maxn];
int deg[maxn];
vi g[maxn];
void solve() {
    int n;
    cin >> n;
    for(int i = 0; i < n - 1; ++i) {
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vi lfs;
    for(int i=1;i<=n;i++){
        deg[i] = g[i].size();
        if(g[i].size()<=1){
            lfs.push_back(i);
        }
    }
    int ans = 0;
    while(lfs.size()){
        int x = lfs.back();
        lfs.pop_back();
        if(rem[x])continue;
        rem[x]=true;
        int nx = -1;
        for(int y:g[x])if(!rem[y]){
            nx = y;
            deg[y]--;
            if(deg[y]==0)rem[y]=true;
            else if(deg[y]==1)lfs.push_back(y);
        }
        if(nx!=-1&&deg[nx]<=1){
            rem[nx]=true;
            for(int y:g[x])if(!rem[y]){
                deg[y]--;
                if(deg[y]==0)rem[y]=true;
                else if(deg[y]==1)lfs.push_back(y);
            }
            ans++;
        }
        else {
            ans+=2;
        }
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed;
    cout.precision(20);
    TIME0 = clock();
    int k=1;
    // cin >> k;
    while(k--)
        solve();
    cerr << "\n\nConsumed " << TIME << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 28468kb

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

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 73ms
memory: 36200kb

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:

112978

result:

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