POJ 1236 Network of School
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的更多相关文章
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- POJ 1236——Network of Schools——————【加边形成强连通图】
Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- POJ 1236 Network of Schools - 缩点
POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好 ...
- poj 1236 Network of Schools(连通图)
题目链接:http://poj.org/problem?id=1236 题目大意:有一些学校,学校之间可以进行收发邮件,给出学校的相互关系,问:1.至少 要向这些学校发送多少份才能使所有的学校都能获得 ...
随机推荐
- centos7修改hostname和hosts
1.修改/etc/hostname vi /etc/hostname 打开之后的内容是: localhost.localdomain 把它修改成想要的名字就可以,比如:master 保存退出 2.修改 ...
- linux内核获取当前进程路径分析
一个简单的问题,·linux下获取当前进程.我们都知道在内核中获取当前进程可以利用current宏 #define get_current() (current_thread_info()->t ...
- Django资源汇总(转)
Django 我和Django¶ 我使用python的很大一部分原因就是django.虽然在以前也用过python,不过始终没有什么特别的感觉.然后接触到了django.可以说django非常对我的胃 ...
- PHP 基础篇 - PHP 正则官方文档汇总
一.PCRE 正则语法 下面是 PHP 的 PCRE 正则语法(模式语法)相关文档,详情请查阅相关链接: 简介 分隔符 元字符 转义序列(反斜线) Unicode字符属性 锚 句点 字符类(方括号) ...
- 把typora改为微软雅黑+Consolas
前言 typora是一款非常方便的书写markdown文本的编辑器.官网:https://www.typora.io/ 对于字体强迫症患者来说,不把字体改成微软雅黑+Consolas,那是相当难受.本 ...
- vim设置tab键默认为4个空格
有两种方法 1.vim /etc/vimrc set ts=4 set sw=4 2.vim /etc/vimrc set ts=4 set expandtab set autoindent 推荐使用 ...
- Linux系统——引导过程与服务控制
一.Linux开机启动原理(十步) (1)开机自检BIOS 开机检测,主板检测 (2)MBR引导 硬盘512字节 (3)GRUB菜单 操作系统菜单 (4)加载内核(kernel) 启动操作系统核心,根 ...
- vue-cli中的.babelrc文件介绍
转载自:http://www.cnblogs.com/ye-hcj/p/7071850.html { // 此项指明,转码的规则 "presets": [ //个人认为多此一举 [ ...
- Java中什么时候使用extends,什么时候使用implements?
1.Extends 是实现单继承一个类的关键字,通过使用extends来显示的指明当前类继承的父类,只要那个类不是声明final或者那个类定义为abstract的就能继承.基本声明格式是 [修饰符] ...
- java中的重量级与轻量级概念
首先轻量级与重量级是一个相对的概念,主要是对应用框架使用方便性和所提供服务特性等方面做比较的. 比方说EJB就是一个重量级的框架,因为它对所编写的代码有限制,同时它也提供分布式等复杂的功能. 相比之下 ...