HDU 4612 Warm up (double connected component indent + tree diameter)

Source: Internet
Author: User

Idea: Strong connected component contraction point, build a new tree, and then find the longest diameter of the tree, and then add a side can remove the number of bridges, is the length of the diameter.

Tree diameter length of the method: two times BFS can be begged, for the first time to find a point u, and then the BFS search to the last point V, must be a diameter of the end (proof withheld), the second point V for the beginning of the BFS, the last point to find out, is the diameter of another endpoint, Record depth is the length of our request. I am here to use the Bfs+dfs, is the same, less open a deep array, save space bar ...

In fact, I do not want to start from the beginning, I thought that a leaf node can do the end point, hand it to WA, was also curious to feel right, the result of a random painting, it is wrong ...

Note: There is a heavy edge in this problem, we can go back to the Father node by the heavy side, but not through the father side back to the Father node, so the original way to mark the father is not possible, here to mark the edge, in the edge structure to add a vis variable, to mark whether this side has been visited, So the heavy side will be able to access the normal.

The problem of data volume is stronger, easy to explode the stack, the usefulness of STL is not strong, so try to use the array simulation, the array can open to a larger space.

Feelings: Feel this piece of the topic really love error, this problem is WA a good many times before, feel OJ don't love me ...

#include <iostream>#include<cstdio>#include<cstring>#include<vector>using namespacestd;#defineN 200020#defineM 2000020structedge{intTo,nxt,vis;} EDGE[M];intHead[n],dfn[n],low[n],vis[n],sta[n],id[n];intTot,all,top,scc,bridge;voidAddedge (intAintBintflag) {edge[tot].to=b; EDGE[TOT].NXT=Head[a]; Edge[tot].vis=Flag; Head[a]= tot++;}voidTarjan (intu) {Vis[u]=1; Dfn[u]= Low[u] = + +All ; Sta[top++] =u;  for(inti = Head[u]; I! =-1; i =edge[i].nxt) {        if(Edge[i].vis)Continue; intv =edge[i].to; Edge[i].vis= edge[i^1].vis =1; if(!Dfn[v])            {Tarjan (v); Low[u]=min (low[u],low[v]); if(Low[v] > Dfn[u]) bridge++; }        Else if(Vis[v]) low[u] =min (low[u],dfn[v]); }    if(Low[u] = =Dfn[u]) {SCC++; intnum;  Do{num= sta[--top]; Id[num]=SCC; Vis[num]=0; }         while(U! =num); }}vector<int>Tree[n];intTreevis[n],maxdeep;voidDfsintUintDeep ) {Treevis[u]=1; Maxdeep=Max (deep,maxdeep); intLen =tree[u].size ();  for(inti =0; i < Len; i++)    {        intv =Tree[u][i]; if(!Treevis[v]) {DFS (V,deep+1); }    }}voidinit () {memset (Vis,0,sizeof(VIS)); memset (DFN,0,sizeof(VIS)); memset (ID,0,sizeof(ID)); memset (Low,0,sizeof(low)); All=0; Top=0; SCC=0; Bridge=0;}intQue[n];intGetstart () {intfront,rear; Front= Rear =0; Que[rear++] =1; treevis[1] =1;  while(Front! =rear) {        intnow = que[front++]; intLen =tree[now].size ();  for(inti =0; i < Len; i++)        {            intNXT =Tree[now][i]; if(TREEVIS[NXT])Continue; TREEVIS[NXT]=1; Que[rear++] =NXT; }    }    returnque[--rear];}intMain () {intn,m,a,b;  while(~SCANF ("%d%d",&n,&m)) {if(!n &&!m) Break; memset (Head,-1,sizeof(head)); Tot=0;  for(inti =0; I < m; i++) {scanf ("%d%d",&a,&b); Addedge (A, B,0); Addedge (B,a,0);        } init (); Tarjan (1);  for(inti =1; I <= SCC; i++) {tree[i].clear (); }         for(intU =1; U <= N; u++)        {             for(inti = Head[u]; I! =-1; i =edge[i].nxt) {                intv =edge[i].to; if(Id[u]! =Id[v]) {                    intUU =Id[u]; intVV =Id[v];                    Tree[uu].push_back (VV);                Tree[vv].push_back (UU); } }} maxdeep=0; memset (Treevis,0,sizeof(Treevis)); intStart =Getstart (); memset (Treevis,0,sizeof(Treevis)); DFS (Start,1); printf ("%d\n", scc-maxdeep); }    return 0;}

HDU 4612 Warm up (double connected component indent + tree diameter)

Related Article

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.