您的位置:首页 > 其它

2014多校8(1004)hdu4948(图论+贪心)

2014-08-15 21:59 120 查看


Kingdom

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 199 Accepted Submission(s): 108

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


题意:给你一副图,没对顶点之间有且仅有一条单向的边,问你可不可以每次找出一个点,使得之前找过的顶点与它的距离不超过2,如果存在则输出点的顺序

思路:因为题目保证输入的图一定是每对顶点之间有且仅有一条单向的边,所以答案是一定存在的,做法只需要将所有顶点的入度记录下来,然后让入度从小到大的顺序输出

点的顺序即可

为什么是这样的呢,其实可以反过来想,可以倒着找一个顶点,使得当前所有的顶点到它的距离不超过2,这样等价于优先找入度最大的顶点,假设我当前找到了入度最大的顶

点u,如果存在一个离它距离大于2的顶点v,那么所有指向u的顶点以及u本身都会指向v,如果不指向v,那么一定是v指向这些顶点,那么v到u的距离就等于2了,就矛盾了,如

果都指向v,那么当前入度最大的顶点就不是u而是v了,这与u为当前最大顶点矛盾,所以这个贪心思想不会有错了,哎,这题真的不想多说什么,只是感叹贪心的题目都是无上

限的啊,想到就秒杀,想不到就= =!了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: