Hdu 4635 strongly connected component + contraction point
Problem Description Give a simple directed graph with N nodes and M edges. please tell me the maximum number of the edges you can add that the graph is still a simple directed graph. also, after you add these edges, this graph must NOT be strongly connected.
A simple directed graph is a directed graph having no multiple edges or graph loops.
A stronugly connected digraph is a directed graph in which it is possible to reach any node starting from any other node by traversing edges in the direction (s) in which they point.
Input The first line of date is an integer T, which is the number of the text cases.
Then T cases follow, each case starts of two numbers N and M, 1 <= N <= 100000, 1 <= M <= 100000, representing the number of nodes and the number of edges, then M lines follow. each line contains two integers x and y, means that there is a edge from x to y.
Output For each case, you should output the maximum number of the edges you can add.
If the original graph is strongly connected, just output-1.
Sample Input
33 31 22 33 13 31 22 31 36 61 22 33 14 55 66 4
Sample Output
Case 1: -1Case 2: 1Case 3: 15
/** Hdu 4635 strongly connected component + point reduction question: Given a graph, ask how many sides can be added at most to make it not a connected graph. Solution: http://www.xuebuyuan.com/1606580.html#/#include
# Include
# Include
Using namespace std; typedef long LL; const int maxn = 100005; struct note {int v, next;} edge [maxn * 10]; int head [maxn], ip; int st [maxn], ins [maxn], dfn [maxn], low [maxn], cnt_tar, index, top; int in [maxn], out [maxn], num [maxn], belong [maxn]; int m, n; void addedge (int u, int v) {edge [ip]. v = v, edge [ip]. next = head [u], head [u] = ip ++;} void init () {memset (head,-1, sizeof (head); ip = 0 ;} void tarjan (int u) {dfn [u] = low [u] = ++ index; St [++ top] = u; ins [u] = 1; for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; if (! Dfn [v]) {tarjan (v); low [u] = min (low [u], low [v]);} else if (ins [v]) {low [u] = min (low [u], dfn [v]) ;}} if (dfn [u] = low [u]) {cnt_tar ++; int j; do {j = st [top --]; ins [j] = 0; belong [j] = cnt_tar; num [cnt_tar] ++;} while (j! = U) ;}} void solve () {top = 0, index = 0, cnt_tar = 0; memset (dfn, 0, sizeof (dfn); memset (low, 0, sizeof (low); for (int I = 1; I <= n; I ++) {if (! Dfn [I]) tarjan (I) ;}} int main () {int T, tt = 0; scanf (% d, & T); while (T --) {scanf (% d, & n, & m); init (); for (int I = 1; I <= m; I ++) {int u, v; scanf (% d, & u, & v); addedge (u, v);} memset (num, 0, sizeof (num); solve (); if (cnt_tar = 1) {printf (Case % d:-1, ++ tt); continue;} memset (in, 0, sizeof (in )); memset (out, 0, sizeof (out); for (int u = 1; u <= n; u ++) {for (int I = head [u]; i! =-1; I = edge [I]. next) {int v = edge [I]. v; if (belong [u] = belong [v]) continue; out [belong [u] ++; in [belong [v] ++ ;}} LL all = (LL) n * (n-1)-m; LL ans = 0; for (int I = 1; I <= cnt_tar; I ++) {if (in [I] = 0 | out [I] = 0) {ans = max (ans, all-(LL) num [I] * (n-num [I]);} printf (Case % d: % I64d, ++ tt, ans);} return 0 ;}