POJ 2553 The Bottom of a Graph

Source: Internet
Author: User
Tags integer numbers

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 10687 Accepted: 4403

Description

We'll use the following-definitions from graph theory. Let VBe a nonempty and finite set, its elements being called vertices (or nodes). Let EBe a subset of the Cartesian product VXV, its elements being called edges. Then g= (v,e)is called a directed graph.
Let NBe a positive integers, and let p= (E1,..., en)Be a sequence of length Nof edges ei∈esuch that ei= (vi,vi+1)For a sequence of vertices (v1,..., vn+1). Then Pis called a path from vertex v1to vertex vn+1Inch GAnd we say that vn+1is reachable from v1, writing (v1→vn+1).
Here is some new definitions. A node vIn a graph g= (v,e)is called a sink, if for every node WInch GThat's reachable from v, vis also reachable from W. The bottom of a graph is the subset of all nodes it is sinks, i.e., Bottom (G) ={v∈v|∀w∈v: (v→w) ⇒ (w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of the which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of g= (v,e)Where the vertices is identified by the integer numbers in the set v={1,..., V}. Assume that 1<=v<=5000. That's followed by a non-negative integer eAnd, thereafter, ePairs of vertex identifiers v1,w1,..., ve,weWith the meaning. (VI,WI) ∈e. There is no edges other than specified by these pairs. The last test case was followed by a zero.

Output

For all test case output the bottom of the specified graph on a single line. To this end, print the numbers of any nodes that is sinks in sorted order separated by a single space character. If the bottom is empty, print a empty line.

Sample Input

3 31 3 2 3 3 12 11 20

Sample Output

1 32

Source

ULM Local 2003
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <vector>5#include <algorithm>6#include <stack>7 #defineMAXM 5000108 #defineMAXN 50109 using namespacestd;Ten intt,n,m; One structedge{ A     intU,v,next; - }E[MAXM],EE[MAXM]; -vector<int>Kp[maxn],ans; the intHEAD[MAXN],JS,HEADD[MAXN],JSS; - BOOLEXIST[MAXN]; - intVisx,cur;//cur--the number of points to indent - intDFN[MAXN],LOW[MAXN],BELONG[MAXN],CHUDU[MAXN]; +stack<int>St; - voidinit () { +memset (Chudu,0,sizeof(Chudu)); AMemset (E,0,sizeof(e)); memset (EE,0,sizeof(EE)); atMemset (Head,0,sizeof(head)); memset (Headd,0,sizeof(Headd)); -Jss=js=visx=cur=0; -memset (exist,false,sizeof(exist)); -      while(!st.empty ()) St.pop (); -memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(Low)); -Memset (Belong,0,sizeof(belong)); inAns.resize (0); -      for(intI=0; i<maxn;i++) Kp[i].resize (0); to } + voidAdd_edge1 (intUintv) { -e[++js].u=u;e[js].v=v; thee[js].next=head[u];head[u]=JS; * } $ voidTarjan (intu) {Panax Notoginsengdfn[u]=low[u]=++Visx; -exist[u]=true; the st.push (u); +      for(intI=head[u];i;i=E[i].next) { A         intv=e[i].v; the         if(dfn[v]==0){ + Tarjan (v); -low[u]=min (low[u],low[v]); $         } $         Else if(Exist[v]&&low[u]>dfn[v]) low[u]=Dfn[v]; -     } -     intJ; the     if(low[u]==Dfn[u]) { -++cur;Wuyi          Do{ theJ=st.top (); St.pop (); exist[j]=false; - Kp[cur].push_back (j); Wubelong[j]=cur; -} while(j!=u); About     } $ } - voidAdd_edge2 (intUintv) { -ee[++jss].u=u;ee[jss].v=v; -ee[jss].next=headd[u];headd[u]=JSS; A } + voidApki (intx) { the      for(intI=0; I<kp[x].size (); i++){ -         intk=Kp[x][i]; $ Ans.push_back (k); the     } the } the intMain () the { -      while(SCANF ("%d%d", &n,&m) = =2){ in init (); the         intu,v; the          for(intI=0; i<m;i++){ Aboutscanf"%d%d",&u,&v); Add_edge1 (u,v); the         } the          for(intI=1; i<=n;i++) {//finding strong connected components the             if(dfn[i]==0) Tarjan (i); +         } -          for(intI=1; i<=m;i++){ the             intu=e[i].u,v=e[i].v;Bayi             if(belong[u]!=Belong[v]) { the Add_edge2 (Belong[u],belong[v]); thechudu[belong[u]]++; -             } -         } the          for(intI=1; i<=cur;i++){ the             if(chudu[i]==0) the Apki (i); the         } - sort (Ans.begin (), Ans.end ()); the          for(intI=0; I<ans.size (); i++) printf ("%d", Ans[i]); theprintf"\ n"); the     }94     return 0; the}

Thought: And POJ2762 not much difference

POJ 2553 The Bottom of a Graph

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.