poj3687Labeling Balls【反向拓扑排序 模板】
来源:互联网 发布:电脑摄像头监控软件 编辑:程序博客网 时间:2024/06/05 04:06
Description
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN in such a way that:
- No two balls share the same label.
- The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled withb".
Can you help windy to find a solution?
Input
The first line of input is the number of test case. The first line of each test case contains two integers,N (1 ≤ N ≤ 200) and M (0 ≤ M ≤ 40,000). The nextM line each contain two integers a and b indicating the ball labeled witha must be lighter than the one labeled with b. (1 ≤ a, b ≤N) There is a blank line before each test case.
Output
For each test case output on a single line the balls' weights from label 1 to labelN. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with the smallest weight for label 3 and so on... If no solution exists, output -1 instead.
Sample Input
54 04 11 14 21 22 14 12 14 13 2
Sample Output
1 2 3 4-1-12 1 3 41 3 2 4
折腾了将近两个小时总算在discuess的只言片语中知道自己错在哪里了,怎么说也是没参考别人代码A的,但是思维严谨性啊……自己写测试数据!自己好好读题!
1.题中有一句特别坑爹的话:For each test case output on a single line the balls' weights from label 1 to labelN. 输出的是这个数字所在的位置 ,不是这个位置上的数字
2.反向输出!这个写几组数据就能看出来吧。但是为什么呢?因为如果只是限制优先级,小的数先输出,有可能得不到正解,反向输出,大的放到后面就解决了这个问题
/****************poj36872016.2.3916K79MSG++1939B****************/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#include<vector>using namespace std;const int maxn=300;int tmp[400];bool mark[205][205];int head[maxn],ip,indegree[maxn];int t,n,m,seq[maxn];struct cmp1{ bool operator ()(int &a,int &b){ return a<b;//最大值优先 }};struct note{ int v,next;} edge[maxn*maxn];void init(){ memset(head,-1,sizeof(head)); ip=0; memset(indegree,0,sizeof(indegree)); memset(mark,0,sizeof(mark));}void addedge(int u,int v){ edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}int topo()///拓扑,可做模板{ priority_queue<int,vector<int>,cmp1>que1; int indeg[maxn]; for(int i=1; i<=n; i++) { indeg[i]=indegree[i]; if(indeg[i]==0) que1.push(i); } int k=n; bool res=false; while(!que1.empty()) { if(que1.size()!=1)res=true; int u=que1.top(); que1.pop(); seq[k--]=u; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; indeg[v]--; if(indeg[v]==0) que1.push(v); } } if(k>0)return -1;///存在有向环,总之不能进行拓扑排序 if(res)return 0;///可以进行拓扑排序,并且只有唯一一种方式,seq数组即是排序完好的序列 return 1;///可以进行拓扑排序,有多种情况,seq数组是其中一种序列}int main(){ 、、freopen("cin.txt","r",stdin); scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); while(m--) { int a,b; scanf("%d%d",&a,&b); if(mark[a][b]) continue; addedge(b,a); indegree[a]++; mark[a][b]=1; } if(topo()==-1) printf("-1\n"); else { for(int i=1;i<=n;i++) tmp[seq[i]]=i; for(int i=1;i<n;i++) printf("%d ",tmp[i]); printf("%d\n",tmp[n]); } } return 0;}
- poj3687Labeling Balls【反向拓扑排序 模板】
- POJ3687Labeling Balls(拓扑排序)
- poj3687Labeling Balls(反向拓扑+优先队列)
- poj3687Labeling Balls(逆序拓扑)
- 3687-Labeling Balls-反向拓扑排序
- POJ 3687 Labeling Balls (反向拓扑排序)
- poj3687 Labeling Balls (反向拓扑排序)
- poj-3687-Labeling Balls-反向建图+拓扑排序
- Labeling Balls 3687(拓扑排序+反向建图)
- POJ 3687 Labeling Balls (反向拓扑排序)
- Labeling Balls POJ3687 【拓扑排序反向建边】【邻接表】
- Labeling Balls poj 3687(拓扑排序反向建图)
- POJ 3687 Labeling Balls(拓扑排序+反向思考)
- Labeling Balls--拓扑排序
- POJ 3684-Labeling Balls(反向拓扑排序-按条件排序输出重量)
- POJ:3687 Labeling Balls (反向建图+拓扑排序+优先队列)
- POJ 3687 Labeling Balls(拓扑排序【反向建图+优先队列】)
- POJ 3687 Labeling Balls 反向拓扑建图是个坑点
- Swift快速入门(一)第一个Swift程序
- java 反射机制构建JSON字符串
- 解析家用净水器漏水的相关知识!
- Tip_压位
- chrome extenstion memo
- poj3687Labeling Balls【反向拓扑排序 模板】
- vi 打开多个文件
- android 时间选择器
- 程序员必须知道的10大基础实用算法及其讲解
- Android中Canvas绘图基础详解(附源码下载)
- python 画宇宙
- Egret引擎与Android端的交互----小白教程(含坑)
- J2EE开发 eclipse中将web工程部署到Tomcat的webApps目录下
- STL--迭代器(iterator)使用详解