题意:

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

思路:

  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. 字符设备驱动中cdev与inode、file_operations的关系

    一.cdev与inode 二.cdev与file_operations

  2. zxing-master core编译

    1.下载 Maven: http://maven.apache.org/download.cgi 2.下载完以后,设一下系统变量:MAVE_HOME 3.Path中加入%MAVEN_HOME%\bin ...

  3. android模拟器打开时比较慢,Run As就找不到模拟器

    1.运行中输入cmd,然后回车,调出command窗口 2.用cd,将目录切换到adb所在目录,然后输入adb kill-server ,adb start-server 3.adb devices就 ...

  4. 【BZOJ1500】[NOI2005]维修数列

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...

  5. 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  6. 改变navigationbar 标题颜色

    navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor yel ...

  7. FormBorderStyle.None 时候最大化不遮盖任务栏

    this.FormBorderStyle = FormBorderStyle.None;             this.MaximumSize = new Size(Screen.PrimaryS ...

  8. 结构体 typedef关键字

    1 结构体 #include <iostream> #include <cstring> using namespace std; void printBook( struct ...

  9. Deadline来了,如何按时结题?

  10. js检测浏览器版本代码,兼容ie11

    原文:http://blog.csdn.net/tenkin/article/details/11640165 <script type="text/javascript"& ...