UVA 1324 the largest clique largest regiment (strongly connected components, deformed)

Source: Internet
Author: User

Test instructions: Give a directional graph, ask to find some points, make any point in these points pairs, either can be interoperable, or one-way to reach.

Idea: The minimum requirement is one-way can reach, namely a->b can be counted in.

The points within the strongly connected components are sure to meet the requirements, can be selected all, but there are a number of strong connected components when it is not, there must be a choice. The old method, the first contraction point, after the point is not the existence of the ring, so is the topology map. If you only give a topology map, the need to find a chain to make the most points on the chain, then you can judge the topology, one by one, the degree of 0 points to delete, and at the time of deletion record the maximum number of points, delete the last point when the results. The same approach applies, except that each point may be a pinch point, and the points within these indents are counted.

Realize:

(1) To find strong connected components.

(2) Statistical reduction of the degree of the point and build (indent) diagram.

(3) The number of points is counted according to the way of judging topological map.

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 using namespacestd;5 Const intn= ++5;6 Const intinf=0x7f7f7f7f;7vector<int> Vect[n], g[n];//picture of the original, after the indentation8 intN, M;9 intDfn[n], Lowlink[n], scc_no[n], Dfn_clock, scc_cnt;//Strong connectivity Component PrerequisitesTenstack<int> Stac;//Strong Unicom Component Stack Oneunordered_map<int,int> Chu[n],ru[n];//just to prevent duplication of statistics A intR[n];//Degree of Access - intNum[n];//Number of points in the strong Unicom component - intDp[n];//Answer the  - voidDFS (intx) - { - Stac.push (x); +dfn[x]=lowlink[x]=++Dfn_clock; -      for(intI=0; I<vect[x].size (); i++) +     { A         intt=Vect[x][i]; at         if(!Dfn[t]) -         { - DFS (t); -lowlink[x]=min (lowlink[x],lowlink[t]); -         } -         Else if(!scc_no[t]) lowlink[x]=min (lowlink[x], dfn[t]); in     } -     if(lowlink[x]==Dfn[x]) to     { +++scc_cnt; -          while(true) the         { *             intt=stac.top (); Stac.pop (); $scc_no[t]=scc_cnt;Panax Notoginseng             if(t==x) Break; -         } the     } + } A  the  + intcal () - { $memset (DFN,0,sizeof(DFN)); $memset (Lowlink,0,sizeof(Lowlink)); -memset (Scc_no,0,sizeof(Scc_no)); -Dfn_clock=scc_cnt=0; the      for(intI=1; i<=n; i++)if(!Dfn[i]) DFS (i); - Wuyi     if(scc_cnt==1)returnN; the      for(intI=1; i<=scc_cnt; i++) G[i].clear (), Chu[i].clear (), Ru[i].clear (); -      for(intI=1; i<=n; i++)//statistical degrees, building maps Wu     { -          for(intj=0; J<vect[i].size (); J + +) About         { $             intt=Vect[i][j]; -             if(scc_no[i]!=Scc_no[t]) -             { -                 if(!chu[scc_no[i]][scc_no[t]])//it hasn't happened yet. A                 { +chu[scc_no[i]][scc_no[t]]=1; the G[scc_no[i]].push_back (scc_no[t]); -                 } $ru[scc_no[t]][scc_no[i]]=1; the             } the         } the     } thedeque<int>que; -Memset (R,0,sizeof(R)); in      for(intI=1; i<=scc_cnt; i++)//statistical degree of access the     { ther[i]=ru[i].size (); About         if(!R[i]) que.push_back (i); the     } the  thememset (NUM,0,sizeof(num)); +      for(intI=1; i<=n; i++) num[scc_no[i]]++;//Statistics Points -  theMemset (DP,0,sizeof(DP));//DP by topological orderBayi     intans=0; the      while(!que.empty ()) the     { -         intsiz=que.size (); -          for(intI=0; i<siz; i++)//all nodes with a 0 entry level the         { the             intt=Que.front (); Que.pop_front (); theAns=max (ans,dp[t]+num[t]); the              for(intj=0; J<g[t].size (); J + +)//each side that starts with T -             { the                 intD=G[t][j]; ther[d]--; the                 if(!R[d]) que.push_back (d);94Dp[d]=max (dp[d],dp[t]+num[t]); the             } the         } the     }98     returnans; About } - 101 102 intMain ()103 {104     //freopen ("Input.txt", "R", stdin); the 106     intT, a, B;107Cin>>T;108      while(t--)109     { thescanf"%d%d", &n, &m);111          for(intI=1; i<=n; i++) vect[i].clear (); the          for(intI=0; i<m; i++)113         { thescanf"%d%d",&a,&b); the Vect[a].push_back (b); the         }117Cout<<cal () <<Endl;118     }119     return 0; -}
AC Code

UVA 1324 the largest clique largest regiment (strongly connected components, deformed)

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.