zoj 1060 Sorting It All Out
2013-09-18 19:46
302 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1060
解题思路:拓扑排序
解题思路:拓扑排序
/////////////////////////////////////////////////////////////////////////// //problem_id: zoj 1060 //user_id: SCNU20102200088 /////////////////////////////////////////////////////////////////////////// #include <algorithm> #include <iostream> #include <iterator> #include <iomanip> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cmath> #include <queue> #include <stack> #include <list> #include <set> #include <map> using namespace std; /////////////////////////////////////////////////////////////////////////// #pragma comment(linker,"/STACK:1024000000,1024000000") #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// const double EPS=1e-9; const double PI=acos(-1.0); const double E=2.7182818284590452353602874713526; const int x4[]={-1,0,1,0}; const int y4[]={0,1,0,-1}; const int x8[]={-1,-1,0,1,1,1,0,-1}; const int y8[]={0,1,1,1,0,-1,-1,-1}; /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// typedef long long LL; typedef int T; T max(T a,T b){ return a>b? a:b; } T min(T a,T b){ return a<b? a:b; } T gcd(T a,T b){ return b==0? a:gcd(b,a%b); } T lcm(T a,T b){ return a/gcd(a,b)*b; } /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// //Add Code: int n,m,rd[30]; char ans[30]; vector<int> v[30]; int TopSort(int k){ int i,j,res=0,temp[30]; for(i=1;i<=n;i++) temp[i]=rd[i]; bool flag=1; while(k--){ int cnt=0; for(i=1;i<=n;i++){ if(temp[i]==0){ cnt++; j=i; } } if(cnt==0) return -1; if(cnt>=1){ if(cnt>1) flag=0; for(i=0;i<v[j].size();i++) temp[v[j][i]]--; ans[res++]=j+'A'-1; temp[j]=-1; ans[res]=0; } } if(flag) return res; return 0; } /////////////////////////////////////////////////////////////////////////// int main(){ /////////////////////////////////////////////////////////////////////// //Add Code: int i,cnt; char c[5]; bool flag[30]; while(scanf("%d%d",&n,&m)!=EOF){ if(n==0 && m==0) break; memset(rd,0,sizeof(rd)); memset(flag,0,sizeof(flag)); for(i=0;i<30;i++) v[i].clear(); int num=0,judge=0; for(i=1;i<=m;i++){ scanf("%s",c); if(judge==0){ int a=c[0]-'A'+1,b=c[2]-'A'+1; rd[b]++; v[a].push_back(b); if(!flag[a]){ num++; flag[a]=1; } if(!flag[b]){ num++; flag[b]=1; } int ret=TopSort(num); if(ret==-1){ judge=-1; cnt=i; } else if(ret==n){ judge=1; cnt=i; } } } if(judge==-1) printf("Inconsistency found after %d relations.\n",cnt); else if(judge==0) printf("Sorted sequence cannot be determined.\n"); else printf("Sorted sequence determined after %d relations: %s.\n",cnt,ans); } /////////////////////////////////////////////////////////////////////// return 0; } /////////////////////////////////////////////////////////////////////////// /* Testcase: Input: 4 6 A<B A<C B<C C<D B<D A<B 3 2 A<B B<A 26 1 A<Z 0 0 Output: Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined. */ ///////////////////////////////////////////////////////////////////////////
相关文章推荐
- zoj 1060 Sorting It All Out 拓扑排序
- poj1094&&zoj1060 Sorting It All Out ——拓扑排序入门题
- zoj 1060 || poj 1094 Sorting It All Out(拓扑排序)
- zoj 1060 Sorting It All Out(拓扑排序)
- ZOJ 1060 poj 1094 Sorting It All Out(拓扑排序或 弗洛德)
- zoj 1060 Sorting It All Out(拓扑排序)
- JOJ 1089 & ZOJ 1060 & poj 1094 Sorting It All Out (邻接表的栈拓扑排序模板)
- POJ 1094 && ZOJ 1060 Sorting It All Out 【拓扑排序入门】
- ZOJ 1060 poj 1094 Sorting It All Out(拓扑排序或 弗洛德)
- POJ 3660 && NYIST 211 Cow Contest && ZOJ 1060 Sorting It All Out【Floyd算法扩展应用】
- POJ 1094 && ZOJ 1060 Sorting It All Out 【拓扑排序入门】
- ZOJ 1060 Sorting It All Out 拓扑排序
- ZOJ 1060 Sorting It All Out
- ZOJ-1060-Sorting It All Out
- zoj 1060 Sorting It All Out(拓扑排序)
- POJ 3660 && NYIST 211 Cow Contest && ZOJ 1060 Sorting It All Out【Floyd算法扩展应用】
- ZJU 1060 Sorting It All Out - 拓扑排序
- poj 1094 Sorting It All Out(拓扑排序 + 邻接表)
- Sorting It All Out (拓扑排序)
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)