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

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为当前最大顶点矛盾,所以这个贪心思想不会有错了,哎,这题真的不想多说什么,只是感叹贪心的题目都是无上

限的啊,想到就秒杀,想不到就= =!了

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; int n; struct node{ int v; int d; }; node vex[510]; char s[510]; bool cmp(node a,node b) { return a.d 
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值