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)的更多相关文章

  1. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  2. hdu 2647 Reward(拓扑排序,反着来)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  3. HDU 2647:Reward(拓扑排序+队列)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. Reward HDU - 2647

    传送门     Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...

  5. hdu 2647 Reward(拓扑排序+优先队列)

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  6. poj 1270(toposort)

    http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...

  7. 拓扑排序 POJ2367Genealogical tree[topo-sort]

    ---恢复内容开始--- Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4875   A ...

  8. POJ 1094 (TopoSort)

    http://poj.org/problem?id=1094 题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序 ...

  9. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

随机推荐

  1. [Algorithm] 7. Serialize and Deserialize Binary Tree

    Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing t ...

  2. Django DTL模板语法中定义变量

  3. Python运算符(Python学习笔记03)

  4. 文件处理: read、readline、 readlines()

    假设a.txt的内容如下所示: Hello Welcome What is the fuck.. 1. read([size])方法 read([size])方法:从文件当前位置起读取size个字节, ...

  5. jquery ajax报Uncaught TypeError :Illegal invocation

    使用jquery ajax异步提交的时候报Uncaught TypeError :Illegal invocation错误,报错信息如图: 上网查了一下jquery的这个错误,导致这个错误的原因有俩点 ...

  6. 170611 NOIP模拟赛

    第一题没做出来不应该: 第二题不难想,就是写起来很麻烦: 第三题因为学了挺久的splay就直接写的splay,没太在意常数问题,一般情况下,第k值问题主席树是比splay稍快的: 盘子序列 [题目描述 ...

  7. vim下多行注释与解注释

    1.多行注释 (1)按esc进入命令行模式 (2)按下Ctrl+v,进入区块模式,并使用上下键选择需要注释的多行 (3)按下“I”(大写)键,进入插入模式 (4)输入注释符(“//”或“#”等) (5 ...

  8. Windows 10+Ubuntu 16.04在MBR分区上安装双系统(转)

    以下内容转自这篇博客: http://www.cnblogs.com/Duane/p/5424218.html http://www.cnblogs.com/Duane/p/6776302.html( ...

  9. FreeMarker与Servlet结合示例

    一.最原始示例 1.引入POM依赖 <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <de ...

  10. Visual Studio 中的 .NET Framework 类库

    Visual Studio 中的 .NET Framework 类库 .NET Framework 类库由命名空间组成.每个命名空间都包含可在程序中使用的类型:类.结构.枚举.委托和接口. 当您在 V ...