POJ 3687 Labeling Balls
2016-08-03 21:36
483 查看
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:
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
Sample Output
#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;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13433 | Accepted: 3879 |
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
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
#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
- Labeling Balls(POJ--3687
- POJ-3687 Labeling Balls
- poj 3687 Labeling Balls 逆向拓扑
- [ACM] 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 (拓扑排序)