Reward(toposort)
http://acm.hdu.edu.cn/showproblem.php?pid=2647
#include <stdio.h>
#include <string.h>
#include <queue>
#define LL long long
using namespace std;
int head[],cnt=;
int val[];
int in[];
int n,m,num;
queue<int>q;
struct node
{
int u,v;
int next;
} edge[];
void add(int u,int v)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void init()
{
cnt = ,num = ;
while(!q.empty()) q.pop();
memset(in,,sizeof(in));
memset(head,-,sizeof(head));
}
void toposort()
{
for (int i = ; i <= n; i++)
{
if (in[i]==)
{
q.push(i);
val[i]=;
}
}
while(!q.empty())
{
int u = q.front();
q.pop();
num++;
for (int j = head[u]; j!=-; j=edge[j].next)
{
int v = edge[j].v;
val[v] = val[v] <= val[u]+? val[u]+:val[u];
if (val[v] <= val[u])
val[v] = val[u]+;
in[v]--;
if(!in[v])
q.push(v);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
int u,v;
for (int i = ; i < m; i++)
{
scanf("%d%d",&u,&v);
add(v,u);
in[u]++;
}
toposort();
if(num < n)
{
puts("-1");
continue;
}
LL ans = ;
for (int i = ; i <= n; i++)
ans += val[i];
printf("%lld\n",ans);
}
return ;
}
Reward(toposort)的更多相关文章
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- hdu 2647 Reward(拓扑排序,反着来)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- HDU 2647:Reward(拓扑排序+队列)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Reward HDU - 2647
传送门 Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- poj 1270(toposort)
http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...
- 拓扑排序 POJ2367Genealogical tree[topo-sort]
---恢复内容开始--- Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4875 A ...
- POJ 1094 (TopoSort)
http://poj.org/problem?id=1094 题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
随机推荐
- swift--字符串替换/过滤/切割
//替换 var ReplaceString = "http://www.aimonkey.cn"; var FilterReplace = ReplaceString.strin ...
- 洛谷——P2054 [AHOI2005]洗牌(扩展欧几里得,逆元)
P2054 [AHOI2005]洗牌 扩展欧拉定理求逆元 $1 2 3 4 5 6$$4 1 5 2 6 3$$2 4 6 1 3 5$$1 2 3 4 5 6$ 手推一下样例,你就会发现是有规律的: ...
- 洛谷 2966 2966 [USACO09DEC]牛收费路径Cow Toll Paths
[题意概述] 给出一个图,点有正点权,边有正边权,通过两点的代价为两点间的最短路加上路径通过的点的点权最大值. 有M个询问,每次询问通过两点的代价. [题解] 先把点按照点权从小到大排序,然后按照这个 ...
- vue api
1. vue.config.silent = true 取消 Vue 所有的日志与警告. 2.vue.config.productionTip= false 设置为 false 以阻止 vue 在启动 ...
- Java基础学习总结(82)——Java泛型实例教程
1.为什么需要泛型 泛型在Java中有很重要的地位,网上很多文章罗列各种理论,不便于理解,本篇将立足于代码介绍.总结了关于泛型的知识.希望能给你带来一些帮助. 先看下面的代码: List list = ...
- 数据库——mysql如何获取当前时间---https://www.cnblogs.com/Chenshuai7/p/5136469.html
数据库——mysql如何获取当前时间 1.1 获得当前日期+时间(date + time)函数:now() -------https://www.cnblogs.com/Chenshuai7/p/51 ...
- 通过混合编程分析的方法和机器学习预测Web应用程序的漏洞
通过混合编程分析的方法和机器学习预测Web应用程序的漏洞 由于时间和资源的限制,web软件工程师需要支持识别出有漏洞的代码.一个实用的方法用来预测漏洞代码可以提高他们安全审计的工作效率.在这篇文章中, ...
- 如何绘制caffe网络训练曲线
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51774966 当我们设计好网络结构后, ...
- HDU 1203 背包问题
题目大意: 根据学校的申请费用,根据已有的钱得到最大的offer率 这里很明显就是一个价值为概率的背包问题 计算两个offer合并的概率 为a + b - a*b #include <cstdi ...
- Codeforces Round #234 (Div. 2)
A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...