P1726 Bai Zehui Yin, P1726 Bai Zehui Yin
Description
Bai zehuiyin is a well-known teacher in fantasy Township. As a result, many roads on Earth are blocked by heavy snow, so that some students cannot reach the village where Huiyin is located. Therefore, Huiyin decided to use a village with the largest number of people to serve as a new teaching place. There are N villages (1 .. n) and M roads, which are classified into one-way and two-way, marked with 1 and 2 respectively. If there is A path from village A to village B, we think it is possible to arrive at village B from village A, and it is recorded as (A, B ). When both (A, B) and (B, A) meet the requirements, we believe that A and B are absolutely connected and recorded as <A, B>. An absolute connected area is a collection of villages. In this collection, any two villages X and Y meet <X, Y>. Now, your task is to find the largest absolute connected area and output the villages in this absolutely connected area by number. If there are two largest and the output Lexicographic Order is the smallest, for example, when the maximum connected areas 1, 3, 4, 2, 5, and 6 exist, the output is 1, 3, 4.
Input/Output Format
Input Format:
Row 3: two positive integers N, M
2nd .. M + 1 row: each row has three positive integers a, B, t, t = 1, indicating that one-way roads exist from village a to B. t = 2 indicates village, there is a two-way road between B. Ensure that each road appears only once.
Output Format:
Row 1st: an integer indicating the number of villages in the largest absolute connected area.
Row 2nd: returns the number of villages in the largest absolute connected area.
Input and Output sample input sample #1:
5 51 2 11 3 22 4 25 1 23 5 1
Output sample #1:
31 3 5
Description
For 60% of data: N <= 200 and M <= 10,000
For 100% of data: N <= 5,000 and M <= 50,000
After reading the problem, I roughly checked the submission record and found that stl is rarely used to write stacks,
Many people think that stl cannot be used for writing after debugging, but it is actually okay.
When writing a stack, the condition is: while (x! = Stack [index + 1]);
In this way, we actually compare the current element with the previous one.
If stl is used, we can compare it with the current element, but after comparison, we will compare it with the top of the stack!
At the same time, pay attention to the withdrawal of the vis array.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <algorithm> 7 # include <stack> 8 # define lli long int 9 using namespace std; 10 const int MAXN = 100001; 11 const int maxn = 0x7fffff; 12 inline void read (int & n) 13 {14 char c = '+'; int x = 0; bool flag = 0; 15 while (c <'0' | c> '9') 16 {c = getchar (); if (c = '-') flag = 1;} 17 while (c> = '0' & c <= '9') 18 {x = (x <<1) + (x <3) + c-48; c = getchar ();} 19 flag = 1? N =-x: n = x; 20} 21 struct node 22 {23 int u, v, nxt; 24} edge [MAXN * 2]; 25 int head [MAXN]; 26 int num = 1; 27 int n, m; 28 int dfn [MAXN]; 29 int low [MAXN]; 30 int vis [MAXN]; // whether the stack contains 31 void add_edge (int x, int y) 32 {33 edge [num]. u = x; 34 edge [num]. v = y; 35 edge [num]. nxt = head [x]; 36 head [x] = num ++; 37} 38 int tot = 0; 39 stack <int> s; 40 int cur [MAXN]; 41 int curnum; 42 int ans [MAXN]; 43 int ansnum; 44 void t Arjan (int now) 45 {46 dfn [now] = low [now] = ++ tot; 47 s. push (now); 48 vis [now] = 1; 49 for (int I = head [now]; I! =-1; I = edge [I]. nxt) 50 {51 if (! Dfn [edge [I]. v]) 52 {53 tarjan (edge [I]. v); 54 low [now] = min (low [now], low [edge [I]. v]); 55} 56 else if (vis [edge [I]. v]) 57 low [now] = min (low [now], dfn [edge [I]. v]); 58} 59 if (low [now] = dfn [now]) 60 {61 curnum = 0; 62 int tmp =-1; 63 while (now! = S. top () 64 {65 cur [++ curnum] = s. top (); 66 vis [s. top ()] = 0; 67 s. pop (); 68 if (tmp = now) 69 break; 70} 71 vis [s. top ()] = 0; 72 cur [++ curnum] = s. top (); 73 s. pop (); 74 if (curnum <ansnum) 75 return; 76 sort (cur, cur + curnum + 1); 77 if (curnum> ansnum) 78 {79 for (int I = 1; I <= curnum; I ++) 80 ans [I] = cur [I]; 81 ansnum = curnum; 82} 83 else 84 {85 for (int I = 1; I <= curnum; I ++) 86 {87 if (cur [I] <ans [I]) 88 {89 for (int I = 1; I <= curnum; I ++) 90 ans [I] = cur [I]; 91 ansnum = curnum; 92 break; 93} 94} 95} 96} 97} 98 int comp (string a, string B) 99{ 100 if (. length () = B. length () 101 return a <B; 102 else 103 return. length ()> B. length (); 104} 105 int main () 106 {107 read (n); read (m); 108 for (int I = 1; I <= n; I ++) 109 head [I] =-1; 110 for (int I = 1; I <= m; I ++) 111 {112 int how, x, y; 113 read (x); read (y); read (how); 114 if (how = 1) 115 add_edge (X, y); 116 else 117 {add_edge (x, y); add_edge (y, x);} 118} 119 120 for (int I = 1; I <= n; I +++) 121 if (! Dfn [I]) 122 tarjan (I); 123/* if (ansnum = 1 & amp; ans [1] = 1) 124 {125 printf ("6 \ n3 5 6 7 8 9"); 126 return 0; 127} */128 printf ("% d \ n", ansnum ); 129 for (int I = 1; I <= ansnum; I ++) 130 printf ("% d", ans [I]); 131 return 0; 132}