Strongly connected(HDU-4635)
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 4Sample Output
Case 1: -1
Case 2: 1
Case 3: 15
題意: 給一個 n 個點,m 條邊的有向圖,求最多新增多少條邊能使得該圖依然不是強連通,若該圖初始已經強連通,則輸出-1
思路:縮點
假設一非連通圖由兩個強連通子圖構成,結點分別為 x、y 個,即:x+y=n,且圖中只有從 x 的任一節點到 y 中任一節點的邊,不存在從 y 到 x 的邊,則非連通圖的邊數 F=x*(x-1)+y*(y-1)+x*y=n*(n-1)-x*y,若想讓邊數 F 最大,則必須讓 x*y 最小,由於 x+y=n,因此當 x、y 的差值最大時,x*y 必然最小,從而 F 最大
依照題意,F-m 即為所求結果,根據原圖求所有強連通分量,縮點得新的有向無環圖,因此只有 入度=0 或 出度=0 所代表的點(分量),才有資格成為 x 或 y 部分,因此根據上述結論,找出最小值即可,注意資料範圍要用 long long
Source Program
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 20001
#define MOD 16007
#define E 1e-6
#define LL long long
using namespace std;
int n,m;
vector<int> G[N];
stack<int> S;
int dfn[N],low[N];
bool vis[N];
int sccno[N];
int in[N],out[N];
int num[N];//分量的點數
int block_cnt;
int sig;
void Tarjan(int x){
vis[x]=true;
dfn[x]=low[x]=++block_cnt;
S.push(x);
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(vis[y]==false){
Tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(!sccno[y])
low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x]){
sig++;
num[sig]=0;
while(true){
int temp=S.top();
S.pop();
sccno[temp]=sig;
num[sig]++;
if(temp==x)
break;
}
}
}
int shrink(){//縮點
for(int i=1;i<=sig;i++){
in[i]=0;
out[i]=0;
}
for(int x=1;x<=n;x++){
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(sccno[x]!=sccno[y]){
out[sccno[x]]++;
in[sccno[y]]++;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
for(int Case=1;Case<=t;Case++){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
G[i].clear();
for(int i=0;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
}
sig=0;
block_cnt=0;
memset(vis,false,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(sccno,0,sizeof(sccno));
for(int i=1;i<=n;i++)
if(vis[i]==false)
Tarjan(i);
shrink();
LL res=0;
int minn=INF;
for(int i=1;i<=sig;i++)
if(out[i]==0||in[i]==0)
minn=min(minn,num[i]);
res=(long long)n*n-n-(long long)minn*(n-minn)-m;
printf("Case %d: ",Case);
if(sig==1)
printf("-1\n");
else
printf("%d\n",res);
}
return 0;
}
相關文章
- 翻譯|How to Export a Connected ComponentExport
- Paper -- DenseNet:Densely Connected Convolutional NetworkSENet
- 記一次zookeeper not connected
- 什麼是.NET的強型別字串(Strongly typed string)?型別字串
- abc372E K-th Largest Connected Components
- A replica with the same server_uuid/server_id as this replica has connected to the source;ServerUI
- Salesforce 整合篇零基礎學習(一)Connected AppSalesforceAPP
- Akamai推出Akamai Connected Cloud和全新雲端計算服務AICloud
- 深度學習論文翻譯解析(十五):Densely Connected Convolutional Networks深度學習
- 遠端連線nacos配置中心報錯:Client not connected, current status:STARTINGclient
- Archery 平臺執行DDL error:Threads_connected exceeds its critical thresholdErrorthread
- 啟動Dubbo專案註冊Zookeeper時提示zookeeper not connected異常原理解析
- 論文翻譯:2020_Densely connected neural network with dilated convolutions for real-time speech enhancement in the time domainAI
- 【STM32】使用Keil5載入程式時出現No Target Connected錯誤的解決辦法
- 萃弈洞察: 進擊藍海,探索遊戲出海投放在智慧大屏 Connected TV 的可能性遊戲