Kingdom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 142    Accepted Submission(s): 84
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
 
Source
 

Mean:

给你一个有n个城市(结点)有向图,现在要发展这些城市,每两个城市之间有且只有一条单向边。
发展城市必须要满足一下条件:
当发展城市w的时候,其他已经发展的城市到w的距离必须小于等于2。
现在要你输出发展的顺序。

analyse:

题目说每两个点之间至少有一条边,所以说数据保证了给的图一定是竞赛图。
由此可知,不能构造的情况是不存在的,也就是说可不能输出-1。
我们只需要对这个图进行一个逆拓扑排序,然后输出就可。

Time complexity:O(n^2)

Source code:

//Memory   Time
// 1347K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
char Map[500][500];
int degree[500];
bool vis[500];
int ans[500];
int main ()
{
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
int n,i,j,k,l;
while(cin>>n,n)
{
getchar();
for(i=0;i<n;++i)
{
gets(Map[i]);
degree[i]=0;
vis[i]=0; }
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(Map[i][j]=='1')
degree[j]++;
int vmax,key,idx=-1;
for(int i=0;i<n;i++)
{
vmax=-1,key=-1;
for(int j=0;j<n;j++)
if(!vis[j]&&degree[j]>vmax)
vmax=degree[j],key=j;
vis[key]=1;
ans[++idx]=key;
for(int i=0;i<n;i++)
if(Map[key][i]=='1')
degree[i]--;
}
for(i=n-1;i>=0;--i)
if(i==n-1)
printf("%d",ans[i]+1);
else
printf(" %d",ans[i]+1);
puts("");
}
return 0;
}

  

没想到这题数据这么水,直接对入度从大到小排序,然后输出就可:

//Memory   Time
// 1347K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
char Map[500][500];
struct Node
{
int degree;
int index;
};
Node node[500];
bool cmp(Node a,Node b)
{
return a.degree>b.degree;
}
int main ()
{
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
int n,i,j,k,l;
while(cin>>n,n)
{
getchar();
for(i=0;i<n;++i)
{
gets(Map[i]);
node[i].degree=0;
node[i].index=i; }
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(Map[i][j]=='1')
node[j].degree++;
sort(node,node+n,cmp);
for(i=n-1;i>=0;--i)
if(i==n-1)
printf("%d",node[i].index+1);
else
printf(" %d",node[i].index+1);
puts("");
}
return 0;
}

  

拓扑排序 --- hdu 4948 : Kingdom的更多相关文章

  1. 拓扑排序 - hdu 1285(普通和优先队列优化)

    2017-09-12 19:50:58 writer:pprp 最近刚开始接触拓扑排序,拓扑排序适用于:无圈图的顶点的一种排序, 用来解决有优先级别的排序问题,比如课程先修后修,排名等. 主要实现:用 ...

  2. hdu 4948 Kingdom(推论)

    hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...

  3. 拓扑排序 HDU - 5695

    众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到NN,在排好队之 ...

  4. HDU 4857 逃生 (反向拓扑排序 & 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  5. ACM: HDU 1285 确定比赛名次 - 拓扑排序

     HDU 1285 确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  6. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  7. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  8. hdu 5098 双队列拓扑排序

    http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...

  9. HDU 5638 拓扑排序+优先队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...

随机推荐

  1. 【腾讯Bugly干货分享】腾讯验证码的十二年

    本文来自于腾讯bugly开发者社区,未经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/581301b146dfb1456904df8d Dev Club 是一个交流移动 ...

  2. 使用SQLIO测试磁盘性能

    SQLIO 是一个用于测试存储系统能力的命令行工具,用以获取存储系统相关的性能指标,以判断系统的 I/O 处理能力. 在微软的网站可以下载 SQLIO 的安装包,安装后目录中会出现如下文件: EULA ...

  3. 作业3.2:psp

    PSP2.1 Personal Software Process Stages Time Planning 计划 20min Estimate 估计这个任务需要多长时间 3.5h Developmen ...

  4. html5 postMessage解决跨域、跨窗口消息传递

    一些麻烦事儿 平时做web开发的时候关于消息传递,除了客户端与服务器传值还有几个经常会遇到的问题 1.页面和其打开的新窗口的数据传递 2.多窗口之间消息传递 3.页面与嵌套的iframe消息传递 4. ...

  5. Android中pullToRefresh使用

    pullToRefresh的导入 首先,点击new按钮 -> import Module 然后在 New Module界面选择已经在本地的含有源代码的pullToRefresh. 打开如下图所示 ...

  6. Node.js与Sails~Model和ORM的持久化

    回到目录 上一讲说了在sails里定义model及相关参数的说明,这一讲主要说一下如何将你的Model持久化到文件,关系数据库和Nosql数据库里,在持久化这点上,sails是统一管理的,它可以在/c ...

  7. github无法访问?试试修改hosts

    github国内无法访问时,可以试试如下修改hosts,亲测有效: 204.232.175.78 http://documentcloud.github.com 207.97.227.239 http ...

  8. HTML 5.1的新增特性

    相对于HTML5,HTML 5.1又带来以下改变,这些可以作为以后的参考. 新增的特性 <picture>和scrset属性允许响应式图片选择: <details>和<s ...

  9. 为什么获取的System.Web.HttpContext.Current值为null,HttpContext对象为null时如何获取程序(站点)的根目录

    ASP.NET提供了静态属性System.Web.HttpContext.Current,因此获取HttpContext对象就非常方便了.也正是因为这个原因,所以我们经常能见到直接访问System.W ...

  10. CKFinder_AspDotNet_2.4 破解方法 去版权

    CKFinder是一个比较好用的Web端文件管理器,虽然UI不是很好看,但是因为能搞到源码,所以比起网上那些只有付费之后才能下载到源码的Web端文件管理器要好许多,至少你可以在确定该控件是否能用在你的 ...