Kingdom
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 500 Accepted Submission(s): 231
Special Judge
Problem Description
Teacher Mai has a kingdom consisting of n cities. He has planned the transportation of the kingdom. Every pair of cities has exactly a one-way road.
He wants develop this kingdom from one city to one city.
Teacher Mai now is considering developing the city w. And he hopes that for every city u he has developed, there is a one-way road from u to w, or there are two one-way roads from u to v, and from v to w, where city v has been developed before.
He gives you the map of the kingdom. Hope you can give a proper order to develop this kingdom.
Input
There are multiple test cases, terminated by a line "0".
For each test case, the first line contains an integer n (1<=n<=500).
The following are n lines, the i-th line contains a string consisting of n characters. If the j-th characters is 1, there is a one-way road from city i to city j.
Cities are labelled from 1.
Output
If there is no solution just output "-1". Otherwise output n integers representing the order to develop this kingdom.
Sample Input
3
011
001
000
0
Sample Output
1 2 3
Author
xudyh
Source
2014 Multi-University Training Contest 8
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> #include<set> #include<map> using namespace std; int n,mp[505][505]; struct node { int x,cnt; }e[505]; bool cmp(node a,node b) { return a.cnt<b.cnt; } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%1d",&mp[i][j]); } e[i].x=i; e[i].cnt=0; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(mp[i][j]) { e[j].cnt++; } } } sort(e,e+n,cmp); printf("%d",e[0].x+1); for(int i=1;i<n;i++) { printf(" %d",e[i].x+1); } printf("\n"); } return 0; }