The 2021 CCPC Weihai Onsite A. Goodbye, Ziyin

题目链接:Problem - A - Codeforces

My Dearest Allie:

I couldn't sleep last night because I know that it's over between us. I'm not bitter anymore, because I know that what we had was real.

And if in some distant place in the future we see each other in our new lives, I'll smile at you with joy and remember how we spent a summer beneath the trees, learning from each other and growing in love. The best love is the kind that awakens the soul and makes us reach for more, that plants a fire in our hearts and brings peace to our minds. And that's what you've given me. That's what I'd hoped to give to you forever.

I love you. I'll be seeing you.

Movie,The Notebook

Through time passes, there is always someone we will never forget.

"Although there were millions of trees in the world, there was only one that The Little Prince took care of with his heart. And although there are countless people in the world, there is only one that I take care of with my heart." said Zayin to Ziyin with affection.

"Millions of trees? I may not agree with you. Just a tree could have millions of structure." answered Ziyin proudly, "Let's do the some math."

...

Thinking of the days they have happily spent together, Zayin can't help bursting into tears. Though Ziyin has been gone for a long time, Zayin still misses her every much.

He remembers it was a sunny afternoon when Ziyin and him were lying in the yard, resting beneath the shade of a tree, working on the number of nodes satisfying that if the tree is rooted by the node, the rooted tree is a rooted binary tree.

When times smoothes all sorrows, Zayin also forgets the answer of the problem. He only remember the structure of the tree. Maybe it's time for Zayin to let all memories about Ziyin go, but he still want to figure out the answer out of sentiment.

As Zayin's friend, you may want Zayin to get released as soon as possible, can you help him?

In short, given an unrooted tree, please calaculate how many nodes satisfying that if the tree is rooted by the node, the rooted tree is a rooted binary tree.

Input

The first line of the input contains an integer n (1≤n≤106), indicating the number of nodes.

Each of the next n−1 line contains two integers u (1≤u≤n) and v (1≤u≤n), denoting that there is a edge connecting node u and v.

It's guatanteed that the n−1 edges form a tree.

Output

Print an integer, denoting the number of satisfactory nodes.

Example

input

6
1 2
1 3
2 4
2 5
3 6

output

5

Note

A rooted binary tree is a rooted tree in which every nodes have at most 2 childrens.

题意:一棵树有n个节点,n - 1条边,(u 和 v相连)问有多少个节点可以做根节点形成二叉树

思路:如果有一个节点的连接情况超过3个点,那么这棵树不是一个二叉树,就输出0,如果一个节点的连接情况大于等于0,小于3,那么他就可以作为二叉树的根节点

#include<bits/stdc++.h>
using namespace std;

vector<int> ve[1000005];

int main(){
	ios::sync_with_stdio(false);
	int n;
	int u, v;
	while(cin >> n){
		for(int i = 1; i <= n; i++){
			ve[i].clear();
		}
		for(int i = 0; i < n - 1; i++){
			cin >> u >> v;
			ve[u].push_back(v);
			ve[v].push_back(u);
		}
		int ans = 0;
		bool flag = 0;
		for(int i = 1; i <= n; i++){
			if(ve[i].size() > 3){
				flag = 1;
				break;
			}
		}
		if(!flag){
			for(int i = 1; i <= n; i++){
				if(ve[i].size() >= 0 && ve[i].size() < 3){
					ans++;
				}
			}
		}
		cout << ans << "\n";
	} 
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值