poj1236/luogu2746 Network of Schools (tarjan)
tarjan缩点后,第一问答案显然是入度为零的点得个数
第二问:考虑到 没有入度或出度为0的点 的图强连通,
所以答案就是max{入度为零的个数,出度为零的个数}
(把出度为零的连到入度为零的点,然后剩下为零的随便连一连就可以)
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
using namespace std;
const int maxn=,maxm=; LL rd(){
LL x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} int N;
int eg[maxm][],egh[maxn],ect,bel[maxn];
int dfn[maxn],low[maxn],stk[maxn],tot,pct,sct;
int tin,tout;
bool instk[maxn],in[maxn],out[maxn]; void tarjan(int x){
dfn[x]=low[x]=++tot;stk[++sct]=x;instk[x]=;
for(int i=egh[x];i!=-;i=eg[i][]){
int j=eg[i][];
if(instk[j]) low[x]=min(low[x],dfn[j]);
else if(!dfn[j]){
tarjan(j);low[x]=min(low[x],low[j]);
}
}
if(dfn[x]==low[x]){
pct++;
while(sct){
instk[stk[sct]]=;
bel[stk[sct]]=pct;
if(stk[sct--]==x) break;
}
}
} inline void adeg(int a,int b){
eg[ect][]=b;eg[ect][]=egh[a];egh[a]=ect++;
} int main(){
int i,j,k;
N=rd();memset(egh,-,sizeof(egh));
for(i=;i<=N;i++){
while(j=rd()) adeg(i,j);
}
for(i=;i<=N;i++){
if(!dfn[i]) tarjan(i);
}
for(i=;i<=N;i++){
for(j=egh[i];j!=-;j=eg[j][]){
k=eg[j][];
int ii=bel[i],kk=bel[k];
if(ii!=kk){
in[kk]=;out[ii]=;
}
}
}for(i=;i<=pct;i++){
if(!in[i]) tin++;
if(!out[i]) tout++;
}
if(pct>)printf("%d\n%d",tin,max(tin,tout));
else printf("1\n0"); }
poj1236/luogu2746 Network of Schools (tarjan)的更多相关文章
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- POJ1236:Network of Schools (思维+Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24880 Accepted: 99 ...
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ 1236 Network of Schools Tarjan缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22729 Accepted: 89 ...
- P2746 [USACO5.3]校园网Network of Schools(Tarjan)
P2746 [USACO5.3]校园网Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- 【tarjan+缩点】POJ1236[IOI1996]-Network of Schools
[题意] 见:http://blog.csdn.net/ascii991/article/details/7466278 [思路] 缩点+tarjan,思路也可以到上面的博客去看.(吐槽:这道题其实我 ...
- 【poj1236】 Network of Schools
http://poj.org/problem?id=1236 (题目链接) 题意 给定一个有向图,求:1.至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点:2.至少要加多少条边,才能使得从任 ...
- POJ1236:Network of Schools——题解
http://poj.org/problem?id=1236 首先还是缩点,然后入度为0的点的个数就是你要投文件个数. 然后我们对于入度和出度为0的点的个数取最大值即为答案. (简单证明:入度和出度为 ...
随机推荐
- .NETCore_初探
1. Core默认的服务注册 Statup文件(rogram类型中创建 WebHost时使用的)中有俩个方法: Configure和ConfigureServices(将服务放置到容器里面) publ ...
- restfull环境搭建-helloword
原文地址:http://blog.csdn.net/u013158799/article/details/39758341 1. REST和RESTful Web Services的简要说明 REST ...
- C_数据结构_循环实现求阶乘
# include <stdio.h> int main(void) { int val; printf("请输入一个数字:"); printf("val = ...
- jdk自带的jvisualvm-监控远程linux
简介 jdk有好多自带的工具比如jconsole.jvisualvm.jstatd等 Windows的路径:%JAVA_HOME/bin/目录下,配置好环境变量直接用cmd执行jvisualvm命令即 ...
- HDOJ2010_水仙花数
一道水题.一直出现Output Limit Exceeded的原因是在while循环中没有终止条件的时候会自动判断并报错,写的时候忘记加!=EOF结束标识了. HDOJ2010_水仙花数 #inclu ...
- 注解Annotation
@java.lang.annotation.Target(value={java.lang.annotation.ElementType.TYPE}) @java.lang.annotation.Re ...
- Java控制台常用命令
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html javaw是java的带console版本,其他一致. h ...
- JQuery插件:图片上传本地预览插件,改进案例一则。
/* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *插 ...
- live-server
live-server的安装与使用 初始化npm:npm init 安装live-server:cnpm install -g live-server 根目录启动live-server:live-se ...
- HMM模型学习笔记(维特比算法)
维特比算法(Viterbi) 维特比算法 编辑 维特比算法是一种动态规划算法用于寻找最有可能产生观测事件序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔可夫模型中.术语“维特比 ...