校园网Network of Schools

第一问:Tarjan缩点,搞出每一个连通块,入度为零的连通块是需要必须接受新软件副本的,统计数量即可

第二问:要让整个图构成一个环,显然要将入度为零点和出度为零点都消灭,ans=max(入度为零点的数量,出度为零点的数量)

#include<iostream>
#include<cstring>
#include<cstdio>
#define N 110
int n,Head[N],Enum,stack[N],top,ans1,ans2;
int dfn[N],cnt,low[N],belong[N],num;
bool ins[N],in[N],out[N];
struct NODE{
int to,next;
} e[N*N];
inline void add(int x,int y){
e[++Enum].to=y;
e[Enum].next=Head[x];
Head[x]=Enum;
}
inline int read(){
int x=; char c=getchar();
while(c<''||c>'') c=getchar();
while(''<=c&&c<='') { x=(x<<)+(x<<)+c-''; c=getchar(); }
return x;
}
inline void Tarjan(int u){
dfn[u]=low[u]=++cnt;
ins[u]=; stack[++top]=u;
for(int i=Head[u];i;i=e[i].next){
int v=e[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=std::min(low[u],low[v]);
}
else if(ins[v])
low[u]=std::min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
belong[u]=++num;
while(stack[top]!=u){
int k=stack[top];
belong[k]=num;
ins[k]=;
top--;
}
top--; ins[u]=;
}
}
int main()
{
n=read(); int x;
for(int i=;i<=n;i++){
x=read();
while(x){ add(i,x); x=read(); }
}
for(int i=;i<=n;i++)
if(!dfn[i]) Tarjan(i);
if(num==) { printf("%d\n%d\n",,); return ; }
for(int i=;i<=n;i++)
for(int j=Head[i];j;j=e[j].next)
if(belong[i]!=belong[e[j].to])
out[belong[i]]=in[belong[e[j].to]]=;
for(int i=;i<=num;i++){
if(!in[i]) ans1++;
if(!out[i]) ans2++;
}
ans2=std::max(ans1,ans2);
printf("%d\n%d\n",ans1,ans2);
return ;
}

双倍经验: P2812

【洛谷P2746】[USACO5.3]校园网Network of Schools的更多相关文章

  1. 洛谷 P2746 [USACO5.3]校园网Network of Schools 解题报告

    P2746 [USACO5.3]校园网Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作"接受学校&q ...

  2. 洛谷 P2746 [USACO5.3]校园网Network of Schools (Tarjan,SCC缩点,DAG性质)

    P2746 [USACO5.3]校园网Network of Schools https://www.luogu.org/problem/P2746 题目描述 一些学校连入一个电脑网络.那些学校已订立了 ...

  3. 洛谷P2746 [USACO5.3]校园网Network of Schools

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...

  4. 洛谷 P2746 [USACO5.3]校园网 Network of Schools 题解

    Tarjan 模板题 第一问就是缩点之后看有多少个入度为零的点就好了. 第二问是在缩点后将每个点的入度和出度都求出(只要有入度或出度就置为1),然后比较哪个有值的多,将多的作为答案输出.原因是由题可得 ...

  5. 洛谷 P2746 [USACO5.3]校园网Network of Schools

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...

  6. 洛谷 P2746 [USACO5.3]校园网Network of Schools schlnet Tarjan强连通分量

    schlnet ★★★   输入文件:schlnet.in   输出文件:schlnet.out   简单对比时间限制:1 s   内存限制:128 MB 描述 一些学校连入一个电脑网络.那些学校已订 ...

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

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

  8. P2746 [USACO5.3]校园网Network of Schools(Tarjan)

    P2746 [USACO5.3]校园网Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 ...

  9. 洛谷P2746 USACO5.1 校园网

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...

  10. 【luogu P2746 [USACO5.3]校园网Network of Schools】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2812 注意:判断出入度是否为0的时候枚举只需到颜色的数量. 坑点:当只有一个强连通分量时,不需要再添加新边. ...

随机推荐

  1. (转) sync命令

    sync sync命令 sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的 ...

  2. cloudemanager安装时出现8475 MainThread agent ERROR Heartbeating to 192.168.30.1:7182 failed问题解决方法(图文详解)

    不多说,直接上干货!   问题详情 解决这个问题简单的,是因为有进程占用了.比如 # ps aux | grep super root ? Ss : : /opt/cm-/lib64/cmf/agen ...

  3. jQuery遍历祖先元素:parentsUntil

    Description: Get the ancestors of each element in the current set of matched elements, up to but not ...

  4. elasticsearch 2.4.0执行update的时候发现的一个问题

    请关注inline参数的变化 正确: POST /test/type1/1/_update{ "script" : { "inline": "ctx. ...

  5. vim代码折叠命令

    1. 折叠方式 可用选项 'foldmethod' 来设定折叠方式:set fdm=*****. 有 6 种方法来选定折叠:           manual           手工定义折叠 ind ...

  6. Beautiful Soup库介绍

    开始前需安装Beautiful Soup 和lxml. Beautiful Soup在解析时依赖解析器,下表列出bs4支持的解析器. 解析器 使用方法 Python标准库 BeautifulSoup( ...

  7. 文档碎片DocumentFragment

    文档碎片是什么? 参考标准的描述,DocumentFragment是一个轻量级的文档对象,能够提取部分文档的树或创建一个新的文档片段,换句话说有文档缓存的作用. createDocumentFragm ...

  8. RocketMQ3.2.6安装部署及调用

    RocketMQ3.2.6安装部署及调用 1.RocketMQ部署架构 所有IP都是127.0.0.1,其中NameServer一个,Broker一个,Producer一个,Consumer一个 2. ...

  9. 日期控件html

    日期控件多的是,这里收录的是最简单的一种 <html> <head> <script type="text/javascript"> funct ...

  10. 简单shell实现

    http://blog.csdn.net/lishuhuakai/article/details/11928055 #include <stdio.h> #include <unis ...