Network POJ-3694 (LCA and check set + connected graph for bridge)

Source: Internet
Author: User

is to find out the number of bridges in the original diagram, add a new edge to each query, and add the number of bridges remaining in the current edge graph.

The number of bridges in the original diagram is calculated, and then the number of bridges between the two ends of the new join edge is subtracted, which is the number of remaining bridges.

To put together (that is, a bridge between two points) that belongs to the same set.

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<algorithm>#include<vector>#defineMem (A, B) memset (A, B, sizeof (a))using namespacestd;Const intMAXN =400100, INF =0x7fffffff;intPRE[MAXN], F[MAXN], PA[MAXN];intDfs_clock;intN, M, Ret;vector<int>G[MAXN];intFindintx) {    returnF[x]==x?x: (f[x]=find (F[x]));}intUnion (intUintv) {    intR =find (U); intL =Find (v); if(r = = L)return 0; F[R]=l; return 1;}intDfsintUintFA) {Pa[u]=FA; intLowu = Pre[u] = + +Dfs_clock;  for(intI=0; I<g[u].size (); i++)    {        intv =G[u][i]; if(!Pre[v]) {     //Pa[v] = u;            intLOWV =Dfs (v, u); Lowu=min (Lowu, LOWV); if(Lowv >Pre[u]) ret++; ElseUnion (U, v); //If there is no bridge between U and V, then you and V belong to a set        }        Else if(Pre[v] < Pre[u] && v! =FA) Lowu=min (Lowu, pre[v]); }    returnLowu;}intLcaintUintv) {    intR =find (U); intL =Find (v); if(r = =l)returnret; if(Pre[u] >Pre[v]) Swap (U, v);  while(Pre[u] <Pre[v]) {        if(Union (pa[v], v)) RET--; V=Pa[v]; }     while(U! = V)//v after the previous while either U or the nearest public ancestor of U and v    {        if(Union (U, Pa[u]) ret--; U=Pa[u]; }    returnret;}voidinit () {mem (Pre,0); Mem (PA,0);  for(intI=0; i<=n; i++) F[i] =i; Dfs_clock=0; RET=0;}intMain () {intKase =0;  while(cin>> n >> m && n+m) {init ();  for(intI=0; i<m; i++)        {            intu, v; CIN>> u >>v;            G[u].push_back (v);        G[v].push_back (U); } DFS (1, -1); intQ; CIN>>Q; printf ("Case %d:\n",++Kase);  for(intI=0; i<q; i++)        {            intu, v; CIN>> u >>v; cout<< LCA (U, v) <<Endl; }    }    return 0;}

Network POJ-3694 (LCA and check set + connected graph for bridge)

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.