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

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[][],visited[],flag,fuck,mark,result[],temp[],count1,flag1;
void topsort(int q)
{
fuck=;
for(int j=;j<n;j++)
temp[j]=visited[j]; //
count1=;
for(int i=;i<=n;i++) // 其实只是普通的 拓扑排序 重复化了一下而已 ...
{
fuck=mark=;
for(int j=;j<n;j++)
{
if(temp[j]==)
{
flag=j;
mark++;
}
}
if(mark==)
{
printf("Inconsistency found after %d relations.\n",q+);
flag1=fuck=;
break;
}
temp[flag]--; // 找到了 flag 他没有儿子 / 现在 将他标记为 -1;
if(mark==)
{
result[i]=flag; // 将 该点储存起来
count1++;
}
for(int j=;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=;
memset(visited,,sizeof(visited));
memset(a,,sizeof(a));
for(int i=;i<m;i++)
{
char d,c,b;
scanf(" %c%c%c",&b,&d,&c);
if(flag1)
continue;
a[b-'A'][c-'A']=; // c 有一个叫做b 的儿子
visited[c-'A']++; // c 的 儿子 数量 ++
topsort(i); // 第一次进去的时候 就相当于 只有一组的关系
if(fuck)
;
else
{
if(count1==n)
{
printf("Sorted sequence determined after %d relations: ",i+);
for(int i=;i<=n;i++)
printf("%c",result[i]+'A');
printf(".\n");
flag1=;
}
}
if(!flag1&&i==m-)
{
printf("Sorted sequence cannot be determined.\n");
flag1=;
}
}
}
return ;
}
 #include<stdio.h>
#include<string.h>
int map[][],indegree[],q[];
int TopoSort(int n) //拓扑排序
{
int c=,temp[],loc,m,flag=,i,j; ////flag=1:有序 flag=-1:不确定
for(i=;i<=n;i++)
temp[i]=indegree[i];
for(i=;i<=n;i++)
{
m=;
for(j=;j<=n;j++)
if(temp[j]==) { m++; loc=j; } //查找入度为零的顶点个数
if(m==) return ; //有环
if(m>) flag=-; // 无序
q[c++]=loc; //入度为零的点入队
temp[loc]=-;
for(j=;j<=n;j++)
if(map[loc][j]==) temp[j]--;
}
return flag;
} int main()
{
int m,n,i,sign; //当sign=1时,已得出结果
char str[];
while(scanf("%d%d",&n,&m))
{
if(m==&&n==) break;
memset(map,,sizeof(map));
memset(indegree,,sizeof(indegree));
sign=;
for(i=;i<=m;i++)
{
scanf("%s",str);
if(sign) continue; //一旦得出结果,对后续的输入不做处理
int x=str[]-'A'+;
int y=str[]-'A'+;
map[x][y]=;
indegree[y]++;
int s=TopoSort(n);
if(s==) //有环
{
printf("Inconsistency found after %d relations.\n",i);
sign=;
}
if(s==) //有序
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=;j<n;j++)
printf("%c",q[j]+'A'-);
printf(".\n");
sign=;
}
}
if(!sign) //不确定
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

Sorting It All Out 拓扑排序+确定点的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  4. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  5. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

  6. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  7. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  8. POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  9. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

随机推荐

  1. linux内核开发程序风格

    变量命名法 这里是linux不是windows,所以匈牙利命名法是不允许使用的,在内核中,局部变量只要可以明确表达自己的意思,可以使用idx,i这种名字的id, 全局函数和变量需要有表达性的名字例如g ...

  2. HDU 5178 pairs(双指针)

    HDU 5178 pairs(双指针) Hdu 5178 解法:因为要求的是绝对值小于等于k,因此数字的序号关系并不重要,那么排序后使用双指针即可解决这个问题. #include<queue&g ...

  3. [bzoj3106][cqoi2013][棋盘游戏] (对抗搜索+博弈论)

    Description 一个n*n(n>=2)棋盘上有黑白棋子各一枚.游戏者A和B轮流移动棋子,A先走. l         A的移动规则:只能移动白棋子.可以往上下左右四个方向之一移动一格. ...

  4. MVC系统学习2—MVC路由

    在MVC下不是通过对物理文件的映射来实行访问的,而是通过定义后的路由Url来实现访问的.在前一篇讲到我们是在全局文件下进行路由配置. routes.MapRoute(                & ...

  5. HDU 4902 (牛叉的线段树)

    Nice boat Problem Description There is an old country and the king fell in love with a devil. The de ...

  6. [bzoj4033][HAOI2015]树上染色_树形dp

    树上染色 bzoj-4033 HAOI-2015 题目大意:给定一棵n个点的树,让你在其中选出k个作为黑点,其余的是白点,收益为任意两个同色点之间距离的和.求最大收益. 注释:$1\le n\le 2 ...

  7. ZOJ——3609 Modular Inverse

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  8. Ubuntu18.04卸载lnmp

    1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...

  9. jq 地区(省市县区)联动菜单

    <pre name="code" class="html"><pre name="code" class="ph ...

  10. LeetCode 970. Powerful Integers (强整数)

    题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...