/*
强连通分量内的点可以互相传送,可以直接缩点
缩点后得到一棵树
第一问的答案是零入度点数量,
第二问: 加多少边后变成强连通图
树上入度为0的点有p个,出度为0的点为q,那么答案就是max(p,q)
如果缩点后是一个点,答案就是0
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
#define maxn 105
struct Edge{int to,nxt;}edge[<<];
int n,head[maxn],tot;
void addedge(int u,int v){
edge[tot].to=v;edge[tot].nxt=head[u];head[u]=tot++;
} int dfn[maxn],low[maxn],stack[maxn],ins[maxn],c[maxn],cnt,top,ind;
void tarjan(int x){
low[x]=dfn[x]=++ind;
stack[++top]=x;
ins[x]=;
for(int i=head[x];i!=-;i=edge[i].nxt){
int y=edge[i].to;
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(ins[y])
low[x]=min(low[x],low[y]);
}
if(dfn[x]==low[x]){
cnt++;
int y;
do{
y=stack[top--];
ins[y]=;
c[y]=cnt;
}while(y!=x);
}
}
void init(){
cnt=tot=ind=top=;
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(stack,,sizeof stack);
memset(ins,,sizeof ins);
memset(c,,sizeof c);
memset(head,-,sizeof head);
}
int main(){
while(cin>>n){
init();
for(int i=;i<=n;i++){
int u=i,v;
while(scanf("%d",&v),v)
addedge(u,v);
}
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i);
if(cnt==){
printf("1\n0\n");
continue;
} //缩点
int in[maxn]={},out[maxn]={},p=,q=;
for(int x=;x<=n;x++)
for(int i=head[x];i!=-;i=edge[i].nxt){
int y=edge[i].to;
if(c[x]==c[y])continue;
in[c[y]]++;out[c[x]]++;
}
for(int i=;i<=cnt;i++){
if(in[i]==)p++;
if(out[i]==)q++;
}
printf("%d\n%d\n",p,max(p,q));
}
}

poj1236 SCC+缩点的更多相关文章

  1. BZOJ 2707: [SDOI2012]走迷宫 [高斯消元 scc缩点]

    2707: [SDOI2012]走迷宫 题意:求s走到t期望步数,\(n \le 10^4\),保证\(|SCC| \le 100\) 求scc缩点,每个scc高斯消元,scc之间直接DP 注意每次清 ...

  2. bzoj1093: [ZJOI2007]最大半连通子图 scc缩点+dag上dp

    一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G'=(V ...

  3. HDU 3072--Intelligence System【SCC缩点新构图 &amp;&amp; 求连通全部SCC的最小费用】

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. UVA11324 The Lagest Lique(SCC缩点+DP)

    Given a directed graph G, con- sider the following transformation. First, create a new graph T(G) to ...

  5. POJ 2186 Popular cows(SCC 缩点)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  6. P2746 P2812 [USACO5.3]校园网Network of Schools[SCC缩点]

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作"接受学校").注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学 ...

  7. 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)

    P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...

  8. bzoj 1179 [Apio2009]Atm——SCC缩点+spfa

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...

  9. 洛谷 P6030 - [SDOI2012]走迷宫(高斯消元+SCC 缩点)

    题面传送门 之所以写个题解是因为题解区大部分题解的做法都有 bug(u1s1 周六上午在讨论区里连发两个 hack 的是我,由于我被禁言才让 ycx 代发的) 首先碰到这种期望题,我们套路地设 \(d ...

随机推荐

  1. drawImg、x5浏览器、react

  2. react-踩坑记录——Link带参数跳转后this.props为空对象

    原因,自组件在挂载时,父组件没向其传props划线部分不可缺少!!!!!!

  3. mysql 原理 ~ 线程与IO

    一 简介:今天来聊聊具体的线程和IO 二 具体线程与作用 1 master thread    mysql的主要工作触发线程 1 redo and binlog日志      2 合并插入缓冲.    ...

  4. 极客时间|AI技术内参

    学习进度: 014 | 精读AlphaGo Zero论文

  5. SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例

    本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...

  6. [转] Implementing a CNN for Text Classification in TensorFlow

    Github上的一个开源项目,文档讲得极清晰 Github - https://github.com/dennybritz/cnn-text-classification-tf 原文- http:// ...

  7. Python代码打印出9*9 九九乘法表

    九九乘法表 一一 小问题展现技术 1.示例一 for i in range(10): s='' for j in range(1,i+1): s+=str(j)+'*'+str(i)+'='+str( ...

  8. python3+selenium入门02-操作火狐浏览器

    使用selenium打开浏览器的时候,需要先去selenium官网下载一个对应浏览器的的driver插件.selenium下载.官网在国外可能打不开.或者可以从我网盘下载,但可能不是最新的网盘链接.将 ...

  9. 设计模式C++学习笔记之二(Proxy代理模式)

      代理,一看名字就知道这只是个中介而已,真实的执行者在代理的后面呢.cbf4life在他的书里提的例子也很有趣,更详细的内容及说明可以参考原作者博客:cbf4life.cnblogs.com.现在贴 ...

  10. unity提示can't add script解决办法

    1.脚本有误没有继承unity类. 2.类名文件名不一致. 3.内部逻辑代码有误.