Is this a tree?

Is this a tree? ⁡ \operatorname{Is this a tree?} This is Yi Ke tree it ?

Title link: luogu T145300 ⁡ \operatorname{luogu\ T145300}l u o g u T 1 4 5 3 0 0  /SSL competition 1516 ⁡ \operatorname{SSL competition\ 1516}S S L ratio race . 1 . 5 . 1 . 6 

topic

DD and QQ are playing games. DD drew a tree (a tree in graph theory) on the ground, and then he told QQ the degree sequence of the tree. QQ immediately said that this is not a tree. DD thought he was despised by QQ, and they quarreled.

But DD later found out that he had calculated the wrong degree sequence, and QQ was right. DD wondered why QQ reacted so quickly.

Now given the degree sequence of a graph, what you need to do is like QQ: judge whether this is the degree sequence of a tree.

enter

The input is only one line, first give an integer NNN represents the number of vertices, followed byNNN integers, representing the degree sequence of this graph, each number does not exceed100 100100

Output

If the input may be the degree sequence of a tree, output "Possible", otherwise output "Impossible".

Sample input 1

1 0

Sample output 1

Possible

Sample input 2

2 1 1

Sample output 2

Possible

Sample input 3

3 2 2 2

Sample output 3

Impossible

Sample input 4

3 1 2 1

Sample output 4

Possible

data range

For 100% 100\%1 0 0 % of the data,1 ≤ N ≤ 100 1\le N\le 1001N100

Ideas

This question is a good inference question.

We can know that the degree of each point must be greater than 0 00 .
(I didn’t expect this place to be able to form a tree because it could be like this, only80 80. 8 0 a, QAQ)
(but whenn = 1 n = 1n=When it is 1 , the degree of that single point must be0 00

Next, we can know that the number of sides of the tree is n − 1 n-1n1 , then let's judge again(n − 1) ∗ 2 (n-1)*2(n1)Whether 2 is equal to the degree can be judged.

Code

#include<cstdio>

using namespace std;

int n, a[101], sum;

int main() {
    
    
	scanf("%d", &n);//读入
	for (int i = 1; i <= n; i++) {
    
    
		scanf("%d", &a[i]);//读入
		sum += a[i];//记录总共有多少度数
		if (a[i] <= 0) {
    
    //不能有完全独立的单独点(除非点的数量只有一个)
			if (n == 1) printf("Possible");//点的数量只有一个
			printf("Impossible");
			return 0;
		}
	}
	
	if (sum & 1) {
    
    //度数之和肯定不是单数(显而易见)
		printf("Impossible");
		return 0;
	}
	if (sum == (n - 1) * 2) {
    
    //边的数量一定是n-1,就是一半的度数和
		printf("Possible");
		return 0;
	}
	else {
    
    
		printf("Impossible");
		return 0;
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/108209333