强连通(hdu4635)最多增加几条单向边后满足最终的图形不是强连通
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1802 Accepted Submission(s): 746
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.
#include"cstdio"
#include"cstring"
#include"cstdlib"
#include"cmath"
#include"string"
#include"map"
#include"cstring"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#define inf 0x3f3f3f3f
#define M 200009
#define eps 1e-8
#define INT int
#define LL __int64
using namespace std;
struct node
{
int u,v,next;
}edge[M*];
int head[M];
int dfn[M];
int belong[M];
int low[M];
int use[M];
int sum[M];
int in[M];
int out[M];
int t,indx,num,cnt,m,n;
stack<int>q;
void init()
{
t=;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
void tarjan(int u)
{
dfn[u]=low[u]=++indx;
q.push(u);
use[u]=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(use[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
int p;
num++;
do
{
p=q.top();
q.pop();
use[p]=;
belong[p]=num;
sum[num]++; }while(p!=u);
}
}
void slove()
{
num=indx=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(use,,sizeof(use));
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
if(num==)
{
printf("-1\n");
return;
}
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(int i=;i<t;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(belong[u]!=belong[v])
{
out[belong[u]]++;
in[belong[v]]++;
}
}
LL ans=;
for(int i=;i<=num;i++)
{
if(!out[i]||!in[i])
{
LL s=(LL)n*(n-)-m-(LL)sum[i]*(n-sum[i]);
if(ans<s)
ans=s;
}
}
printf("%I64d\n",ans);
}
int main()
{
int T,a,b,kk=;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
printf("Case %d: ",kk++);
slove();
}
return ;
}
强连通(hdu4635)最多增加几条单向边后满足最终的图形不是强连通的更多相关文章
- 2017 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。
/** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...
- HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】
Strongly connected Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- history统计命令最多的20条
1.1.1 统计使用命令最多的20条 [root@ob1 ~]# history|awk '{ml[$2]++}END{for (i in ml) print i,ml[i]}'|sort -nrk ...
- 震惊,当我运行了这条Linux命令后,服务器竟然... (Linux中的删除命令)
震惊,当我运行了这条Linux命令后,服务器竟然... 0X00 写在前面 大家都听说过删库命令rm -rf /*,但是谁又真正实践过呢?但作为一个程序员,不看看这条命令执行后会发生什么,怎么能甘心呢 ...
- HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 增加一条新记录,同时返回其自增id
方法一.是在Insert或Update触发器中用select来返回需要的字段值.默认情况下,当insert时,触发其insert触发器,它的默认返回值是影响到的行数,语句是:select @@rowc ...
- 强连通图(最多加入几条边使得图仍为非强连通图)G - Strongly connected HDU - 4635
题目链接:https://cn.vjudge.net/contest/67418#problem/G 具体思路:首先用tarjan缩点,这个时候就会有很多个缩点,然后再选取一个含有点数最少,并且当前这 ...
- 笔试算法题(19):判断两条单向链表的公共节点 & 字符集删除函数
出题:给定两个单向链表的头结点,判断其是否有公共节点并确定第一个公共节点的索引: 分析: 由于是单向链表,所以每个节点有且仅有一个后续节点,所以只可能是Y型交叉(每条链表中的某个节点同时指向一个公共节 ...
- Strongly connected HDU - 4635 原图中在保证它不是强连通图最多添加几条边
1 //题意: 2 //给你一个有向图,如果这个图是一个强连通图那就直接输出-1 3 //否则,你就要找出来你最多能添加多少条边,在保证添加边之后的图依然不是一个强连通图的前提下 4 //然后输出你最 ...
随机推荐
- uuu
#include<stdio.h> int map[10][10]={0}; int step[30]={0}; int max=99999; int q; int ans[99]={0} ...
- Summary of java stream classes
Java’s stream classes are good for streaming sequences of bytes, but they’re not good for streaming ...
- Startssl 现在就启用 HTTPS,免费的!
为什么要使用HTTPS 主要是为了安全,虽然没有100%的安全,但是我们可以尽量提高安全级别,目前大型网站都已经使用HTTPS了 注册StartSSL 注册页面 选择国家 和 输入 邮箱 他们会通过 ...
- ecshop换用redis做缓存
<?php /** * ecshop SESSION 保存类 * ================================================================ ...
- 用angularJS实现Bootstrap的“手风琴”
主页面代码(发现Bootstrap官网上手风琴的实例样式有问题,在这里依然使用3.0.~版本) <!DOCTYPE html> <html ng-app="ct" ...
- QFile文件操作-QT
#include <QCoreApplication> #include<QFile> #include<QString> #include<QDebug&g ...
- HTML+CSS结构与表现原则
CSS网页布局即版式布局,是网页设计师将有限的视觉元素进行有机的排列组合.主要通过CSS的浮动.定位功能来实现UI设计的布局要求. 常见的布局有:一列布局,两列布局,三列布局和混合布局. HTML清除 ...
- 20145211《Java程序设计》第5周学习总结——独上高楼,望尽天涯路
教材学习内容总结 异常处理 JAVA异常 异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.异常就是出现在运行时出现不正常 ...
- ios UIPickerView 技巧集锦(包括循环滚动)
摘自: http://blog.csdn.net/ipromiseu/article/details/7436521 http://www.cnblogs.com/dabaopku/archive/2 ...
- Java学习-014-文本文件写入实例源代码(两种写入方式)
此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 第一种:文本文件写入,若文件存在则删除原文件,并重新创建文件.源代码如下所示: /** * @fun ...