360 staff GUI recently applied for a long vacation, a person carrying a bag out of self-help tour.
On the way, he passed a small town and found that the people in the town were quarrelling around a tree. GUI approached the situation, learned that small town people are missing a mayor, they want to choose a well-known and just mayor, that is, we hope to elect a person, everyone knows him, but at the same time he does not know the town in addition to other people (in this case, we are the default of everyone to know themselves). But there are too many people in the small town, all of a sudden everyone can not persuade who.
"It's simple." "said Gui. So he counted out the people in the town to each other's understanding of the relationship, and suddenly found the right mayor candidate.
Now you've got a list of such a relationship. The above-mentioned cognitive relationship is one-way, that is, a understanding B and B know a is independent of each other, only give a know B can not think B know a, for example, I know you, you do not necessarily know me. Moreover, the cognitive relationship here is not transitive, that is, a understanding of b,b C, but this does not mean that a know C. At the same time, for ease of handling, the list of n individuals in the town is numbered 1 to N. Can you find the right mayor as quickly as GUI?
Input Description:
First a positive integer T (t≤20) that represents the number of data groups. Then the first line of each group of data has 2 integers n and M (1≤n≤105, 0≤m≤3x105), which in turn represent the number of people in the town and the relationship between each other. After M-line, line I lines two numbers AI and bi (1≤ai, Bi≤n ), which represents AI understanding Bi. (Ensure that there is no duplication of understanding, but there may be a cognitive relationship of their own) to ensure that 80% of all data meets n≤1000,m≤10000
Output Description:
A total of 2T rows, each set of data corresponds to 2 rows. The first line, an integer, indicates the appropriate number of mayor candidates you are looking for num I. The second line, num i integers, is separated by a space in the middle of every two digits, indicating the number of the appropriate mayor you have chosen. In particular, if the proper mayor is not found, the first line outputs a number of 0, and the second line is left blank (see example).
Input Example:
32 03 21 23 24 51 12 13 14 13 3
Output Example:
01211
Thought: Looking for a node with a degree of n-1 and a degree of 0
The code is as follows:
Import Java.util.scanner;public class main{public static void Main (String args[]) {Scanner sc= new Scanner (Syst em.in); int T=sc.nextint (); if (t==1) {System.out.println (1+ "\ n" + 1); } for (int i=0;i<t;i++) {int n=sc.nextint (); int M=sc.nextint (); if (m<n-1) {System.out.println (0+ "\ n"); Continue }else{int []degree=new int[n]; for (int j=0;j<m;j++) {int a=sc.nextint (); int B=sc.nextint (); if (a!=b) {degree[a-1]--; degree[b-1]++; }} int k=0; for (; k<n;k++) {if (degree[k]==n-1) break; } if (k<n) {System.out.println (1+ "\ n" + (k+1)); }else{ System.out.println (0+ "\ n"); } } } }}
2016 360 written programming questions 2