poj 2186 强连通分量
poj 2186 强连通分量
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 33414 Accepted: 13612
Description
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is
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
* Line 1: Two space-separated integers, N and M
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
* Line 1: A single integer that is the number of cows who are considered popular by every other cow.
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.
我们可以将一个强联通分量看成一个点进行处理,因为这个强连通分量中的点都是相互可达的,那么只要其中一头牛成为红人,那么其他牛也是一样的,同时我们可以得到两个结论
- 最终答案必然只是一个强连通分量(如果有两个,那么根据所有点必须可达这个点的条件,那么这两个点集必然属于一个强连通分量,与假设不合,证明成立)
- 最终答案就是拓扑排序最后的那个强连通分量,这是根据拓扑排序的性质得来的
所以我们只需要求出那个强连通分量,最终再反向dfs验证是否可达每个点,这题就解出来了。
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=1e4+10;
const int M=5e4+10;
int A[M],B[M],n,m;
vector<int> G[N]; //图
vector<int>rG[N]; //反向图
vector<int>vs; //后序遍历的顶点列表
bool vis[N];
int cmp[N]; //所属强连通分量的拓扑序
int sum[N];
void Addedge(int from,int to){
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v){
vis[v]=true;
for(int i=0;i<G[v].size();i++){
if(!vis[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v,int k){
vis[v]=true;
cmp[v]=k;
sum[k]++;
for(int i=0;i<rG[v].size();i++){
if(!vis[rG[v][i]]) rdfs(rG[v][i],k);
}
}
int scc(){
memset(vis,0,sizeof(vis));
vs.clear();
for(int v=0;v<n;v++){
if(!vis[v]) dfs(v);
}
memset(vis,0,sizeof(vis));
int k=0;
for(int i=vs.size()-1;i>=0;i--){
if(!vis[vs[i]]) rdfs(vs[i],k++); //遍历每个联通分量的点集
}
return k;
}
int main(){
// while(true)
{
n=read();m=read();
for(int i=0; i<m; i++){
A[i]=read();B[i]=read();
Addedge(A[i]-1,B[i]-1);
}
int count=scc();
int u=0,num=sum[count-1];
for(int v=0;v<n;v++){
if(cmp[v]==count-1){
u=v;
break;
}
}
memset(vis,0,sizeof(vis));
rdfs(u,0);
for(int v=0;v<n;v++){
if(!vis[v]){
num=0;
break;
}
}
printf("%d\n",num);
}
return 0;
}
/*
5 5
1 2
1 3
2 4
4 5
5 2
*/
poj 2186 强连通分量的更多相关文章
- POJ(2186)强连通分量分解
#include<cstdio> #include<vector> #include<cstring> using namespace std; ; vector& ...
- Popular Cows POJ - 2186(强连通分量)
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
- poj 1904(强连通分量+输入输出外挂)
题目链接:http://poj.org/problem?id=1904 题意:有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国 ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
- poj 1904 强连通分量
思路:先有每个儿子向所有他喜欢的姑娘建边,对于最后给出的正确匹配,我们建由姑娘到相应王子的边.和某个王子在同一强连通分量,且王子喜欢的姑娘都是该王子能娶得.思想类似匈牙利算法求匹配的时候,总能找到增广 ...
- poj 1904(强连通分量+完美匹配)
传送门:Problem 1904 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:http://www.cnblogs.co ...
- poj 1236(强连通分量分解模板题)
传送门 题意: N(2<N<100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输. 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都 ...
- poj 2186 强连通入门题目
每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...
- poj 2186 (强连通缩点)
题意:有N只奶牛,奶牛有自己认为最受欢迎的奶牛.奶牛们的这种“认为”是单向可传递的,当A认为B最受欢迎(B不一定认为A最受欢迎),且B认为C最受欢迎时,A一定也认为C最受欢迎.现在给出M对这样的“认为 ...
随机推荐
- htm 与 html 的区别
htm 与 html 的区别 前者是超文本标记(Hypertext Markup) 后者是超文本标记语言(Hypertext Markup Language) 可以说 htm = html 同时,这两 ...
- 转】[1.0.2] 详解基于maven管理-scala开发的spark项目开发环境的搭建与测试
场景 好的,假设项目数据调研与需求分析已接近尾声,马上进入Coding阶段了,辣么在Coding之前需要干马呢?是的,“统一开发工具.开发环境的搭建与本地测试.测试环境的搭建与测试” - 本文详细记录 ...
- [转]广义正交匹配追踪(gOMP)
广义正交匹配追踪(Generalized OMP, gOMP)算法可以看作为OMP算法的一种推广,由文献[1]提出,第1作者本硕为哈工大毕业,发表此论文时在Korea University攻读博士学位 ...
- Triangular Pastures POJ - 1948
Triangular Pastures POJ - 1948 sum表示木条的总长.a[i]表示第i根木条长度.ans[i][j][k]表示用前i条木条,摆成两条长度分别为j和k的边是否可能. 那么a ...
- 暴力/图论 hihoCoder 1179 永恒游戏
题目传送门 /* 暴力:也是暴力过了,无语.无向图,两端点都要加度数和点 */ #include <cstdio> #include <algorithm> #include ...
- 题解报告:hdu1995汉诺塔V(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1995 Problem Description 用1,2,...,n表示n个盘子,称为1号盘,2号盘,. ...
- hbase rpc这点事
年前的时候系统梳理了一下hbase rpc的实现,并且对组里的小伙伴做了一次分享.趁着热乎劲还没完全消失殆尽,准备赶紧记录下来. hbase中rpc概况 作为一个分布式系统,hbase的设计是典型的m ...
- ATM机(非函数版)
#include<stdio.h>#include<stdlib.h>int main(void){char zhangHao[]="123";int mi ...
- Oracle 的备份和恢复
Oracle数据库有三种标准的备份方法,它们分别是导出/导入(EXP/IMP).热备份和冷备 份.导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一. 导出/导入(Export/Import) 利 ...
- web调用手机相册,并实现动态增加图片功能
注:经测试h5调用相册效果有兼容性问题,安卓仅能调用拍照功能(部分安卓可能会调不起来,所以建议用app原生调用),ios可调起拍照和相册功能. <html xmlns="http:// ...