拓扑排序 --- 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条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...
随机推荐
- C#中的线程三 (结合ProgressBar学习Control.BeginInvoke)
C#中的线程三(结合ProgressBar学习Control.BeginInvoke) 本篇继上篇转载的关于Control.BeginInvoke的论述之后,再结合一个实例来说明Cotrol.Begi ...
- Programming Entity Framework CodeFirst--表关系约定
表之间的关系分为一对多,多对多,一对一三种,实质就是对外键进行配置. 一.一对多 1. Required Destination包含Lodging>的集合. public class Desti ...
- (翻译)反射处理java泛型
当我们声明了一个泛型的接口或类,或需要一个子类继承至这个泛型类,而我们又希望利用反射获取这些泛型参数信息.这就是本文将要介绍的ReflectionUtil就是为了解决这类问题的辅助工具类,为java. ...
- js实现DOM结构
/* 编写一段js脚本生成下面的DOM结构.要求使用标准的DOM方法或属性 <div id='example'> <p class='slogan'>淘,你喜欢</p&g ...
- mac命令
mac下卸载nodesudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}xc ...
- IOS 多线程03-GCD
如果在本文之前要了解一下线程的基本知识,请访问下面的网址:http://www.cnblogs.com/alunchen/p/5337608.html 1.简介 GCD不仅适用于Object-C,也适 ...
- AFNetworking+Python+Flask+pyOpenSSL构建iOS HTTPS客户端&服务器端
对于HTTPS我在网上找了一堆资料看了下, 各种协议和证书已经有点晕了 最后我现有的感觉是, 在HTTP服务器上放一个证书, 在原本的HTTP访问之前客户端先检查证书是否正确 如果客户端证书检查正确, ...
- 《Effective Java》—— 创建与销毁对象
本篇主要总结的是<Effecticve Java>中关于创建和销毁对象的内容. 比如: 何时以及如何创建对象 何时以及如何避免创建对象 如何确保及时销毁 如何管理对象销毁前的清理动作 考虑 ...
- CSS滚动条
× 目录 [1]条件 [2]默认 [3]尺寸[4]兼容[5]自定义 前面的话 滚动条在网页中经常见到,却并没有受到足够的重视.只有当因为滚动条的问题需要处理兼容性时,才进行调试操作.本文将就滚动条的常 ...
- 去除IE6浏览器下获得焦点的元素的虚线框的两个小办法
[1]onfocus = "this.blur()"//得到焦点时,失去焦点 e.g. <a href="#" onfocus = "this. ...