Topic: Given an undirected graph, you are required to turn all the undirected edges into a forward edge, and add the least of the forward edges, making the new forward graph strong connected
Problem solving ideas: This and POJ-3352 Road construction
Similar, but the problem is not necessarily connected graph, it is possible to shrink points after the occurrence of isolated points, but the general idea is the same
The front of the unknown said, you can see the stamp here has been written, here to talk about how to deal with isolated points
If there are n points, it is required to add a forward edge between the N points so that the N points become strongly connected, then the number of edges to be added is n
With the above conclusion, the time is better to do, when processing, only need to count all the degrees of 1 and 0 of the contraction point (the block is reduced to a point) the number can be
The number of assumptions is 1 a (leaf node, connected graph)
The number of degrees is 2 B (isolated points)
So the need to connect the edge is (A + 1+ 2 * B)/2
A detailed explanation of the great God is attached.
#include <cstdio>#include <cstring>#define N 1010#define M 20010#define MIN (a) < (b)? (a): (b))structedge{intTo, next;} E[M];intHead[n], bridge[n][2], Belong[n], Degree[n],Stack[N], pre[n];intN, M, tot, Dfs_clock, bcc_cnt, Bnum, top;voidAddedge (intUintV) {e[tot].to = v; E[tot].next = Head[u]; Head[u] = tot++; u = u ^ v; v = u ^ v; u = u ^ v; E[tot].to = v; E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {memset(Head,-1,sizeof(head)); tot =0;intU, v; for(inti =0; I < m; i++) {scanf("%d%d", &u, &v); Addedge (U, v); }}intDfsintUintFA) {intLowu = Pre[u] = ++dfs_clock;Stack[++top] = u; for(inti = Head[u]; I! =-1; i = e[i].next) {intv = e[i].to;if(!pre[v]) {intLOWV = DFS (v, u); Lowu = min (Lowu, LOWV);if(Lowv > Pre[u]) {bridge[bnum][0] = u; bridge[bnum++][1] = V; bcc_cnt++; while(1) {intx =Stack[top--]; BELONG[X] = bcc_cnt;if(x = = v) Break; } } }Else if(Pre[v] < Pre[u] && v! = FA) {Lowu = min (Lowu, pre[v]); } }returnLowu;}voidSolve () {memset(Pre,0,sizeof(pre));memset(Degree,0,sizeof(degree)); Dfs_clock = bcc_cnt = Bnum = top =0; for(inti =1; I <= N; i++)if(!pre[i]) {DFS (I,-1); bcc_cnt++; while(top) {intx =Stack[top--]; BELONG[X] = bcc_cnt;if(x = = i) Break; } }if(bcc_cnt = =1) {printf("0\n");return; } for(inti =0; i < Bnum; i++) {intU = bridge[i][0];intv = bridge[i][1]; degree[belong[u]]++; degree[belong[v]]++; }intblock =0; for(inti =1; I <= bcc_cnt; i++)if(Degree[i] = =0) Block + =2;Else if(Degree[i] = =1) Block + =1;printf("%d\n", (Block +1) /2);}intMain () { while(scanf("%d%d", &n, &m)! = EOF) {init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-10972 Revolc Faelon (Edge dual connected component)