题解:

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

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

很显然的是要先缩点

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

考虑一下环上的点的入度出度一定都大于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. PHP 进行支付宝开发中return_url和notify_url的区别分析

    在支付宝处理业务中return_url,notify_url是返回些什么状态呢,我们要根据它来做一些处理就必须了解return_url,notify_url的区别,下面我就来给大家介绍; 一.问题描述 ...

  2. 以前的 Delphi版本

                    Delphi 1 Delphi 2 Delphi 3 Delphi 4 Delphi 5 Delphi 6 Delphi 7 Delphi 8 Delphi 2005

  3. Android Handler面试解析

  4. Android五种数据存储方式

    android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...

  5. 1.内网ntp服务器的搭建

    1.拓扑图

  6. ProcessHacker可编译版本

    说明 做一个批量进程内搜索字符串的工具. 试了processhacker-2.39-src.zip. https://sourceforge.net/projects/processhacker/fi ...

  7. 三 、 Multivariance Linear Regssion练习(转载)

    转载:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2962116.html 前言: 本文主要是来练习多变量线性回归问题(其实本文也就3个 ...

  8. ubuntu 下 teamview 取消自动启动 autostart

    sudo teamviewer daemon disable

  9. 深入理解node.js异步编程:基础篇

    ###[本文是基础内容,大神请绕道,才疏学浅,难免纰漏,请各位轻喷] ##1. 概述 目前开源社区最火热的技术当属Node.js莫属了,作为使用Javascript为主要开发语言的服务器端编程技术和平 ...

  10. 【转】void及void指针的深刻解析

    void的含义 void即“无类型” ,void*则为“无类型指针”,可以指向任何数据类型,所以又叫做“通用指针”. void指针使用规范 ①void指针可以只想任意类型的数据,亦即可用任意数据类型的 ...