hdu-4635
来源:互联网 发布:vuex刷新页面数据丢失 编辑:程序博客网 时间:2024/06/16 20:34
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.
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题意:题目明显告诉了这是强连通的题,询问最多添加多少条边,结果得到的依旧不是强连通图,如果一开始就是强连通图,则输出“-1”。
题解:首先,特判的肯定先解决,依旧是tarjan算法,如果是一个强连通图,输出“-1”。然后因为该题是求最大,而且是连通图和非连通图的临界条件。于是可以yy一下最大值的情况是怎样的。因为还是tarjan然后求DAG,这是最基本的,即我们已经将原图变成DAG了,此时还不能是强连通图,则必须有一个DAG对于其他所有的DAG都只有出边而不能有入边(或者只有入边而没有出边,这是一样的),而答案就是完全图的边数(n*(n-1))- (这个特殊的DAG与外界不能相连的边数,即该DAG里的顶点个数乘以除该DAG的顶点外的所有顶点数)- (原来图本就存在的边数m)。而要想这个值最大,则这个特殊的DAG的元素必须最少(或者最大),而求DAG的元素个数就是简单的在推栈的过程中加上个计数器,并储存到一个数组里。最后的操作就只需要在出入度为0的DAG中找出元素最少的,做刚才的运算就可以了。
代码:
//Wud#include <iostream>#include <algorithm>#include <cstdio>#include <stack>#include <cstring>#include <vector>#include <bitset>#include <string>#include <cmath>#include <set>#include <map>#include <queue>using namespace std;typedef long long ll;const int maxn = 1e5+7;vector<int> g[maxn];stack<int> s;int dfn[maxn],low[maxn],vis[maxn],id[maxn],num[maxn];int in[maxn],out[maxn];int n,m,tot,cnt,msum;void tarjan(int x){ dfn[x] = low[x] = ++tot; s.push(x),vis[x] = 1; for (int i = 0;i < g[x].size();i++){ int v = g[x][i]; if(!dfn[v]) { tarjan(v); low[x] = min(low[x],low[v]); } else if(vis[v]){ low[x] = min(low[x],dfn[v]); } } if(dfn[x] == low[x]){ cnt++; while (1){ int now = s.top(); s.pop(); vis[now] = 0; num[cnt]++; id[now] = cnt; if(now==x) break; } }}int main(){ int t; int k = 1; scanf("%d",&t); while (t--){ memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); memset(id,0,sizeof(id)); memset(num,0,sizeof(num)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(id,0,sizeof(id)); scanf("%d %d",&n,&m); for (int i = 1;i <= n;i++) g[i].clear(); for (int i = 0;i < m;i++){ int a,b; scanf("%d %d",&a,&b); g[a].push_back(b); } tot = 0,cnt = 0; for (int i = 1;i <= n;i++){ if(!dfn[i]) tarjan(i); } if(cnt==1){ printf("Case %d: -1\n",k++); continue; } for (int i = 1;i <= n;i++){ for (int j = 0;j < g[i].size();j++){ int u = id[i],v = id[g[i][j]]; if(u!=v){ out[u]++; in[v]++; } } } msum = 1e9; for (int i = 1;i <= cnt;i++){ if(out[i]==0){ msum = min(msum,num[i]); } if(in[i]==0){ msum = min(msum,num[i]); } } ll ans = (long long)n*(n-1); ans-=(long long)msum*(n-msum); printf("Case %d: %d\n",k++,ans-m); } return 0;}
1 0
- HDU 4635
- hdu-4635
- hdu 4635
- HDU 4635 多校四-1004
- hdu 4635Strongly connected
- HDU 4635 Strongly connected
- HDU 4635 Strongly connected
- hdu 4635 Strongly connected
- hdu 4635(tarjan)
- hdu(4635)
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- php内置进制转换函数
- Java学习日志 17.04.30 之前一道题目
- 多线程(7):
- 多线程(8):
- web安全
- hdu-4635
- 安全专家发现GE Multilin SR的一个关键漏洞对全球电网构成严重威胁。
- Android6.0权限记录
- 欢迎使用CSDN-markdown编辑器
- Matlab与CUDA C的混合编程配置出现的问题及解决方案
- springmvc 表单提交到后台乱码问题
- 三个因素决定你的薪水高低
- Hibernate复习一-----Hibernate配置jdbc
- SDUT 3565 Feed the monkey && sdut 2879 Colorful Cupcakes (dp)