POJ 2186 (strong connected component entry)

Source: Internet
Author: User

Portal: Problem 2186

Https://www.cnblogs.com/violet-acmer/p/9739990.html

Test instructions

  Every cow wants to be a Red Bull in the herd.

Given the herd of n cows and M-ordered pairs (A, B), (A, b) that the cow a thinks that cow B is a red man, the relationship is transitive, so if cow a thinks cow B is a red man, Ox B thinks that Ox C is the red man, then bull a also thinks that Ox C is a red.

However, a given ordered pair may contain (a, b) and (b, c), but do not contain (a, c).

The total number of cows that are considered to be reds by all other cows.

Analysis:

Consider a direction graph with the apex of an ox, and a forward edge from A to B for each ordered pair (a, b), then the vertex corresponding to the cow that all other cows consider to be red, that is, vertices that can be reached from all other vertices.

Although this can be obtained by searching from each vertex, the total complexity is O (NM), which is not feasible and must be considered for more efficient algorithms.

If two cows A and B are considered to be reds by all other cows, it is obvious that a is considered a red man by B and B is a red man, that is, there is a circle with a and b two vertices, or a and B belong to a strong connected component. Conversely, if a cow is considered a red by all the other cows, all the cows in its strong connected component are considered to be reds by all other cows.

Therefore, after the strong connected component decomposition of the graph, at most, there is a strong connected component satisfies the condition of the problem.

We can also get the order of the topological ordering of each strong connected component when the strong connected component is decomposed by the algorithm described earlier,[1] the only strong connected component that can become the solution only the final topological order.

So in the end, we just have to check if this strong connected component is up from all the vertices.

The complexity of the algorithm is O (n+m), enough to solve the original problem within the time limit.

The above analysis comes from the Challenge Program design contest.

Understanding of [1] :

The characteristics of strong connected components satisfying the conditions are:

(1) Out of 0

(2) The remaining nodes will either indirectly or directly point to any node of this strongly connected component

Combined with the role of Vector vs, in DFS (), the first node stored in VS is definitely a node in the strongly connected component that satisfies the condition, and in VS, the higher the topological order of the forward node.

AC Code:

1#include <iostream>2#include <vector>3#include <cstdio>4#include <cstring>5 using namespacestd;6 Const intmaxv=1e4+Ten;7 Const intmaxe=5e4+Ten;8 9 intn,m;Ten BOOLVIS[MAXV]; Onevector<int>vs; Avector<int>Edge[maxe],redge[maxe]; -  - voidAddedge (intUintv) the { - Edge[u].push_back (v); - redge[v].push_back (u); - } + voidDfs (intu) - { +vis[u]=true; A      for(intI=0; i < edge[u].size (); + +i) at     { -         intto=Edge[u][i]; -         if(!Vis[to]) - Dfs (to); -     } - vs.push_back (u); in } - voidRDfs (intu) to { +vis[u]=true; -      for(intI=0; i < redge[u].size (); + +i) the     { *         intto=Redge[u][i]; $         if(!Vis[to])Panax Notoginseng RDfs (to); -     } the } + voidScc () A { thememset (Vis,false,sizeofvis); + vs.clear (); -      for(intI=1; I <= n;++i) $         if(!Vis[i]) $ Dfs (i); -  -memset (Vis,false,sizeofvis); theSameid=0; -      for(intI=vs.size ()-1; I >=0;--i)Wuyi     { the         intv=Vs[i]; -         if(!Vis[v]) Wu         { -sameid++; About RDfs (v); $         } -     } - } - intSolve () A { + Scc (); the     intres=0; -     intu; $      for(intI=1; I <= n;++i) the         if(Scc[i] = =Sameid) the         { theres++;//the number of strong connected components at the end of topological sequence theU=i;//u = = Topology Sort last point -         } inmemset (Vis,false,sizeofvis); the rDfs (u); the      for(intI=1; I <= n;++i) About         if(!vis[i])//Judging if all is up to theres=0; the     returnRes; the } + intMain () - { thescanf"%d%d",&n,&M);Bayi      for(intI=1; I <= m;++i) the     { the         intu,v; -scanf"%d%d",&u,&v); - Addedge (u,v); the     } theprintf"%d\n", Solve ()); the     return 0; the}
View Code

POJ 2186 (strong connected component entry)

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.