2013 Multi-University Training Contest 4的题目。
图论,强连通缩点,找到入度或者出度为0的点.
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 412 Accepted Submission(s): 175
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.
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.
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.
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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
const int N = 100005;
const int INF = 0x3f3f3f3f;
int cnt, scc, head[N], belong[N], dfn[N], low[N];
bool in[N];
int num[N];
vector<int> G[N];
int index;
stack <int> S;
void tarjan(int u){
dfn[u] = low[u] = ++index;
S.push(u);
in[u] = true;
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(in[v])
low[u] = min(low[u], dfn[v]);
}
int j;
if(dfn[u]==low[u]){
scc++;
do{
num[scc]++;
j = S.top();
S.pop();
belong[j] = scc;
in[j] = false;
}while(u != j);
}
}
int main(){
int cases;
cin>>cases;
for(int t = 1; t <= cases; t++){
while(!S.empty()) S.pop();
int n, m;
cin>>n>>m;
for(int i = 0; i <= n; i++)
G[i].clear();
cnt = 0;
memset(in, false, sizeof(in));
memset(head, -1, sizeof(head));
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(belong, 0, sizeof(belong));
for(int i = 1; i <= m; i++){
int a, b;
cin>>a>>b;
G[a].push_back(b);
}
index = 0, scc = 0;
memset(num, 0, sizeof(num));
for(int i = 1; i <= n; i++){
if(!dfn[i])
tarjan(i);
}
int minnum = INF;
LL sum = -m;
sum += n * (n-1);
int out[N], in[N];
memset(out, 0, sizeof(out));
memset(in, 0, sizeof(in));
for(int i = 1; i <= n; i++){
for(int j = 0; j < G[i].size(); j++){
int u = belong[i];
int v = belong[G[i][j]];
if(u != v){
out[u]++;
in[v]++;
}
}
}
int tmp = INF;
for(int i = 1; i <= scc; i++){
if(out[i]==0) tmp = min(tmp,num[i]*(n-num[i]));
if(in[i]==0) tmp = min(tmp,num[i]*(n-num[i]));
}
printf("Case %d: ", t);
if(scc == 1) cout<<-1<<endl;
else cout<<sum - tmp<<endl;
}
return 0;
}