Sorting It All Out 拓扑排序+确定点
这一道题的话 数据有一点问题 ........ 例如 不过 还是 能理解一下 试试吧 .........
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 拓扑排序+确定点的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- [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 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- 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 ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
随机推荐
- XML中的特殊(保留)字符数据
XML中的特殊(保留)字符数据 制作人:全心全意 在XML文档中,有些字符会被XML解析器当作标记进行处理.如果希望把这些字符作为普通字符处理,就需要使用实体引用或CDATA段. 使用实体引用 为了避 ...
- docker插件
import docker c = docker.Client(base_url='unix://var/run/docker.sock',version='1.15',timeout=10) pri ...
- 【模板】Lca倍增法
Codevs 1036 商务旅行 #include<cstdio> #include<cmath> #include<algorithm> using namesp ...
- Python基础(三) 数据类型
我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点型以及布尔类型.这些基本数据类型组成了基本控制块,从而创建的Python应用程序. 一.基本结构 1.数值: Python支持不同的 ...
- 一、ECharts简介
ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...
- [luoguP1130] 红牌(DP)
传送门 幼儿园DP. ——代码 #include <cstdio> #include <iostream> ; << ); int a[MAXN][MAXN], f ...
- Ubuntu安装deb时错误:“dpkg:错误:另外一个进程已经为 dpkg 状态数据库 加锁”解决
以下方式任选一个即可: 1.重启系统 2.执行(这种方式不要尝试,系统很容易挂) sudo rm /var/lib/dpkg/lock 然后执行修复 sudo dpkg --configure -a
- Filter过滤器机制
tomcat内部过滤器采用了责任链的设计模式, Tomcat的过滤器主要由Filter.FilterChain组成,FilterChain包含一个Filter数组.当Wrapper执行FilterCh ...
- jq仿苹果的时间/日期选择效果
1.html文件,index.html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- jq 笔记
http://bbs.miaov.com/forum.php?mod=forumdisplay&fid=40 2014.10.10jquery 2.0 不兼容ie 6 7 8,以上更适合做移动 ...