Labeling Balls

时间:2023-03-08 23:55:05
Labeling Balls

poj3687:http://poj.org/problem?id=3687

题意:有N个重量1到N的点,把这N个点涂色,要求在一定的约束下颜色a必须比颜色b要轻,如果有多种选择则让重量最小的对应编号1,然后剩下中重量最小的给编号2,一次类推

题解:逆向建图,这样取出来的就是最后选择的点,并标上最大重量,把邻接点入度为0的加入到优先队列中,然后取编号最大的,为的是使得留着编号最小的给重量最大的

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
int edge[maxn][maxn];
int cnt[maxn];
int ans[maxn];
int tot;
int toporder(int n)
{
priority_queue <int> q;
int i,j,k;
for(i=;i<=n;i++)
if(cnt[i] == ) q.push(i);
for(i=;i<=n;i++)
if(q.empty()) return ;
else
{
j = q.top();q.pop();
ans[j] = tot--;
for(k=;k<=n;k++)
if(edge[j][k] && (--cnt[k]) == ) q.push(k);
}
return ;
}
int main ()
{
int n,m,t,i,u,v;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(edge,,sizeof(edge));
memset(cnt,,sizeof(cnt));
tot = n;
for(i=;i<m;i++)
{
scanf("%d%d",&u,&v);
if(edge[v][u] == ) cnt[u]++;
edge[v][u] = ;
}
if(toporder(n))
{
for(i=;i<=n;i++)
if(i == n)printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
}
else printf("-1\n");
}
return ;
}