poj3687反向建图+拓扑排序
来源:互联网 发布:常用的特征选择算法 编辑:程序博客网 时间:2024/05/18 03:21
//首先,对于是否能构造出这样的序列,使用拓扑排序即可解决
//其次,对于答案序列的字典序尽可能小,可以采用反向建图,让先出队列的元素尽可能大且标号大,则问题解决
#include<iostream>#include<cstdio>#include<queue>#include<algorithm>#include<cstring>#include<vector>using namespace std;const int N=210;int n,m;int into[N];int ans[N];vector<int>V[N];priority_queue<int>q;int main(){int T;cin>>T;while(T--){cin>>n>>m;memset(into,0,sizeof(into));for(int i=0;i<=n;++i)V[i].clear();for(int i=1;i<=m;++i){int a,b;scanf("%d%d",&a,&b);into[a]++;V[b].push_back(a);}int size=q.size();while(size--)q.pop();for(int i=1;i<=n;++i){if(into[i])continue;q.push(i);}int num=n+1;//cout<<"T="<<T<<endl;while(!q.empty()){int cur=q.top(); q.pop();ans[cur]=--num;//cout<<"cur="<<cur<<endl;for(int i=0;i<(int)V[cur].size();++i){int v=V[cur][i];into[v]--;if(into[v])continue;q.push(v);}}if(num!=1){printf("-1\n");}else if(num==1){for(int i = 1; i < n; ++i) printf("%d ",ans[i]);printf("%d\n",ans[n]);}}return 0;}
0 0
- poj3687反向建图+拓扑排序
- poj3687 反向建图拓扑排序
- POJ3687-反向拓扑排序
- Labeling Balls POJ3687 【拓扑排序反向建边】【邻接表】
- poj3687 Labeling Balls (反向拓扑排序)
- poj3687 图论 拓扑排序
- poj3687 拓扑排序
- 拓扑排序变形poj3687
- poj3687【拓扑排序】
- POJ3687拓扑排序+贪心
- poj3687 Labeling Balls 拓扑排序
- [poj3687]拓扑排序的应用
- poj3687 Labeling Balls【拓扑排序】
- poj 3687 拓扑排序 - 反向建图
- HDU2647(拓扑排序+反向建图)
- poj 3687 拓扑排序 反向建图
- poj 3687 反向建图 拓扑排序
- hdu4857 逃生【反向建图+拓扑排序】
- 无线路由的参数
- 调用系统dialog
- 如何才算是真正能从事企业级的项目开发
- 垂直领域分布式爬虫
- 前端素材集锦
- poj3687反向建图+拓扑排序
- 第二周——(FPGA坤宏)
- AfxMessageBox和MessageBox区别
- 如何留住有能力、负责任又不提要求的员工?——为他的潜力付钱
- 润乾——五种常用web服务器jvm参数设置
- Android重写view时onAttachedToWindow () 和 onDetachedFromWindow ()
- struts的常量配置
- php内置Mysql函数(2)
- C中的 access()函数