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认为 ...
随机推荐
- 创建表 添加主键 添加列常用SQL语句
--删除主键 alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段 ...
- 使用 libevent 和 libev 提高网络应用性能——I/O模型演进变化史
构建现代的服务器应用程序需要以某种方法同时接收数百.数千甚至数万个事件,无论它们是内部请求还是网络连接,都要有效地处理它们的操作. 有许多解决方案,但事件驱动也被广泛应用到网络编程中.并大规模部署在高 ...
- 九度oj 1525 子串逆序打印
原题链接:http://ac.jobdu.com/problem.php?pid=1525 字符串简单题,注意开有结尾有空格的情况否则pe or wa #include<algorithm> ...
- OpenStack: 安装准备
>安装准备1. 安装MySQL# apt-get install python-mysqldb mysql-server将/etc/mysql/my.cnf修改bind-address为&quo ...
- Ubuntu 修改用户密码与启动root账号
passwd sban 修改当前帐号 sudo passwd root 修改root帐号 修改/etc/ssh/sshd_config,改: PermitRootLogin without-passw ...
- XAML特殊字符
此部分只限制在XAML中,代码中不受此类限制. 1.特殊字符转义 XAML 特殊字符转义 特殊字符 转义 小于号 < < 大于号 > > 取址符 & & 引号 ...
- iOS代码实践总结
转载地址:http://mobile.51cto.com/hot-492236.htm 最近一个月除了专门抽时间和精力重构之外,还有就是遇到需要添加功能的模块的时候,由于项目中的代码历史因素比较多,第 ...
- 关于UIView需要看的一些官方文档
View Controller PG(Programming Guide) 看过一遍 View PG 正在看 Drawing and Printing PG Quartz 2D PG 更高级的cus ...
- shell 编程基础
1 创建shell脚本文件 要创建一个shell脚本文件,必须在第一行指定要使用的shell,其格式为: #! /bin/bash 接着加上该shell文件的注释,说明该脚本文件用来干什么,有谁创建, ...
- 16位CPU多周期设计
16位CPU多周期设计 这个工程完成了16位CPU的多周期设计,模块化设计,有包含必要的分析说明. 多周期CPU结构图 多周期CPU设计真值表 对应某一指令的情况,但仅当对应周期时才为对应的输出,不是 ...