首页 技术 正文
技术 2022年11月8日
0 收藏 587 点赞 1,618 浏览 1485 个字

题目传送门!(luogu)

首先考虑问题一


不难想到,如果有一个学校作为终端机,那么跟其处于同一个强联通中的所有学校就可以不用作为终端机了。 那么,问题一也就迎刃而解了:找到所有入度为0的缩点。因为这个学校(强联通中至少有一个学校)必须作为终端机,毕竟它收不到别的学校传来的,只能自给自足。

然后考虑问题二

“任意一个学校都能作为母鸡”?试想一下,任意选取一个学校作为终端,要使得其余所有学校都能收到,只能是全图联通。因此,找到出度为0和入度为0的缩点的个数取max就ok了。(即从出度为0的点连向入度为0 的点)

(注意代码的细节部分呀!)

#include <bits/stdc++.h>
using namespace std;
#define N 5000010
#define orz 0inline int read(){
int x = , s = ;
char c = getchar();
while(!isdigit(c)){
if(c == '-')s = -;
c = getchar();
}
while(isdigit(c)){
x = (x << ) + (x << ) + (c ^ '');
c = getchar();
}
return x * s;
} struct node{
int u, v;
int next;
} t[N];
int f[N];//日常邻接表 int stac[N], top = ;
int dfn[N], scc[N], low[N];
bool vis[N];
//tarjan 板子,不多说
int in[N], out[N];//入度出度
int ans1 = , ans2 = ;int bian = ;
inline void add(int u, int v){
t[++bian].v = v;
t[bian].u= u;
t[bian].next = f[u];
f[u] = bian;
return ;
}int cac = , cnt = ;
void tarjan(int now){//有向图强联通板子
dfn[now] = low[now] = ++ cac;
stac[++top] = now;
vis[now] = ;
for(int i = f[now]; i;i = t[i].next){
int v = t[i].v;
if(!dfn[v]){
tarjan(v);
low[now] = min(low[now], low[v]);
}
else if(vis[v])low[now] = min(low[now], dfn[v]);
}
if(dfn[now] == low[now]){
int cur;
cnt++;
do{
cur = stac[top--];
vis[cur] = ;
scc[cur] = cnt;
}while(cur != now);
}
return ;
}int main(){
int n = read();
for(int i = ;i <= n; i++){
int x = read();
while(x){
add(i, x);
x = read();
}
}
for(int i = ;i <= n;i++)
if(!dfn[i]) tarjan(i);//注意防止有的点漏掉
for(int i = ;i <= bian; i++){ //统计所有的边
int u = t[i].u, v = t[i].v;
if(scc[u] != scc[v]){//如果起点和终点不在同一个缩点 (即在两个缩点的交界处的边)
out[scc[u]]++;
in[scc[v]]++;
} }
for(int i = ;i <= cnt; i++){
if(!in[i])ans1++;
if(!out[i])ans2++;//统计
}
if(cnt == ) printf("1\n0"); /*记得特判呀*/
else printf("%d\n%d", ans1, max(ans1, ans2));
return orz; //%一下CCF求AC
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898