POJ 2186 Popular Cows (strong unicom component)

Source: Internet
Author: User

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

Main topic:

The desire of every cow is to become one of the most popular cows. Now there are n cows, give you m-pairs of integers (a, a, b), indicating that cow A is considered to be popular. This relationship is transitive, and if a thinks B is popular, B thinks C is popular, then bull a also thinks that Ox C is popular. Your task is to find out how many cows are considered popular by all cows. Problem Solving Ideas:Suppose there are two cows A and B are all the other cows are considered to be red, then obviously, a by B is considered to be a red, B is also considered a red, that is, there is a and B two vertices of the circle, or a, b belong to a strong unicom component. So if a cow is considered a red-bull by all the other cows, all the cows within its strong unicom component are considered to be reds by all other cows. After we have a strong connectivity component decomposition, at most, there is a strong unicom component to meet the conditions of the problem. Practice: First use Tarjan to find each strong connected components, and then shrink points, statistics of each point out, if there is only 1 out of 0 points, the output of this point contains the number of nodes, otherwise output 0. Code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6#include <stack>7 using namespacestd;8 Const intn=1e4+5;9 Ten intCnt,num; One intDfn[n],low[n],fa[n],sze[n],outdeg[n]; Astack<int>SK; -vector<int>V[n]; -  the voidTarjan (intu) { -dfn[u]=low[u]=++CNT; - sk.push (u); -      for(intI=0; I<v[u].size (); i++){ +         intt=V[u][i]; -         if(!dfn[t]) {//Point T is not accessed + Tarjan (t); Alow[u]=min (low[u],low[t]); at         } -         Else if(!fa[t]) low[u]=min (low[u],dfn[t]);//Point T has been accessed and T is still in the stack -     } -     if(low[u]==Dfn[u]) { -num++; -          while(1){ in             intt=sk.top (); - Sk.pop (); toFa[t]=num;//indent operations, and place these points as point Num +sze[num]++; -             if(T==u) Break; the         } *     } $ }Panax Notoginseng  - intMain () { the     intn,m; +scanf"%d%d",&n,&m); A      for(intI=1; i<=m;i++){ the         intb; +scanf"%d%d",&a,&b); - V[a].push_back (b); $     } $      for(intI=1; i<=n;i++){ -         if(!Dfn[i]) Tarjan (i); -     } the      for(intI=1; i<=n;i++){ -          for(intj=0; J<v[i].size (); j + +){Wuyi             intt=V[i][j]; the             if(Fa[t]!=fa[i]) outdeg[fa[i]]++; -         } Wu     } -     //A point with a scale of 0 can only have one after the point is shrunk, otherwise it does not meet the conditional output 0 About     intans=0; $      for(intI=1; i<=num;i++){ -         if(!Outdeg[i]) { -             if(ans>0){ -Puts"0"); A                 return 0; +             } theans=Sze[i]; -         } $     } theprintf"%d\n", ans); the     return 0; the}

POJ 2186 Popular Cows (strong unicom component)

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.