标签:拓扑排序
#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; int t; int ma[30][30]; char a[105][105]; void tpusort(){ int d[30]; queue<int> que; queue<int> ans; int dd = 0; for(int i = 0;i < 26;i++){ int cc = 0; for(int j = 0;j < 26;j++){ cc += ma[j][i]; } d[i] = cc; if(cc == 0){ que.push(i); } } while(!que.empty()){ int tm = que.front(); que.pop(); ans.push(tm); for(int i = 0;i < 26;i++){ if(ma[tm][i] == 1){ ma[tm][i] = 0; d[i]--; if(d[i] == 0){ que.push(i); } } } } if(ans.size() == 26){ while(!ans.empty()){ int tm = ans.front(); ans.pop(); putchar(tm+'a'); } printf("\n"); } else{ printf("Impossible\n"); } } int main(){ scanf("%d",&t); memset(ma,0,sizeof(ma)); for(int i = 0;i < t;i++){ scanf("%s",a[i]); } int flag = 1; for(int i = 0;i < t;i++){ for(int j = 0;j < i;j++){ for(int k = 0;a[j][k];k++){ if(a[i][k] == '\0'){ flag = 0; break; } if(a[i][k] != a[j][k]){ ma[a[j][k]-'a'][a[i][k]-'a'] = 1; break; } } } } if(flag){ tpusort(); } else{ printf("Impossible\n"); } return 0; }
codeforces 510c Fox And Names 拓扑排序
标签:拓扑排序
原文地址:http://blog.csdn.net/qq_24667639/article/details/46041203