/*
强连通分量内的点可以互相传送,可以直接缩点
缩点后得到一棵树
第一问的答案是零入度点数量,
第二问: 加多少边后变成强连通图
树上入度为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. python - 代码练习 - 备份目录/文件(windos环境)

    备份目录/文件 import zipfile import os,datetime class beifen(object): def __init__(self,files,new_files,co ...

  2. logistic回归为什么要使用sigmoid函数

    https://www.baidu.com/link?url=LnDjrhLG7Fx6YVgR9WljUILkPZrIzOR402wr2goIS-ARtDv9TwZ2VYVbY74fyVpQlE22n ...

  3. P4843 清理雪道

    题目地址:P4843 清理雪道 上下界网络流 无源汇上下界可行流 给定 \(n\) 个点, \(m\) 条边的网络,求一个可行解,使得边 \((u,v)\) 的流量介于 \([B(u,v),C(u,v ...

  4. iframe教程

    有关iframe的最强大的强大的教程 $(window.parent.document).contents().find("#tab_release"+taskId2+" ...

  5. 876. Middle of the Linked List

    1. 原始题目 Given a non-empty, singly linked list with head node head, return a middle node of linked li ...

  6. Deep Learning Tutorial - Convolutional Neural Networks(LENET)

    CNN很多概述和要点在CS231n.Neural Networks and Deep Learning中有详细阐述,这里补充Deep Learning Tutorial中的内容.本节前提是前两节的内容 ...

  7. [转载]linux内存映射mmap原理分析【转】

    转自:http://www.cnblogs.com/wanpengcoder/articles/5306688.html 转自:http://blog.csdn.net/yusiguyuan/arti ...

  8. 题解-USACO18DEC Sort It Out

    Problem 洛谷5156 题意概要:给定一个长为\(n\)的排列,可以选择一个集合\(S\)使这个集合内部元素排到自己在整个序列中应该在的位置(即对于集合\(S\)内的每一个元素\(i\),使其排 ...

  9. BZOJ 3620: 似乎在梦中见过的样子

    似乎在梦中见过的样子.... 一道水题调了这么久,还半天想不出来怎么 T 的...佩服自己(果然蒟蒻) 这题想想 KMP 但是半天没思路瞟了一眼题解发现暴力枚举起始点,然后 KMP 如图: O( n2 ...

  10. Hash之哈希表的详解

    Hash算法 Hash算法的原理; 决绝冲突的办法是: 线性探查法; 双散列函数法; 拉链法处理碰撞; 哈希原理及实现; 哈希表-Hash table, 也叫散列表;