Strongly connected HDU
来源:互联网 发布:战锤冰川网络 编辑:程序博客网 时间:2024/06/11 15:28
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 Sample Output
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.
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.
If the original graph is strongly connected, just output -1.
33 31 22 33 13 31 22 31 36 61 22 33 14 55 66 4
Case 1: -1Case 2: 1Case 3: 15
题意:给一个简单无向图,让你加最多的边,使他还是一个简单无向图。
思路:首先要把这个图变成一个完全图,然后减去最初的m条边。然后想要加入的边最大化,你需要强联通缩点,把入度为0或者出度为0的内含节点最少的联通块找出来,然后再减去最小联通块内的点与其他点的连接边就可以了。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll __int64const int N = 100005;struct EDG{ int to,next;} edg[N];int eid,head[N];int low[N],dfn[N],vist[N],num[N],id[N],deep,stack1[N],tn,top;int in[N],out[N];void init(){ eid=tn=top=deep=0; memset(head,-1,sizeof(head)); memset(vist,0,sizeof(vist)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(num,0,sizeof(num));}void addEdg(int u,int v){ edg[eid].to=v; edg[eid].next=head[u]; head[u]=eid++;}void tarjer(int u){ stack1[++top]=u; vist[u]=1; deep++; low[u]=dfn[u]=deep; for(int i=head[u]; i!=-1; i=edg[i].next) { int v=edg[i].to; if(vist[v]==0) { vist[v]=1; tarjer(v); low[u]=min(low[u],low[v]); } else if(vist[v]==1) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { tn++; do { vist[stack1[top]]=2; num[tn]++; id[stack1[top]]=tn; } while(stack1[top--]!=u); }}ll solve(int n,int m){ ll ans=n*(n-1)-m; int minnum=N; for(int i=1; i<=n; i++) if(vist[i]==0) tarjer(i); if(tn==1) return -1; for(int u=1; u<=n; u++) for(int i=head[u]; i!=-1; i=edg[i].next) { int v=edg[i].to; if(id[u]!=id[v]) in[id[v]]++,out[id[u]]++; } for(int i=1; i<=tn; i++) if(in[i]==0||out[i]==0) { minnum=min(minnum,num[i]); } ans-=minnum*(n-minnum); return ans;}int main(){ int T,n,m,c=0,a,b; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); init(); for(int i=1; i<=m; i++) { scanf("%d%d",&a,&b); addEdg(a,b); } printf("Case %d: %I64d\n",++c,solve(n,m)); }}
阅读全文
0 0
- hdu 4635Strongly connected
- HDU 4635 Strongly connected
- HDU 4635 Strongly connected
- hdu 4635 Strongly connected
- Strongly connected HDU
- hdu 4635 Strongly connected(Tarjan)
- hdu 4635 Strongly connected (tarjan)
- [tarjan] hdu 4635 Strongly connected
- hdu acm 4635 Strongly connected
- hdu 4635——Strongly connected
- HDU 4635 Strongly connected 解题报告
- HDU 4635 Strongly connected(图论)
- hdu 4635 Strongly connected 强连通
- hdu 4635 Strongly connected (强连通分量)
- hdu-4635-Strongly connected-强连通分量
- HDU 4635 Strongly connected 强连通
- hdu 4635 Strongly connected(连通分量)
- HDU 4635 Strongly connected(强连通分量)
- 网易2018笔试题五
- L1 L2范式的详解以及Scikit-learn上基于L1 L2范式正则化的实例
- 欢迎使用CSDN-markdown编辑器
- mongodb中update的使用
- Ajax状态值及状态码整理
- Strongly connected HDU
- 结构性伪类选择器
- 多线程共享数据 synchronized和使用ThreadLocal区别
- Python入门
- ceph-deploy osd activate xxx bluestore ERROR
- Linux系统根目录详解
- 怎样才能够修改PDF文件中的文字大小
- AIDL原理分析
- 进程间通信(二)----消息队列