pku 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, bN) 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.

然后这题是一个裸的拓扑排序,但是输出有点忽悠人,要求输出标签代表的球的重量,按照标签号排序,而且对于重复情况需要标签ID小的球的质量尽量小。
这里有一个错误折腾死人,如果按照正向拓扑,给标签号小的标签分配给质量小的球是不对的,应为对于拓扑序来说,很多链是平行的,给链头元素最小值的贪心策略并不能保证全局最小,如下图两条平行链,如果给3分配了较小质量的球,1就得不到最小质量的球,导致结果不满组最优,但是如果逆向拓扑,给链头标签最大的分配最重的物体一定能保证正向拓扑序最小
4 2 5
3 1
 1 # include <cstdio>
 2 # include <queue>
 3 # include <vector>
 4 # include <cstring>
 5 using namespace std;
 6 priority_queue<int,vector<int>,less<int> >q;
 7 int g[205],degree[205];
 8 int nxt[50000],v[50000],c,n,m;
 9 int ans[205],num;
10 int main()
11 {
12     int testcase;
13     scanf("%d",&testcase);
14     while(testcase--)
15     {
16         memset(g,-1,sizeof(g));
17         memset(degree,0,sizeof(degree));
18         c=num=0;
19     while(!q.empty()) q.pop();
20         scanf("%d%d",&n,&m);
21         while(m--)
22         {
23             int a,b;
24             scanf("%d%d",&a,&b);
25             v[c]=a;
26             nxt[c]=g[b];
27             g[b]=c++;
28         degree[a]++;
29         }
30         for(int i=1;i<=n;i++)
31             if(!degree[i])
32                 q.push(i);
33     num=n;
34         while(!q.empty())
35         {
36             int top=q.top();
37             q.pop();
38             ans[top]=num--;
39             for(int p=g[top];p!=-1;p=nxt[p])
40             {
41                 degree[v[p]]--;
42                 if(!degree[v[p]])
43                     q.push(v[p]);
44             }
45         }
46         if(num>0)
47             printf("-1\n");
48         else
49         {
50             printf("%d",ans[1]);
51             for(int i=2;i<=n;i++)
52           printf(" %d",ans[i]);
53             printf("\n");
54         }
55     }
56     return 0;
57 }

posted on 2010-10-22 02:24 yzhw 阅读(148) 评论(0)  编辑 收藏 引用 所属分类: graph


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2011年2月>
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜