http://poj.org/problem?id=1236

题意:

给出一个图,至少要选多少个点才能遍历全图和至少需要添加多少边使得整个图是强连通。

思路:

强连通计算连通分量后缩点,计算入度为0的点和出度为0的点。

第一个答案就是出度为0的点,第二个就是max(出度,入度)。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std; const int maxn=+; int n; vector<int> G[maxn];
int in[maxn],out[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack<int> S; void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
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("D:\\input.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) G[i].clear();
for(int u=;u<=n;u++)
{
while(true)
{
int x;
scanf("%d",&x);
if(x==) break;
G[u].push_back(x);
}
}
find_scc(); for(int i=;i<=scc_cnt;i++) in[i]=out[i]=;
for(int u=;u<=n;u++)
{
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(sccno[u]!=sccno[v]) in[sccno[v]]=out[sccno[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==) a=,ans=;
printf("%d\n%d\n",a,ans);
}
return ;
}

POJ 1236 Network of School的更多相关文章

  1. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  2. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  3. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  4. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  5. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  7. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  8. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  9. POJ 1236 Network of Schools - 缩点

    POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好 ...

  10. poj 1236 Network of Schools(连通图)

    题目链接:http://poj.org/problem?id=1236 题目大意:有一些学校,学校之间可以进行收发邮件,给出学校的相互关系,问:1.至少 要向这些学校发送多少份才能使所有的学校都能获得 ...

随机推荐

  1. Period II---fzu1901(Next数组)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 给你一个字符串 s 求出所有满足s[i] == s[i+p] ( 0 < i+p < len ...

  2. golang 系统包自动填写插件

    Make sure $GOPATH/bin is in your $PATH (Windows: %GOPATH%\bin goes in your %PATH%). [保证你的golang环境正常] ...

  3. 源码包安装(Python mysql redis)

    一  源码包安装 (1)python3.6源码包安装 ./configure ------> 定制功能 make make install mysql 源码包 cmake make make i ...

  4. python包管理一防丢失

    pip3 freeze >list.txt      导出当前环境安装的所有包(list是当前项目录下的文件,可以自己命名)pip3 install -r list.txt     安装文件中所 ...

  5. 《Python核心编程》第五章:数字

    PS:[笔记+代码+图片]在GitHub上持续更新,欢迎star:https://github.com/gdouchufu/Core-Python-Programming 本章大纲 介绍Python支 ...

  6. 如何通过 ClickBank 等类似虚拟平台进行在线销售并获得收益

    CLICKBANK在国内被广大赚友简称为CB,和淘金小站之前介绍过的PayDotCom一样,都是国外非常著名的CPS广告联盟.CLICKBANK成立于1998年,是全球四个最大的虚拟商品零售平台之一( ...

  7. 简单的SQL注入学习

    引贴: http://blog.163.com/lucia_gagaga/blog/static/26476801920168184648754/ 首先需要编写一个php页面,讲php页面放入/opt ...

  8. 从原型链看DOM--Element类型

    Element类型用于表现XML或HTML元素,提供对元素标签名,子节点及特性的访问.原型链的继承关系为 某节点元素.__proto__->(HTML某元素Element.prototype)- ...

  9. kettle添加hadoop cluster时报错Caused by: java.lang.IllegalArgumentException: Does not contain a valid host:port authority: hadoop:password@node56:9000

    完整报错是: Caused by: java.lang.IllegalArgumentException: Does not contain a valid host:port authority: ...

  10. struct初始化

    C语言中struct初始化 • 普通结构体的初始化 假设我们有如下的一段代码,其中已有Student结构体,要求实例化一个Student对象并将其初始化. #include <stdio.h&g ...