POJ 2186 Popular Cows (kosaraju+ strong Unicom component template)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=2186

The main topic: given n cattle and M ordered pairs (A, B), (A, B) said that A Bull is a red man, the relationship has transitivity, if cow a think that cow B is a red, cow B is the bull C is the red, then cow A also think that the Ox C is the Reds. The total number of cows considered by all other cows to be Red Bull.

The idea of solving a problem: to treat all cows as vertices and order pairs (a, a, A, b) as a forward edge from a to a, the title becomes the total number of vertices that all vertices can reach. We can get a conclusion that if a strong connected component has a cow that is considered a red, then all the cows in the strong Unicom component are red, which is clearly correct. Since I use Kosaraju to find strong unicom components, according to the algorithm nature, Red Bull will only be in the final topological sequence of strong unicom components, I only need to locate the last piece of strong unicom components, take one of the vertices, to see if all the points can be reached.

1#include <iostream>2#include <cstring>3#include <algorithm>4#include <vector>5 using namespacestd;6 Const intn=1e4+5;7 8vector<int>G[N];//adjacency Table of graphs9vector<int>rG[N];//adjacency table for reverse graphsTenvector<int>vs;//list of vertices of sequential traversal order One BOOLUsed[n];//whether the record point is accessed A intCmp[n];//Cmp[i] represents the topological sequence of the strong unicom component of point I -  - intv,e; the  - voidAddedge (intUintv) { - G[u].push_back (v); - rg[v].push_back (u); + }  -  + voidDfsintv) { Aused[v]=true; at      for(intI=0; I<g[v].size (); i++){ -         if(!Used[g[v][i]]) - DFS (G[v][i]); -     } -     //marking prior to backtracking - Vs.push_back (v); in } -  to voidRdfsintVintk) { +used[v]=true; -     //Point v belongs to the K-strong connected component thecmp[v]=K; *      for(intI=0; I<rg[v].size (); i++){ $         if(!Used[rg[v][i]])Panax Notoginseng Rdfs (rg[v][i],k); -     } the } +  A intSCC () { thememset (Used,false,sizeof(used)); + vs.clear (); -     //First time Dfs $      for(intI=1; i<=v;i++){ $         if(!Used[i]) - DFS (i); -     } thememset (Used,false,sizeof(used)); -     intk=0;//number of strong Unicom component blocksWuyi     //Dfs for the second time the      for(intI=vs.size ()-1; i>=0; i--){ -         if(!Used[vs[i]]) WuRdfs (vs[i],++k); -     } About     returnK; $ } -  - voidsolve () { -     //get the number of strong unicom blocks A     intn=SCC (); +     //number of statistical alternative solutions the     intu=0, num=0; -      for(intI=1; i<=v;i++){ $         if(cmp[i]==N) { theu=i; thenum++; the         } the     } -     //Check if all points are up to inmemset (Used,0,sizeof(used)); theRdfs (U,0); the      About      for(intI=1; i<=v;i++){ the         if(!Used[i]) { thenum=0; the              Break; +         } -     } the      for(intI=1; i<=v;i++){Bayicout<<"i="<<i<<"cmp="<<cmp[i]<<Endl; the     } theprintf"%d\n", num); - } -  the intMain () { thescanf"%d%d",&v,&E); the      for(intI=1; i<=e;i++){ the         intu,v; -scanf"%d%d",&u,&v); the Addedge (u,v); the     } the solve ();94     return 0;  the}

POJ 2186 Popular Cows (kosaraju+ strong Unicom component template)

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.