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数据更新或潜在发 ...
随机推荐
- struts1——静态ActionForm与动态ActionForm
在struts1中,我们能够使用ActionForm来获取从client端提交上来的数据.并通过action配置中的name属性.将某个ActionForm配置到某次请求应答的Action中.作为本次 ...
- 什么是鸭子类型(duck typing)
"当看到一仅仅鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这仅仅鸟就能够被称为鸭子." 我们并不关心对象是什么类型,究竟是不是鸭子,仅仅关心行为. 比方在python中.有 ...
- Hadoop Web项目--Friend Find系统
项目使用软件:Myeclipse10.0,JDK1.7,Hadoop2.6,MySQL5.6.EasyUI1.3.6.jQuery2.0,Spring4.1.3. Hibernate4.3.1,str ...
- POJ - 3280Cheapest Palindrome-经典区间DP
POJ - 3280 Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & ...
- consul 集群安装
上图是官网提供的一个事例系统图,图中的Server是consul服务端高可用集群,Client是consul客户端.consul客户端不保存数据,客户端将接收到的请求转发给响应的Server端.Ser ...
- C - Elephant(贪心)
Problem description An elephant decided to visit his friend. It turned out that the elephant's house ...
- [转]Microsoft Solutions Framework (MSF) Overview
本文转自:http://msdn.microsoft.com/zh-CN/library/jj161047(v=vs.120).aspx [This documentation is for prev ...
- window 10 多版本激活工具
window 10 通用版激活工具 云盘地址:https://pan.baidu.com/s/1bo3L4Kn 激活工具网站:http://www.tudoupe.com/win10/win10jih ...
- Vue报错笔记
1.错误信息:[Vue warn]: Property or method "object" is not defined on the instance but referenc ...
- 用户 'NT Service\MSSQLServerOLAPService' 登录失败
初学SSAS,部署微软官方示例项目AdventureWorksDW2012Multidimensional时出现错误:用户 'NT Service\MSSQLServerOLAPService' 登录 ...