poj-1094-Sorting It All Out
2012-12-16 12:14
253 查看
拓扑排序的一道题目,主要是考查拓扑排序判断环;
如果存在环就自相矛盾。
如果存在多个入度为0的点,就关系不明确。
这道题目有个比较坑的地方就是,如果关系明确了,就不要管后面的,直接输出就行了。
比如说,如果已经得出了他们的所有关系,就输出
如果存在环就自相矛盾。
如果存在多个入度为0的点,就关系不明确。
这道题目有个比较坑的地方就是,如果关系明确了,就不要管后面的,直接输出就行了。
比如说,如果已经得出了他们的所有关系,就输出
Sorted sequence determined after。。。。
即使后面再有关系表明他们自相矛盾也不输出了。
#include<iostream> #include<string.h> #include<string> using namespace std; int du[27],list[27],map[27][27],re[27]; int find(char *str,int n) { int start = str[0] - 'A'; int end = str[2] - 'A'; int i,j,k; if(map[start][end]==0) { map[start][end]=1; list[end]++; } for(i=0;i<n;i++) du[i]=list[i]; int w,leap=1,cnt,repos=0; for(k=0;k<n;k++) { cnt=0; for(j=0;j<n;j++) { if(du[j]==0) { w=j; cnt++; } } if(cnt == 0) return -1; else if(cnt>1) leap = 0; du[w]--; re[repos++]=w; for(j=0;j<n;j++) { if(map[w][j] == 1) du[j]--; } } if(leap) return 1; else return 0; } void ff(int n,int m) { memset(list,0,sizeof(list)); memset(map,0,sizeof(map)); char str[10000]; int leap=1; int c; for(c=0;c<m;c++) { cin>>str; if(leap) { int t=find(str,n); if(t==1) { cout<<"Sorted sequence determined after "<<c+1<<" relations: "; for(int q=0;q<n;q++) cout<<char(re[q]+'A'); cout<<'.'<<endl; leap=0; } else if(t==-1) { cout<<"Inconsistency found after "<<c+1<<" relations."<<endl; leap=0; } } } if(leap) cout<<"Sorted sequence cannot be determined."<<endl; } int main() { int n,m,i; char str[10000]; while(cin>>n>>m) { if(n==0&&m==0)break; if(n-1>m) { for(i=0;i<m;i++) { cin>>str[i]; } cout<<"Sorted sequence cannot be determined."<<endl; } else { ff(n,m); } } }
相关文章推荐
- POJ - 1094 Sorting It All Out
- POJ-1094-Sorting It All Out
- poj-1094 Sorting It All Out
- poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out (拓补)
- POJ 1094 Sorting It All Out
- poj 1094 Sorting It All Out(nyoj 349)
- poj 1094 Sorting It All Out
- 【改了一天的拓扑排序】POJ 1094——Sorting It All Out
- POJ 1094 Sorting It All Out
- zoj 1060 && poj 1094 Sorting It All Out
- [POJ] 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- POJ-1094 Sorting it All Out
- POJ-1094-Sorting It All Out
- HOJ 1336 POJ 1094 Sorting It All Out
- POJ-1094-Sorting It All Out
- poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out