HDU4635 Strongly connected
2015-08-08 12:18
225 查看
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
Recommend
zhuyuanchen520
题意:给定N个点,M条边。问最多再添几条有向边使他恰好不为强连通图。
思路:假设有结果图。则必然是分成两块区域,且其中一区域的点有另一区域的全部路径,而另一区域无回路。
分别设为A,B。A中含有a条边,B中含有b条边。a+b = N。则结果图的总边数为all = a*(a-1)+ b*(b-1) + a*b ;
化简后 all = N*N- N - a*b 。 已知a+b = N 。 求all 最大值 , 则 a ,b 相差越大, a*b越小 .(证a= (N-x)/2 , b = (N+x)/2 ) 。
A,B前提必是强连通分量,且入度或出度为0。
回到题中, 对给定的连通图,用tarjan算法求强连通个数(并记录每个强连通分量中点的个数),若原图为强连通图,输出-1;
否则对原图缩点,求出入度,出度。再求入度或出度为0的点中缩点前点集元素最少个数。
ans = all - M;
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
Recommend
zhuyuanchen520
题意:给定N个点,M条边。问最多再添几条有向边使他恰好不为强连通图。
思路:假设有结果图。则必然是分成两块区域,且其中一区域的点有另一区域的全部路径,而另一区域无回路。
分别设为A,B。A中含有a条边,B中含有b条边。a+b = N。则结果图的总边数为all = a*(a-1)+ b*(b-1) + a*b ;
化简后 all = N*N- N - a*b 。 已知a+b = N 。 求all 最大值 , 则 a ,b 相差越大, a*b越小 .(证a= (N-x)/2 , b = (N+x)/2 ) 。
A,B前提必是强连通分量,且入度或出度为0。
回到题中, 对给定的连通图,用tarjan算法求强连通个数(并记录每个强连通分量中点的个数),若原图为强连通图,输出-1;
否则对原图缩点,求出入度,出度。再求入度或出度为0的点中缩点前点集元素最少个数。
ans = all - M;
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <stack> #define maxn 100000+10 using namespace std; int dfn[maxn] , low[maxn] ; int belong[maxn] ; int indegree[maxn] , outdegree[maxn] ; int times , bcnt ; bool instack[maxn] ; int snum[maxn] ; int n , m ,minn; vector<int> mapp[maxn] ; stack<int> S ; void tarjan(int u) { int temp ; dfn[u] = low[u] = times++ ; instack[u] = true ; S.push(u) ; for( int i = 0 ; i < mapp[u].size() ;++i){ temp = mapp[u][i] ; if(!dfn[temp] ) { tarjan(temp) ; low[u] = low[u] > low[temp] ? low[temp] : low[u] ; } else if( instack[temp] ) { low[u] = low[u] > dfn[temp] ? dfn[temp] : low[u] ; } } if( low[u] == dfn[u] ) { bcnt++; do { temp = S.top() ; S.pop() ; instack[temp] = false ; belong[temp] = bcnt ; snum[bcnt]++; } while (temp != u) ; } } int solve() { for( int i = 1; i <= n ; ++i ) { if(!dfn[i]) { tarjan(i) ; } } if( bcnt == 1 ) { return false ; } for( int i = 1; i <= n ; ++i ) { for( int j = 0 ;j < mapp[i].size() ; ++j ) { int temp = mapp[i][j] ; if( belong[i] != belong[temp] ) { indegree[belong[temp]]++ ; outdegree[belong[i]]++ ; } } } for( int i = 1; i <= bcnt ; ++i ){ if( !indegree[i] || !outdegree[i] ) { minn = minn > snum[i] ? snum[i] : minn ; } } return minn ; } void init() { memset(dfn,0,sizeof(dfn)) ; memset(indegree,0,sizeof(indegree)) ; memset(outdegree,0,sizeof(outdegree)) ; memset(instack, 0 , sizeof(instack)) ; memset(belong,0,sizeof(belong)) ; memset(snum,0,sizeof(snum)) ; while(!S.empty()) { S.pop() ; } for( int i = 0; i <= n ; ++i ) { mapp[i].clear() ; } times = 0 ; bcnt = 0 ; } int main () { int t , ncase = 1 ,num1, num2; cin >> t ; while(t--) { scanf("%d%d",&n,&m) ; init () ; for(int i = 0 ; i < m ; ++i ) { scanf("%d%d",&num1,&num2) ; mapp[num1].push_back(num2) ; } minn = maxn ; printf("Case %d: ",ncase++) ; if(solve()) { cout << n*n-n-(n-minn)*minn - m << endl ; } else cout << -1 << endl; } return 0; }
相关文章推荐
- OCX学习记录
- 剑指offer刷题之c++实现的反转链表
- android 设置布局动画
- Java并发编程之ConcurrentHashMap原理分析
- 对冲基金
- npm 慢
- 剑指offer刷题之c++实现的合并两个有序的链表
- uva 116 数塔 动态规划
- beetl模板入门例子
- Spring3 MVC 深入研究
- 数据库设计三大范式
- Unity3D中uGUI事件系统简述及使用方法总结
- Thread02:线程同步通信
- 剑指offer刷题之c++实现的替换空格
- 安卓开发中防止内存溢出浅析
- IPC(进程间通信)机制
- 设计原则开篇
- 剑指offer刷题之c++实现的求链表中倒数第k个值
- poj2387 Til the Cows Come Home(邻接矩阵,邻接表)
- 小根堆+Kruskal算法