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认为 ...
随机推荐
- C基础 数据序列化简单使用和讨论
前言 C中对序列化讨论少, 因为很多传输的内容都有自己解析的轮子. 对于序列化本质是统一编码, 统一解码的方式. 本文探讨是一种简单的序列化方案. 保证不同使用端都能解析出正确结果. 在文章一开始, ...
- 图片模糊度判断程序(C++、opencv)
//#include<opencv2\opencv.hpp> //using namespace cv; #include <opencv2/core/core.hpp> #i ...
- qu
离骚 (3481人评分) 8.5 朝代:先秦 作者:屈原 原文: 帝高阳之苗裔兮,朕皇考曰伯庸.摄提贞于孟陬兮,惟庚寅吾以降.皇览揆余初度兮,肇锡余以嘉名:名余曰正则兮,字余曰灵均.纷吾既有此内美 ...
- [译] Swift 的响应式编程
原文 https://github.com/bboyfeiyu/iOS-tech-frontier/blob/master/issue-3/Swift的响应式编程.md 原文链接 : Reactiv ...
- CentOS 6.3 安装以及配置Apache php mysql
准备篇: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dp ...
- 读取、添加、删除、修改配置文件 如(Web.config, App.config)
private Configuration config; public OperateConfig() : this(HttpContext.Current.Request.ApplicationP ...
- Easy-UI 动态添加DataGrid的Toolbar按钮
在前人的基础上进行的修改,不知道他是从哪里引用来的,所以没有粘贴引用地址. 原代码不支持1.3.6. 修改功能: 1.如果之前没有添加过工具,用这个方法不能添加(已修复): 2.估计是不支持1.3.6 ...
- Qt的QTabelWidget
QTableWidget的用法总结 http://blog.csdn.net/zb872676223/article/details/39959061 [Qt]在QTableWidget中添加QCh ...
- verilog简易实现CPU的Cache设计
verilog简易实现CPU的Cache设计 该文是基于博主之前一篇博客http://www.cnblogs.com/wsine/p/4661147.html所增加的Cache,相同的内容就不重复写了 ...
- 【转】matlab采样函数
dyaddown 功能:对时间序列进行二元采样,每隔一个元素提取一个元素,得到一个降采样时间序列. 格式: 1.y = dyaddown(x, EVENODD) 当EVENODD=0时,从x中第二个元 ...