[poj] 1236 networks of schools
原题
这是一道强连通分量板子题。
显然subtask1 是要输出入度为0的点的个数
而subtask2,我们考虑一下最优一定是把一个出度为零的点连到入度为零的点上,这样我们要输出的就是max(出度为零的个数,入度为零的个数)
另外,如果只有一个强连通分量,那么subtask2答案是0
#include<cstdio>
#include<algorithm>
#include<stack>
#define N 10010
#define M 50010
using namespace std;
int n,m,head[N],dfn[N],low[N],cnt=1,t,sum,bel[N],out[N],num[N],in[N],ans,as;
bool instk[N];
stack <int> stk;
struct hhh
{
int to,next;
}edge[M];
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void add(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void Tarjan(int x)
{
dfn[x]=low[x]=++t;
stk.push(x);
instk[x]=1;
int v;
for (int i=head[x];i;i=edge[i].next)
{
v=edge[i].to;
if (!dfn[v])
{
Tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (instk[v]) low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x])
{
sum++;
do
{
v=stk.top();
stk.pop();
instk[v]=0;
bel[v]=sum;
num[sum]++;
}while(v!=x);
}
}
int main()
{
n=read();
for (int i=1,a;i<=n;i++)
{
a=read();
while(a!=0)
{
add(i,a);
a=read();
}
}
for (int i=1;i<=n;i++)
if (!dfn[i]) Tarjan(i);
for (int i=1;i<=n;i++)
for (int j=head[i],v;j;j=edge[j].next)
{
v=edge[j].to;
if (bel[i]!=bel[v]) in[bel[v]]++,out[bel[i]]++;
}
for (int i=1;i<=sum;i++)
{
if (!in[i]) ans++;
if (!out[i]) as++;
}
printf("%d\n",ans);
if (sum==1) printf("0\n");
else printf("%d",max(ans,as));
return 0;
}
[poj] 1236 networks of schools的更多相关文章
- 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 题意: 有好 ...
随机推荐
- link链接外部样式表
一个完整的link标签: <link href="[css adress]" rel="stylesheet" type="text/css&q ...
- LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal
题目 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9, ...
- 【期望dp 质因数分解】cf1139D. Steps to One
有一种组合方向的考虑有没有dalao肯高抬啊? 题目大意 有一个初始为空的数组$a$,按照以下的流程进行操作: 在$1\cdots m$中等概率选出一个数$x$并添加到$a$的末尾 如果$a$中所有元 ...
- 【Effective C++ 读书笔记】条款03: 尽量使用 const
关键字const多才多艺,变化多端却不高深莫测. const 修饰指针 面对指针, 你可以指出 指针自身.指针所指物.或者两者都不是 const. 如果关键字 const 出现在星号左边,表示被指物是 ...
- 通信服务器哈希Socket查找(Delphi)
在Socket通信服务器的开发中,我们经常会需要Socket与某个结构体指针进行绑定.当连接量很大时,意味着需要个高效的查找方法 Delphi中提供了哈希算法类,以此类为基础,修改出Socket专用M ...
- 解决scp命令pemission denied,please try again的问题
问题描述:输入命令scp a.txt root@192.168.0.105:/tmp(将当前目录下的文件a.txt复制到服务器IP为192.168.0.105的root用户的/tmp/目录下),结果会 ...
- pyhton——logging日志模块的学习
https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模 ...
- 编辑工具_vi
vi/vim平时经常会用到,但是一直没有时间系统的整理下,今天看到了一篇不错的介绍文章.引用下,就当做笔记了,但是不晓得该怎么填引用路径,如有侵权请告知,补上引用路径 01. vi 简介 1.1 学习 ...
- Android 数据库的线程合作
前言:之前琢磨了很多线程相关的东西,一直摸不着头脑,直到学到了数据库,终于发现世界原来如此美好,任何事物都有存在的理由. 1.主线程,我把它比作长江,作为母亲河的长江,想必大家每个人都不会很陌生. 2 ...
- IAR FOR STM8S 错误 An error occurred while retrieving GDI features: gdi-error [40201]解决方法
今早使用IAR调试编译调试一个工程,发现IAR竟然出现如下错误信息 An error occurred ]: Can't access configuration database 在网上查看了一下, ...