POJ 2186 Popular cows(SCC 缩点)
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
#define eps 1e-8
typedef pair<int,int> P;
const int maxn=5e4+;
int N,M,u[maxn],v[maxn],tot,times,blocks;
int head[maxn],dfn[maxn],lowv[maxn];
int ins[maxn],outd[maxn],belong[maxn],sz[maxn];
struct Node{
int v,nxt;
} edge[maxn];
stack<int> st;
void Init()
{
memset(head,-,sizeof head);
memset(dfn,,sizeof dfn);
memset(lowv,,sizeof lowv);
memset(ins,,sizeof ins);
memset(outd,,sizeof outd);
memset(sz,,sizeof sz);
memset(belong,,sizeof belong);
tot=times=blocks=;
while(!st.empty()) st.pop();
} void Addedge(int u,int v)
{
edge[tot].v=v;
edge[tot].nxt=head[u];
head[u]=tot++;
} void Tarjan(int u)
{
dfn[u]=lowv[u]=++times;
st.push(u);
ins[u]=;
for(int i=head[u];~i;i=edge[i].nxt)
{
int v=edge[i].v;
if(!dfn[v]) Tarjan(v),lowv[u]=min(lowv[u],lowv[v]);
else if(ins[v]) lowv[u]=min(lowv[u],dfn[v]);
} if(dfn[u]==lowv[u])
{
++blocks;
int v;
do
{
v=st.top(); st.pop();
belong[v]=blocks;
sz[blocks]++;
ins[v]=;
} while(u!=v);
} } int main()
{
ios::sync_with_stdio(false);
cin>>N>>M;
Init();
for(int i=;i<=M;i++)
{
cin>>u[i]>>v[i];
Addedge(u[i],v[i]);
}
int cnt=,flag;
for(int i=;i<=N;i++) if(!dfn[i]) Tarjan(i);
for(int i=;i<=M;i++) if(belong[u[i]]!=belong[v[i]]) outd[belong[u[i]]]++;
for(int i=;i<=blocks;i++) if(outd[i]==) cnt++,flag=i; if(cnt!=) cout<<<<endl;
else cout<<sz[flag]<<endl;
return ;
}
POJ 2186 Popular cows(SCC 缩点)的更多相关文章
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- 强连通分量分解 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 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27698 Accepted: 11148 De ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
随机推荐
- Windows系统下搭建WAMP环境
Wamp就是Windos Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件.其中php环境配置是至关重要的一部分,本文就针对php在本地的 ...
- 服务器spring boot版本,平滑升级
1.在pom文件中加入: <!--平滑升级包 开始 --> <dependency> <groupId>org.springframework.boot</g ...
- jwt 实践应用以及特殊案例思考
JSON Web Token 是 rfc7519 出的一份标准,使用 JSON 来传递数据,用于判定用户是否登录状态. jwt 之前,使用 session 来做用户认证. 以下代码均使用 javasc ...
- setState何时同步,何时异步,为什么?
setState何时同步,何时异步,为什么 答案:在React库控制时,异步:否则同步. 示例代码如下: constructor(props){ super(porps); this.state = ...
- tar使用笔记
解压到指定目录 -C tar -zxvf test.tar.gz -C /opt
- word使用指南(经常更新)
一.快捷键 Ctrl+C 复制 Ctrl+X 剪切 Ctrl+V 粘贴 Ctrl+F 查找 Ctrl+A 全选 Ctrl+Z/Y 撤销/还原撤销 Ctrl+D 打开字体对话框 Ctrl+S 另存为 C ...
- ReactRouter中HashRouter和BrowserRouter的区别
仅个人理解,如有不当请指正 一.从原理上 HashRouter在路径中包含了#,相当于HTML的锚点定位.(# 符号的英文叫hash,所以叫HashRouter,和散列没关系哦)) 而BrowserR ...
- vscode启动黑屏
今天打开vscode的时候突然就黑屏了,一脸懵 于是上网找了一下,根据这位博主的解决办法: https://blog.csdn.net/insgo/article/details/102975986 ...
- Java基础面试题及答案(二)
容器 18. java 容器都有哪些? 常用容器的图录: 19. Collection 和 Collections 有什么区别? java.util.Collection 是一个集合接口(集合类的一个 ...
- php之自动加载(懒加载)
有A类和B类,如果在A类实例化B类,最简单直接的方法就是在B中使用include require_once A的文件,但是这种方法显然是不友好的,在框架中叶不是这么做的,在框架中使用的是自动加载的机制 ...