hdu 2647拓扑排序 容器
#include<stdio.h>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
#define N 11000
int n,m,sum;
int indegree[N],value[N];
vector<int>now[N];
int Max(int a,int b) {
return a>b?a:b;
}
int find() {
queue<int>q;
int i,cur,f;
for(i=1;i<=n;i++)
if(indegree[i]==0) {
value[i]=888;
q.push(i);
}
f=n;
while(!q.empty()) {
cur=q.front();
q.pop();
f--;
for(i=0;i<now[cur].size();i++) {
value[now[cur][i]]=Max(value[cur]+1,value[now[cur][i]]);
if(--indegree[now[cur][i]]==0)
q.push(now[cur][i]);
}
}
if(f==0)
return 0;
return 1;
}
int main() {
int i,a,b;
while(scanf("%d%d",&n,&m)!=EOF) {
for(i=1;i<=n;i++)
now[i].clear();
memset(indegree,0,sizeof(indegree));
memset(value,0,sizeof(value));
while(m--) {
scanf("%d%d",&a,&b);
indegree[a]++;
now[b].push_back(a);
}
if(find()) {
printf("-1\n");
continue;
}
sum=0;
for(i=1;i<=n;i++)
sum+=value[i];
printf("%d\n",sum);
}
return 0;
}
hdu 2647拓扑排序 容器的更多相关文章
- hdu 2647 (拓扑排序 邻接表建图的模板) Reward
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...
- hdu 2647拓扑排序 结构体模拟容器
#include<stdio.h> #include<queue> #include<iostream> using namespace std; #define ...
- HDU 2647 拓扑排序
题意:每个人的工资至少888,然后有m个条件,前者比后者要多.求最少工资. 分析: 最开始的开邻接矩阵的肯定超时,如果dfs,会出现由于刚开始不是从入度为0的点出发,后期修改不了.比较麻烦. 正确方式 ...
- HDU 4857 拓扑排序 优先队列
n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu 1811拓扑排序+并查集(容器实现)
http://www.cnblogs.com/newpanderking/archive/2012/10/18/2729566.html #include<stdio.h> #includ ...
- HDU 5638 拓扑排序+优先队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...
- 传递 hdu 5961 拓扑排序有无环~
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5961 题目为中文,这里就不描述题意了. 思路: 从题目陈述来看,他将一个有向图用一个邻接矩阵来表示,并且分 ...
随机推荐
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】
凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...
- JavaScript--DOM删除节点removeChild()
删除节点removeChild() removeChild() 方法从子节点列表中删除某个节点.如删除成功,此方法可返回被删除的节点,如失败,则返回 NULL. 语法: nodeObject.remo ...
- 【js】callback时代的变更
最近团队开始越来越多的使用es7标准的async/await,从最开始的promise到后面的generator,再到现在async,对于异步,每个时期都有着其特有的解决方案,今天笔者就以自己的接触为 ...
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- 在Azure Ubunt Server 14.04虚机中使用Deep-Visualization-Toolbox
参考网站 a) https://zhuanlan.zhihu.com/p/24833574?utm_source=tuicool&utm_medium=referral b) ht ...
- CF540C Ice Cave
思路: 搜索. 加回溯会超时,不加回溯就过了,不是很理解. 实现: #include <iostream> #include <cstdio> using namespace ...
- iOS设计模式——Category和 Extension
什么是Category Category模式用于向已经存在的类添加方法从而达到扩展已有类的目的,在很多情形下Category也是比创建子类更优的选择.新添加的方法同样也会被被扩展的类的所有子类自动继承 ...
- BZOJ 1176: [Balkan2007]Mokia KDtree
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin), ...
- Spring Boot 与任务
一.任务 1.异步任务 package com.yunche.task.service; import org.springframework.stereotype.Service; /** * @C ...
- Luogu P2052 [NOI2011]道路修建
吐槽一下 我开了\(-O2\)优化结果跑的更慢了什么鬼???!!! 我怕不是吸了一口毒氧气 不要脸的放上我的博客,欢迎大家前来面基 题目大意 给定一棵有\(n\)个节点的树,树中有\({n-1}\)条 ...