题意

    强连通分量,找独立的块

  强连通分量裸题

  

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream> using namespace std; const int maxn = ;
int n, m;
struct Edge
{
int v, next;
Edge (int v = , int next = ):
v(v), next(next) {}
}e[maxn*];
int head[maxn], label;
int stack[maxn], Scnt;
int belong[maxn], Bcnt;
int dfn[maxn], low[maxn], dfs_clock;
bool instack[maxn];
int siz[maxn], chu[maxn]; void ins(int u, int v)
{
e[++label] = Edge(v, head[u]);
head[u] = label;
} void dfs(int u)
{
dfn[u] = low[u] = ++dfs_clock;
stack[++Scnt] = u;
instack[u] = true;
for (int i = head[u]; i != -; i = e[i].next)
{
int v = e[i].v;
if (!dfn[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else
if (instack[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
Bcnt ++;
int v;
do
{
v = stack[Scnt --];
belong[v] = Bcnt;
instack[v] = false;
}while(v != u);
}
} int main()
{
scanf("%d %d", &n, &m);
for (int i = ; i <= n; ++i)
head[i] = -;
label = -;
for (int i = ; i <= m; ++i)
{
int u, v;
scanf("%d %d", &u, &v);
ins(u, v);
}
for (int i = ; i <= n; ++i)
instack[i] = belong[i] = dfn[i] = ;
Scnt = Bcnt = dfs_clock = ;
for (int i = ; i <= n; ++i)
if (!dfn[i])
dfs(i);
for (int i = ; i <= Bcnt; ++i)
siz[i] = chu[i] = ;
for (int i = ; i <= n; ++i)
{
siz[belong[i]] ++;
for (int j = head[i]; j != -; j = e[j].next)
{
int v = e[j].v;
if (belong[v] == belong[i])
continue ;
chu[belong[i]] ++;
}
}
int cnt = , ans;
for (int i = ; i <= Bcnt; ++i)
if (chu[i] == )
cnt ++, ans = siz[i];
if (cnt > )
ans = ;
printf("%d\n", ans);
return ;
}

POJ 2186 Popular Cows 强连通分量模板的更多相关文章

  1. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  2. POJ 2186 Popular Cows --强连通分量

    题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...

  3. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  4. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  5. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  6. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  7. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  8. [强连通分量] POJ 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31815   Accepted: 12927 De ...

  9. POJ 2186 Popular cows(Kosaraju+强联通分量模板)

    题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...

随机推荐

  1. IE9 下 ellipsis bug fix

    fiddle: http://jsfiddle.net/tagliala/TtbuG/10/ original: https://github.com/FortAwesome/Font-Awesome ...

  2. spring web 生命周期理解

    spring web /bean 生命周期 反射注解 aop代理类生成 init servlet  初始化 load spring-context.xml load XmlParser 类解析对象   ...

  3. git中如何查看一个文件的修改(更新)历史

    有些时候有些文件或文件夹被移除了, 或者更换了路径或被改名了, 想跟踪一下这个文件被修改(更新)的历史, 可以用如下命令: git log -p matser -- filename 格式是: git ...

  4. linux命令:crontab命令(转)

    一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...

  5. ifconfig与内核通信 ifreq 结构体分析和使用

    结构原型: /* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have p ...

  6. csu 1550(字符串处理思路题)

    1550: Simple String Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 481  Solved: 211[Submit][Status][ ...

  7. springboot 集合 meshsite3

    工程目录 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  8. Hadoop案例(二)压缩解压缩

    压缩/解压缩案例 一. 对数据流的压缩和解压缩 CompressionCodec有两个方法可以用于轻松地压缩或解压缩数据.要想对正在被写入一个输出流的数据进行压缩,我们可以使用createOutput ...

  9. 【58沈剑架构系列】细聊分布式ID生成方法

    一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id (2)订单标识:order-id (3)帖子标识:tiezi-id 这个记录标识往往就是数据 ...

  10. sed实践

    后来也找到一篇文章讲的很详细: http://www.cnblogs.com/ctaixw/p/5860221.html --------------------------------------- ...