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的点的个数取最大值即为答案. (简单证明:入度和出度为 ...
随机推荐
- [Python]Python 函数调用小例子
函数定义: In [78]: def printme(str): ....: print str ....: return ....: 调用: In [79]: printme('This is Ji ...
- [Codeforces1137D]Cooperative Game
[Codeforces1137D]Cooperative Game Tags:题解 题意 这是一道交互题. 给你一张下面这样的地图,由一条长为\(t\)的有向链和一个长为\(c\)的环构成. 现在你有 ...
- 校内模拟赛 虫洞(by NiroBC)
题意: n个点m条边的有向图,每一天每条边存在的概率都是p,在最优策略下,询问从1到n的期望天数. 分析: dijkstra. 每次一定会优先选dp最小的后继走,如果这条边不存在,选次小的,以此类推. ...
- SpringCloud Eureka参数配置项详解(转)
Eureka涉及到的参数配置项数量众多,它的很多功能都是通过参数配置来实现的,了解这些参数的含义有助于我们更好的应用Eureka的各种功能,下面对Eureka的配置项做具体介绍,供大家参考. Eure ...
- 使用Hexo+Github搭建属于自己的博客(进阶)
主题的配置:这里以NexT主题作为题材 1.安装NexT,在其文件夹中鼠标右键,点击Git Base Here.输入命令:git clone https://github.com/iissnan/he ...
- 关于树莓派HDMI转VGA线接显示器黑屏
经过数种折腾,找到了解决方法,在SD卡内有个config.txt文件,在其中找到“#hdmi_safe=1”,把#消除掉,变更后成为 # uncomment if you get no picture ...
- LVM : 扩展文件系统的容量
如果发现文件系统的容量不足了,可以通过 LVM 轻松的进行扩展(当然也可以进行缩减操作).本文将紧接前文中的 demo 详细的介绍扩展文件系统的操作过程.说明:本文的演示环境为 ubuntu 16.0 ...
- this.$http.post ||this.$http.put||vue 获取url参
getClasslist() { this.$http.get('/xxxxx/childlist', { params: { ServiceUnitId: localStorage.getItem( ...
- hdu 1263 水果 结构的排序+sort自定义排序
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- html转js字符串拼接
https://www.bejson.com/convert/html_js/ html转js字符串拼接