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.

我们可以将一个强联通分量看成一个点进行处理,因为这个强连通分量中的点都是相互可达的,那么只要其中一头牛成为红人,那么其他牛也是一样的,同时我们可以得到两个结论

  1. 最终答案必然只是一个强连通分量(如果有两个,那么根据所有点必须可达这个点的条件,那么这两个点集必然属于一个强连通分量,与假设不合,证明成立)
  2. 最终答案就是拓扑排序最后的那个强连通分量,这是根据拓扑排序的性质得来的

    所以我们只需要求出那个强连通分量,最终再反向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 强连通分量的更多相关文章

  1. POJ(2186)强连通分量分解

    #include<cstdio> #include<vector> #include<cstring> using namespace std; ; vector& ...

  2. 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, ...

  3. poj 1904(强连通分量+输入输出外挂)

    题目链接:http://poj.org/problem?id=1904 题意:有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国 ...

  4. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  5. poj 1904 强连通分量

    思路:先有每个儿子向所有他喜欢的姑娘建边,对于最后给出的正确匹配,我们建由姑娘到相应王子的边.和某个王子在同一强连通分量,且王子喜欢的姑娘都是该王子能娶得.思想类似匈牙利算法求匹配的时候,总能找到增广 ...

  6. poj 1904(强连通分量+完美匹配)

    传送门:Problem 1904 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:http://www.cnblogs.co ...

  7. poj 1236(强连通分量分解模板题)

    传送门 题意: N(2<N<100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输. 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都 ...

  8. poj 2186 强连通入门题目

    每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...

  9. poj 2186 (强连通缩点)

    题意:有N只奶牛,奶牛有自己认为最受欢迎的奶牛.奶牛们的这种“认为”是单向可传递的,当A认为B最受欢迎(B不一定认为A最受欢迎),且B认为C最受欢迎时,A一定也认为C最受欢迎.现在给出M对这样的“认为 ...

随机推荐

  1. Noip2014生活大爆炸版石头剪刀布【水模拟】

    模拟暴力也要优雅. https://www.luogu.org/problemnew/show/P1328 像我这种蒟蒻就会敲无数个ifelse qaq. 可以优雅地进行预处理一下. 膜法真是好东西q ...

  2. logstash | logstash && logstash-input-jdbc 安装

    Windows系统:          1.安装Logstash  1.1 进入官网下载zip包                  [1]  https://artifacts.elastic.co/ ...

  3. Canny检测理解和Matlab实现

    图象的边缘是指图象局部区域亮度变化显著的部分,该区域的灰度剖面一般可以看作是一个阶跃,既从一个灰度值在很小的缓冲区域内急剧变化到另一个灰度相差较大的灰度值. 1.Canny边缘检测的基本特征 (1) ...

  4. 递推DP UVA 1291 Dance Dance Revolution

    题目传送门 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少分析:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程: ...

  5. Matlab实现Butterworth滤波器 分类: 图像处理 2014-06-02 00:05 527人阅读 评论(0) 收藏

    下面是用Matlab实现的Butterworth高通.低通滤波器. clc;clear all;close all; I=imread('cameraman.tif'); subplot(3,2,1) ...

  6. uwp选取文件夹并读取其中的图片

    uwp对文件的操作和wpf,winform等等有很大的不同,主要原因是uwp对权限的要求比较严格,不能想从前那样随心所欲的读取文件. 1.首先找到Package.appxmanifest这个文件,在功 ...

  7. 安装11g 数据库

    出现问题解决: 1.首先确认下载的安装包完整性.2解压包的时候,按顺序解压,解压第一个包后,解压第二个包的时候,要把解压地址与解压第二包的地址要一样. 安装的时候,需要把两个压缩包都解压,并将目录wi ...

  8. Objective-C Memory Management 内存管理 2

    Objective-C Memory Management 内存管理  2  2.1 The Rules of Cocoa Memory Management 内存管理规则 (1)When you c ...

  9. R in action 读书笔记(1)--第五章:高级数据管理

    5.2.1数学函数 函数 描述 abs(x) 绝对值 sqrt(x) 平方根 ceiling(x) 不小于x的最小整数 floor(x) 不大于x的最大整数 trunc(x) 向0的方向截取的X中的整 ...

  10. 调度工具taskctl跨调度服务依赖实现

    调度工具taskctl虽然支持分布式调度,但是有的时候,不同重要程度的调度服务还是要区分开来,在区分开后,不同调度服务之间怎么实现依赖啦, 其实有很多方式,比如写文件,写数据库之类的,这些都可以根据用 ...