poj1236 SCC+缩点
/*
强连通分量内的点可以互相传送,可以直接缩点
缩点后得到一棵树
第一问的答案是零入度点数量,
第二问: 加多少边后变成强连通图
树上入度为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+缩点的更多相关文章
- BZOJ 2707: [SDOI2012]走迷宫 [高斯消元 scc缩点]
2707: [SDOI2012]走迷宫 题意:求s走到t期望步数,\(n \le 10^4\),保证\(|SCC| \le 100\) 求scc缩点,每个scc高斯消元,scc之间直接DP 注意每次清 ...
- 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 ...
- HDU 3072--Intelligence System【SCC缩点新构图 && 求连通全部SCC的最小费用】
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- UVA11324 The Lagest Lique(SCC缩点+DP)
Given a directed graph G, con- sider the following transformation. First, create a new graph T(G) to ...
- 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, ...
- P2746 P2812 [USACO5.3]校园网Network of Schools[SCC缩点]
题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作"接受学校").注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学 ...
- 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)
P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...
- bzoj 1179 [Apio2009]Atm——SCC缩点+spfa
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...
- 洛谷 P6030 - [SDOI2012]走迷宫(高斯消元+SCC 缩点)
题面传送门 之所以写个题解是因为题解区大部分题解的做法都有 bug(u1s1 周六上午在讨论区里连发两个 hack 的是我,由于我被禁言才让 ycx 代发的) 首先碰到这种期望题,我们套路地设 \(d ...
随机推荐
- valgrind 开发板调试
1.http://www.360doc.com/content/13/1205/13/7256015_334661588.shtml
- 【Thymeleaf】Thymeleaf模板对html实时刷新
解决方案 spring: thymeleaf: cache: false 修改完html代码后Ctrl+Shift+F9,重新编译即可刷新页面内容!
- 微服务之Spring cloud
微服务 Spring cloud Spring Cloud provides tools for developers to quickly build some of the common patt ...
- kotlin中“==”和“===”的区别
code 1 fun main(args: Array<String>) { val a : Int = 1000 println(a == a) //true println(a === ...
- 【逆向笔记】2017年全国大学生信息安全竞赛 Reverse 填数游戏
2017年全国大学生信息安全竞赛 Reverse 填数游戏 起因是吾爱破解大手发的解题思路,觉得题挺有意思的,就找来学习学习 这是i春秋的下载链接 http://static2.ichunqiu.co ...
- TensorFlow架构学习
0 - TensorFlow 基于数据流图,节点表示某种抽象计算,边表示节点之间联系的张量. Tensorflow结构灵活,能够支持各种网络模型,有良好的通用性和扩展性. 1 - 系统概述 Tenso ...
- Django学习手册 - admin后台 切换成中文显示/添加数据表
Django admin后台管理 切换成中文界面: 站点显示为中文: 在setting 里面修改 LANGUAGE_CORE = 'zh-Hans' 字段名显示中文 class Test(models ...
- sonar排除实体类配置
sonar覆盖率检查可以将一些实体类排除,maven项目可以在pom.xml文件中添加如下配置 <properties> <sonar.exclusions> src/main ...
- MVC传参数给js的时候 如果是数值 变量要进行一下转换才能正确识别 例如var aaa = parseInt('@Model.ClickIndex');
这是拼接参数的格式 <div id="a1" style="font-size:12px" onclick="location = '@item ...
- 如何生成添加前缀的顺序DIV
今天我们这边的需求是生产类似于 div1 div2 div3 这种的方式. filters: { pre: function (value) { return 'div' + value; } }, ...