HDU 4635(强连通分量分解

题目:给出一个有向图,要求添加最多的边数,使得图仍然不强连通.

思路:首先这个图在添加边之后肯定变成了两个强连通分量,现在就看怎么分.然后我们可以注意到,原图进行强连通分量分解之后必然存在一些分量的出度或入度为0,最小的分量肯定在这些分量之中.那么找出这个分量就可以得出的结果了.

/*
* @author:  Cwind
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <set>
#include <cmath>
using namespace std;
#define IOS std::ios::sync_with_stdio (false);std::cin.tie(0)
#define pb push_back
#define PB pop_back
#define bk back()
#define fs first
#define se second
#define sq(x) (x)*(x)
#define eps (1e-7)
#define IINF (1<<29)
#define LINF (1ll<<59)
#define INF 1000000000
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> P;
inline void debug_case(){static int cas=1;printf("<---------------Case #:%d------------------>\n",cas);cas++;
}
const int maxn=1e5+300;
int T;
int n,m;
vector<int> G[maxn];
int dfn[maxn];
int clo;
int inscc[maxn],stk[maxn],low[maxn],top,sccid,cnt[maxn];
bool instk[maxn];
void tarjan(int v,int f){low[v]=dfn[v]=++clo;stk[top++]=v;instk[v]=1;for(int i=0;i<G[v].size();i++){int u=G[v][i];if(!dfn[u]){tarjan(u,v);low[v]=min(low[v],low[u]);}else if(instk[u]){low[v]=min(low[v],dfn[u]);}}if(low[v]==dfn[v]){sccid++;do{inscc[stk[--top]]=sccid;cnt[sccid]++;instk[stk[top]]=0;}while(stk[top]!=v);}
}
int d1[maxn],d2[maxn];
void init(){for(int i=0;i<=n;i++){G[i].clear();d2[i]=d1[i]=cnt[i]=dfn[i]=0;}sccid=top=clo=0;
}
int cas=0;
int main(){freopen("/home/files/CppFiles/in","r",stdin);//freopen("defense.in","r",stdin);//freopen("defense.out","w",stdout);cin>>T;while(T--){//    debug_case();scanf("%d%d",&n,&m);init();for(int i=0;i<m;i++){int a,b;scanf("%d%d",&a,&b);G[a].pb(b);}for(int i=1;i<=n;i++){if(!dfn[i]) tarjan(i,-1);}for(int i=1;i<=n;i++){for(int j=0;j<G[i].size();j++){int u=G[i][j];if(inscc[u]!=inscc[i]){d1[inscc[u]]++;d2[inscc[i]]++;}}}int minn=1e9;for(int i=1;i<=sccid;i++){if(d1[i]==0) minn=min(minn,cnt[i]);if(d2[i]==0) minn=min(minn,cnt[i]);}ll a=n-minn,b=minn;ll ans=(ll)n*(n-1)-(ll)a*b-(ll)m;if(sccid==1) ans=-1;printf("Case %d: %lld\n",++cas,ans);}return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Cw-trip/p/4828674.html

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

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

相关文章

Android方法的概括,android中的Filter接口简介

一、结构public abstract class Filter extends Objectjava.lang.Objectandroid.widget.Filter二、概述过滤器通过过滤模式来约束数据&#xff0c;通常由实现了Filterable接口的子类来生成。 过滤操作是通过调用filter(CharSequence)或者filter(CharSequence, android.widget.F…

android webview 长按菜单拦截,javascript-在Android上长按时禁用上下文菜单

javascript-在Android上长按时禁用上下文菜单我想禁用长按(触摸并按住)Web应用程序中的图像后出现的上下文菜单。 我见过关于如何做到这一点的不同想法&#xff0c;但似乎没有一个对我有用。有没有办法通过HTML / CSS / Javascript在Android上执行此操作&#xff1f;12个解决方…

oracle之数据处理之约束1

--创建表 create table emp4( id number(10) constraint emp2_id_nn not null, name varchar2(20) not null, salaty number(10,2) )--查询表 select *from emp4--插入 insert into emp4 values(1001,geyao,2000) 运行结果 --创建表 create table emp5( --列级约束 id numbe…

java中注解的使用与实例(一)

注解目前非常的流行&#xff0c;很多主流框架都支持注解&#xff0c;而且自己编写代码的时候也会尽量的去用注解&#xff0c;一时方便&#xff0c;而是代码更加简洁。 注解的语法比较简单&#xff0c;除了符号的使用之外&#xff0c;它基本与Java固有语法一致。Java SE5内置了三…

oracle之数据处理之约束2

--修改约束 alter table emp5 modify (salary number(10,2) not null)运行结果 --删除约束 alter table emp5 drop constraint emp5_name_nn 运行结果

css实现一个写信的格式

一、目标 目标实现如下效果&#xff1a; 二、完成 1、分析 这个效果看起来很简单&#xff0c;实际上可能并不那么容易实现。 首先是全部东西都居中显示&#xff0c;除了“亲爱的starof”这个称呼的地方。这也是难点&#xff0c;也是本文要重点说的地方。 开始我尝试将“ 亲爱的…

android 6.0版本名字,棉花糖Marshmallow 是Android 6.0的名字

Android 6.0 五月下旬在 I/O 大会上亮相&#xff0c;将于今年秋天与用户见面。但是依照惯例&#xff0c;谷歌并没有宣布新 Android 的代号全称&#xff0c;而 Android M 中 M 所代表的甜品&#xff1a;Marshmallow(棉花糖)。并不是MM巧克力豆&#xff0c;巧克力豆的粉丝&#x…

选项卡,下拉菜单操做时的页面数据更新,highcharts,d3 结合。

1.选项卡&#xff1a;给要选中的元素添加css样式&#xff0c;加active&#xff0c;单击时先移除active&#xff0c;再把当前单击元素添加active。 单击时页面切换&#xff0c;按钮和页面要有关联&#xff0c;通过获取$(this).text();年龄&#xff0c;教育&#xff0c;职业&…

小米9android系统怎么关闭,小米MIUI系统怎么禁用虚拟键 小米MIUI系统禁用虚拟键方法...

想新很多米粉对对miui系统不会陌生。这个系统还很是不错。但是刚刚入手小米手机的米粉可能就不太熟悉了。那么&#xff0c;虚拟键要怎么去禁用呢&#xff1f;下面就一起来看看小米MIUI系统虚拟键禁用方法。大家用安卓手机的时候是否曾遇到过以下折磨人的场景&#xff1a;小米MI…

C#性能优化:延迟初始化LazyT

1. 概述 我们创建某一个对象需要很大的消耗&#xff0c;而这个对象在运行过程中又不一定用到&#xff0c;为了避免每次运行都创建该对象&#xff0c;这时候延迟初始化&#xff08;也叫延迟实例化&#xff09;就出场了。 延迟初始化出现于.NET 4.0&#xff0c;主要用于提高性能&…

android手机值不值得买,安卓直屏Redmi K40手机值得买吗?

描述还记得我们上一次谈论直屏与曲面屏的关系吗&#xff1f;小Z从后台留言中发现了很多人确实对如今清一色的旗舰曲面屏感到不满&#xff0c;用着远没有几年前的直屏来得顺手。但在如今的手机市场中&#xff0c;想要找到一款屏幕素质不错&#xff0c;硬件配置足够旗舰&#xff…

oracle之数据处理之视图

--创建视图 create view empview as select employee_id,last_name,salary from employees where department_id80;--查询视图 select * from empview--修改视图 update empview set salary20000 where employee_id179 运行结果 --修改视图 create or replace view empview2 a…

oracle之数据处理之其他数据库对象

--创建序列 create sequence empseq increment by 10---每次增长10 start with 10--从10开始 maxvalue 100--提供最大值 cycle --循环 nocache --不需要缓存登录 运行结果 --查看 查看 select empseq.nextval from dual 运行结果 --创建表 create table emp11 as select empl…

android 刷rom,刷ROM是什么?刷ROM是什么意思?

刷ROM是什么意思首先&#xff0c;ROM是由英文Read only Memory的首字母构成的&#xff0c;意为只读存储器。顾名思义&#xff0c;就是这样的存储器只能读&#xff0c;不能像RAM一样可以随时读和写。它只允许在生产出来之后有一次写的机会&#xff0c;数据一旦写入则不可更改。它…

oracle之set运算符和练习

--创建表 create table emp01 as select * from employees where department_id in (70,80)-- --创建表 create table emp02 as select * from employees where department_id in (80,90) --查询 --并集 select * from employees where department_id in (70,80) union all se…

活动页面html设计,活动查看页面.html

&#xfeff;活动查看页面$axure.utils.getTransparentGifPath function() { return resources/images/transparent.gif; };$axure.utils.getOtherPath function() { return resources/Other.html; };$axure.utils.getReloadPath function() { return resources/reload.html…

oracle之高级子查询1

--子查询 查询last_name为chen的manager信息select employee_id,last_name from employees where employee_id( select manager_id from employees where last_nameChen ) 运行结果 --查询 select employee_id,manager_id,department_id from employees where manager_id in( …

intp适合学计算机吗,⑨MBTI测试,分析INTP型人格的专业选择和职业选择

原标题&#xff1a;⑨MBTI测试&#xff0c;分析INTP型人格的专业选择和职业选择在MBTI职业性格测试中&#xff0c;INTP型人格&#xff0c;沉默、自主、思维敏捷、自主性强&#xff0c;对理论和科学有自己的追求。喜欢提出新主张&#xff0c;也爱用逻辑去分析问题&#xff0c;为…

STL学习笔记(仿函数)

仿函数(Functors) 仿函数(functor)&#xff0c;就是使一个类的使用看上去象一个函数。其实现就是类中实现一个operator()&#xff0c;这个类就有了类似函数的行为&#xff0c;就是一个仿函数类了。 例如我们定义一个类&#xff1a; class X{public:return-value operator()(arg…