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

思路:首先这个图在添加边之后肯定变成了两个强连通分量,现在就看怎么分.然后我们可以注意到,原图进行强连通分量分解之后必然存在一些分量的出度或入度为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

HDU 4635(强连通分量分解相关推荐

  1. poj2186(强连通分量分解)

    题目大概意思为有 N 头牛,有些牛认为有些牛是红人,该关系具有传递性,例如果牛A觉得牛B是红人,牛B认为牛C是红人,则牛A也会认为牛C是红人,求被其他所有牛认为是红人的牛的总数 假设被其他所有牛认为是 ...

  2. 有向图最长路径算法_算法数据结构 | 三个步骤完成强连通分量分解的Kosaraju算法...

    强连通分量分解的Kosaraju算法 今天是算法数据结构专题的第35篇文章,我们来聊聊图论当中的强连通分量分解的Tarjan算法. Kosaraju算法一看这个名字很奇怪就可以猜到它也是一个根据人名起 ...

  3. 强连通分量分解详解 超级详细

    (写的有点小多,慢慢看,会有收获的) (1) 首先我们得了解,什么是强连通? 如果在一个有向图顶点子集内,任取两个点 u 和 v ,都能找到一条路径从 u 到 v ,则称该子集为强连通 (2) 其次我 ...

  4. UVa 12167 HDU 2767 强连通分量 Proving Equivalences

    题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. 1 ...

  5. Strongly connected HDU - 4635(tarjan+强连通分量)

    题意: 给一个简单有向图,让你加最多的边,使他还是一个简单有向图. 题目: Give a simple directed graph with N nodes and M edges. Please ...

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

    题干: Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the ...

  7. poj2186Popular Cows(Kosaraju算法--有向图的强连通分量的分解)

    1 /* 2 题目大意:有N个cows, M个关系 3 a->b 表示 a认为b popular:如果还有b->c, 那么就会有a->c 4 问最终有多少个cows被其他所有cows ...

  8. hdu 5420 Victor and Proposition 线段树建图+强连通分量

    题意: http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=620&pid=1003 题目求有多少对互为充要 ...

  9. HDU 4685 Prince and Princess(二分图+强连通分量)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:给出n个王子和m个公主.每个王子有一些自己喜欢的公主可以匹配.设最大匹配为M.那么对于每个 ...

最新文章

  1. Cissp-【第3章 安全工程】-2021-2-24(322页-376页)
  2. 2015-12-18 学习心得
  3. 页面的数据缓存,包括文字和图片
  4. php mysql 查询数据出现连接重置_php使用mysql和mysqli连接查询数据
  5. PowerShell 7.0 RC 发布
  6. mac上使用终端生成RSA公钥和密钥
  7. python leetcode_八十二、Python | Leetcode贪心算法系列
  8. 服务器来电后自动开机|xfs文进系统备份恢复linux下误删除的文件
  9. 非常实用的面试题,也可以当作学习资料(转载)
  10. LeetCode Two Sum
  11. DirectX修复工具出现0xc000007b错误——分析与解决
  12. 转转首席架构师 孙玄:如何成为一个有情怀的工程师?
  13. JAVA实现FTP上传与下载
  14. Sql Server——Sql Server中进行查询操作时提示“对象名无效”
  15. Android 打开系统文件管理器选择文件
  16. 重度办公用户,哪款平板更合适?
  17. 基于Java技术的Web环境下分布式数据库互操作性的实现
  18. Android模拟器实现windows虚拟串口通信调试
  19. Java学习005 #Java编程进行时01#
  20. NVIDIA JETSON AGX XAVIER DEVELOPER KIT刷机教程(各种踩雷篇)

热门文章

  1. Hi3515的开发板 Hi3515 SDK编译出错 提示缺少libpciv.a的解决办法
  2. 飞凌开发板 cramfs 镜像文件修改
  3. 前端学习(2971):前一天回顾
  4. [html] 你认为table的作用和优缺点是什么呢?
  5. [css] css如何消除字体的锯齿?
  6. 工作242:关于第二个git仓库提交代码
  7. 前端学习(2548):模板解析
  8. 前端学习(2521):设定添加博客和最终效果展示
  9. 前端学习(2061):vue的mvvm
  10. 前端学习(1384):多人管理项目4资源的路径