CSU 1526 Beam Me out! Strong connectivity

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

A direction graph for a given n points (1 is the starting point and N is the end point)

Each of the following lines gives the degree of a point and the next point to which it is connected.

The nth point is not out of the degree

The picture is like this: 1->2, 1->3, 2->3

First question:

If there is a scheme that allows this person to enter a point and no longer reach the end of the output PRISON, otherwise the output pardon

Second question:

If this person can walk infinite steps in the diagram output unlimited, otherwise the output limited

#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath > #include <cstring> #include <queue> #include <set> #include <map> #include <vector> Template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-') && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} Using namespace Std;typedef long Long ll;typedef pair<int, int> pii;const int N = 50500;const int inf = 1e8;//n is the maximum number of points const int M = 7500000;//m is the maximum number of sides int n;//n M is the number of points and sides of the struct edge{int from, to, Nex;bool sign;//is the bridge}edge[m];int Head[n], Edgenu m;void Add (int u, int v) {//the start and end point of the Edge E = {u, V, head[u], false};Edge[edgenum] = e;head[u] = edgenum++;} int dfn[n], low[n], Stack[n], top, time; Low[u] is the dfn[v of the point set {U-point and subtree in the root of the U-point (all reverse arcs) can be pointed (that is, the nearest ancestor V from the root) value (i.e. V-point timestamp) int taj;//Connected Branch label, starting from 1 int belong[n];//belong[i] Indicates that I point belongs to the connected branch bool Instack[n];vector<int> Bcc[n]; Marking starting from 1 void Tarjan (int u, int fa) {Dfn[u] = low[u] = ++time; stack[top++] = U;instack[u] = 1;for (int i = head[u]; ~i; i = edge[i].nex) {int v = edge[i].to;if (dfn[v] = =-1) {Tarjan (V, u); Low[u] = min (Low[u], low[v]), if (Dfn[u] < Low[v]) {edge[i].sign = 1;//for Cut bridge}}else if (Instack[v]) low[u] = min (Low[u], DFN [v]);} if (low[u] = = Dfn[u]) {int now;taj++; Bcc[taj].clear ();d O{now = Stack[--top];instack[now] = 0; Belong[now] = Taj;bcc[taj].push_back (now);} while (now! = u);}}  void Tarjan_init (int all) {memset (DFN,-1, sizeof (DFN)), memset (instack, 0, sizeof (instack)), top = time = Taj = 0;for (int i = 1; I <= all; i++) if (dfn[i] = =-1) Tarjan (i, I); Notice the start point!!! }vector<int>g[n];int Du[n], Hehe[n];bool itself[n];void Suodian () {memset (Du, 0, sizeof (du)); foR (int i = 1; I <= Taj; i++) {g[i].clear (); Hehe[i] = false;for (int j = 0; J < Bcc[i].size (), j + +) if (itself[bcc[i][j ]]) hehe[i] = true;} for (int i = 0; i < Edgenum; i++) {int u = belong[edge[i].from], v = belong[edge[i].to];if (U = v) g[u].push_back (v), Du [V]++;}} void Init () {memset (head,-1, sizeof (head)); edgenum = 0;} BOOL Vis[n];int siz, Bad;void bfs () {memset (Vis, 0, sizeof vis);queue<int> Q; Q.push (belong[1]); vis[belong[1]] = Tru E;siz = 0;bad = 0;while (!q.empty ()) {int u = q.front (); Q.pop ()//printf ("--%d\n", u); if ((int) bcc[u].size () > 1 | | he He[u]) siz++;if ((int) g[u].size () = = 0) {if (U! = belong[n]) bad++;} for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];//printf ("**%d\n", V), if (!vis[v]) {Vis[v] = True;q.push (v);}} }int Main () {while (CIN >> N) {init (); for (int i = 1, num, j; i < n; i++) {Itself[i] = false;rd (num); while (num--& gt;0) {Rd (J), Add (i, J), if (j = = i) itself[i] = true;}} Itself[n] = False;tarjan_init (n); Suodian ();//for (int i = 1; i <= N; i++) printf ("%d", belong[i]); Puts (""); BFS ("");//printf ("leaf:%d siz:%d\n", Leaf, siz); if (Vis[belong[n]] && bad = = 0) printf ("pardon"); else pri NTF ("PRISON");! Siz? Puts ("LIMITED"): Puts ("UNLIMITED");} return 0;} /*3213*/


CSU 1526 Beam Me out! Strong connectivity

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.