hdu 4635 Strongly connected (Tarjan+缩点)

Strongly connectedTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1700Accepted Submission(s): 710

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 strongly 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

题意:给你一个有向图,问你最多能添加多少条边使得这个图依然不是强联通的。

并且图中没有重边和自环。思路:最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到

X的边,那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部

中每个点到Y部的每个点都有一条边,假设X部有x个点,Y部有y个点,有x+y=n,同时

边数F=x*y+x*(x-1)+y*(y-1),整理得:F=N*N-N-x*y,(然后去掉已经有了的边m,就是

答案),当x+y为定值时,二者越接近,x*y越大,所以要使得边数最多,那么X部和Y部

的点数的个数差距就要越大,所以首先对于给定的有向图缩点,对于缩点后的每个点,

如果它的出度或者入度为0,那么它才有可能成为X部或者Y部,所以只要求缩点之后的

出度或者入度为0的点中,包含节点数最少的那个点,令它为一个部,其它所有点加起

来做另一个部,就可以得到最多边数的图了。

思路转自 :

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <stack>using namespace std;const int maxn=100010;int n,m,time,cnt,dfn[maxn],low[maxn],belong[maxn],sum[maxn],in[maxn],out[maxn];bool visited[maxn];vector <int > G[maxn];stack <int > st;void initial(){memset(in,0,sizeof(in));memset(out,0,sizeof(out));memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(sum,0,sizeof(sum));memset(belong,0,sizeof(belong));memset(visited,0,sizeof(visited));while(!st.empty()) st.pop();for(int i=0; i<maxn; i++) G[i].clear();time=1;cnt=0;}void input(){int u,v;scanf("%d %d",&n,&m);for(int i=0; i<m; i++){scanf("%d %d",&u,&v);G[u].push_back(v);}}void tarjan(int u){dfn[u]=low[u]=time++;st.push(u);visited[u]=1;for(int i=0; i<G[u].size(); i++){int v=G[u][i];if(!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);}else if(visited[v]) low[u]=min(low[u],dfn[v]);}if(low[u]==dfn[u]){cnt++;while(1){int t=st.top();st.pop();belong[t]=cnt;sum[cnt]++;visited[t]=0;if(t==u) break;}}}void ready(){for(int i=1; i<=n; i++)for(int j=0; j<G[i].size(); j++){int u=belong[i],v=belong[G[i][j]];if(u!=v){out[u]++;in[v]++;}}}long long get_ans(){long long ans=-1;for(int i=1; i<=cnt; i++){if(!in[i] || !out[i]){long long x=sum[i],y=n-sum[i];ans=max(ans,x*(x-1)+y*(y-1)+x*y-m);}}return ans;}void solve(int co){for(int i=1; i<=n; i++) if(!dfn[i]) tarjan(i);if(cnt==1){printf("Case %d: -1\n",co);return ;}ready();printf("Case %d: %I64d\n",co,get_ans());}int main(){int T;scanf("%d",&T);for(int co=1; co<=T; co++){initial();input();solve(co);}return 0;}

,勇于接受自己的不完美,认清自己不足的地方,

hdu 4635 Strongly connected (Tarjan+缩点)

相关文章:

你感兴趣的文章:

标签云: