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

首先还是缩点,然后入度为0的点的个数就是你要投文件个数。

然后我们对于入度和出度为0的点的个数取最大值即为答案。

(简单证明:入度和出度为0的点可以互相连一下成一个环,但是无法匹配的时候就只能随便连了)

#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
inline int read(){
int x=,w=;char ch=;
while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*w;
}
const int maxn=;
int cnt=,head[maxn];
struct node{
int w;
int to;
int nxt;
}edge[];
void add(int u,int v){
cnt++;
edge[cnt].to=v;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
int dfn[maxn];
int low[maxn];
bool instack[maxn];
int dis[maxn];
int indeg[maxn];
int outdeg[maxn];
int to[maxn];
int t=,l=;
stack<int>q;
void tarjan(int u){
int v;
t++;
dfn[u]=t;
low[u]=t;
q.push(u);
instack[u]=;
for(int i=head[u];i!=;i=edge[i].nxt){
v=edge[i].to;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}else if(instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
l++;
do{
v=q.top();
q.pop();
instack[v]=;
to[v]=l;
}while(v!=u);
}
return;
}
int main(){
int n=read();
for(int i=;i<=n;i++){
int a=read();
while(a){
add(i,a);
a=read();
}
}
for(int i=;i<=n;i++){
if(!dfn[i]){
tarjan(i);
}
}
for(int i=;i<=n;i++){
for(int j=head[i];j;j=edge[j].nxt){
int v=to[edge[j].to];
int u=to[i];
if(u!=v){
indeg[v]++;
outdeg[u]++;
}
}
}
int ans=,anss=;
for(int i=;i<=l;i++){
if(!indeg[i]){
ans++;
}
if(!outdeg[i]){
anss++;
}
}
if(l!=)
printf("%d\n%d\n",ans,max(ans,anss));
else
printf("%d\n%d\n",,);
return ;
}

POJ1236:Network of Schools——题解的更多相关文章

  1. P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools

    P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...

  2. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

  3. POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度

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

  4. poj-1236.network of schools(强连通分量 + 图的入度出度)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27121   Accepted: 10 ...

  5. POJ1236 Network of Schools (强连通)(缩点)

                                                                Network of Schools Time Limit: 1000MS   ...

  6. POJ-1236 Network of Schools,人生第一道Tarjan....

    Network of Schools 题意:若干个学校组成一个计算机网络系统,一个学校作为出发端连接着若干个学校,信息可以传送到这些学校.被链接的学校不需要再次与出发端相连,现在问你:A:最少选几个学 ...

  7. POJ1236 Network of Schools (强连通分量,注意边界)

    A number of schools are connected to a computer network. Agreements have been developed among those ...

  8. POJ1236:Network of Schools(tarjan+缩点)?

    题目: http://poj.org/problem?id=1236 [题意] N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1 ...

  9. poj1236 Network of Schools(SCC缩点+结论推导)

    第一问简单不讲. 第二问简化后问题是给一张DAG求最少添加几条边使得DAG变成一个SCC.首先所有中间点(有入度有出度)肯定直接顺着走到无出度点,所以肯定是无出度点连向无入度点. 把无入度点作为点集S ...

随机推荐

  1. 代码混淆防止APP被反编译指南

    本文来自网易云社区 安卓App安全包含很多内容,包括混淆代码.整体Dex加固.拆分 Dex 加固.虚拟机加固等方面.事实上,这些内容也是国内近几年Android App安全保护的一种主要趋势. 混淆代 ...

  2. hdu2899Strange fuction(解方程+二分)

    Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. html div内第二行文字显示不下的时候才用省略号代替 css实现

    有时候文字太多,但为了美观想要在第二行的时候才显示省略号,而不是第一行超出马上就出现省略号 下面是css代码: overflow:hidden;text-overflow: ellipsis;//显示 ...

  4. HTTP基本定义

    一.网络的简单定义: 1.http:是www服务器传输超文本向本地浏览器的传输协议.(应用层) 2.IP:是计算机之间相互识别通信的机制.(网络层) 3.TCP:是应用层通信之间通信.(传输层) IP ...

  5. Git与远程仓库关联以及关联错误解决方法

    假设你github的用户名是  helloworld  ,你在上面创建了一个 名为 hello 的 repository. 一. 与本地仓库进行关联 1.1用原生ssh进行关联,速度快: git re ...

  6. sqlserver错误126解决方法

    是不是很尴尬! 华丽的分割线下便是解决方法: 1.打开sqlserver配置管理器. 2.选择sqlserver网络配置,并禁用VIA协议确定保存. 3.在服务里面启动[SQL Server (SQL ...

  7. Java基础知识:Java实现Map集合二级联动1

    Java实现Map集合二级联动 Map集合可以保存键值映射关系,这非常适合本实例所需要的数据结构,所有省份信息可以保存为Map集合的键,而每个键可以保存对应的城市信息,本实例就是利用Map集合实现了省 ...

  8. P4语法(2) Parser

    这里参考学习了: P4语言规范 P4台湾社群 Parser 关于parser 在P4程序中,有着大量的首部(header)和首部实例,但每次只有部分首部实例会对数据包进行操作,而parser会用于生成 ...

  9. 导弹拦截与Dilworth定理

    这两天被Dilworth.链和反链搞到头昏脑胀,终于有点眉目,现在来总结一下. Dilworth定理说的是:对于一个偏序集,其最少链划分数等于其最长反链的长度. Dilworth定理的对偶定理说的是: ...

  10. 第三章——供机器读取的数据(XML)

    本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...