usaco 校园网
题解:
显然当一个图上的点是一个环时能满足题目要求
那么我们来考虑怎么形成一个环
很显然的是要先缩点
缩完点就成为了森林,如何让森林成环呢?
考虑一下环上的点的入度出度一定都大于1
而连一条边可以增加某个点的入度和某个点的出度
问题就变成了求入度为0或者出度为0的最大值
代码:
#include <bits/stdc++.h>
#define rint register int
#define IL inline
#define rep(i,h,t) for (int i=h;i<=t;i++)
#define dep(i,t,h) for (int i=t;i>=h;i--)
using namespace std;
const int N=3e5;
struct re{
int a,b,from;
}e[N*];
int head[N],col[N],cd[N],rd[N],l,n;
bool ins[N];
void arr(int x,int y)
{
e[++l].a=head[x];
e[l].b=y;
e[l].from=x;
head[x]=l;
}
stack<int> S;
int cnt,color,dfn[N],low[N];
void tarjan(int x)
{
S.push(x); dfn[x]=low[x]=++cnt; ins[x]=;
for (rint u=head[x];u;u=e[u].a)
{
int v=e[u].b;
if (!dfn[v])
{
tarjan(v);
dfn[x]=min(dfn[x],low[v]);
}
if (ins[v]) dfn[x]=min(dfn[x],dfn[v]);
}
if (low[x]==dfn[x])
{
color++;
while ()
{
int y=S.top(); S.pop(); ins[y]=;
col[y]=color;
if (y==x) break;
}
}
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
ios::sync_with_stdio(false);
cin>>n;
for (int i=;i<=n;i++)
{
while (true)
{
int x;
cin>>x;
if (x) arr(i,x); else break;
}
}
int rdans=,cdans=;
for (int i=;i<=n;i++) if (!dfn[i]) tarjan(i);
if (color==) cout<<<<endl<<;
else{
for (int i=;i<=l;i++)
if (col[e[i].b]!=col[e[i].from])
rd[col[e[i].b]]++,cd[col[e[i].from]]++;
for (int i=;i<=color;i++)
{
if (!rd[i]) rdans++;
if (!cd[i]) cdans++;
}
cout<<rdans<<endl<<max(rdans,cdans);
}
}
#include <bits/stdc++.h>
using namespace std;
int n,x,l;
#define maxn 100000
struct re{
int a,b,from;
}a[maxn];
int color,dfn[maxn],low[maxn],col[maxn];
int rdans,cdans,head[maxn],now,rd[maxn],cd[maxn];
bool ins[maxn];
int arr(int x,int y)
{
a[++l].a=head[x];
a[l].b=y;
a[l].from=x;
head[x]=l;
}
stack <int> s;
void tarjan(int x)
{
dfn[x]=low[x]=++now;
int u=head[x];
s.push(x); ins[x]=;
while (u)
{
int v=a[u].b;
if (!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
if (ins[v]) low[x]=min(low[x],dfn[v]);
u=a[u].a;
}
if (dfn[x]==low[x])
{
color++;
while (true)
{
int y=s.top();
col[y]=color;
s.pop();
ins[y]=;
if (y==x) break;
}
}
}
int main()
{
freopen("noip.in","r",stdin);
freopen("noip.out","w",stdout);
cin>>n;
memset(ins,,sizeof(ins));
for (int i=;i<=n;i++)
{
while (true)
{
cin>>x;
if (x) arr(i,x); else break;
}
}
for (int i=;i<=n;i++) if (!dfn[i]) tarjan(i);
if (color==) cout<<<<endl<<;
else{
for (int i=;i<=l;i++)
if (col[a[i].b]!=col[a[i].from])
rd[col[a[i].b]]++,cd[col[a[i].from]]++;
for (int i=;i<=color;i++)
{
if (!rd[i]) rdans++;
if (!cd[i]) cdans++;
}
cout<<rdans<<endl<<max(rdans,cdans);
}
}
usaco 校园网的更多相关文章
- Luogu USACO Training 刷水记录
开个坑记录一下刷USACO的Training的记录 可能会随时弃坑 只有代码和做法简述 可能没有做法简述 [USACO1.1]你的飞碟在这儿Your Ride Is He… 模拟,细节已忘 #incl ...
- tarjan算法+缩点--cojs 908. 校园网
cojs 908. 校园网 ★★ 输入文件:schlnet.in 输出文件:schlnet.out 简单对比时间限制:1 s 内存限制:128 MB USACO/schlnet(译 b ...
- P2746 [USACO5.3]校园网Network of Schools(Tarjan)
P2746 [USACO5.3]校园网Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 ...
- cogs——908. 校园网
908. 校园网 ★★ 输入文件:schlnet.in 输出文件:schlnet.out 简单对比 时间限制:1 s 内存限制:128 MB USACO/schlnet(译 by Fe ...
- P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools
P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...
- cogs908. 校园网
908. 校园网 ★★ 输入文件:schlnet.in 输出文件:schlnet.out 简单对比时间限制:1 s 内存限制:128 MB USACO/schlnet(译 by Fel ...
- 锐捷linux客户端常用命令(主要用来连接校园网或公司局域网)
锐捷访问校园网,.sh脚本文件rjsu*.sh-u 用户名-P 密码-S 参数1保存密码参数0不保存密码 其实: 直接使用md5认证方式输入用户名密码并且配置好ip之后,重新打开网卡即可有一定 ...
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
随机推荐
- 四、移植 JZ2440 开发板
4.1 移植第一步 前面已经分析过了 .config 的过程,可以知道移植需要用到的文件: .config 文件 arch/arm/cpu 下的文件 board 目录 .config 文件是根据后面 ...
- C# 读取Excel和DBF文件
//获excel中多个sheet中的数据 /// <summary> /// 读取导入Excel文件内容 /// </summary> /// <param name=& ...
- sql 分页row_number() over(order by key)
select * from ( select row_number() over(order by BD008_001) as row ,* from (select * from bd008)t ) ...
- Java SE之正则表达式Demo
@Test public void regex() {//匹配教务系统课程 // String content = "公共机座 (1-10)".replaceAll(" ...
- CF418D Big Problems for Organizers
传送门 题意,给一棵树,每次给两个点\(x,y\),求\(\max_{i=1}^{n}(\min(di_{x,i},di_{y,i}))\) 看std看了好久 以下是一个优秀的在线做法,\(O(nlo ...
- ActiveMQ学习笔记1
1.接口 JMS 公共 点对点域 发布/订阅域 ConnectionFactory QueueConnectionFactory TopicConnectionFactory Connection Q ...
- mysql 索引无法使用问题
今天碰到一个问题,表中有一个索引不使用,怎么强制也没用 ,force index都没用, 后来才发现是类型不对, 比如索引字段是int,如果参数使用varchar,那么是无法使用索引的,参数类型最好统 ...
- 关于python中的module
python中的module(模块),关于这个概念以及使用时主要有以下几点需要注意: (1)import xx时,会首先将这个xx module中的代码执行一遍(且仅执行一遍): 例如: (2)模块包 ...
- HTML表格的简单使用1
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- zabbix3.0.4通过自定义shell脚本添加对关键日志文件的监控
zabbix添加对自定义无规则日志文件的监控 项目背景及思路: zabbix自带有针对日志文件的监控,自带的监控只能监控到指定文件或者正则匹配的固定日志文件,但当需要监控的文件名没有规律的时候自带监控 ...