博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Labeling Balls(拓扑排序wa)
阅读量:7108 次
发布时间:2019-06-28

本文共 2584 字,大约阅读时间需要 8 分钟。

Labeling Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12466   Accepted: 3576

Description

Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:

  1. No two balls share the same label.
  2. The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled with b".

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 next M line each contain two integers a and b indicating the ball labeled with a 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 label N. 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 题解:反向建图,却是wa。。。先放着吧 wa代码:
1 #include
2 #include
3 #include
4 #define mem(x,y) memset(x,y,sizeof(x)) 5 using namespace std; 6 const int MAXN=210; 7 const int MAXM=40010<<1; 8 int head[MAXM]; 9 int que[MAXN],ans[MAXN];10 struct Edge{11 int from,to,next;12 }; 13 Edge edg[MAXM];14 int edgnum;15 int N;16 void add(int u,int v){17 Edge E={u,v,head[u]};18 edg[edgnum]=E;19 head[u]=edgnum++;20 } 21 void topu(){22 priority_queue
dl;23 int top=1;24 for(int i=1;i<=N;i++)25 if(!que[i])dl.push(i);26 while(!dl.empty()){27 int k=dl.top();28 dl.pop();29 ans[top++]=k;30 que[k]=-1;31 for(int i=head[k];i!=-1;i=edg[i].next){32 int v=edg[i].to;33 que[v]--;34 if(!que[v])dl.push(v);35 }36 }37 if(top!=N+1)puts("-1");38 else{39 for(int i=N;i>=1;i--){40 if(i!=N)printf(" ");41 printf("%d",ans[i]);42 }43 puts("");44 }45 }46 int main(){47 int T,M,a,b;48 scanf("%d",&T);49 while(T--){50 scanf("%d%d",&N,&M);51 for(int i=1;i<=N;i++)ans[i]=i;52 mem(que,0);53 mem(head,-1);54 edgnum=0;55 while(M--){56 scanf("%d%d",&a,&b);57 add(b,a);//58 que[a]++;//反向建图 59 }60 topu();61 }62 return 0;63 }

 

转载地址:http://zvlhl.baihongyu.com/

你可能感兴趣的文章
WPF 关于圆角的制作
查看>>
MySql(十四):MySql架构设计——可扩展性设计之数据切分
查看>>
Ocelot简易教程(二)之快速开始1
查看>>
思绪:常想一二
查看>>
Flash调用XML文件的方法
查看>>
JSF---->其他标签
查看>>
Add Console Application Program to the MFC Program
查看>>
Oracle中可被并行化执行的SQL操作
查看>>
新的Layout布局系统
查看>>
java链表
查看>>
VC获取操作系统版本和名称
查看>>
禁止复制
查看>>
使用GLSL实现更多数量的局部光照 【转】
查看>>
rundll32命令大全
查看>>
OC 内存管理-02 autorelease 概念 以及用法
查看>>
IPC——匿名管道
查看>>
AsyncSocket长连接棒包装问题解决
查看>>
ios调用dismissViewController的一个小陷阱
查看>>
[Android Pro] static 和 Volatile 的区别
查看>>
深入理解PHP内核(八)变量及数据类型-预定义变量
查看>>