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. Kattis - missinggnomesD Missing Gnomes (思路题)

    题目: 题意: 给出已经去除了几个数的一个序列,任务是将去除的数字插回去补全这个序列,输出字典序排在第一的那个补全的序列. 例如: 样例输入: 5 3 1 4 2 样例输出: 1 3 4 2 5 思路 ...

  2. Openstack manila的一些命令

    (本文是测试环境进行的操作:) 1.查看一些信息: [root@openstackcontroller ~]# manila type-list [root@openstackcontroller ~ ...

  3. 虚拟机上CentOS-6.9-x86_64系统安装教程

    最近想学学Linux系统如何使用,于是想用VM安装虚拟机学习一下. linux系统比较多,我这里用的是CentOS-6.9-x86_64 一.下载系统 下载地址:https://www.centos. ...

  4. Thinkphp 批量更新方法 saveALL

    批量更新只适用于一个字段的更新,原理是用自定义函数拼接sql语句,然后再执行sql语句. //数据 $data[] = array('id'=>1,'value'=>value1); $d ...

  5. 工作用linux命令汇总

    mv [filepath] [filepath] 移动,前者位置移动到后面位置,也可以用来重命名(mv test.txt newname.txt) cp [filepath] [filepath] 复 ...

  6. Maven学习总结(30)——Maven项目通用三级版本号说明

     项目版本号说明     当前版本号:1.0.0-SNAPSHOT     本项目采用通用的三级版本号,版本号格式是[主版本号].[副版本号].[修复版本号]-[稳定状态],如:1.0.0-SNAPS ...

  7. C# 解决EXCEL单元格合并,行高无法自适应问题

    解决方法:根据单元格内容长度,设置单元格所在行的行高 public static float getExcelCellAutoHeight(string strtest, float fontCoun ...

  8. Extended symmetrical multiprocessor architecture

    An architecture for an extended multiprocessor (XMP) computer system is provided. The XMP computer s ...

  9. android从sdcard中读取bitmap

    String sdcard_path=Environment.getExternalStorageDirectory().getCanonicalPath(); String file_path=sd ...

  10. hdu_1024_糖果大战_201404021640

    糖果大战 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...