【结论?】这是一颗树吗?

L i n k Link Link

l u o g u   T 145300 luogu\ T145300 luogu T145300

D e s c r i p t i o n Description Description

【结论?】这是一颗树吗?_第1张图片

S a m p l e Sample Sample I n p u t Input Input

1

1 0

2

2 1 1

3

3 2 2 2

4

3 1 2 1

S a m p l e Sample Sample O u t p u t Output Output

1

Possible

2

Possible

3

Impossible

4

Possible

H i n t Hint Hint

S o l u t i o n Solution Solution

就。。推着推着就发现一个结论???
s u m = ( n − 1 ) ∗ 2 sum = (n - 1 )* 2 sum=(n1)2???
然后特判1啥的就没了

C o d e Code Code

#include
#include
#include

using namespace std;

int n, sum, a[105], ans; 

int main()
{
	scanf("%d", &n);
	if (n == 1) {
		int x; 
		scanf("%d", &x);
		if (!x) printf("Possible");
		else printf("Impossible");
		return 0;
	}
	else {
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &a[i]);
			if (a[i] <= 0) {
				printf("Impossible");
				return 0;
			}//判负
			if (a[i] == n) {
				printf("Impossible");
				return 0;
			} 
			ans += a[i];
		}
		if (ans / 2 == n - 1) printf("Possible");
		else printf("Impossible");
	}
	return 0;
}

你可能感兴趣的:(其他)