HDU 4635 Strongly connected 填边判强连通

一、内容

 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

二、思路

在这里插入图片描述

三、代码

#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int N = 1e5 + 5, M = 1e5 + 5;
struct E { int v, next;} e[M];
int t, n, m, u, v, len, h[N], minv, scc_cnt, top, num, id[N], scc[N], ind[N], outd[N], dfn[N], low[N], stack[N];
bool in_st[N];  
void add(int u, int v) {e[++len].v = v; e[len].next = h[u]; h[u] = len;}
void tarjan(int u) {
	dfn[u] = low[u] = ++num;
	stack[++top] = u; in_st[u] = true;
	for (int j = h[u]; j ; j = e[j].next) {
		int v = e[j].v;
		if (!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if (in_st[v]) low[u] = min(low[u], dfn[v]);
	} 
	if (dfn[u] == low[u]) {
		int v; scc_cnt++;
		do {
			v = stack[top--]; in_st[v] = false;
			id[v] = scc_cnt; scc[scc_cnt]++;
		} while (u != v);
	}
}
int main() {  
	scanf("%d", &t); int cas = 1;
	while (t--) {
		memset(h, 0, sizeof(h)); len = num = top = scc_cnt = 0; minv = 1e9; 
		memset(in_st, false, sizeof(in_st)); 
		memset(dfn, 0, sizeof(dfn));
		memset(ind, 0, sizeof(ind));
		memset(outd, 0, sizeof(outd));
		memset(id, 0, sizeof(id));
		memset(scc, 0, sizeof(scc));
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= m; i++) {
			scanf("%d%d", &u, &v);
			add(u, v);
		}
		for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
		for (int u = 1; u <= n; u++) {
			for (int j = h[u]; j; j = e[j].next) {
				int v = e[j].v;
				if (id[v] == id[u]) continue;
				ind[id[v]]++, outd[id[u]]++;
			}
		}
		//求出度为0 或者 入度为0的SCC的最少点数 
		for (int i = 1; i <= scc_cnt; i++) {
			if (!ind[i] || !outd[i]) minv = min(minv, scc[i]);
		}
		if (scc_cnt == 1) printf("Case %d: -1\n", cas++);
		else printf("Case %d: %lld\n", cas++, (ll)n * (n - 1) - (ll)m - (ll)minv * (n - minv));
	}
	return 0; 
} 
发布了456 篇原创文章 · 获赞 466 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104441555