POJ 2186
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22189 | Accepted: 9076 |
Description
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack> using namespace std; int N,M;
const int MAX_N = ;
const int edge = ;
stack<int> S;
int pre[MAX_N],low[MAX_N],cmp[MAX_N];
int first[MAX_N],Next[edge],v[edge];
int rfirst[MAX_N],rNext[edge],rv[edge];
int dfs_clock,scc_cnt; void dfs(int u) {
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int e = first[u]; e != -; e = Next[e]) {
if(!pre[ v[e] ]) {
dfs(v[e]);
low[u] = min(low[u],low[ v[e] ]);
} else if(!cmp[ v[e] ]) {
low[u] = min(low[u],pre[ v[e] ]);
}
} if(low[u] == pre[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} void scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre));
for(int i = ; i <= N; ++i) {
if(!pre[i]) dfs(i);
}
} void dfs1(int u) {
pre[u] = ;
for(int e = rfirst[u]; e != -; e = rNext[e]) {
if(!pre[ rv[e] ]) {
dfs1(rv[e]);
}
}
} int solve() {
scc();
memset(pre,,sizeof(pre));
int u,ans = ;
for(int i = ; i <= N; ++i)
if(cmp[i] == ) {
u = i;
++ans;
} dfs1(u);
for(int i = ; i <= N; ++i) {
if(!pre[i]) return ;
}
return ans;
} void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} void radd_edge(int id,int u) {
int e = rfirst[u];
rNext[id] = e;
rfirst[u] = id;
}
int main()
{
// freopen("sw.in","r",stdin);
scanf("%d%d",&N,&M);
for(int i = ; i <= N; ++i) rfirst[i] = first[i] = -;
for(int i = ; i < M; ++i) {
int u;
scanf("%d%d",&u,&v[i]);
rv[i] = u;
add_edge(i,u);
radd_edge(i,v[i]);
} printf("%d\n",solve());
return ;
}
POJ 2186的更多相关文章
- poj 2186 强连通分量
poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POj 2186 Popular Cows[连通分量]
题目大意:给出N头牛,有M种关系u, v.代表u牛崇拜v牛.要求找出有多少头牛被所有牛崇拜着题目链接:http://poj.org/problem?id=2186解题思路:1>求出强连通分量,标 ...
- POJ 2186 Popular Cows(Tarjan)
http://poj.org/problem?id=2186 题意 :给你n头牛,m对关系,每对关系由两个编号组成,u和v代表着u认为v是受欢迎的,如果1认为2是受欢迎的,2认为3是受欢迎的,那1认为 ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- POJ 2186 Popular Cows(强联通分量)
题目链接:http://poj.org/problem?id=2186 题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种 ...
- (连通图 缩点 强联通分支)Popular Cows -- poj --2186
http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...
- POJ 2186 Popular cows(Kosaraju+强联通分量模板)
题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...
随机推荐
- angularjs2 学习笔记(六) Form
Angular 2 Form表单 在angular2 form表单中我们需要了解表单数据绑定.数据验证.数据提交等内容,在下面的示例中并没有实际提交到后台,这部分内容在今后webapi中加以练习. 表 ...
- sed实例一则
1.背景: test.txt文件里有这些语句 li^E1026^D20150802B07QH800^B698.^C20150801B08CDP00^B514.^C20150803D00A8L00^B2 ...
- 黑客群体的露面说明互联网公司开始回馈IT行业了,
揭开中国黑客群体的神秘面纱 年薪数百万 2015-04-26 09:59:45 15259 次阅读 14 次推荐 稿源:经济观察报 33 条评论 在网络世界有专属的代号,那里才是他们最习惯的“世界 ...
- EF 随机排序
/// <summary> /// 数据上下文扩展 /// </summary> public partial class dbDataContext : IUnitOfWor ...
- JavaScript 字符串处理详解【转自:http://www.cnblogs.com/mondLei/p/4096855.html】
一.创建字符串 创建一个字符串,将一组字符串用引号包起来,将其赋值给一个字符串变量. var JsStr="Hello,JavaScript String!"; 二.字 ...
- sharepoint 浏览页面导航不正确
问题是这样的: sharepoint网站上建立一个二级站点,然后在网站中创建几个页面.当浏览二级网站的页面的时候,顶部导航的位置总是在首页的地方,而不是我的二级站点的导航位置. 解决方法: 转到网站集 ...
- HMMPfam的安装使用手记(转载)
转载至:http://blog.sina.com.cn/s/blog_3f6403290100rb61.html(感谢原文作者) HMMPfam的安装使用手记前言 简要介绍一下 HMMPfam吧.这还 ...
- 【BZOJ 1997】[Hnoi2010]Planar
Description Input Output 找到哈密尔顿环之后找到不在哈密尔顿环上的边 这些边如果同时在里面相交那他们同时在外面也相交,所以只能一外一内,这就变成了2-SAT,判一下就好了 ...
- VS中设置#define _CRT_SECURE_NO_WARNINGS的两种方式
1.我们在编译老的用C语言的开源项目如lua源包的时候,可能因为一些老的.c文件使用了strcpy,scanf等不安全的函数,而报警告和错误,而导致无法编译通过. 2.此时我们有两种解决方案: a.在 ...
- M1事后分析报告(Postmortem Report)
M1事后分析报告(Postmortem Report) 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们项目组所开发的软件为一个基于Andro ...