
Labeling Balls
一道拓补排序的题,虽然a了但是是看题解过的……存在的疑问已在注释中标明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <list> #include <set> #include <map> #include <cctype> #include <string> #include <queue> #define debug printf("debugn") #define mst(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f const int maxn=1e5+5; using namespace std; typedef long long ll; typedef unsigned long long ull; int gra[205][205]; int deg[40005]; int ans[40005]; int n,m; bool () { int tag;int cnt=0; for(int i=n;i>=1;i--){ for(int j=n;j>=1;j--){ if(deg[j]==0){ tag=j; deg[j]--; ans[j]=i; cnt++; break; } } for(int j=1;j<=n;j++){ if(gra[tag][j]>0) deg[j]--; } } if(cnt<n) return false; return true; } int main() { int t; cin>>t; while(t--) { cin>>n>>m; mst(deg,0);mst(gra,0);mst(ans,0); for(int i=0;i<m;i++){ int a,b; cin>>a>>b; if(!gra[b][a]) deg[a]++; gra[b][a]=1; } if(!topo()) cout<<-1<<endl; else{ int pr=0; for(int i=1;i<=n;i++){ if(!pr) printf("%d",ans[i]); else printf(" %d",ans[i]); pr++; } printf("n"); } } return 0; }
|
近期评论