2021 Niu Ke Winter Holiday Algorithm Basic Training Camp 5 F. My heart is Bingbing (sign in)

F. My heart is icy

Title link: https://ac.nowcoder.com/acm/contest/9985/F

Title description:

Ze Gege likes Wang Bingbing very much. In order to prove whether he is worthy of Bingbing, Sister Ye asked him a question: Given a tree with n points, you need to dye each point of the tree, and It is required that every two adjacent points (that is, connected by edges) have different colors. Sister Ye wants to know at least how many different colors are needed to complete this dyeing? Ze Gege thinks this question is too simple, so come and answer it, clever!

Enter a description:

Enter an integer T in the first line, indicating the number of groups of the sample

Enter an integer n in the first line of each group of samples

Next, enter n-1 lines, each line enter two integers u and v, which means that there is an undirected edge from point u to point v on the tree (1≤u≤n,1≤v≤n)

Ensure that the input must be a tree

[Data scale and agreement]

1≤T≤50,1≤n≤10^5

Output description:

Output T rows, each row outputs an integer x, where x represents at least the required number of color types

Example 1:

Input
1
3
1 2
2 3
Output
2
Description
Only two colors are needed: dye point 1 and point 3 into the same color, and dye point 2 into another color.

Problem-solving ideas:

Check-in questions
When n=1, output 1; when n>1, output 2.

code show as below:

#include<iostream>
#include<cstdio>
using namespace std;  
int main(){
    
    
	int t;
	scanf("%d",&t);
	while(t--){
    
    
		int n;
		scanf("%d",&n);
		if(n==1){
    
    
			printf("1\n");
		}else{
    
    
			for(int i=1;i<n;i++){
    
    
				int u,v;
				scanf("%d%d",&u,&v);
			}
			printf("2\n");
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/113949010