题意

    强连通分量,找独立的块

  强连通分量裸题

  

#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. c语言学习笔记.条件编译.#if,#ifdef,if的区别

    最近遇到了,以此做个记录. 条件编译 是C预处理部分的内容. 其判断语句包括 #if  #else if  #else 以及 #ifdef 和 #endif. 使用 #if (表达式) codes1. ...

  2. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  3. 2017 WebStorm 激活码 更新 Pycharm同样可用

    [有效时间到2017 年 11月 23日] BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1Iiw ...

  4. sqlmap的使用方法 ——时光凉春衫薄

    普通注入 Sqlmap -u “http://www.xxxxxx.com/xxxx/xxx/xxx.xxx?xx=xx” --dbs 找到一个sql的注入点 探测他的库名   access的直接探表 ...

  5. html的loadrunner脚本

    Action(){ char strs[20]; lr_start_transaction("api_sync_order");   web_add_header("SO ...

  6. redis aof文件过大问题

    http://www.itnose.net/detail/6682725.html 最近新安装了一台redis,版本为redis-3.2.5 数据盘用的是固态硬盘. 之前用的是普通硬盘,redis日志 ...

  7. (一) Mysql 简介及安装和配置

    第一节:Mysql 简介 百度百科 第二节:Mysql 安装及配置 1,Mysql5.1 下载及安装 2,Mysql 数据库编码配置 utf-8 3,Mysql 图形界面 Sqlyog 下载及安装

  8. 爬虫基础库之beautifulsoup的简单使用

    beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...

  9. 一张图解AlphaGo原理及弱点

    声明:本文转载自(微信公众号:CKDD),作者郑宇 张钧波,仅作学习收录之用,不做商业目的. 近期AlphaGo在人机围棋比赛中连胜李世石3局,体现了人工智能在围棋领域的突破,作为人工智能领域的工作者 ...

  10. 「Ionic」WebStorm的使用錯誤-

    前言:遇到這個錯誤,不要慌張,搶按照濤叔下面的方式處理就可以了. 1.Couldn't find ionic.config.json file. Are you in an Ionic project ...