拓扑排序 --- hdu 4948 : Kingdom
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
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.
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.
011
001
000
0
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]&°ree[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的更多相关文章
- 拓扑排序 - hdu 1285(普通和优先队列优化)
2017-09-12 19:50:58 writer:pprp 最近刚开始接触拓扑排序,拓扑排序适用于:无圈图的顶点的一种排序, 用来解决有优先级别的排序问题,比如课程先修后修,排名等. 主要实现:用 ...
- hdu 4948 Kingdom(推论)
hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...
- 拓扑排序 HDU - 5695
众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到NN,在排好队之 ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- ACM: HDU 1285 确定比赛名次 - 拓扑排序
HDU 1285 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu 5098 双队列拓扑排序
http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...
- HDU 5638 拓扑排序+优先队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...
随机推荐
- Java虚拟机8:虚拟机性能监控与故障处理工具
前言 定位系统问题的时候,知识.经验是基础,数据是依据,工具是运用知识处理数据的手段.这里说的数据包括:运行日志.异常堆栈.GC日志.线程快照.堆转储快照等.经常使用适当的虚拟机监控和分析的工具可以加 ...
- angularjs组件之input mask
今天将奉献一个在在几个angularjs项目中抽离的angular组件 input mask.在我们开发中经常会对用户的输入进行控制,比如日期,货币格式,或者纯数字格式之类的限制,这就是input m ...
- Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock
概要 Java的JUC(java.util.concurrent)包中的锁包括"独占锁"和"共享锁".在“Java多线程系列--“JUC锁”02之 互斥锁Ree ...
- mac下apache配置,解决It is not safe to rely on the system's timezone settings.
之前一直转windows平台下做php,很少遇到问题.现在有了macbook,还在慢慢的熟悉中,搭建php开发环境,熟悉mac系统文档组织还有命令,颇费功夫. 今天我在mac下做一个php的练习,用到 ...
- redis系列-主从复制
redis自身提供了主从的机制,通过配置可以实现服务的备份(Master->Slave). 配置项 slaveof <masterip> <masterport> mas ...
- Hibernate缓存(转)
来自:http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html 一.why(为什么要用Hibernate缓存?) Hibernate是一个 ...
- Vue.js2.0从入门到放弃---入门实例
最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...
- Redis中5种数据结构的使用场景介绍
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/108.html?1455861435 一.redis 数据结构使用场景 原 ...
- Java-数组练习2
2.找出如下数组中最大的元素和最小的元素,a[][]={{3,2,6},{6,8,2,10},{5},{12,3,23}} int[][] i={{3,2,6},{6,8,2,10},{5},{12, ...
- file:///Users/xmg/Desktop/xiangmu~Bsbdejie/BaisibudejieTheSecondtime/BaisibudejieTheSecond/BaisibudejieTheSecond/AppDelegate.m: warning: Missing file: /Users/xmg/Desktop/xiangmu~Bsbdejie/BaisibudejieT
warning: Missing file: is missing from working copy fatal error: file '-.h' has been modified since ...