题意:

  给一个有向图,问添加几条边可以使其强连通。

思路:

  tarjan算法求强连通分量,然后缩点求各个强连通分量的出入度,答案是max(入度为0的缩点个数,出度为0的缩点个数)。

 #include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
using namespace std;
const int N=+;
const int INF=0x7f7f7f7f; vector<int> vect[N];
stack<int> stac;
bool chu[N], ru[N];
int scc_no[N], lowlink[N], dfn[N];
int dfn_clock, scc_cnt;
int n, m; void DFS(int x)
{
stac.push(x);
dfn[x]=lowlink[x]=++dfn_clock;
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(!dfn[t])
{
DFS(t);
lowlink[x]=min(lowlink[x],lowlink[t]);
}
else if(!scc_no[t]) //如果t不属于任何一个强连通分量
lowlink[x]=min(lowlink[x],dfn[t]);
}
if(lowlink[x]==dfn[x])
{
scc_cnt++;
while(true)
{
int t=stac.top();
stac.pop();
scc_no[t]=scc_cnt;
if(t==x) break;
}
}
} int cal()
{
memset(dfn,,sizeof(dfn));
memset(lowlink,,sizeof(lowlink));
memset(scc_no,,sizeof(scc_no)); dfn_clock=scc_cnt=;
for(int i=; i<=n; i++) if(!dfn[i]) DFS(i); //深搜
if(scc_cnt==) return -; memset(chu,,sizeof(chu));
memset(ru,,sizeof(ru));
for(int i=; i<=n; i++)
for(int j=; j<vect[i].size(); j++) //统计出入度
if(scc_no[i]!=scc_no[vect[i][j]]) chu[scc_no[i]]=ru[scc_no[vect[i][j]]]=true;//这里麻烦了点,小心点出错 int c=, r=;
for(int i=; i<=scc_cnt; i++)
{
if(!chu[i]) c++;
if(!ru[i]) r++;
}
return max(c,r)-;
} int main()
{
freopen("input.txt", "r", stdin);
int a, b, t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++) vect[i].clear();
for(int i=; i<m; i++)
{
scanf("%d%d", &a, &b);
vect[a].push_back(b);
}
printf("%d\n",cal());
}
return ;
}

AC代码

UVALive Proving Equivalences (强连通分量,常规)的更多相关文章

  1. 训练指南 UVALive - 4287 (强连通分量+缩点)

    layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...

  2. Proving Equivalences UVALive - 4287(强连通分量 水题)

    就是统计入度为0 的点 和 出度为0 的点  输出 大的那一个,, 若图中只有一个强连通分量 则输出0即可 和https://www.cnblogs.com/WTSRUVF/p/9301096.htm ...

  3. HDU2767Proving Equivalences[强连通分量 缩点]

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. hdu2767 Proving Equivalences --- 强连通

    给一个图,问至少加入�多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入�最少的边连成环,就是把图上入度为0和出度为0的点连 ...

  5. hdu - 2667 Proving Equivalences(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=2767 求至少添加多少条边才能变成强连通分量.统计入度为0的点和出度为0的点,取最大值即可. #include & ...

  6. UvaLive 4287 Proving Equivalences 强连通缩点

    原题链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  8. HDU 2767:Proving Equivalences(强连通)

    题意: 一个有向图,问最少加几条边,能让它强连通 方法: 1:tarjan 缩点 2:采用如下构造法: 缩点后的图找到所有头结点和尾结点,那么,可以这么构造:把所有的尾结点连一条边到头结点,就必然可以 ...

  9. HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

    Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...

随机推荐

  1. Hello World 的makefile模板及其分析

    makefile模板: ifeq ($(KERNELRELEASE),) //判断KERNELRELEASE是否为空,只有执行make的当前目录为内核源代码目录时,该变量才不为空. KERNELDIR ...

  2. Python遍历文件夹枚举所有文件类型

    >>> import os >>> def enumfiles(path, dest): files = os.listdir(path) for f in fil ...

  3. Ubuntu 14.04的SWAP 为0

    [@@@@@@]# free total used free shared buffers cached Mem: 1024976 248992 775984 0 16820 73128 -/+ bu ...

  4. MongoDB 日期 插入时少8小时

    存储在mongodb中的时间是标准时间UTC +0:00  而咱们中国的失去是+8.00 . C#的驱动支持一个特性,将实体的时间属性上添加上这个特性并指时区就可以了.例如:[BsonDateTime ...

  5. iOS 通览(二)

    一.关键词 extern:C语言的函数外部声明. 如果你要在一个.c或者.m中使用另外一个.c文件的函数的话,需要在文件中写入目标函数的外部引用的声明. 二.自定义View 自定义View添加控件对象 ...

  6. (转载)Cocos2dx-OpenGL ES2.0教程:使用VBO索引(4)

    在上一篇文章中,我们介绍了uniform和模型-视图-投影变换,相信大家对于OpenGL ES 2.0应该有一点感觉了.在这篇文章中,我们不再画三角形了,改为画四边形.下篇教程,我们就可以画立方体了, ...

  7. appStore上传苹果应用程序软件发布流程(之前都是同事发,复制一份备用)

    首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Portal,然后点击DisTribution看到的是下图中那样, ...

  8. 进阶:使用 EntityManager

    JPA中要对数据库进行操作前,必须先取得EntityManager实例,这有点类似JDBC在对数据库操作之前,必须先取得Connection实例,EntityManager是JPA操作的基础,它不是设 ...

  9. 1030-ACM程序设计之马拉松竞赛

    描述 校ACM协会近四个月举行了为期100天ACM程序设计之马拉松竞赛,竞赛题总数为1000,同学们反响热烈,先后有许多ACM程序设计竞赛爱好者开始先后编号,成功解答的题目数为选手的成绩. 今天进行成 ...

  10. HDU1796+容斥原理

    给定n和m个数,询问在小于n的数中 有多少个能整除m中的某个数.. 容斥原理. PS:注意64位整数! /* 容斥原理 */ #include<stdio.h> #include<s ...