[c]sdnuoj 1031 拓扑排序
2015-07-28 21:29
351 查看
http://210.44.14.31/problem/show/1031
题目标题:字母排序
题目大意:给出若干两个字母的排序,求能不能算出总的排序
这是个拓扑排序的题,要注意的是需要输出最少要前几个条件就能排序,所以每输入一次就要排一次,还有个特别要注意的地方,在注释中给出
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
int a[27][27],in[27];
string re;
int x,y;
int TOP(int n){
re="";
int sign=1;
int nn;
int now[27]={0};
for(int i=0;i<x;i++) now[i]=in[i];
for(int i=0;i<x;i++){
int inx=0;
for(int j=0;j<x;j++){
if(now[j]==0){
inx++;
nn=j;
}
}
if(inx==0) return -1;
if(inx>1) sign=0;//这个地方不能写return,如果写了return有可能忽略有环的情况
re+=(char)(nn+'A');
now[nn]--;
for(int j=0;j<x;j++) if(a[nn][j]==1) now[j]--;
}
return sign;
}
int main()
{
while(cin>>x>>y){
if(x==0 && y==0) break;
memset(a,0,27*27*sizeof(int));
memset(in,0,27*sizeof(int));
re="";
int sign=0;
for(int i=0;i<y;i++){
string s;
cin>>s;
if(sign==1) continue;
int n=s[0]-'A';
int m=s[2]-'A';
if(s[1]=='<') {
a
[m]=1;
in[m]++;
}
else if(s[1]=='>'){
a[m]
=1;
in
++;
}
int r=TOP(x);
if(r==1){
printf("Sorted sequence determined after %d relations: ",i+1);
cout<<re<<'.'<<endl;
sign=1;
}
if(r==-1){
printf("Inconsistency found after %d relations.\n",i+1);
sign=1;
}
}
if(sign==0){
printf("Sorted sequence cannot be determined.\n");
}
}
return 0;
}
题目标题:字母排序
题目大意:给出若干两个字母的排序,求能不能算出总的排序
这是个拓扑排序的题,要注意的是需要输出最少要前几个条件就能排序,所以每输入一次就要排一次,还有个特别要注意的地方,在注释中给出
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
int a[27][27],in[27];
string re;
int x,y;
int TOP(int n){
re="";
int sign=1;
int nn;
int now[27]={0};
for(int i=0;i<x;i++) now[i]=in[i];
for(int i=0;i<x;i++){
int inx=0;
for(int j=0;j<x;j++){
if(now[j]==0){
inx++;
nn=j;
}
}
if(inx==0) return -1;
if(inx>1) sign=0;//这个地方不能写return,如果写了return有可能忽略有环的情况
re+=(char)(nn+'A');
now[nn]--;
for(int j=0;j<x;j++) if(a[nn][j]==1) now[j]--;
}
return sign;
}
int main()
{
while(cin>>x>>y){
if(x==0 && y==0) break;
memset(a,0,27*27*sizeof(int));
memset(in,0,27*sizeof(int));
re="";
int sign=0;
for(int i=0;i<y;i++){
string s;
cin>>s;
if(sign==1) continue;
int n=s[0]-'A';
int m=s[2]-'A';
if(s[1]=='<') {
a
[m]=1;
in[m]++;
}
else if(s[1]=='>'){
a[m]
=1;
in
++;
}
int r=TOP(x);
if(r==1){
printf("Sorted sequence determined after %d relations: ",i+1);
cout<<re<<'.'<<endl;
sign=1;
}
if(r==-1){
printf("Inconsistency found after %d relations.\n",i+1);
sign=1;
}
}
if(sign==0){
printf("Sorted sequence cannot be determined.\n");
}
}
return 0;
}
相关文章推荐
- HDU 3342
- hdu 1285 确定比赛名次 (简单的拓扑排序)
- hdu 2049 产生冠军(拓扑排序)
- [BZOJ2330][SCOI2011][拓扑排序][强连通分量][Tarjan]Candy
- Course Schedule
- LeetCode Course Schedule II
- 【Leetcode】Course Schedule II #210
- poj1094 拓扑排序
- (转)拓扑排序
- [leetcode]Course Schedule
- poj_1094
- cf_290(div2)A,B,C
- 图论算法(1)--- 如何求每一个节点在DAG中的最大深度
- hdu 3342 拓扑排序
- hdu 2647 拓扑排序
- UVa 1572 拓扑排序 判断有向图是否有环
- UVa10305拓扑排序入门题 可做模板用
- Sorting It All Out(简单的拓扑排序)
- POJ 1094 Sort it all out (Topsort)
- 有向图的拓扑排序~TopologicalSort