QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#286145#7936. Eliminate Treeucup-team2894#WA 66ms34620kbC++201.7kb2023-12-17 04:07:242023-12-17 04:07:25

Judging History

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

  • [2023-12-17 04:07:25]
  • 评测
  • 测评结果:WA
  • 用时:66ms
  • 内存:34620kb
  • [2023-12-17 04:07:24]
  • 提交

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]==1)lfs.push_back(y);
        }
        if(nx!=-1&&deg[nx]<=1){
            rem[nx]=true;
            for(int y:g[nx])if(!rem[y]){
                deg[y]--;
                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;
}

詳細信息

Test #1:

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

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

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 66ms
memory: 34620kb

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:

169547

result:

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