poj1094
来源:互联网 发布:世界秩序 知乎 编辑:程序博客网 时间:2024/05/28 14:56
拓扑排序:对每一条式子建图,然后做一次拓扑排序,每一次操作后需要判断是否有环,是否有序,如果无序则继续下一个操作。拓扑操作是先建完图后每次找入度为零的点,然后断掉这个点与图内其他点相连的边,接着继续重复,如果找不到入度为零的点则存在环
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <iomanip>
using namespace std;
int mapp[35][35],ru[35],q[35],cnt,tmp[35];
int n,m;
int toposort()
{
int flag=1;
int i,j,k,ori,sum;
cnt=0;
for(i=0;i<=n;i++)tmp[i]=ru[i];
for(i=1;i<=n;i++)
{
sum=0;
for(j=1;j<=n;j++)
if(tmp[j]==0)
{
sum++;
ori=j;
}
if(sum==0)return 0;
if(sum>1)flag=-1;
//cout<<sum<<' '<<flag<<endl;
q[cnt++]=ori;
tmp[ori]=-1;
for(k=1;k<=n;k++)
if(mapp[ori][k]==1)
tmp[k]--;
}
return flag;
}
int main()
{
while(cin>>n>>m)
{
if(n+m==0)break;
memset(mapp,0,sizeof(mapp));
memset(ru,0,sizeof(ru));
bool flag=false;
for(int i=1;i<=m;i++)
{
cnt=0;
string a;
cin>>a;
if(flag)continue;
int x=a[0]-'A'+1;
int y=a[2]-'A'+1;
mapp[x][y]=1;
ru[y]++;
int ans=toposort();
if(ans==0)
{
printf("Inconsistency found after %d relations.\n",i);
flag=1;
}
if(ans==1)
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;j++)
{
char c=q[j]+'A'-1;
cout<<c;
}
printf(".\n");
flag=1;
}
}
if(!flag)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
- POJ1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- POJ1094
- poj1094注意事项
- POJ1094解题报告
- Poj1094拓扑排序总结
- 拓扑排序 poj1094
- poj1094 拓扑排序
- 简单方法解决设置IBOutlet变量时中出现的“Could not insert new outlet connection”
- 详解基于jsp的文件的上传下载
- 使用pinyin4j将TreeMap按照key的汉字拼音排序
- java web 将http 强转为https
- >Error Occured in WS-AppServe
- poj1094
- 一张图理解mapreduce的shuffle
- windows 远程桌面 Ubuntu14.04 配置记录
- photoshop不透明度和填充度原理
- hdu4463Outlets
- Android5.1中surface和CpuConsumer下生产者和消费者间的处理框架简述
- Android调试 Logcat中查看System.out信息
- Dubbo学习总结(2)——Dubbo架构详解
- maven常用命令