强连通(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 //然后输出你最 ...
随机推荐
- Delphi格式化输出函数(1): Format
vars: string;begin//指令类型 types := Format('最大整数是: %d; 最小整数是: %d',[MaxInt,Low(Integer)]);//返回: 最大整数是: ...
- Java Blocking Queue
//Listing 8-1. The Blocking Queue Equivalent of Chapter 3’s PC Application import java.util.concurre ...
- Java高级之内存模型分析
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 下文是博主感悟,请带着怀疑性的态度阅读! 需要了解基本变量所占 ...
- 利用快速排序原理找出数组中前n大的数
#include <stdio.h> #include <stdint.h> #include <stdlib.h> #define MAX_SIZE 400001 ...
- 怎么清除file控件的文件路径
还记得上次做一个文件上传,后来测试告诉我说,如果我要是不选择文件了呢?该怎么办?我说:简单啊,做一个取消按钮不就完事了吗!然后我就想一个file空间做一个取消是多么简单的事,用js处理可是想怎么样就怎 ...
- SQL Server 2008 R2不支持limit(限制行数)
SQL Server 2008 R2不支持limit 可用:select top 3 * from Websites2 MySQL 语法 SELECT *FROM PersonsLIMIT 5; Or ...
- gulp教程
1. http://www.tuicool.com/articles/FJVNZf 2.http://www.ydcss.com/archives/18 3.手动创建package.json: 如:c ...
- js - 驼峰命名
1. // 驼峰命名 console.log(hump('border-bottom-color')) function hump( str) { if (typeof str != 'string' ...
- linux命令之tee
功能说明:读取标准输入的数据,并将其内容输出成文件.语 法:tee [-ai][--help][--version][文件...]补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出设 ...
- 智能生活 科技无限 CTO VOICE 第二期 智能硬件创新创业专场演讲嘉宾招募
生活不只有诗和远方,还有当下的痛点和需求 当可穿戴设备.虚拟现实.无人机.机器人进入人们视线甚至生活当中 下一个风口就在智能硬件领域上凸显 那么,创业者如何撕掉智能外衣,设计一款有竞争力的智能硬件? ...