(topological sort) POJ-2367 genealogical tree

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.