QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#124066 | #2596. Even Forest | UNos_maricones# | WA | 8ms | 28720kb | C++20 | 990b | 2023-07-14 05:57:06 | 2023-07-14 05:57:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
const int N = 1e6+10;
int n;
int ans = 0;
vector<int> g[N];
int color[N];
void dfs(int u, int p = -1){
int cnt0 = 0, cnt1 = 0;
int c = 1;
for(auto& v : g[u]){
if(v == p)continue;
dfs(v,u);
if(!color[v])cnt1++;
else cnt0++;
c &= color[v];
}
if(cnt0+cnt1 <= 1){
color[u] = c^1;
return;
}
if(cnt0 > cnt1){
ans += cnt1;
color[u] = 1;
}else{
ans += cnt0;
color[u] = 0;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
if(n == 1){
return puts("0"),0;
}
if(n == 2){
return puts("1"),0;
}
int root = 1;
for(int i=0; i<n-1; i++){
int u, v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
if(g[u].size() > 1)root = u;
}
dfs(root);
cout<<ans<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28720kb
input:
4 1 2 2 3 3 4
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 8ms
memory: 27204kb
input:
4 1 2 1 3 1 4
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 27556kb
input:
6 1 2 2 3 4 2 4 5 6 4
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'