Description
There are n children in a country marked by integers from 1 to n.
They often fight with each other. For each pair of child A and child B, either A beats B or B beats A. It is not possible that both A beats B and B beats A. Of course, one never fight with himself.
Child A is not afraid of child B if A can beat B.
Child A is not afraid of child B if A can beat C and C can beat B either. Because A can say "I will call C to beat you" to B.
A child is called a king of children if he is not afraid of any other child.
Give you the beating relations.Find a king.
Input
The first line contains a integer n which is between 1 and 1000. The following n lines contains n characters respectively. Character is either '0' or '1'. The Bth character of (A+1)th line will be '1' if and only if A can beat B. Input is terminated by EOF.
Output
A number representing a king of children on a line. If such a king does not exist, output -1. If there are multiple kings, any one is accepted.
Sample Input
2 01 00
Sample Output
1
给跪了,这个题目,一开始就知道了找出最大度的点。但频频WA,蛋疼。然后用DFS,Warshall都试了。甚至出现TLE,结果排了一个小时错,发现是index没有初始化就一直WA,我真心给跪了。。。。这个我觉得真没什么意义。但处于规范,还是加下吧。。。。。。
代码如下:
#include <iostream> #include <string> using namespace std; int main() { int N; string King; int i,j; int out; while(cin >> N) { int max = 0; int index = 0; //这里没有初始化就一直WA for(i = 0;i < N;i++) { cin >> King; out = 0; for(j = 0;j < N;j++) { if(King[j] == '1') out++; } if(max < out) { max = out; index = i; } } cout << index + 1 << endl; } return 0; }