POJ 3687 Labeling Balls
来源:互联网 发布:办公软件培训视频 编辑:程序博客网 时间:2024/04/28 00:39
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:
- No two balls share the same label.
- 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
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int mp[211][211]; int wei[211]; int chu[211]; int main() { int t; cin>>t; while(t--) { int n,m; cin>>n>>m; int i,j; memset(mp,0,sizeof(mp)); memset(chu,0,sizeof(chu)); memset(wei,0,sizeof(wei)); for(i=1;i<=m;i++) { int a,b; cin>>a>>b; if(mp[a][b]==0) { mp[a][b]=1; chu[a]++; } } int t=n; while(1) { for(i=n;i>=1;i--) if(wei[i]==0&&chu[i]==0) break; if(i==0) break; wei[i]=t--; for(j=1;j<=n;j++) if(mp[j][i]==1) chu[j]--; } if(t==0) { for(i=1;i<=n;i++) { if(i>1) cout<<" "; cout<<wei[i]; } cout<<endl; } else cout<<"-1"<<endl; } return 0; }
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj-3687-Labeling Balls
- POJ 3687 Labeling Balls
- POJ 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ【3687】Labeling Balls
- 数学基础 —— 向量基础(二)
- hdoj1863畅通工程(最小生成树模板题)
- 网易面试
- 安卓初学者之一个简单的记事本
- 【HNOI2012】bzoj2730 矿场搭建 【解法一】
- POJ 3687 Labeling Balls
- android:ellipsize属性运用
- H5学习之20 CSS-table
- Android CustomShapeImageView对图片进行各种样式裁剪:圆形、星形、心形、花瓣形等
- 动态规划经典5题
- Python黑帽编程2.2 数值类型
- Java异常处理
- 杭电1233
- CodeForces 687C The Values You Can Make