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 ...
 
随机推荐
- LINUX-DEB 包 (Debian, Ubuntu 以及类似系统)
			
dpkg -i package.deb 安装/更新一个 deb 包 dpkg -r package_name 从系统删除一个 deb 包 dpkg -l 显示系统中所有已经安装的 deb 包 dpkg ...
 - react入门----基础语法
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - Linux学习总结(19)——Linux中文本编辑器vim特殊使用方法
			
1. vim比对功能 在linux的环境下 用于观察两个文件的一致性的时候我们一般用diff这个命令来比对,但是这个命令不能你特别详细的比对出 具体的位置或者行对比.这里就用到了vim的对比功能 vi ...
 - Codeforces Round #219 (Div. 2) D题
			
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
 - SGU -1500 - Pass Licenses
			
先上题目: 1500. Pass Licenses Time limit: 2.5 secondMemory limit: 64 MB A New Russian Kolyan believes th ...
 - [K/3Cloud]如何解决K3Cloud 2.0审批流提交时报“队列不存在,或您没有足够的权限执行该操……
			
按照图上的操作即可解决不可提交的问题,但如果应用服务器是部署在域环境下,应该不会出错,这是微软support上说的
 - Lein: Exception in thread "Thread-3" java.net.ConnectException: Connection refused
			
leiningen Leiningen是你的主要工具, 它用于: 启动一个 REPL 下载+安装类库 运行你的程序 启动一个服务器, 运行你所写的webapps 安装 brew install lei ...
 - Android学习路线(十八)支持不同设备——支持不同的屏幕
			
Android系统使用两个普通属性:尺寸和密度,来对设备屏幕进行分类. 你须要先预測你的应用将会在什么样屏幕的设备上安装,包含屏幕尺寸和密度.这种话,你就须要提供一些可选的资源类让你的应用在不同屏幕的 ...
 - nodejs fs.open
			
fs.open(path, flags, [mode], [callback(err, fd)])是 POSIX open 函数的封装,与 C 语言标准库中的 fopen 函数类似.它接受两个必选参数 ...
 - 如何快速查看EPS,AI等矢量文件
			
使用Adobe Bridge可以快速查看所有这些格式的资源 查看EPS格式图片: 查看AI格式: 某些AI文件则无法预览(此外还有一些CDR的格式) 相比之下,ACDSee的效果则不如Adob ...