Test Instructions: in a family, the character relationship is very complex, now to sort each person's voice, requires each ancestor's discourse power is higher than the descendants, the output discourse right from the big to the small order.
Analysis:
Originally thought that the problem has a pit, the result casually a common topological sequence on a, good water.
The ancestors pointed to the descendants, each time taken out into the degree of 0.
Code:
1#include <iostream>2#include <cstdio>3#include <string>4#include <vector>5#include <map>6#include <Set>7#include <queue>8#include <cstring>9#include <algorithm>Ten#include <cmath> One A using namespacestd; - - thetypedefLong Longll; -typedef unsignedLong Longull; - #defineINF (0X3F3F3F3F) - #defineLNF (0x3f3f3f3f3f3f3f3f) + #defineEPS (1E-8) - intSgnDoublea) { + returnA <-eps? -1: A < EPS?0:1 ; A } at - //--------------------------------------------- - - - - Const intmaxn= the; in intINDU[MAXN]; -vector<int>V[MAXN]; tovector<int>ans; + intN; - the voidinit () { * for(intI=1; i<=n; i++) { $ v[i].clear ();Panax Notoginseng } - ans.clear (); thememset (Indu,0,sizeof(Indu)); + } A the voidToposort () { +queue<int>Q; - for(intI=1; i<=n; i++) { $ if(indu[i]==0) { $ Q.push (i); -indu[i]--; - } the } - while(!Q.empty ()) {Wuyi intu=Q.front (); the Q.pop (); - ans.push_back (u); Wu for(intI=0; I<v[u].size (); i++) { -indu[v[u][i]]--; About if(indu[v[u][i]]==0) Q.push (V[u][i]); $ } - } - - } A + voidsolve () { the while(~SCANF ("%d",&N)) { - init (); $ for(intI=1; i<=n; i++) { the intx; the while(1) { thescanf"%d",&x); the if(x==0) Break; - v[i].push_back (x); inindu[x]++; the } the } About Toposort (); the for(intI=0; I<ans.size ()-1; i++){ theprintf"%d", Ans[i]); the } +printf"%d\n", Ans[ans.size ()-1]); - the }Bayi the } the - - intMain () { the the #ifndef Online_judge theFreopen ("1.in","R", stdin); the //freopen ("1.out", "w", stdout); - #endif the //Iostream::sync_with_stdio (false); the solve (); the}
(topological sort) POJ-2367 genealogical tree