拓扑排序 --- 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条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...
随机推荐
- angular.js 例子
angular.js是一个前端的MVC框架,12年的时候曾近在一个portal平台的项目中使用过. 下面给出一个angular.js的典型例子,涵盖一些基础的知识点,用以复习备忘: <html ...
- 自己写一个java.lang.reflect.Proxy代理的实现
前言 Java设计模式9:代理模式一文中,讲到了动态代理,动态代理里面用到了一个类就是java.lang.reflect.Proxy,这个类是根据代理内容为传入的接口生成代理用的.本文就自己写一个Pr ...
- 示例篇-购物车的简单示例和自定义JS
简介: 支持平台: Android4.0,iOS7.0,Windows 10, Windows 10 mobile 说明:主要是演示listview所在的ui和模板cell所在的ui之间数据的交互,点 ...
- 冲刺阶段 day 9
项目进展 昨天终于完成了教师部分的内容,今天我们又重新开始对之前系部设置不能实现的内容进行了编写,之前缺少删除和查询也做了补充,在与数据库的连接上也做了修改和更新. 存在问题 由于是之前遇到困难没做完 ...
- [.net 面向对象程序设计进阶] (2) 正则表达式 (一) 快速入门
[.net 面向对象程序设计进阶] (2) 正则表达式 (一) 快速入门 1. 什么是正则表达式? 1.1 正则表达式概念 正则表达式,又称正则表示法,英文名:Regular Expression(简 ...
- CentOS Linux系统下安装Redis过程和配置参数说明
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/102.html?1455869303 安装过程: 代码如下: wget h ...
- memcache和redis区别
memcache官方定义 Free & open source, high-performance, distributed memory object caching system, gen ...
- Python内建的对象列表
Python内建的对象列表 刚写Python肯定会遇到这样的情况,想写些什么,但又不知从何写起... 在我看来问题在于我们不知道有什么东东可以拿来玩,这里列出Python的内建对象,稍微归类了一下,多 ...
- python+selenium运行报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
使用python+selenium运行自动化脚本时,打印某一段文字出现UnicodeEncodeError: 'ascii' codec can't encode characters in posi ...
- GOF设计模式特烦恼
这段时间,学习状态比较一般,空闲时基本都在打游戏,和研究如何打好游戏,终于通过戏命师烬制霸LOL,玩笑了.为了和"学习"之间的友谊小船不翻,决定对以往学习过的GOF设计模式做一个简 ...