HDU2647 topsort
#include<bits/stdc++.h>
using namespace std;
vector<int> mp[20005];
int n,m;
int a,b;
int in[20005];
int L[20005];
int flag;
int re;
struct node
{
int sum;
};
queue<node>q;
struct node N,now;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
mp[i].clear();
memset(in,0,sizeof(in));
memset(L,0,sizeof(L));
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
mp[b].push_back(a);
in[a]++;
}
for(int i=1;i<=n;i++)
{
if(in[i]==0)
{
N.sum=i;
q.push(N);
}
}
int jishu=n;
int temp;
while(!q.empty())
{
now=q.front();
q.pop();
jishu--;
temp=L[now.sum];
//cout<<now.sum<<endl;
for(unsigned int i=0;i<mp[now.sum].size();i++)
{
if(--in[mp[now.sum][i]]==0)
{
N.sum=mp[now.sum][i];
L[N.sum]=temp+1;
q.push(N);
}
}
}
re=0;
if(jishu>0)
printf("-1\n");
else
{
for(int i=1;i<=n;i++)
re+=L[i];
printf("%d\n",re+888*n);
}
}
return 0;
}
HDU2647 topsort的更多相关文章
- HDU2647
第一道逆拓扑纪念一下... #include<iostream> #include<cstdio> #include<cstring> #include<cm ...
- 拓扑排序(topsort)
本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...
- POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)
题目大意: 为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的. 每一次Wind 选择两个房间 x 和 y, 让他的儿子从一个房间走到 ...
- poj1094 topsort
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32275 Accepted: 11 ...
- POJ - 3249 Test for Job (DAG+topsort)
Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...
- 拓扑排序 topsort详解
1.定义 对一个有向无环图G进行拓扑排序,是将G中所有顶点排成一个线性序列,通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列. 举例: h3 { marg ...
- poj 3648 2-SAT建图+topsort输出结果
其实2-SAT类型题目的类型比较明确,基本模型差不多是对于n组对称的点,通过给出的限制条件建图连边,然后通过缩点和判断冲突来解决问题.要注意的是在topsort输出结果的时候,缩点后建图需要反向连边, ...
- 经典问题----拓扑排序(HDU2647)
题目简介:有个工厂的老板给工人发奖金,每人基础都是888,工人们有自己的想法,如:a 工人想要比 b 工人的奖金高,老板想要使花的钱最少 那么就可以 给b 888,给a 889 ,但是如果在此基础上, ...
- 拓扑排序基础 hdu1258,hdu2647
由这两题可知拓扑排序是通过“小于”关系加边建图的 hdu2647 /* 拓扑排序的原则是把“小于”看成有向边 此题反向建图即可 并且开num数组来记录每个点的应该得到的权值 */ #include&l ...
随机推荐
- yarn logs -applicationId命令java版本简单实现
import java.io.DataInputStream; import java.io.EOFException; import java.io.FileNotFoundException; i ...
- 告别加载dll 出错开机加载项大揭秘
提到开机加载(load)项,大家不要以为就是系统启动(run)项.最简单的例子是,杀毒软件或者用户手动删除病毒文件后,注册表中的自动加载信息仍在,登陆系统时就会提示"加载*dll出错,系统找 ...
- linux c语言 fork() 和 exec 函数的简介和用法
linux c语言 fork() 和 exec 函数的简介和用法 假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<std ...
- Python3 数据类型-集合
在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 集合 ...
- js如何使浏览器允许脚本异步加载
js如何使浏览器允许脚本异步加载 如果脚本体积很大,下载和执行的时间就会很长,因此造成浏览器堵塞,用户会感觉到浏览器“卡死”了,没有任何响应.这显然是很不好的体验,所以浏览器允许脚本异步加载,下面就是 ...
- sql update limit1
更新限制:为了避免全表更新,错误更新影响太多,加上limit1 多了一层保障.
- android BadgeView的使用(图片上的文字提醒)
BadgeView主要是继承了TextView,所以实际上就是一个TextView,底层放了一个label,可以自定义背景图,自定义背景颜色,是否显示,显示进入的动画效果以及显示的位置等等: 这是Gi ...
- 大型网站架构演化(八)——使用NoSQL和搜索引擎
随着网站业务越来越复杂,对数据存储和检索的需求也越来越复杂,网站需要采用一些非关系数据库技术如NoSQL和非数据库查询技术如搜索引擎,如图. NoSQL和搜索引擎都是源自互联网的技术手段,对可伸缩的分 ...
- VS2012或VS2010 工具栏中无法显示DevExpress控件
进入命令提示符 跳转到Dev控件安装目录,如[目录D:\Program Files (x86)\DevExpress\DXperience 12.2\Tools]下, 然后执行命令: ToolboxC ...
- C# .net 获取外网ip
public string GetIP() { string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri ...