UVA 10510 Cactus

Source: Internet
Author: User

Test instructions: Give a direction graph, ask is not cactus diagram. Cactus diagram: A strongly connected graph of each side only within a normal ring.

Solution: Tarjan Determine whether the strong connected component is 1, record the path of the loop, the number of times to traverse the path record point when each ring is found, if a bit is recorded two times, it means that there is not only one ring in the edge.

Code:

#include <stdio.h>#include<iostream>#include<algorithm>#include<string>#include<string.h>#include<math.h>#include<limits.h>#include<time.h>#include<stdlib.h>#include<map>#include<queue>#include<Set>#include<stack>#include<vector>#defineLL Long Longusing namespacestd;intN, M;vector<int> edge[20005];intdfn[20005], low[20005];intCNT =1;intAns =0;intfa[20005], out[20005];intFlag =1;voidSignintVintu) {     while(Fa[u]! =v) { out[u]++; if( out[U] >1) {flag=0; return ; } u=Fa[u]; }}voidTarjan (intv) {Dfn[v]= Low[v] = cnt++; intLen =edge[v].size ();  for(inti =0; i < Len; i++)    {        intU =Edge[v][i]; if(!Dfn[u]) {Fa[u]=v;            Tarjan (U); LOW[V]=min (Low[v], low[u]); }        Else{Low[v]=min (Low[v], dfn[u]);            sign (U, v); if(!flag)return ; }    }    if(Dfn[v] = =Low[v]) {        inttmp; Ans++; if(Ans >1) {flag=0; return ; }    }}intMain () {intT; scanf ("%d", &T);  while(t--) {CNT=1; Ans=0; Flag=1; scanf ("%d%d", &n, &m);  for(inti =0; I < n; i++) edge[i].clear ();  for(inti =0; I < m; i++)        {            intV, u; scanf ("%d%d", &v, &T);        Edge[v].push_back (U); } memset (DFN,0,sizeofDFN); memset (FA,0,sizeofFA); memset ( out,0,sizeof  out);  for(inti =0; I < n; i++)            if(!Dfn[i]) Tarjan (i); if(flag) printf ("yes\n"); Elseprintf ("no\n"); }    return 0;}

UVA 10510 Cactus

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.