hdu4635:http://acm.hdu.edu.cn/showproblem.php?pid=4635

题意:给你一个有向图,然后问你最多可以加多少条边,是的原图不是一个强连通图。

题解:这一题确实不会,图论做的太少了,一下是一个人分析,觉得分析的很不错,代码也是看别人的。

首先强连通缩点,缩点之后,最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边;  *那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部中每个点到Y部的每个点都有一条边;  *假设X部有x个点,Y部有y个点,则x+y=n; 同时边数F=x*y+x*(x-1)+y*(y-1),然后去掉已经有了的边m,则为答案; 当x+y为定值时,二者越接近,x*y越大,所以要使得边数最多,那么X部和Y部的点数的个数差距就要越大; 对于给定的有向图缩点,对于缩点后的每个点,如果它的出度或者入度为0,那么它才有可能成为X部或者Y部; 然后找出最大值即可;

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=;
const int INF=0xffffffff;
struct Edge{
int to,next;
} edge[M]; int n,m,cnt,dep,top,atype;
int dfn[N],low[N],vis[N],head[N],st[N],belong[N],in[N],out[N],sum[N];
//sum[i]记录第i个连通图的点的个数,in[i],out[i],表示缩点之后点的入度和初度。
void init(){
cnt=dep=top=atype=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(belong,,sizeof(belong));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(sum,,sizeof(sum));
}
void addedge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Tarjan(int u){
dfn[u]=low[u]=++dep;
st[top++]=u;
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=st[--top];
belong[j]=atype;
sum[atype]++; //记录每个连通分量中点的个数
vis[j]=;
}
while(u!=j);
}
} long long solve(){
if(n==){
return -;
}
init();
int u,v;
for(int i=; i<m; i++){
scanf("%d%d",&u,&v);
addedge(u,v);
}
for(int i=; i<=n; i++)
if(!dfn[i])
Tarjan(i);
if(atype==){
return -;
}
for(int u=; u<=n; u++)
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].to;
if(belong[u]!=belong[v]){
out[belong[u]]++;
in[belong[v]]++;
}
}
long long tmp=;
for(int i=; i<=atype; i++)
if(in[i]== || out[i]==){
tmp=min(tmp,(long long)sum[i]);
}
return tmp*(tmp-)+(n-tmp)*(n-tmp-)+tmp*(n-tmp)-m;
}
int cas;
int main(){
scanf("%d",&cas);
int tt=;
while(cas--){
scanf("%d%d",&n,&m);
printf("Case %d: %I64d\n",tt++,solve());
}
return ;
}

Strongly connected的更多相关文章

  1. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  2. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  3. cf475B Strongly Connected City

    B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  4. Strongly connected(hdu4635(强连通分量))

    /* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...

  5. 【CF913F】Strongly Connected Tournament 概率神题

    [CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...

  6. HDU 4635 Strongly connected (Tarjan+一点数学分析)

    Strongly connected Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  7. 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP

    [题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...

  8. HDU4625:Strongly connected(思维+强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】

    Strongly connected Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. Lua的安装

      Lua 是一个扩展式程序设计语言,它被设计成支持通用的过程式编程,并有相关数据描述的设施. Lua 也能对面向对象编程,函数式编程,数据驱动式编程提供很好的支持.它可以作为一个强大.轻量的脚本语言 ...

  2. 8 Pratical Examples of Linux “Touch” Command--reference

    In Linux every single file is associated with timestamps, and every file stores the information of l ...

  3. android开发之Toast的多种应用

    Toast最基本的功能就是弹出一个弱提示,这个很简单我就不说了,说说Toast一些其他的作用. 来公司的时候,公司产品的1.0版本已经发布出去了,但是1.0是一个必须联网才能使用的产品,在2.0中想让 ...

  4. 使用passenger在Centos7部署Puma+Nginx+Ruby on Rails

    安装ruby环境 RVM(ruby版本管理工具)安装 gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A170311 ...

  5. MyBatis返回主键,MyBatis Insert操作返回主键

    MyBatis返回主键,MyBatis Insert操作返回主键 >>>>>>>>>>>>>>>>> ...

  6. C++专题 - 面向对象总结

    1.         什么是类?什么是对象?对象与类的关系是什么? 答:类就是相同的数据和相同的一组对象的集合,即类是对具有相同数据结构和相同操作的一类对象的描述: 对象是描述其属性的数据以及对这些数 ...

  7. 使用Gulp构建本地开发Web服务器

    前端模拟ajax,就需要配置web服务器(apache,iis,nginx),有点麻烦 代码有一点点修改,就需要F5刷新页面很麻烦 Gulp + Gulp-connect + watch + live ...

  8. OJ的文件流操作

    我们刷题的时候除了编码外,测试也是非常重要的,当测试样例比较小的时候,我们完全可以手打,但是当测试样例比较大时候,我们就抓狂了~ 相信不少人都知道利用文件流,但是应该还有新手跟我一样,一遍又一遍地输入 ...

  9. iframe中的各种跳转方法(转)

    一.背景A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,在D中跳转页面的写法区别如下. 二.JS跳转window.location.href.locatio ...

  10. canvas之----浮动小球

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...