【HDU - 4635】Strongly connected(缩点,新图性质,建图,Tarjan求强连通分量)

题干:

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

3
3 3
1 2
2 3
3 1
3 3
1 2
2 3
1 3
6 6
1 2
2 3
3 1
4 5
5 6
6 4

Sample Output

Case 1: -1
Case 2: 1
Case 3: 15

解题报告:

    这题首先缩点,因为这些点之间肯定是强连通的,我们为了不让他是个强连通图,就是要新图不再是一个强连通分量就可以了。Tarjan处理完了之后得到每个集合的点数和一张新图,我们不去建图,只需要记录入度和出度,这样就得到了一张DAG图。我们要想加的边尽量多但是不让他是一个强连通图,那么就需要让这个新图的scc控制在2。也就是刚刚好不是个强连通,所以我们先假设连上了所有的边,然后考虑哪些边是不能加的。特就是将新图分成两个大集合,让他们之间只有单向边就可以了。也就是我们要找到一个大集合A和大集合B,元素个数假设分别为x和n-x,这样要减去的边数就是x*(n-x),要让这个值尽量小,所以我们枚举每一个小集合(要出度为0或者入度为0的小集合),求出以他为A大集合,需要减去的边数,维护最小值就可以了。

至于为什么要出度为0或者入度为0的小集合呢?因为如果是中间的某个集合,那么也要去掉的是  与当前DAG序列的所有入度为零的小集合 之间的那些连边,所以那样肯定减去的很多,而且可以直接归属在我们写的这种算法中。

当然也可以不用减法,直接A*B + A*(A-1) + B*(B-1) - m

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
struct Edge {int to;int ne;
} e[MAX];
int head[MAX],tot;
int DFN[MAX],LOW[MAX],col[MAX],clk,index,scc,stk[MAX],vis[MAX];
ll cnt[MAX];
int n,m;
int in[MAX],out[MAX];
void add(int u,int v) {e[++tot].to = v;e[tot].ne = head[u];head[u] = tot;
}
void Tarjan(int x) {DFN[x] = LOW[x] = ++clk;vis[x] = 1;stk[++index] = x;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].to;if(!DFN[v]) {Tarjan(v);LOW[x] = min(LOW[x],LOW[v]);}else if(vis[v]) LOW[x] = min(LOW[x],DFN[v]);}if(LOW[x] == DFN[x]) {scc++;while(1) {int tmp = stk[index];index--;vis[tmp] = 0;col[tmp] = scc;cnt[scc]++;if(tmp == x) break;}}
} 
void init() {memset(head,-1,sizeof head);tot = 0;for(int i = 1; i<=n; i++) {DFN[i] = LOW[i] = cnt[i] = 0;in[i] = out[i] = 0;}clk = 0;index = 0;scc = 0;
}
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d%d",&n,&m);init();for(int a,b,i = 1; i<=m; i++) {scanf("%d%d",&a,&b);add(a,b);}for(int i = 1; i<=n; i++) {if(!DFN[i]) Tarjan(i);}if(scc == 1) {printf("Case %d: -1\n",++iCase);continue;}for(int u = 1; u<=n; u++) {for(int i = head[u]; ~i; i = e[i].ne) {int v = e[i].to;if(col[u] != col[v]) {out[col[u]]++;in[col[v]]++;}}}ll minn = n;for(int i = 1; i<=scc; i++) {if(in[i] == 0 || out[i] == 0) {minn = min(minn,cnt[i]);}} ll ans = 1LL * n * (n-1) - m;//最多还能增加这些边 printf("Case %d: %lld\n",++iCase,ans - minn * (n-minn) );}return 0 ;
}
/*
14:36 - 14:53
*/

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440525.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python bind sock_python 在bind端口之后创建的socket如果不关闭的话会被回收吗?

在进行tcpsocket编程的时候&#xff0c;遇到一个问题&#xff1a;我创建一个Asocket&#xff0c;在进行bind之后&#xff0c;这个socket为 在进行tcpsocket编程的时候&#xff0c;遇到一个问题&#xff1a; 我创建一个Asocket&#xff0c;在进行bind之后&#xff0c;这个socket…

【ZOJ - 2955】Interesting Dart Game(背包,结论,裴蜀定理,数论)

题干&#xff1a; Recently, Dearboy buys a dart for his dormitory, but neither Dearboy nor his roommate knows how to play it. So they decide to make a new rule in the dormitory, which goes as follows: Given a number N, the person whose scores accumulate e…

接口测试 java_接口测试--Java

1&#xff1a;interface :定义接口接口用于 模块与模块之间的连接 或者系统与系统间的连接2&#xff1a;软件系统UI层&#xff1a;程序的界面&#xff0c;主要为用户提供交互和操作--查看&#xff0c;输入&#xff0c;点击&#xff0c;等。业务逻辑层&#xff1a;进行复杂的业…

【ZOJ - 2949】Coins of Luck (概率dp,期望)

题干&#xff1a; 给你两种泡面 各N包&#xff0c;然后你扔硬币&#xff0c;正面朝上吃第一种&#xff0c;反面呢 吃第二种&#xff0c;有任意一种吃完时 就不需要抛硬币了&#xff0c;求停止抛硬币的期望。 Sample Input 2 1 2Sample Output 1.00 2.50 解题报告&#xff…

java perl_在Java中调用Perl脚本

有两种方法&#xff0c;第一种是直接 Runtime.getRuntime().exec("...");这里推荐第二种&#xff0c;可以得到输出和返回值&#xff1a;源代码如下&#xff1a;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;public c…

*【HDU - 6201】transaction transaction transaction(树形dp 或 spfa最长路 或 网络流)

题干&#xff1a; 题目大意&#xff1a; 给出一棵n个顶点的树&#xff0c;每个点有一个权值&#xff0c;代表商品的售价&#xff0c;树上每一条边上也有一个权值&#xff0c;代表从这条边经过所需要的花费。现在需要你在树上选择两个点&#xff0c;一个作为买入商品的点&#…

java 手写签名_Android 自定义View手写签名并保存图片

GIF压缩有问题&#xff0c;运行很顺滑&#xff01;&#xff01;&#xff01;1.自定义View——支持设置画笔颜色&#xff0c;画笔宽度&#xff0c;画板颜色&#xff0c;清除画板&#xff0c;检查是否有签名&#xff0c;保存画板图片(复制粘贴可直接使用)/*** Created by YyyyQ o…

【2019第十届蓝桥杯省赛C/C++B组题解】(非官方题解)

A。 数数题。 答案&#xff1a;490 B。 26进制模拟。 答案&#xff1a;BYQ C。 类似fib数列求值&#xff0c;递推一下就好。 答案&#xff1a;4659 D。 注意两个坑点&#xff0c;一个是正整数&#xff0c;所以枚举要从1开始。第二个坑点是互不相同的&#xff0c;为了达到这…

java对象模型 指令_深入理解多线程(二)—— Java的对象模型

上一篇文章中简单介绍过synchronized关键字的方式&#xff0c;其中&#xff0c;同步代码块使用monitorenter和monitorexit两个指令实现&#xff0c;同步方法使用ACC_SYNCHRONIZED标记符实现。后面几篇文章会从JVM源码的角度更加深入&#xff0c;层层剥开synchronized的面纱。在…

【POJ - 1651】Multiplication Puzzle(区间dp)

题干&#xff1a; The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken …

java hive demo_java 操作hive通过jdbc

直接代码吧&#xff1a;记得要开启hive jdbc服务hive --service hiveserverpackage hive;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class HiveDemo {static{//注册jdbc驱动try {Class.forName(&…

【牛客 - 551D】CSL 的字符串(单调栈,思维)

题干&#xff1a; CSL 以前不会字符串算法&#xff0c;经过一年的训练&#xff0c;他还是不会……于是他打算向你求助。 给定一个字符串&#xff0c;只含有可打印字符&#xff0c;通过删除若干字符得到新字符串&#xff0c;新字符串必须满足两个条件&#xff1a; 原字符串中…

java 学生课程成绩_Java课设--学生成绩管理系统一

写在前面这个项目是Java课程的课设&#xff0c;一共花了5天的时间去完成它&#xff0c;在这期间感谢一些博主的帮助&#xff0c;让我了解到了一些新的技术知识&#xff0c;所以打算写这一系列博客来介绍一整个课设项目&#xff0c;也为了帮助之后的人&#xff0c;如有错误&…

【牛客 - 551E】CSL 的魔法(贪心,思维,STLmap,分块)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/E 来源&#xff1a;牛客网 有两个长度为 n 的序列&#xff0c;a0,a1,…,an−1a0,a1,…,an−1和 b0,b1,…,bn−1b0,b1,…,bn−1。CSL 有一种魔法&#xff0c;每执行一次魔法&#xff0c;可以任意挑…

java调用cplex案例_【CPLEX教程03】java调用cplex求解一个TSP问题模型

前面我们已经搭建好cplex的java环境了&#xff0c;相信大家已经跃跃欲试&#xff0c;想动手写几个模型了。今天就来拿一个TSP的问题模型来给大家演示一下吧~CPLEX系列教程可以关注我们的公众号哦&#xff01;获取更多精彩消息&#xff01;01 TSP寤烘ā关于TSP建模&#xff0c;就…

【牛客 - 551C】CSL 的密码(后缀数组,后缀自动机,随机算法)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/C 来源&#xff1a;牛客网 为了改变这一点&#xff0c;他决定重新设定一个密码。于是他随机生成了一个很长很长的字符串&#xff0c;并打算选择一个子串作为新密码。他认为安全的密码长度至少为…

java在一个类里实现存款_用Java编写一个简单的存款

package desposit.money;public class DespositMoney {public static void main(String[] args) {Customer c1 new Customer("第一个顾客",3);Customer c2 new Customer("第二个顾客",10);Customer c3 new Customer("第三个顾客",5);c1.start…

【牛客 - 551G】CSL的训练计划(二分 + 拓扑排序 + 优化卡常)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/G 来源&#xff1a;牛客网 题目描述 众所周知&#xff0c;CSL 是一个负责的集训队队长。为了让集训队的学弟们训练更加饱和&#xff0c;他根据每个人的能力&#xff0c;提出了 m 个题数要求。假如…

java 自动启动监听_Spring Boot 启动事件和监听器,太强大了!

大家都知道&#xff0c;在 Spring 框架中事件和监听无处不在&#xff0c;打通了 Spring 框架的任督二脉&#xff0c;事件和监听也是 Spring 框架必学的核心知识之一。一般来说&#xff0c;我们很少会使用到应用程序事件&#xff0c;但我们也不要忘了它们的存在&#xff0c;比如…

【Gym - 101196F】Removal Game (环形区间dp,环状,细节优化)

题干&#xff1a; 健健开发了一个游戏叫做<<者护守形隐>>&#xff0c;里面有一个情节是这样的&#xff0c;女主子纯藤武被坏人关在了密室里&#xff0c;作为男主的肖健当然要英雄救美。但是要打开密室的门&#xff0c;必须解开一道谜题。 门上有几个数字围成的一个…