HDU 4635 Strongly connected 连通图

来源:互联网 发布:苹果电脑删除mac系统 编辑:程序博客网 时间:2024/06/04 19:51

题目描述:

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 

题目分析:

题目大意就是有一个有向图,你可以至多加多少条边使这个有向图依然不能强连通。若原图就是强连通图,则输出-1。
先用Tarjan算连通分支,如果只有一个连通分支,说明这是强连通图,输出-1。
接下来把问题等价转化成一个完全图,减去最少的边使这个图不是强连通图。将各个强连通分量缩成点,去除所有强联通分量中含有点数最少的点的所有进边,就能得出答案。

代码如下:

#include <stdio.h>#include <algorithm>#include <string.h>#include <iostream>#include <map>#include <vector>#include <queue>#include <set>#include <string>#include <math.h>const int MAXN=100010;const int INF= 0x3f3f3f3f;typedef long long LL;using namespace std;struct Edge{    int to,next;}edge[MAXN];int head[MAXN],tot;int low[MAXN],dfn[MAXN],stack[MAXN],belong[MAXN];int index,top;int scc;bool instack[MAXN];int num[MAXN];//各个强连通分量包含的点的个数int id[MAXN],od[MAXN];//出度和入度void init(){    tot=0;    memset(head,-1,sizeof(head));}void addedge(int u,int v){    edge[tot].to=v;    edge[tot].next=head[u];    head[u]=tot++;}void tarjan(int u)//kuangbin模板{    int v;    low[u]=dfn[u]=++index;    stack[top++]=u;    instack[u]=true;    for(int i=head[u]; i!=-1; i=edge[i].next)    {        v=edge[i].to;        if (!dfn[v])        {            tarjan(v);            if (low[u]>low[v]) low[u]=low[v];        }        else if (instack[v] && low[u]>dfn[v]) low[u]=dfn[v];    }    if (low[u]==dfn[u])    {        scc++;        do        {            v=stack[--top];            instack[v]=false;            belong[v]=scc;            num[scc]++;        }        while(v!=u);    }}void solve(int n){    memset(dfn,0,sizeof(dfn));    memset(instack,false,sizeof(instack));    memset(num,0,sizeof(num));    index=scc=top=0;    for(int i=1; i<=n; i++)    {        if (!dfn[i]) tarjan(i);    }}int T;int n,m;int main(){    scanf("%d",&T);    for(int t=1; t<=T; t++)    {        init();        scanf("%d%d",&n,&m);        for(int i=0; i<m; i++)        {            int x,y;            scanf("%d%d",&x,&y);            addedge(x,y);        }        solve(n);        printf("Case %d: ",t);        if (scc==1) {printf("-1\n");continue;}        memset(id,0,sizeof(id));        memset(od,0,sizeof(od));        for(int u=1; u<=n; u++)        {            for(int i=head[u]; i!=-1; i=edge[i].next)            {                int v=edge[i].to;                if (belong[u]!=belong[v])                {                    od[belong[u]]++;                    id[belong[v]]++;                }            }        }        LL sss=(LL)n*(n-1)-m;        LL ans=0;        for(int i=1; i<=scc; i++)        {            if(id[i]==0 || od[i]==0)                ans=max(ans,sss-(LL)num[i]*(n-num[i]));        }        printf("%lld\n",ans);    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕妇吃的水果少怎么办 6个月宝宝厌奶怎么办 5个月宝宝厌奶怎么办 11个月宝宝厌奶怎么办 母乳宝宝不喝奶粉怎么办 换奶粉不拉大便怎么办 宝宝和妈妈不亲怎么办 冬天榨果汁太凉怎么办 四个月宝宝拉水怎么办 婴儿吃奶吃撑了怎么办 1岁的宝宝腹泻怎么办 一岁宝宝老拉肚子怎么办 一岁宝宝拉肚子怎么办啊 小孩发烧怎么办39度要吃消炎药吗 孩子不爱吃水果蔬菜怎么办 一岁宝宝不吃水果怎么办 一岁宝宝不爱吃水果怎么办 1岁多宝宝不吃水果怎么办 中学生不爱与家长交流怎么办 孩子一直37度多怎么办 小孩39度3算高烧怎么办 7岁发烧怎么办如何退烧 7岁反复发烧7天怎么办 一岁发烧39.8度怎么办 反复发烧39度4天怎么办 大人发烧到39度怎么办 小孩高烧39度多怎么办 孩子嗓子发炎发烧怎么办吃什么药 猫咪吃了点桃子怎么办 猫吃了牛油果怎么办 苹果手机死机开不了机怎么办 新生儿出生第一天没奶水怎么办 第一天断奶奶水一直流出怎么办 3岁宝宝不长个子怎么办 5个月宝宝不长个怎么办 孩子比同龄人矮很多怎么办 孕妇做春梦宫缩怎么办 减肥掉头发很厉害怎么办 孕妇吃了金枪鱼罐头怎么办 怀孕吃了烂水果怎么办 快递水果压坏了怎么办