Poj 3352-edge dual-connection

Source: Internet
Author: User

The question requires at least several sides to be added to make the graph still connected after removing people and one edge

First, perform a dual-connection deflation point. A connected block is equivalent to a point in the new graph.

The new graph is a tree.

If you want to remove any edge in an original image after adding several edges, the image is still connected. We should connect the leaves so that any leaf has edges connected to other leaves.

Obviously, when the total number of leaves CNT is an even number, the answer is CNT/2;

When the total number of leaves is odd, the answer is (CNT + !) /2;

View code

# Include <stdio. h>
# Include <queue>
# Include < String . H>
# Include <vector>
Using Namespace STD;
Const Int Maxn =5005 ;
Struct Edge
{
Int S, T;
Int Next;
Int Vis;
} Edge [ 1000005 ];
Int Head [maxn];
Int E;
Void Add_edge ( Int S,Int T)
{
Edge [e]. S = s;
Edge [e]. t = T;
Edge [e]. vis = 0 ;
Edge [e]. Next = head [s];
Head [s] = e ++;
}
Int Btype, time;
Int Dfn [maxn], low [maxn], belong [maxn];
Int St [maxn], top;
Inline Int Min ( Int A,Int B ){ Return A <B? A: B ;}
Void DFS ( Int S)
{
Int I, T, ID;
St [++ top] = s;
Dfn [s] = low [s] = ++ time;
For (I = head [s]; I! =- 1 ; I = edge [I]. Next)
{
If (Edge [I]. Vis) Continue ;
Edge [I]. vis = edge [I ^ 1 ]. Vis = 1 ;
T = edge [I]. t;
If (! Dfn [T])
{
DFS (t );
Low [s] = min (low [s], low [T]);
}
Else Low [s] = min (low [s], dfn [T]);
}
If (Dfn [s] = low [s])
{
Btype ++;
Do {
T = sT [top --];
Belong [T] = btype;
} While (T! = S );
}
}
Void SCC ( Int N)
{
Int I;
Time = 0 ; Btype = 0 ; Top = 0 ;
Memset (dfn, 0 ,Sizeof ( Int ) * (N + 1 ));
For (I = 1 ; I <= N; I ++) If (! Dfn [I])
DFS (I );
}
Int Chu [maxn];
Int N, m;
Int Ma [ 1010 ] [ 1010 ];
Int Main ()
{
Int I, J, K;
Int A, B;
While (Scanf ( " % D " , & N, & M )! = EOF)
{
Memset (Head ,- 1 , Sizeof (Head); E = 0 ;
Memset (CHU,0 , Sizeof (CHU ));
For (I = 0 ; I <m; I ++)
{
Scanf ( " % D " , & A, & B );
Add_edge (A, B );
Add_edge (B, );
}
SCC (N );
Memset (MA, 0 , Sizeof (Ma ));
For (I = 1 ; I <= N; I ++)
{
For (J = head [I]; J! =- 1 ; J = edge [J]. Next)
{
Int V = edge [J]. t;
If (Belong [I]! = Belong [v] &! Ma [belong [I] [belong [v])
{
Ma [belong [I] [belong [v] = ma [belong [v] [belong [I] = 1 ;
Chu [belong [I] ++;
Chu [belong [v] ++;
}
}
}
Int Ans = 0 ;
For (I = 1 ; I <= btype; I ++)
{
If (Chu [I] = 1 )
Ans ++;
}
If (ANS % 2 = 0 ) Ans/= 2 ;
Else Ans = (ANS + 1 )/ 2 ;
Printf ( " % D \ n " , ANS );
}
Return 0 ;
}

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.