Poj 3177 (Bridge + contraction point)

Source: Internet
Author: User

Same as poj 3352CodeYes.

# Include <stdio. h> # Include < String . H> # Include <Iostream> Using   Namespace  STD;  # Define N 5050 # Define M 10010 # Define INF 0x3fffffff Struct  Node {  Int  From  , To, next;} edge [  2 * M];  Int  N, m;  Int  CNT, pre [N];  Bool Mark [ 2 * M];  Bool Save [ 2 * M];  Int  Low [N]; Int  Link [N];  Int  D [N];  Void Add_edge ( Int U, Int  V) {edge [CNT]. = V; edge [CNT].  From = U; edge [CNT]. Next = Pre [u]; Pre [u] = CNT ++ ;}  Void DFS (Int S, Int  Num) {LOW [s] = Num;  Int Mi = Num;  For ( Int P = pre [s]; P! =- 1 ; P = Edge [p]. Next ){  Int V = Edge [p].;  If (MARK [p] = 1 )Continue  ;  If (Low [v] =- 1  ) {Mark [p] = 1  ; Mark [p ^ 1 ] = 1  ; DFS (v, num + 1  );  If (Low [v]> num) // Note that edge [p] is a bridge  {Save [p] = 1  ; Save [p ^ 1 ] = 1  ;} Mi = Min (MI, low [v]);} low [s] = Mi ;}  Void Dfs1 ( Int S, Int  Num) {link [s] =Num; Mark [s] = 1  ;  For ( Int P = pre [s]; P! =- 1 ; P = Edge [p]. Next ){  Int V = Edge [p].;  If (MARK [v] = 1 | Save [p] = 1 ) Continue  ; Dfs1 (v, num );}} Int  Main () {CNT = 0  ; Memset (PRE, - 1 , Sizeof  (Pre); scanf (  "  % D  " , & N ,& M );  For ( Int I = 0 ; I <m; I ++ ){ Int  X, Y; scanf (  "  % D  " , & X ,& Y); add_edge (x, y); add_edge (Y, x);} memset (low, - 1 , Sizeof  (Low); memset (mark,  0 , Sizeof  (Mark); memset (save,  0 , Sizeof (SAVE); DFS (  1 , 0  ); Memset (mark,  0 , Sizeof  (Mark ));  Int Id = 0  ;  For ( Int I = 1 ; I <= N; I ++ ){  If (MARK [I] =0  ) {Dfs1 (I, ++ ID) ;}} memset (D,  0 , Sizeof  (D ));  If (ID = 1  ) {Printf (  "  0 \ n  "  );}  Else  { For ( Int I = 0 ; I <CNT; I + = 2  ){  Int X = edge [I]. From  ;  Int Y = Edge [I].;  If (Link [x]! = Link [y]) {d [LINK [x] ++ ; D [LINK [y] ++;}}  Int Ans = 0  ;  For ( Int I = 1 ; I <= ID; I ++ ){  If (D [I] = 1  ) Ans ++ ;} Printf (  "  % D \ n " , ANS/ 2 + Ans % 2  );}  Return   0  ;} 

 

Redundant paths
Time limit:1000 ms   Memory limit:65536 K
Total submissions:7054   Accepted:3075

Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1 .. f) to another field, Bessie and the rest of the herd are forced to cross near the tree of rotten apples. the cows are now tired of often being forced to take a particle path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. they currently have at least one route between each pair of fields and want to have at least two. of course, they can only travel on official paths when they move from one field to another.

Given a description of the current set of R (F-1 <= r <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: two space-separated integers: F and R

Lines 2. R + 1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

 
7 71 22 33 42 54 55 65 7

Sample output

 
2

Hint

Explanation of the sample:

One visualization of the paths is:

 
1 2 3
+ --- +
|
|
6 + --- + 4
/5
/
/
7 +

Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.

 
1 2 3
+ --- +
: |
: |
6 + --- + 4
/5:
/:
/:
7 + ----

Check some of the routes:
1-2: 1-> 2 and 1-> 6-> 5-> 2
1-4: 1-> 2-> 3-> 4 and 1-> 6-> 5-> 4
3-7: 3-> 4-> 7 and 3-> 2-> 5-> 7 
Every pair of fields is, in fact, connected by two routes.

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

Source

Usaco 2006 January gold

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.