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的邮票,可以推出 ...
随机推荐
- 我的vim插件配置
set nocompatible " be iMproved, required filetype off " required " set the runtime pa ...
- postgresql时间处理
时间取到截取 例:select date_trunc('second', "reportTime") from travel_message limit 10; 结果: 他人博客: ...
- Struts2_day04
一.回顾 1什么是值栈 (1)action和servlet区别 2 如何获取值栈对象 3 值栈内部结构 (1)root和context 4 向值栈放数据 (1)向值栈放字符串 (2)向值栈放对象 (3 ...
- Python排序算法之快速排序
快速排序(quickSort) 快排的思想:首先任意选取一个数据(通常选用数组的第一个数)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序. 百度百 ...
- Linux - 文件ACL权限控制
getfacl 1.test # 查看文件ACL权限 setfacl -R -m u:xuesong:rw- 1.test # 对文件增加用户的读写权限 -R 递归
- CF101D Castle
传送门 首先,一定要把所有点遍历一遍,这时答案应该是\(\frac{\sum 某个点第一次被遍历的时间点}{n-1}\quad\),而且每条边只能走两次,所以每次要遍历完某棵子树才能遍历其它子树. 考 ...
- nodejs对mongodb数据库的增删改查操作(转载)
首先要确保mongodb的正确安装,安装参照:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-l ...
- mysql之 innobackupex备份+binlog日志的完全恢复【转】
前言: MySQL的完全恢复,我们可以借助于完整的 备份+binlog 来将数据库恢复到故障点. 备份可以是热备与逻辑备份(mysqldump),只要备份与binlog是完整的,都可以实现完全恢复. ...
- Python3学习笔记19-继承和多态
在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承, 新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.Sup ...
- zabbix客户端日志报错no active checks on server [192.168.3.108:10051]: host [192.168.3.108] not found
zabbix客户端日志报错: 45647:20160808:220507.717 no active checks on server [192.168.3.108:10051]: host [192 ...