Hdu_3836 equivalent set (Trajan strongly connected component contraction point)

Source: Internet
Author: User

Calculate the strongly connected component and reduce the point. Obtain the Dag of The New Graph and calculate the max value in the DAG (number of points with an inbound degree of 0 and number of points with an outbound degree of 0 );

Slag code:

View code

1 # include <iostream>
2 # include <cstdio>
3 # include <cstring>
4 # include <stack>
5
6 using namespace STD;
7
8 const int n = 20010;
9 const int M = 50010;
10
11 struct node {
12 INT;
13 node * next;
14} * First [m];
15
16 int head [N], blong [N];
17 int dfn [N], low [N];
18 int in [N], out [N];
19 int CNT, T, IND;
20 bool vis [N];
21
22 stack <int> S;
23
24 void Init (){
25 memset (Head, 0, sizeof (head ));
26 memset (blong, 0, sizeof (blong ));
27 memset (dfn, 0, sizeof (dfn ));
28 memset (low, 0, sizeof (low ));
29 memset (Out, 0, sizeof (out ));
30 memset (VIS, 0, sizeof (VIS ));
31 memset (in, 0, sizeof (in ));
32 memset (first, null, sizeof (first ));
33
34 t = 1; ind = CNT = 0;
35}
36
37 void add (int u, int v ){
38 node * TMP = new node;
39 TMP-> to = V;
40 TMP-> next = first [u];
41 First [u] = TMP;
42}
43
44 void Tarjan (int u ){
45 int V;
46 vis [u] = true;
47 s. Push (U );
48 low [u] = dfn [u] = ++ ind;
49 for (node * Pos = first [u]; pos! = NULL; Pos = pos-> next ){
50 V = pos->;
51 if (! Dfn [v]) {
52 Tarjan (v );
53 low [u] = min (low [v], low [u]);
54} else if (vis [v]) {
55 low [u] = min (low [u], low [v]);
56}
57}
58 If (low [u] = dfn [u]) {
59 CNT ++;
60 do {
61 V = S. Top (); S. Pop ();
62 blong [v] = CNT;
63 vis [v] = false;
64 // printf ("% d \ n", V, CNT );
65} while (V! = U );
66}
67}
68
69 int main (){
70 // freopen ("data. In", "r", stdin );
71
72 int n, m, A, B, I;
73 while (~ Scanf ("% d", & N, & M )){
74 Init ();
75 for (I = 1; I <= m; I ++ ){
76 scanf ("% d", & A, & B );
77 add (B, );
78}
79
80 for (I = 1; I <= N; I ++ ){
81 If (! Dfn [I]) Tarjan (I );
82}
83 If (CNT = 1) {printf ("0 \ n"); continue;} // note that when only one strongly connected component is used
84 for (I = 1; I <= N; I ++ ){
85 for (node * P = first [I]; P! = NULL; P = p-> next ){
86 A = p->;
87 If (blong [I]! = Blong [a]) {
88 in [blong [a] ++;
89 out [blong [I] ++;
90}
91}
92}
93 a = B = 0;
94 for (I = 1; I <= CNT; I ++ ){
95 If (! In [I]) A ++;
96 If (! Out [I]) B ++;
97}
98 printf ("% d \ n", max (a, B ));
99}
100 return 0;
101}
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.