P2746 [USACO5.3]校园网Network of Schools

任务一:求缩完点后入度为0的点的个数(有向边)

任务二:求缩完点后入度为0和出度为0的最大值(要把图构造成强连通分量)

注意,任务二需要特判整张图是一个强联通

#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=;
int pre[maxn],other[maxn],last[maxn],l;
int n;
void add(int x,int y)
{
l++;
pre[l]=last[x];
last[x]=l;
other[l]=y;
}
int dfn[maxn],low[maxn];
int cnt;
int belong[maxn],qw;
int ru[maxn];
stack<int> s;
void dfs(int x)
{
low[x]=dfn[x]=++cnt;
s.push(x);
//ru[x]=1;
for(int p=last[x];p;p=pre[p])
{
int v=other[p];
// if(v==fa) continue;
if(!dfn[v])
{
dfs(v);
low[x]=min(low[x],low[v]);
}
else if(!belong[v])
{
low[x]=min(low[x],dfn[v]);
}
}
if(dfn[x]==low[x])
{
belong[x]=++qw;
while()
{
int y=s.top();
s.pop();
//ru[y]=0;
belong[y]=qw;
if(y==x) break;
}
}
}
int ans1,ans2;
int chu[maxn];
int main()
{
scanf("%d",&n);
for(int i=,x;i<=n;i++) while(scanf("%d",&x)&&x){add(i,x);}
for(int i=;i<=n;i++)
{
if(!dfn[i])
{
dfs(i);
}
}
for(int i=;i<=n;i++)
{
for(int p=last[i];p;p=pre[p])
{
int v=other[p];
if(belong[i]!=belong[v])
{
chu[belong[i]]++;
ru[belong[v]]++;
}
}
}
for(int i=;i<=qw;i++)
{
if(!ru[i]) ans1++;
if(!chu[i]) ans2++;
}
if(qw==)
{
printf("1\n0");
return ;
}
printf("%d\n",ans1);
printf("%d",max(ans1,ans2));
return ;
}

tarjan——校园网(缩点,再构图)的更多相关文章

  1. tarjan算法+缩点--cojs 908. 校园网

    cojs 908. 校园网 ★★   输入文件:schlnet.in   输出文件:schlnet.out   简单对比时间限制:1 s   内存限制:128 MB USACO/schlnet(译 b ...

  2. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

  3. Tarjan的缩点&&割点概述

    What is Tarjan? Tarjan,是一种用来解决图的联通性的一种有效途径,它的一般俗称叫做:缩点.我们首先来设想一下: 如果我们有一个图,其中A,B,C构成一个环,那么我们在某种条件下,如 ...

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

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

  5. BZOJ1179 [Apio2009]Atm Tarjan 强连通缩点 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1179 题意概括 有一个有向图,每一个节点有一个权值,其中有一些结束点. 现在,你要从S出发,到达任 ...

  6. BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...

  7. HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边

    Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...

  8. tarjan算法+缩点:求强连通分量 POJ 2186

    强连通分量:1309. [HAOI2006]受欢迎的牛 ★★   输入文件:cow.in   输出文件:cow.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 每一头牛 ...

  9. NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序

    原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...

随机推荐

  1. (六)easyUI之对话框窗口

    一.拥有HTML的对话框 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  2. 【前端】安装wampserver提示丢失MSVCR100.dll的解决方法

    先装Visual C++,再装wampserver 下载的时候请注意选择对应的32bit还是64bit的.然后安装. 再安装wamp

  3. Python函数知识点总结

    1.函数的定义2.如何定义一个函数以及函数语法3.函数的调用4.函数的参数(形参,实参)以及参数的传递5.函数的返回值6.变量的作用域7.匿名函数8.嵌套函数和闭包9.装饰器10.函数思维导图 1.函 ...

  4. 有选择性的启用SAP UI5调试版本的源代码

    在低版本的SAP UI5应用中,我们一旦切换成调试模式,那么应用程序源代码和UI5框架程序的源代码的调试版本都会重新加载,耗时很长. 我最近发现UI5新版本1.66.1提供了选择性加载调试版本的源代码 ...

  5. [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)

    每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...

  6. 页面使用element-tree

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. What Linux bind mounts are really doing

    Lots of Unixes have some form of 'loopback' mounts, where you can mount a bit of an existing filesys ...

  8. tiny4412 串口驱动分析九 --- shell终端

    作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...

  9. 马的遍历(BFS

    https://www.luogu.org/problemnew/show/P1443 模板BFS...... #include<iostream> #include<cstdio& ...

  10. Paper Reading:Receptive Field Block Net for Accurate and Fast Object Detection

    论文:Receptive Field Block Net for Accurate and Fast Object Detection 发表时间:2018 发表作者:(Beihang Universi ...