poj 3687 Labeling Balls

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

5

4 0

4 1
1 1

4 2
1 2
2 1

4 1
2 1

4 1
3 2

Sample Output

1 2 3 4
-1
-1
2 1 3 4
1 3 2 4

题意::N个标签分别是1,2,3......N。N个小球,质量分别是1,2,3.....N。现在给球贴标签,有M个限制条件。其中每个限制条件为贴标签a的球要比贴标签b的球轻。
依次输出标签1的球的质量,标签2的球的质量.........且字典序最小。

题解:如果没有字典序最小的限制条件,那么标签a比标签b轻,则建一条边从a到b。如果图存在环在,则无解。如果图没环,直接进行拓扑排序(正向),将每次选出的点贴给当前最小的质量的
球,
即可求得一组解。
题目要求让字典序最小,刚开始想的是正向建图,然后每次选取一个最小的质量用队列进行拓扑排序,可是这样是错误的,这样只能保证在每一步(当前情况,题目要求得是结果)的选择中使当前入度为0并且编号最小的球重量最小,
并不能保证得到的结果中编号最小的球重量最轻。后来采用了反向建图,在拓扑排序中每一步选择当前最大的编号给他分配当前最大的重量(即此时入度为零的球),这样可以保证得到的结果f
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cstdlib>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <limits>
10 #define MAX 205
11 
12 using namespace std;
13 int G[MAX][MAX];
14 int in[MAX];
15 int data[MAX];
16 int main()
17 {
18     int t;
19     scanf("%d",&t);
20     while(t--)
21     {
22         // getchar();
23         int a,b,m,n;
24         scanf("%d%d",&n,&m);
25         memset(G,0,sizeof(G));
26         memset(in,0,sizeof(in));
27         while(m--)
28         {
29             scanf("%d%d",&a,&b);
30             if(!G[b][a])//重复边
31             {
32                 G[b][a]=1;
33                 in[a]++;
34             }
35         }
36         int k=0;
37         for(int i=n; i>=1; i--)
38         {
39             int flag=1,s;
40             for(int j=n; j>=1; j--)
41                 if(in[j]==0)
42                 {
43                     data[j]=i;//i为最外层的循环,将当前度为零的即最大的质量放在最大编号球
44                     in[j]--;
45                     flag=0;
46                     k++;
47                     s=j;
48                     break;
49                 }
50             if(flag)
51             {
52                 k=0;
53                 break;
54             }
55             for(int j=n; j>=1; j--)
56                 if(G[s][j])
57                     in[j]--;
58         }
59         if(k!=n)
60         {
61             printf("-1");   printf("\n");
62         }
63         else
64             for(int i=1;i<=n;i++)
65                 printf("%d%c",data[i],i==n?'\n':' ');
66 
67     }
68     return 0;
69 }
View Code

 

全部评论

相关推荐

2025-12-17 11:44
吉首大学 平台产品
黑着眼圈看手机:pdd秋招笔试挂了,春招还行吗
点赞 评论 收藏
分享
老粉都知道小猪猪我很久没更新了,因为秋招非常非常不顺利,emo了三个月了,接下来说一下我的情况吧本人是双非本&nbsp;专业是完全不着计算机边的非科班,比较有优势的是有两段大厂实习,美团和字节。秋招面了50+场泡池子泡死的:滴滴&nbsp;快手&nbsp;去哪儿&nbsp;小鹏汽车&nbsp;不知名的一两个小厂其中字节13场&nbsp;两次3面挂&nbsp;两次2面挂&nbsp;一次一面挂其中有2场面试题没写出来,其他的都是全a,但该挂还是挂,第三次三面才面进去字节,秋招加暑期总共面了22次字节,在字节的面评可以出成书了快手面了8场,2次实习的,通过了但没去,一次2面挂&nbsp;最后一次到录用评估&nbsp;至今无消息滴滴三面完&nbsp;没几天挂了&nbsp;所有技术面找不出2个问题是我回答不上来的,三面还来说我去过字节,应该不会考虑滴滴吧,直接给我干傻了去哪儿一天速通&nbsp;至今无消息小鹏汽车hr&nbsp;至今无消息美团2面挂&nbsp;然后不捞我了,三个志愿全部结束,估计被卡学历了虾皮二面挂&nbsp;这个是我菜,面试官太牛逼了拼多多二面挂&nbsp;3道题也全写了&nbsp;也没问题是回答不出来的&nbsp;泡一周后挂腾讯面了5次&nbsp;一次2面挂&nbsp;三次一面挂,我宣布腾讯是世界上最难进的互联网公司然后还有一些零零散散的中小厂,但是数量比较少,约面大多数都是大厂。整体的战况非常惨烈,面试机会少,就算面过了也需要和各路神仙横向对比,很多次我都是那个被比下去的人,不过这也正常,毕竟谁会放着一个985的硕士不招,反而去招一个双非读化学的小子感觉现在互联网对学历的要求越来越高了,不仅仅要985还要硕士了,双非几乎没啥生存空间了,我感觉未来几年双非想要进大厂开发的难度应该直线上升了,唯一的打法还是从大二刷实习,然后苟个转正,不然要是去秋招大概率是炮灰。而且就我面字节这么多次,已经开始问很多ai的东西了,你一破本科生要是没实习没科研懂什么ai啊,纯纯白给了
不知名牛友_:爸爸
秋招你被哪家公司挂了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务