POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int in[27],out[27];
char index[27];
bool vis[27];
int mem[27][27];
int n,m;
int tlen;
bool floyd(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(mem[i][k]&&mem[k][j])mem[i][j]=1;
}
}
}
for(int i=1;i<n;i++){
if(mem[i][i])return false;
}
return true;
}
bool calc(){
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mem[i][j]==1){
out[i]++;
in[j]++;
}}
}
for(int i=0;i<n;i++){
if(in[i]+out[i]!=n-1)return false;
}
return true;
}
bool topologicalsort(){
memset(vis,0,sizeof(vis));
int len=0;
bool fl=false;
while(len<n){
fl=false;
for(int i=0;i<n;i++){
if(in[i]==0&&!vis[i]){
fl=true;
vis[i]=true;
index[len]=i+'A';
len++;
for(int j=0;j<n;j++){
if(mem[i][j])in[j]--;
}
break;
}
}
if(!fl)return false;
}
return true;
}
int main(){
char ch,ch2;
while (scanf("%d %d",&n,&m)==2&&n){
bool fl=false;
getchar();
memset(mem,0,sizeof(mem));
memset(index,0,sizeof(index));
for(int i=1;i<=m;i++){
scanf(" %c< %c",&ch,&ch2);
in[ch2-'A']++;
out[ch-'A']++;
mem[ch-'A'][ch2-'A']=1;
getchar();
if(fl)continue;
if(floyd()){
if(calc()){
if(topologicalsort()){
fl=true;
index[n]=0;
printf("Sorted sequence determined after %d relations: %s.\n",i,index);
continue;
}
}
}
else {
printf("Inconsistency found after %d relations.\n",i);
fl=true;
continue;
}
}
if(!fl)printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
POJ 1094 Sorting It All Out 拓扑排序 难度:0的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
随机推荐
- SQL Server数据库性能优化(三)之 硬件瓶颈分析
参考文献 http://isky000.com/database/mysql-performance-tuning-hardware 由于对DBA 工作了解不多 所以只从网上简单的看了下 硬件 ...
- mfc ui2
引用:http://www.cnblogs.com/likwo/archive/2010/10/22/1858716.html CJLib(mfc扩展开发包,是xtreme toolkit的前生,但x ...
- spring aop 中获取 request
使用aop时需要request 和response 使用方法调用时 HttpServletRequest request = ((ServletRequestAttributes)RequestCon ...
- Dynamics AX 2012 R2 配置报表服务器
今天Reinhard在使用报表的过程中,发现以下错误: The default Report Server Configuration ID could not be found in the SRS ...
- Lucene学习总结
在使用Lucene前,我们先大致熟悉下Lucene的几个核心类. 核心索引类: public class IndexWriter 索引过程的中心组件,把它想象成一个可以对索引进行写操作的对象. pub ...
- android常见问题
1.广播接收器中启动Activity,需要在intent中添加FLAG_ACTIVITY_NEW_TASK /** * Demo描述: * 在BroadcastReceiver中启动Activity的 ...
- 【Nginx】配置Nginx的负载均衡
参考的优秀文章 tomcat配置文件server.xml详解 AJP协议总结与分析 Using nginx as HTTP load balancer 在本机运行2个Tomcat 现需要运行两个Tom ...
- Codeforces Round #356 (Div. 2)
A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Kanzi UI Solution
Kanzi UI Solution是一个完整的跨平台的UI解决方案, 基于OpenGL 和 OpenGL ES.Kanzi为UI的设计.开发和部署在嵌入式设备上的图形用户界面提供一个完善的开发平台. ...
- win7 用户目录
robocopy "C:\Users" "D:\Users" /E /COPYALL /XJ /XD "C:\Users\Administrator& ...