poj 3687 两种写法,差别好大,估计没人按第二种写,就当练习邻接表了

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int main()
{
	int map[250][250],re[250],in[250];
	int n,m,k,i,j,a,b,temp,u;
	cin>>n;
	for(i=0;i<n;i++)
	{
		memset(map,0,sizeof(map));
		memset(in,0,sizeof(in));
		cin>>m>>k;
		for(j=0;j<k;j++)
		{
			cin>>a>>b;
			if(!map[b][a])
			{
				in[a]++;
				map[b][a]=true;      //避免重边
			}
		}
		priority_queue<int> q;       //优先队列
		for(j=1;j<=m;j++)
			if(in[j]==0)
				q.push(j);
		temp=m;
		while(!q.empty())
		{
			u=q.top();         //此题中队列用的真是好,top可以将队列排序,然后删掉队首,也就是最大的,再从队尾插入,在排序
			q.pop();
			re[u]=temp--;    //  巧妙记录重量
			for(j=1;j<=m;j++)
			{
				if(map[u][j])
				{
					in[j]--;
					if(in[j]==0)
						q.push(j);
				}
			}
		}
		if(temp)
			cout<<"-1"<<endl;
		else
		{
			for(j=1;j<m;j++)
				cout<<re[j]<<" ";
			cout<<re[j]<<endl;
		}
	}
}



















#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct NODE
{
	int v,next;
}e[40010];
int head[300],k,in[300],queue[300],a1[300];
void insert(int u,int v)
{
	e[k].v=v;
	e[k].next=head[u];
	head[u]=k++;
}
bool cmp(int a,int b)
{
	return  a>b;
} 
int tuopu(int n)
{
	int i,j=0,f=0,r=0,u;
	for(i=1;i<=n;i++)
		if(in[i]==0)
			queue[r++]=i;
	while(f<r)
	{
		sort(queue+f,queue+r,cmp);
		u=queue[f++];
		a1[j++]=u;
		for(i=head[u];i!=-1;i=e[i].next)
		{
			in[e[i].v]--;
			if(in[e[i].v]==0)
				queue[r++]=e[i].v;
		}
	}
	return j;
}
int main()
{
	int n,i,j,a,b,m,k1,temp;
	int c[300];
	cin>>m;
	for(i=0;i<m;i++)
	{
		memset(head,-1,sizeof(head));
		memset(in,0,sizeof(in));
		k=0;
		cin>>n>>k1;
		for(j=0;j<k1;j++)
		{
			cin>>a>>b;
			insert(b,a);
			in[a]++;
		}
		temp=tuopu(n);
		if(temp<n)
			cout<<"-1"<<endl;
		else
		{
			for(j=0;j<n;j++)
				c[a1[j]]=n-j;
			for(j=1;j<n;j++)
				cout<<c[j]<<" ";
			cout<<c[j]<<endl;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值