hdu 4635 Strongly connected(强联通分量+缩
2016-09-23 11:02
393 查看
题目链接
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2510 Accepted Submission(s): 1048
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
3
3 3
1 2
2 3
3 1
3 3
1 2
2 3
1 3
6 6
1 2
2 3
3 1
4 5
5 6
6 4
Sample Output
Case 1: -1
Case 2: 1
Case 3: 15
Source
2013 Multi-University Training Contest 4
题意:
给你一个DAG图,问你最多能添加多少条边使得这个DAG图依然不是强联通的;
题解:参考博客
最终添加完边的图,肯定可以分成两个部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),然后去掉已经有了的边m,则为答案;
当x+y为定值时,二者越接近,x*y越大,所以要使得边数最多,那么X部和Y部的点数的个数差距就要越大;
对于给定的有向图缩点,对于缩点后的每个点,如果它的出度或者入度为0,那么它才有可能成为X部或者Y部; 只需要找到一个出度为0或入度为0包含的点数最少的点集作为x部,另一部分作为y部即可。
然后找出最大值即可;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<stack> using namespace std; const int MAXN=1000000+100; const int MAXM=2000000+100; int tol; int head[MAXN]; struct node { int from,to,next; }edge[MAXM]; void init() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v) { edge[tol].from=u,edge[tol].to=v,edge[tol].next=head[u],head[u]=tol++; } //scc_cnt为SCC计数器,sccno[i]为i所在的SCC编号 //scc_cnt为SCC个数,scc的 编号从1开始 int pre[MAXN],lowlink[MAXN],sccno[MAXN],dfs_clock,scc_cnt; stack<int> S; void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; S.push(u); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(!pre[v]) { dfs(v); lowlink[u]=min(lowlink[u],lowlink[v]); } else if(!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]); } if(lowlink[u]==pre[u]) { scc_cnt++; while(1) { int x=S.top(); S.pop(); sccno[x]=scc_cnt; if(x==u) break; } } } void find_scc(int n) { dfs_clock=scc_cnt=0; memset(sccno,0,sizeof(sccno)); memset(pre,0,sizeof(pre)); memset(lowlink,0,sizeof(lowlink)); for(int i=0;i<n;i++) if(!pre[i]) dfs(i); } int in[MAXN],out[MAXN],cnt[MAXN]; int main() { int cas; scanf("%d",&cas); for(int k=1;k<=cas;k++) { memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(cnt,0,sizeof(cnt)); init(); int n,m; scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); u--,v--; addedge(u,v); } find_scc(n); if(scc_cnt==1) { printf("Case %d: -1\n",k); continue; } for(int i=0;i<tol;i++) { int u=edge[i].from,v=edge[i].to; if(sccno[u]!=sccno[v]) out[sccno[u]]++,in[sccno[v]]++; } for(int i=0;i<n;i++) cnt[sccno[i]]++; int x=n,y=0; for(int i=1;i<=scc_cnt;i++) { if(!in[i]) x=min(x,cnt[i]); if(!out[i]) x=min(x,cnt[i]); } int ans=0; y=n-x; ans=x*x+y*y+x*y-x-y; ans-=m; printf("Case %d: %d\n",k,ans); } return 0; }
相关文章推荐
- HDU 4635 强联通分量
- 爆零后的感受外加一道强联通分量HDU 4635的题解
- hdu 4635 Strongly connected Kosaraju/Tarjan求强联通分量大小
- HDU 4635 多校第四场 1004 强联通
- hdu 1269 强联通分量
- HDU 3836 强联通分量
- hdu1269 迷宫城堡 tarjan求强联通分量
- HDU 4612 双联通分量+树的直径
- HDU 1269 迷宫城堡(tarjan强联通分量)
- HDU 5739 点-双联通分量
- HDU 1827 3072强联通分量
- HDU - 3861 The King’s Problem(强联通分量+最小路径覆盖)
- hdu 2242(边双联通分量)
- Hdu 3686 Traffic Real Time Query System(双联通分量+LCA)
- HDU 3861 强联通分量+最小路径覆盖
- hdu 1827 强联通分量模板
- [ACM] HDU 1269 迷宫城堡(Tarjan算法求强联通分量)
- hdu 1269 (强联通分量Tarjan入门)
- HDU-4738 Caocao's Bridges 边联通分量