HDU 4635 Strongly connected 强连通

来源:互联网 发布:卡盟官网源码带后台 编辑:程序博客网 时间:2024/06/05 21:55

Strongly connected

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1804    Accepted Submission(s): 748


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
33 31 22 33 13 31 22 31 36 61 22 33 14 55 66 4
 

Sample Output
Case 1: -1Case 2: 1Case 3: 15
 

Source
2013 Multi-University Training Contest 4 

题意:给出一个有向图,问最多添加多少条边使得图仍不是强连通的。

若本来就是强连通的,输出-1

x*y+x*(x-1)+y*(y-1)-m 的最大值

x+y=n

x*y尽可能小,选择缩点后入度或出度为0的连通块中点数最小的块作为x部分,剩余作为y部分


/** Author: ☆·aosaki(*’(OO)’*)  niconiconi★ **///#pragma comment(linker, "/STACK:1024000000,1024000000")//#include<bits/stdc++.h>#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <algorithm>#include <functional>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <list>//#include <tuple>#define ALL(v) (v).begin(),(v).end()#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)#define SIZE(v) ((int)(v).size())#define mem(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define lp(k,a) for(int k=1;k<=a;k++)#define lp0(k,a) for(int k=0;k<a;k++)#define lpn(k,n,a) for(int k=n;k<=a;k++)#define lpd(k,n,a) for(int k=n;k>=a;k--)#define sc(a) scanf("%d",&a)#define sc2(a,b) scanf("%d %d",&a,&b)#define lowbit(x) (x&(-x))#define ll long long#define pi pair<int,int>#define vi vector<int>#define PI acos(-1.0)#define pb(a) push_back(a)#define mp(a,b) make_pair(a,b)#define TT cout<<"*****"<<endl;#define TTT cout<<"********"<<endl;inline int gcd(int a,int b){    return a==0?b:gcd(b%a,a);}#define INF 1e9#define eps 1e-8#define mod 10007#define maxn 100010#define maxm 100010using namespace std;int v[maxn],col[maxn];int rea[maxn],low[maxn],stack[maxn];int in[maxn],out[maxn],num[maxn];int n,m,tot=0,color,tm=0,index=0,top=0;int pre[maxn];struct Side{    int to,next;}e[maxm];void add(int u,int v){    e[tot].to=v;    e[tot].next=pre[u];    pre[u]=tot++;}void tarjan(int i){    v[i]=1;    top++;    stack[top]=i;    ++tm;    rea[i]=tm;    low[i]=tm;    for(int j=pre[i];j!=-1;j=e[j].next)        {            int x=e[j].to;            if(v[x]==0) tarjan(x);            if(v[x]<2) low[i]=min(low[i],low[x]);        }    if(rea[i]==low[i])    {        color++;        while(stack[top+1]!=i)        {            col[stack[top]]=color;            v[stack[top]]=2;            top--;            num[color]++;        }    }}void init(){    mem(col);    mem(v);    mem1(pre);    tot=0;    top=0;    tm=0;    index=0;    color=0;    mem(in);    mem(out);    mem(low);    mem(rea);    mem(num);    mem(stack);}int main(){     //freopen("in.txt","r",stdin);     int uu,vv;     int t,ji=0;     cin>>t;     while(t--)     {        sc2(n,m);        init();        lp(i,m)          {              sc2(uu,vv);              add(uu,vv);          }        lp(i,n)         if(!rea[i])             tarjan(i);        if(color==1)         {            printf("Case %d: -1\n",++ji);            continue;         }         lp(i,n)         {             for(int j=pre[i];j!=-1;j=e[j].next)            {               int x=e[j].to;               if(col[i]!=col[x])               {                  out[col[i]]++;                  in[col[x]]++;               }            }         }         ll re=0;         ll tmp=0;       lp(i,color)        if(in[i]==0 || out[i]==0)        {            tmp=(ll)num[i];            re=max(re,tmp*(tmp-1)+(n-tmp)*(n-tmp-1)+tmp*(n-tmp)-(ll)m);        }        printf("Case %d: %lld\n",++ji,re);     }    return 0;}



0 0
原创粉丝点击