题解:

显然当一个图上的点是一个环时能满足题目要求

那么我们来考虑怎么形成一个环

很显然的是要先缩点

缩完点就成为了森林,如何让森林成环呢?

考虑一下环上的点的入度出度一定都大于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 校园网的更多相关文章

  1. Luogu USACO Training 刷水记录

    开个坑记录一下刷USACO的Training的记录 可能会随时弃坑 只有代码和做法简述 可能没有做法简述 [USACO1.1]你的飞碟在这儿Your Ride Is He… 模拟,细节已忘 #incl ...

  2. tarjan算法+缩点--cojs 908. 校园网

    cojs 908. 校园网 ★★   输入文件:schlnet.in   输出文件:schlnet.out   简单对比时间限制:1 s   内存限制:128 MB USACO/schlnet(译 b ...

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

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

  4. cogs——908. 校园网

    908. 校园网 ★★   输入文件:schlnet.in   输出文件:schlnet.out   简单对比 时间限制:1 s   内存限制:128 MB USACO/schlnet(译 by Fe ...

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

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

  6. cogs908. 校园网

    908. 校园网 ★★   输入文件:schlnet.in   输出文件:schlnet.out   简单对比时间限制:1 s   内存限制:128 MB USACO/schlnet(译 by Fel ...

  7. 锐捷linux客户端常用命令(主要用来连接校园网或公司局域网)

     锐捷访问校园网,.sh脚本文件rjsu*.sh-u 用户名-P 密码-S 参数1保存密码参数0不保存密码   其实:  直接使用md5认证方式输入用户名密码并且配置好ip之后,重新打开网卡即可有一定 ...

  8. 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 ...

  9. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

随机推荐

  1. vue错误记录

    启动时报错如下 D:\QQFile\\FileRecv\industry_vue>cnpm run dev > vue_demo@ dev D:\QQFile\\FileRecv\indu ...

  2. DotNetBar TextBoxDropDown响应按键事件

    textBoxDropDownHelp.TextBox.KeyDown += new KeyEventHandler(textBoxDropDownHelp_KeyDown); private voi ...

  3. change丶未来科技公众号成立了!!!!!!!!!

    其实笔者好早之前就开始弄个公众号了,但是出于各种各样的原因,一直被耽搁,现在可以了.总算弄好了,弄个公众号不难,其他原因而已 欢迎大家在Change丶未来科技交流成长. 互联网博文全新模式,分享你我的 ...

  4. 51nod1331 狭窄的通道

    题目传送门 这道题 51nod只Ac了十二个人 没有题解可以研究 所以就自己YY了半天 在这里先感谢一波岚清大爷 orz 然后这道题我分了两种情况 一种是左边的往左跑右边的往右跑 中间有一部分直接走不 ...

  5. Netty入门(2) - 核心概念

    Netty Crash Course 一个Netty程序一般开始于Bootstrap类,通过设置程序后,使用Handlers来处理特定的event和设置Netty中的事件,从而处理多个协议数据,比如实 ...

  6. JavaScript之打印页面局部区域

    /** * [print part area of html page] * @Author JohnnyZen * @DateTime 2017-10-23 * @copyright [johnny ...

  7. USB摄像头无法正常读取问题

    opencv读取摄像头或者视频一种是早期版本的IplImage结构体,图片就存在结构体指针IplImage*中,另一种是Mat类,两者在操作上略有差异,且opencv2都兼容这两个版本,前面的博客也说 ...

  8. CentOS7配置网络

    #进入目录 cd /etc/sysconfig/network-scripts/ #编辑ifcfg-XXX vi ifcfg-eno167777 #把onboot=no 改为yes #重启 shutd ...

  9. 根据txt中的文件名将文件复制到目标文件夹中

    功能如标题,之所以这么做是有的时候文件数目较多,一个一个复制太复杂了,代码如下: # -*- coding:utf-8 -*- #2018_03_18 #实现功能:根据文件名字将对应的文件复制到目标地 ...

  10. SIFT feature

    转载:http://www.cnblogs.com/wangguchangqing/p/4853263.html 1.SIFT概述 SIFT的全称是Scale Invariant Feature Tr ...