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 ...
随机推荐
- illuminate/routing 源码分析之注册路由
我们知道,在 Laravel 世界里,外界传进来一个 Request 时,会被 Kernel 处理并返回给外界一个 Response.Kernel 在处理 Request 时,会调用 illumina ...
- 突击战 (UVA 11729)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28436 思路:任务从开始时就不停执行,与其他任务毫无关联,当然是执 ...
- hibernate的QBC查询之Criteria用法
//return (DeliverCost) super.getSession().createCriteria(getMyClass()).add(Restrictions.eq("isd ...
- Leetcode 80.删除重复数组的重复项
删除重复数组的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...
- T1462 素数和 codevs
题目描述 Description 给定2个整数a,b 求出它们之间(不含a,b)所有质数的和. 输入描述 Input Description 一行,a b(0<=a,b<=65536) 输 ...
- SystemTapでMySQL 5.5のDisk I/Oを分析する
http://d.hatena.ne.jp/sh2/20111121 2010年1月の記事SystemTapでMySQLのDisk I/Oを分析するの続きです.以前作成したSystemTapスクリプト ...
- docker: useful commands
docker build -t stock_data_repo_instance24 . docker run -v /opt/log:/opt/log -d -it stock_data_repo_ ...
- Leetcode题解(5):L58/Length of Last Word
L58: Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space cha ...
- Linux网络编程:UDP Socket编程范例
TCP协议提供的是一种可靠的,复杂的,面向连接的数据流(SOCK_STREAM)传输服务,它通过三段式握手过程建立连接.TCP有一种"重传确认"机制,即接收端收到数据后要发出一个肯 ...
- 项目构建之maven篇:1.环境搭建
maven下载: 下载地址 环境变量设置 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fon ...