nyoj 120 建边构强连通
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 200 struct node
{
int u,v,next;
} bian[N*N];
int n,dfn[N],low[N],stac[N],yong,head[N],visit[N],ans,top,inde,suo[N],indegree[N],outdegree[N];
void init()
{
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(head,-1,sizeof(head));
memset(stac,0,sizeof(stac));
memset(visit,0,sizeof(visit));
memset(suo,0,sizeof(suo));
memset(indegree,0,sizeof(indegree));
memset(outdegree,0,sizeof(outdegree));
ans=0;
yong=0;
top=0;
inde=0;
}
void addedge(int u,int v)
{
bian[yong].u=u;
bian[yong].v=v;
bian[yong].next=head[u];
head[u]=yong++;
}
int Max(int a,int b)
{
return a>b?a:b;
}
int Min(int a,int b)
{
return a>b?b:a;
}
void tarjan(int u)
{
dfn[u]=low[u]=++inde;
stac[++top]=u;
visit[u]=1;
int i;
for(i=head[u]; i!=-1; i=bian[i].next)
{
if(!dfn[bian[i].v])
{
tarjan(bian[i].v);
low[u]=Min(low[u],low[bian[i].v]);
}
else if(visit[bian[i].v]==1)
low[u]=Min(low[u],dfn[bian[i].v]); }
if(dfn[u]==low[u])
{
ans++;
int t;
do
{
t=stac[top--];
visit[t]=2;
suo[t]=ans;
}
while(t!=u);
}
}
int main()
{
int t,n,m,i,fin,fout;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(i=1; i<=n; i++)
{
while(scanf("%d",&m),m)
addedge(i,m);
}
for(i=1; i<=n; i++)
if(visit[i]!=2)
tarjan(i);
if(ans==1) {//important
printf("0\n");
continue;
}
for(i=0;i<yong;i++)
if(suo[bian[i].u]!=suo[bian[i].v]) {
indegree[suo[bian[i].u]]++;
outdegree[suo[bian[i].v]]++;
}
fin=0;
fout=0;
for(i=1; i<=ans; i++)
{
if(indegree[i]==0)
fin++;
if(outdegree[i]==0)
fout++;
}
printf("%d\n",Max(fin,fout));
} return 0;
}
nyoj 120 建边构强连通的更多相关文章
- nyoj 120 校园网络
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=120 思路:先将原图强连通缩点为新图,统计新图中入度,出度为0的点的个数,两者取最大值即为 ...
- hdu 3836 强连通+缩点:加边构强连通
#include<stdio.h>//求出其所有的强连通分量缩点,选出出度和入度最大的那个就是要求的边 #include<string.h> #include<stdli ...
- nyoj 120 校园网络(求添加多少条边使整个图强连通)
校园网络 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 南阳理工学院共有M个系,分别编号1~M,其中各个系之间达成有一定的协议,如果某系有新软件可用时,该系将允许一 ...
- HDU5934 强连通分量
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5934 根据距离关系建边 对于强连通分量来说,只需引爆话费最小的炸弹即可引爆整个强连通分量 将所有的强连通分 ...
- hdu3861 The King’s Problem 强连通缩点+DAG最小路径覆盖
对多校赛的题目,我深感无力.题目看不懂,英语是能懂的,题目具体的要求以及需要怎么做没有头绪.样例怎么来的都不明白.好吧,看题解吧. http://www.cnblogs.com/kane0526/ar ...
- BZOJ 1179 Atm 题解
BZOJ 1179 Atm 题解 SPFA Algorithm Tarjan Algorithm Description Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来 ...
- 【Tarjan】+【SPFA】APIO2009 Atm
一.算法介绍 tarjan——求解有向图强连通分量.这个算法在本人的一篇blog中有介绍,这里就不赘述了.贴上介绍tarjan的的blog链接:http://www.cnblogs.com/Maki- ...
- hdu 4685 简单匹配+Tarjan算法
思路:首先看到这题以为能用poj1904的模版直接A掉,WA了几次,然后又TLE了几次.还是想到了正解. 一开始我想的大致方向已经是对的了.先是由王子向每个喜欢的公主建边,再求一次最大匹配,找出匹配后 ...
- 教你MySQL Binlog实用攻略
本文由云+社区发表 1.概述 binlog是Mysql sever层维护的一种二进制日志,与innodb引擎中的redo/undo log是完全不同的日志:其主要是用来记录对mysql数据更新或潜在发 ...
随机推荐
- Linux开发环境搭建与使用——Linux必备软件之Samba
假如我们是在ubuntu环境上做对应的开发.有的时候,我们须要把我们写的程序共享给别人,或者,自己拷贝出来备份一份.我们习惯用U盘拷贝,假设须要频繁拷贝的话,这样会不太方便.这里给大家介绍一种更好的方 ...
- GammaRay 是一个允许你查看 Qt 应用程序甚至在某种程度上修改它的独特应用,可谓是 Debugger 的良好补充
GammaRay is a tool to poke around in a Qt-application and also to manipulate the application to some ...
- 消除ADB错误“more than one device and emulator”的方法
当我连着手机充电的时候,启动模拟器调试,运行ADB指令时,报错. C:\Users\gaojs>adb shell error: more than one device and emulato ...
- h5-10 canvas 简易祖玛
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hdoj--1281--棋盘游戏(最小点覆盖+枚举)
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 表格td内容过多时,td显示省略号,鼠标移入显示全部内容。
转自:https://blog.csdn.net/weixin_42193908/article/details/80405014 两种方式显示: 1.title方式显示: <!DOCTYPE ...
- 第20章 Redis配置
20.1 Redis基础配置文件 20.2 Redis备份(持久化) save 900 1 save 300 10 save 60 10000 # By default Redis will stop ...
- linux系统下块设备驱动程序
顾名思义,块设备驱动程序就是支持以块的方式进行读写的设备.块设备和字符设备最大的区别在于读写数据的基本单元不同.块设备读写数据的基本单元为块,例 如磁盘通常为一个sector,而字符设备的基本单元为字 ...
- SNMP简单概述
一.SNMP简单概述 1.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议& ...
- king枚举帮助类
可以方便的实现枚举 枚举 public enum DeptType { [Description("科室1")] Professional = , [Description(&qu ...