QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#124066#2596. Even ForestUNos_maricones#WA 8ms28720kbC++20990b2023-07-14 05:57:062023-07-14 05:57:09

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-14 05:57:09]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:28720kb
  • [2023-07-14 05:57:06]
  • 提交

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;
}

詳細信息

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'