首页 > 代码库 > Poj 1094 拓扑排序 水题
Poj 1094 拓扑排序 水题
Sad..这么水的题WA了无数发,题目要看仔细啊,留下来做个警告把
#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxn = 30;int N,M,fa[maxn],incnt[maxn];bool vis[maxn],sorted[maxn];int mp[maxn][maxn];int toposort(vector<int> &ans) { int tmp[maxn]; bool flag = true; ans.clear(); memset(vis,0,sizeof(vis)); for(int i = 1;i <= N;i++) tmp[i] = incnt[i]; for(int k = 1;k <= N;k++) { int x,ncnt = 0; for(int i = 1;i <= N;i++) if(!vis[i] && tmp[i] == 0) { x = i; ncnt++; } if(ncnt > 1) flag = false; //就算不能判断出来,不能直接return,还要继续判断是否有环 if(ncnt == 0) return -1; vis[x] = true; ans.push_back(x); for(int i = 1;i <= N;i++) if(!vis[i] && mp[x][i] == 1) { tmp[i]--; } } return flag;}int main() { while(scanf("%d%d",&N,&M),N) { vector<int> ans; memset(mp,-1,sizeof(mp)); memset(incnt,0,sizeof(incnt)); bool determined = false,inconsistency = false; int cnt = 1,kase; for(int i = 1;i <= M;i++) { char buf[10]; scanf("%s",buf); int a = buf[0] - ‘A‘ + 1,b = buf[2] - ‘A‘ + 1; if(inconsistency || determined) continue; if(mp[a][b] == -1) incnt[b]++; mp[a][b] = true; int ret = toposort(ans); if(ret == 1) determined = true,kase = i; if(ret == -1) inconsistency = true; if(inconsistency) { printf("Inconsistency found after %d relations.\n",i); continue; } } if(!inconsistency) { if(determined) { printf("Sorted sequence determined after %d relations: ",kase); for(int i = 0;i < ans.size();i++) printf("%c",ans[i] - 1 + ‘A‘); puts("."); } else puts("Sorted sequence cannot be determined."); } } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。