Codeforces982C、Cut 'em all!(DFS)

C. Cut 'em all!

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You're given a tree with nn vertices.

Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.

Input

The first line contains an integer nn (1≤n≤1051≤n≤105) denoting the size of the tree.

The next n−1n−1 lines contain two integers uu, vv (1≤u,v≤n1≤u,v≤n) each, describing the vertices connected by the ii-th edge.

It's guaranteed that the given edges form a tree.

Output

Output a single integer kk — the maximum number of edges that can be removed to leave all connected components with even size, or −1−1 if it is impossible to remove edges in order to satisfy this property.

Examples

input

Copy

4
2 4
4 1
3 1

output

Copy

1

input

Copy

3
1 2
1 3

output

Copy

-1

input

Copy

10
7 1
8 4
8 10
4 7
6 5
9 3
3 5
2 10
2 5

output

Copy

4

input

Copy

2
1 2

output

Copy

0

Note

In the first example you can remove the edge between vertices 11 and 44. The graph after that will have two connected components with two vertices in each.

In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is −1−1

 

.一、原题地址

    传送门

二、大致题意

    给出n个点,有n-1条边让他们相连。

    询问最多删除多少边,让他们成为偶数的块

三、思路

    DFS裸题,只要能找到一个偶数的块,就使答案++

四、代码

    

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf=0x3f3f3f3f;
long long  gcd(long long  a,long long  b){ return a == 0 ? b : gcd(b % a, a); } 

const int MAXM = 200005*2;
struct Edge
{
	int v, next;
}e[MAXM];
int head[MAXM];
int ind;
int cnt[MAXM];
int ans = 0;
void add(int from, int to)
{
	e[ind].next = head[from];
	e[ind].v = to;
	head[from] = ind++;
}
int n;
int DFS(int u, int fa)
{
	for (int i = head[u]; i != -1; i = e[i].next)
	{
		int v = e[i].v;
		if (v == fa)continue;
		int x = DFS(v, u);
		if (x & 1)cnt[u] += x;
		else ans++;
	}
	return cnt[u] + 1;
}
int main()
{
	ind = 0;
	memset(head, -1, sizeof(head));
	scanf("%d", &n);
	int minn = inf;
	for (int i = 0; i < n - 1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		add(u, v);
		add(v, u);
	}
	if (n & 1)
	{
		printf("-1\n");
		return 0;
	}
	DFS(1, -1);
	cout << ans << endl;
		
	getchar();
	getchar();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值