https://vjudge.net/problem/UVALive-4287

题意:

给出n个结点m条边的有向图,要求加尽量少的边,使得新图强连通。

思路:
强连通分量缩点,然后统计缩点后的图的每个结点是否还需要出度和入度。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
int tot;
int dfs_clock;
int scc_cnt;
int in[maxn];
int out[maxn];
int pre[maxn];
int head[maxn];
int sccno[maxn];
int lowlink[maxn]; stack<int> S; struct node
{
int v;
int next;
}e[+]; void addEdge(int u, int v)
{
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
} void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(!pre[v])
{
dfs(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u]=min(lowlink[u],pre[v]);
}
} if(lowlink[u]==pre[u])
{
scc_cnt++;
for(;;)
{
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u) break;
}
}
} void find_scc()
{
dfs_clock=scc_cnt=;
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
for(int i=;i<=n;i++)
{
if(!pre[i]) dfs(i);
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
tot=;
memset(head,-,sizeof(head));
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
}
find_scc();
for(int i=;i<=scc_cnt;i++) in[i]=out[i]=;
for(int u=;u<=n;u++)
{
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(sccno[u]!=sccno[v]) in[sccno[v]]=out[sccno[u]]=; //u,v是桥,所以v不需要入度,u不需要出度
}
} int a=,b=;
for(int i=;i<=scc_cnt;i++)
{
if(in[i]) a++;
if(out[i]) b++;
}
int ans=max(a,b);
if(scc_cnt==) ans=;
printf("%d\n",ans);
}
return ;
}

LA 4287 等价性证明(强连通分量缩点)的更多相关文章

  1. LA 4287 等价性证明

    题目链接:http://vjudge.net/contest/141990#overview 题意是告诉你有n个命题,m条递推关系,表示某个命题可以推出另外一个命题. 现在问你至少在增加多少个递推关系 ...

  2. UVALIVE 4287 Proving Equivalences (强连通分量+缩点)

    题意:给定一个图,问至少加入多少条边能够使这个图强连通. 思路:首先求出这个图的强连通分量.然后把每个强连通分量缩成一个点.那么这个图变成了一个DAG,求出全部点的入度和出度,由于强连通图中每个节点的 ...

  3. LA 4287 有相图的强连通分量

    大白书P322 , 一个有向图在添加至少的边使得整个图变成强连通图, 是计算整个图有a个点没有 入度, b 个点没有出度, 答案为 max(a,b) ; 至今不知所云.(求教) #include &l ...

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

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

  5. HDU 3639 Hawk-and-Chicken(强连通分量+缩点)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013480600/article/details/32140501 HDU 3639 Hawk-a ...

  6. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

  7. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  8. POJ1236Network of Schools(强连通分量 + 缩点)

    题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...

  9. HD2767Proving Equivalences(有向图强连通分量+缩点)

    题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...

随机推荐

  1. 006-spring cloud gateway-GatewayAutoConfiguration核心配置-GatewayProperties初始化加载、Route初始化加载

    一.GatewayProperties 1.1.在GatewayAutoConfiguration中加载 在Spring-Cloud-Gateway初始化时,同时GatewayAutoConfigur ...

  2. vue中两种路由跳转拼接参数

    this.$router.push({name:"Home",query:{id:1,name:2}}) // 取到路由带过来的参数 let routerParams = this ...

  3. 万恶之源 - Python基础

    Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程 ...

  4. [vue]vue-book

    我们打算要做这几个模块 首页 列表 收藏 添加 home.vue --> list.vue -->app.vue --> main.js 安装环境 npm i vue-cli -g ...

  5. 畅通工程续 (SPFA模板Floy模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 SPFA #include <iostream> #include <stdio.h&g ...

  6. Andrew Ng-ML-第十六章-异常检测

    1.问题动机 图1.飞机发动机检测例子 对飞机引擎的例子,如果选取了两个特征x1热量产生度,x2震动强度.并得到如下的图,如果有一个新的引擎来检测其是否正常,x_test,那么此时如果点落在和其他点正 ...

  7. racle修改字段类型时报"要更改的列必须为空"处理方法

    执行以下语句报"要修改数据类型,则要更改的列必须为空"      alter table 表名 modify (目标字段 varchar2(100)); 解决步骤: 第一步,在表中 ...

  8. sql中字符串如何比大小

    从字符串的第一个字符开始比较ASSCII码值,如果相等则看下一个,以此类推. 数字的ASCII码<大写字母的ASCII码<小写字母的ASCII码. ASCII码

  9. Centos下添加PHP对MSSQL的支持

    Leave a reply 其实很少会有连接SQL Server的机会,不过我们公司刚好有个应用需要使用的SQL Server的数据库,所以也知道给LNMP安装MSSQL的扩展. 搜索网上的相关文章一 ...

  10. python之路 socket、socket server

    一.socket socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思.通常也 称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可 ...