HDU 5409 CRB and Graph

Source: Internet
Author: User

Test instructions

Give an image without a direction.

Ask to delete each edge, if there is a pair (u,v) St Delete this edge, U and V are not connected, and u<v, if there are many pairs, the output U maximum, and then v the smallest.

Data range 10^5

Ideas:

First, it is obvious to use Tarjan to deal with each strong connected block first. By the way, what is the maximum number of points in each block? The Belong[x] indicates which block of x this point belongs to.

For an edge, if he is not a bridge, then delete him will not appear u,v, the result is 0 0

If it's a bridge, add a new image to the Edge (Belong[x], belong[y])

Then we got a tree after we finished.

If the bridge side: Too (wo) Qiao (tai) Miao (Chun) provoke.

The first consensus is: if I this block, the largest point is maxn[i], then point maxn[i]+1 and not in I this block.

Similarly, if maxn[i] represents the maximum value on the I subtree, then point maxn[i]+1 is not on this I subtree

Then process:

1. Treat the tree as the root of the block where N is located. The next thing is to treat the block as a dot because of typing trouble = =

2. Deal with Maxn[i] is a subtree of I is worth the maximum value is maxn[i] = Max{maxn[j] | I is the ancestor of J}, this can be processed by a simple tree-shaped DP. By the way, the depth of each point is processed.

3. Proceed to the previous enumeration of each edge of the original. For points on both sides of the edge:

(1) belong to the same block, the answer is as above.

(2) belong to different blocks. The block they are located in is U, v. It can be known that in the tree just now, u must be the father of V or V must be the father of U (the side of the tree directly connected them), in order to express the convenience we set U is father. After deleting the edge, it becomes a V subtree and a piece other than the V subtree. Because there is maxn[v] <= Maxn[u], and there is maxn[v] + 1 is not on the V subtree, so maxn[v] + 11 on another piece, that is, they are separated, then this is a set of feasible solutions. How does it prove to be the biggest? = = because Maxn[v] is the maximum value of the V subtree, no other value is bigger than him. And will not choose maxn[v] = n because v must be a son, and N is on the root of the whole tree, his block must not be a son.

Probably said very clearly,, wordy patient's Daily =

Other look at the code, although the comment is not much = = then Minn is useless array hahaha = =

1#include <cstring>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 Const intN =100005;6 7 intHead[n], maxn[n], minn[n], dfn[n], low[n], st[n], belong[n];8 intDeep[n];9 Ten structpoint{ One     intu, V, next; A Point () {}; -PointintXintYintz) { -U=x, V = y, next =Z; the     } -}p[n<<1]; - intno, top, num, id; - structans{ +     intx, y; - Ans () {}; +Ans (int_u,int_v) { Ax = _u; y =_v; at     } - }ans[n]; - voidinit () { -Memset (Head,-1,sizeof(head)); -memset (DFN,-1,sizeof(DFN)); -memset (MAXN,0,sizeof(MAXN)); inMEMSET (Minn,0x3f,sizeof(Minn)); -No = ID = num = top =0; to } +  - voidAddintXinty) { theP[no] =Point (x, Y, head[x]); *HEAD[X] = no++; $P[no] = Point (Y, X, head[y]); Head[y] =no++;Panax Notoginseng } - //Tarjan strong connectivity, Belong[i] indicates which block belongs to the //record the maximum number of values in each block + voidTarjan (intXintFA) { A     intCNT =0; theLOW[X] = dfn[x] = + +num; +St[++top] =x; -     inti, y; $      for(i = head[x]; I! =-1; i =P[i].next) { $y =p[i].v; -         if(y = = FA)Continue; -         if(Dfn[y] = =-1)    { the Tarjan (y, x); -LOW[X] =min (low[x], low[y]);Wuyi         } the         ElseLOW[X] =min (low[x], dfn[y]); -     } Wu     if(Dfn[x] = =Low[x]) { -ID + +; About          Do{ $y = st[top--]; -Belong[y] =ID; -Maxn[id] =Max (Maxn[id], y); -Minn[id] =min (minn[id], y); A} while(X! =y); +     } the } - //MAXN represents the maximum value of I and its subtrees $ voidDfsintXintFA) { the     inti, y; the      for(i = head[x]; I! =-1; i =P[i].next) { they =p[i].v; the         if(y = = FA)Continue; -Deep[y] = Deep[x] +1; in dfs (y, x); theMAXN[X] =Max (maxn[x], maxn[y]); the     } About  the } the intMain () { the     intTC, N, M, I, X, y, u, v; +scanf"%d", &TC); -      while(tc--){ thescanf"%d%d", &n, &m);Bayi init (); the          for(i =1; I <= m; i++){ thescanf"%d%d", &x, &y); - Add (x, y); -         } theTarjan (1,1); the  them =No; theNo =0; -Memset (Head,-1,sizeof(head)); the         //re-build the tree the          for(i =0; I < m; i+=2){ thex = belong[p[i].u]; y =BELONG[P[I].V];94             //belong to the same block, non-bridge edge, after the deletion of the point does not appear the             if(x = = y) ans[i/2] = Ans (0,0); the             Else { theans[i/2] =Ans (x, y);98 Add (x, y); About             } -         }101deep[1] =0;102         //take the block where n is the root, Dfs this tree, find out the maxn of the subtree103DFS (Belong[n),0);104M >>=1; the          for(i =0; I < m; i++){106             if(ans[i].x = =0) printf ("0 0\n");107             Else{108                 //because there's only one side of the line109u = ans[i].x; v =ans[i].y; the                 if(Deep[u] >Deep[v]) {111printf"%d%d\n", Maxn[u], Maxn[u] +1); the}Else {113printf"%d%d\n", Maxn[v], Maxn[v] +1); the                 } the             } the         }117     }118     return 0;119}

HDU 5409 CRB and Graph

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.