hdu-3836 equivalent sets (strong connectivity)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=3836

The judgment requires at least a few edges to make the graph strong and connected.

The point after which the graph is indented and the points with a degree of 0 are counted, and the maximum value in both is the edge that needs to be connected,

For example, assuming that there are more than 0 points, each time a point with a degree of 0 is connected to a point with an edge of 0, a ring is formed.

So a strong connected component is formed, and the same can be obtained with a degree of more than 0 points.

This code with g++ Re, C + + in order to AC.

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <algorithm>7#include <string>8#include <Set>9#include <functional>Ten#include <numeric> One#include <sstream> A#include <stack> -#include <map> -#include <queue> the  - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) -  - #definell Long Long + #defineINF 0x7f7f7f7f - #defineLC L,m,rt<<1 + #defineRC M + 1,r,rt<<1|1 A #definePi ACOs (-1.0) at  - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) - #defineMax (x, y) (x) < (y)? (y): (x) in #defineE (x) (1 << (x)) - #defineIabs (x) (x) < 0? -(x): (x) to #defineOut (x) printf ("%i64d\n", X) + #defineLowbit (x) (x) & (-X) - #defineRead () freopen ("A.txt", "R", stdin) the #defineWrite () freopen ("Dout.txt", "w", stdout); *  $ using namespacestd;Panax Notoginseng #defineN 20100 - //n is the maximum number of points the #defineM 150100 + //m is the maximum number of sides A intN, M;//N M is the number of points and sides the  + structedge{ -     int  from, to, NEX; $     BOOLSign//whether it is a bridge $}edge[m<<1]; - intHead[n], edgenum; - voidAddintUintV) {//the beginning and end of the edge theEdge E={u, V, Head[u],false}; -Edge[edgenum] =E;WuyiHead[u] = edgenum++; the } -  Wu intDfn[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) that can point to (the nearest ancestor V from the root ) (i.e., V-point timestamp) - intTaj//connecting branch designator, starting from 1 About intBelong[n];//Belong[i] represents the connected branch where I point belongs $ BOOLInstack[n]; -vector<int> Bcc[n];//marking starting from 1 -  - voidTarjan (intU,intFA) { ADfn[u] = Low[u] = + +Time ; +Stack[top + +] =u; theInstack[u] =1 ; -  $      for(inti = Head[u]; ~i; i =Edge[i].nex) { the         intv =edge[i].to; the         if(Dfn[v] = =-1) the         { the Tarjan (V, u); -Low[u] =min (Low[u], low[v]); in             if(Dfn[u] <Low[v]) the             { theEdge[i].sign =1;//to cut the bridge . About             } the         } the         Else if(Instack[v]) Low[u] =min (Low[u], dfn[v]); the     } +     if(Low[u] = =Dfn[u]) { -         intNow ; theTaj + +; Bcc[taj].clear ();Bayi          Do{ thenow = stack[--top]; theInstack[now] =0 ; -Belong [Now] =Taj; - Bcc[taj].push_back (now); the} while(Now! =u); the     } the } the  - voidTarjan_init (intAll ) { thememset (DFN,-1,sizeof(DFN)); thememset (Instack,0,sizeof(Instack)); thetop = time = Taj =0;94      for(intI=1; i<=all;i++)if(dfn[i]==-1) Tarjan (i, I);//Notice the start point!!!  the } thevector<int>G[n]; the intDu[n];98 voidSuodian () { AboutMemset (Du,0,sizeof(du)); -      for(inti =1; I <= Taj; i++) g[i].clear ();101      for(inti =0; i < Edgenum; i++){102         intU = belong[edge[i]. from], V =belong[edge[i].to];103         if(u!=v)104         { theG[u].push_back (v), du[v]++;106            //printf ("%d%d\n", u,v);107         }108     }109 } the voidInit () {memset (head,-1,sizeof(head)); edgenum=0;}111 intMain () the {113     //Read (); the     intb; the      while(~SCANF ("%d%d",&n,&m)) the     {117 init ();118         //scanf ("%d%d", &n,&m);119          for(intI=0; i<m;i++) -         {121scanf"%d%d",&a,&b);122 Add (A, b);123         }124 tarjan_init (n); the Suodian ();126         if(taj==1) {printf ("0\n");Continue;}//The diagram is strong connectivity .127         intx1=0, x2=0; -          for(intI=1; i<=taj;i++)129         { the             if(g[i].size () = =0) x1++;//number of points with a degree of 0131             if(du[i]==0) x2++;//the number of degrees is 0 points the         }133         //printf ("%d%d\n", x1,x2);134printf"%d\n", Max (x1,x2));135     }136     return 0;137}

hdu-3836 equivalent sets (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.