HDU 3394 Railway (application of point dual connected components)

Source: Internet
Author: User

Test instructions: Given an absence graph, the number of edges that are not in any ring and the amount of edges in two or more rings are calculated respectively.

Solution: The side of the bridge is not in any ring. And if the number of edges in a two-connected component is more than the number of points, then all the sides of the two-connected component are in two or more rings at the same time (this can be imagined by adding a different edge to a simple ring, so that the simple ring is divided into two small simple rings, Any edge in a large ring will be in one of the small rings at the same time.

In the Tarjan algorithm, when lowv>pre[u] (U is the starting point, V is the next point of the edge), The Edge (U,V) is a bridge. The number of edges of the point double connected component can be counted by the number of POPs in the stack.

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < string> #include <cmath> #include <set> #include <climits> #include <queue> #include < vector> #include <stack> #include <map> #include <climits>using namespace std;const int maxn=10005; struct Edge{int u,v; Edge (int uu,int vv): U (UU), V (vv) {}edge () {}};int Pre[maxn],bccno[maxn],dfs_clock,bcc_cnt;bool iscut[maxn];std:: vector<int> g[maxn],bcc[maxn];int ans,ans1;stack<edge>s;int dfs (int u,int fa) {int lowu=pre[u]=++dfs_ Clock;int child=0;for (int i=0;i<g[u].size (); i++) {int v=g[u][i]; Edge E=edge (u,v), if (!pre[v]) {S.push (e); Child++;int Lowv=dfs (V,u); Lowu=min (LOWU,LOWV); if (Lowv>=pre[u])//conditions must be " =, if only write = =, then the side of the bridge will not be ejected from the stack, resulting in the subsequent double-connected component of the number of side calculation error {if (Lowv>pre[u]) ans1++;//(U,V) is a bridge iscut[u]=true;bcc_cnt++;bcc[bcc_ Cnt].clear (); int cnt=0;for (;;) {cnt++; Edge x=s.top (); S.pop (); if (bccno[x.u]!=bcc_cnt) {bcc[bcc_cnt].push_back (x.u); bccno[x.u]=bcc_cnt;} if (bccno[x.v]!=BCC_CNT) {bcc[bcc_cnt].push_back (X.V); bccno[x.v]=bcc_cnt;} if (x.u==u&&x.v==v) break;} if (Cnt>bcc[bcc_cnt].size ()) ans+=cnt;//when the number of edges in a point double connected component is greater than the number of points}}else if (PRE[V]&LT;PRE[U]&AMP;&AMP;V!=FA) {S.push (E) ; Lowu=min (Lowu,pre[v]);}} if (fa<0&&child==1) Iscut[u]=false;return Lowu;} void find_bcc (int n) {memset (pre,0,sizeof (PRE)), memset (bccno,0,sizeof (BCCNO)), memset (iscut,0,sizeof (iscut));d Fs_ clock=bcc_cnt=0;for (int i=0;i<n;i++) {if (!pre[i]) DFS (I,-1);}} int main () {int n,m;while (scanf ("%d%d", &n,&m)!=eof) {if (n==0&&m==0) break;int a,b;for (int i=0;i<n; i++) G[i].clear (); for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b); G[a].push_back (b); G[b].push_back (a);} ANS=0,ANS1=0;FIND_BCC (n);p rintf ("%d%d\n", Ans1,ans);}}


HDU 3394 Railway (application of point dual connected components)

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.