您的位置:首页 > 其它

Sorting It All Out 拓扑排序+确定点

2016-04-08 20:24 246 查看

这一道题的话 数据有一点问题 ........ 例如 不过 还是 能理解一下 试试吧 .........

3 5
A<B
B<C
C<A
A<C
B<A
这几组数据 明显反映出来  这是成环的    ,      然后  按照 输入输出案例来说 这个是 有序的   ABC


 




题目要求 在每组数据的 第一行 给你需要排序 的 字母数 和 他们之间的关系数量 然后 输入每组数据 你首先许亚萍判断在输入 第几组 数据的时候 出现了 环 其次判断 到第几组关系的时候 可以确定唯一的序列 如果上面两个 都不行的话 就输出 第三种情况 不能确定 唯一 的 排序序列

内存越界.....醉了 . 明天看 睡觉觉

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
using namespace std;
int n,m,a[30][30],visited[30],flag,fuck,mark,result[30],temp[30],count1,flag1;
void topsort(int q)
{
fuck=0;
for(int j=0;j<n;j++)
temp[j]=visited[j];   //
count1=0;
for(int i=1;i<=n;i++)         //   其实只是普通的  拓扑排序   重复化了一下而已   ...
{
fuck=mark=0;
for(int j=0;j<n;j++)
{
if(temp[j]==0)
{
flag=j;
mark++;
}
}
if(mark==0)
{
printf("Inconsistency found after %d relations.\n",q+1);
flag1=fuck=1;
break;
}
temp[flag]--;  //  找到了   flag  他没有儿子  / 现在  将他标记为 -1;
if(mark==1)
{
result[i]=flag;  //  将  该点储存起来
count1++;
}
for(int j=0;j<n;j++)   //  将 flag的 所有爸爸的 儿子数  -1
{
if(a[flag][j])
{
temp[j]--;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m),(n||m))
{
flag1=fuck=count1=mark=flag=0;
memset(visited,0,sizeof(visited));
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
{
char d,c,b;
scanf(" %c%c%c",&b,&d,&c);
if(flag1)
continue;
a[b-'A'][c-'A']=1;   //     c 有一个叫做b 的儿子
visited[c-'A']++;        //  c 的 儿子 数量  ++
topsort(i);          //  第一次进去的时候 就相当于   只有一组的关系
if(fuck)
;
else
{
if(count1==n)
{
printf("Sorted sequence determined after %d relations: ",i+1);
for(int i=1;i<=n;i++)
printf("%c",result[i]+'A');
printf(".\n");
flag1=1;
}
}
if(!flag1&&i==m-1)
{
printf("Sorted sequence cannot be determined.\n");
flag1=1;
}
}
}
return 0;
}


#include<stdio.h>
#include<string.h>
int map[27][27],indegree[27],q[27];
int TopoSort(int n) //拓扑排序
{
int c=0,temp[27],loc,m,flag=1,i,j;  ////flag=1:有序 flag=-1:不确定
for(i=1;i<=n;i++)
temp[i]=indegree[i];
for(i=1;i<=n;i++)
{
m=0;
for(j=1;j<=n;j++)
if(temp[j]==0) { m++; loc=j; }  //查找入度为零的顶点个数
if(m==0) return 0;  //有环
if(m>1) flag=-1;  // 无序
q[c++]=loc;   //入度为零的点入队
temp[loc]=-1;
for(j=1;j<=n;j++)
if(map[loc][j]==1) temp[j]--;
}
return flag;
}

int main()
{
int m,n,i,sign;  //当sign=1时,已得出结果
char str[5];
while(scanf("%d%d",&n,&m))
{
if(m==0&&n==0) break;
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
sign=0;
for(i=1;i<=m;i++)
{
scanf("%s",str);
if(sign) continue; //一旦得出结果,对后续的输入不做处理
int x=str[0]-'A'+1;
int y=str[2]-'A'+1;
map[x][y]=1;
indegree[y]++;
int s=TopoSort(n);
if(s==0) //有环
{
printf("Inconsistency found after %d relations.\n",i);
sign=1;
}
if(s==1) //有序
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;j++)
printf("%c",q[j]+'A'-1);
printf(".\n");
sign=1;
}
}
if(!sign) //不确定
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: