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为当前最大顶点矛盾,所以这个贪心思想不会有错了,哎,这题真的不想多说什么,只是感叹贪心的题目都是无上
限的啊,想到就秒杀,想不到就= =!了
相关文章推荐
- 2014鞍山网络预选赛1004(贪心)hdu5000
- jzoj3929【NOIP2014模拟11.6】创世纪(图论,bfs,贪心)
- HDU 4864 Task (2014多校联合训练第一场1004) 解题报告(贪心)
- HDU 4864 Task(2014多校第一场1004)(贪心)
- BZOJ 3721 PA 2014 Final Bazarek 贪心
- 图论;最小生成树;普利姆算法;贪心策略;可用最小堆实现;
- [NOI2014]起床困难综合症(二进制拆分+贪心)
- 【图论】[NOIP2014]联合权值
- 2014多校5(1002)hdu4912(贪心+LCA)
- Luogu 1351 NOIP 2014 联合权值(贪心,计数原理)
- BZOJ 3668 NOI2014 起床困难综合症 贪心
- 【BZOJ3829】[Poi2014]FarmCraft 树形DP(贪心)
- BZOJ3523 [Poi2014]Bricks 【贪心】
- BZOJ_3671_[Noi2014]随机数生成器_set+贪心
- 【BZOJ3671】[Noi2014]随机数生成器【模拟】【贪心】【卡空间】
- codevs 3311 [NOI2014] 起床困难综合症 贪心
- 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)
- 3709: [PA2014]Bohater (贪心)
- HDU 5045 Contest(概率dp,状态压缩,2014上海网络赛1004)
- bzoj 3725: PA2014 Final Matryca (贪心)