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. 30行JavaScript代码实现一个比特币量化策略

    精简极致的均线策略 30行打造一个正向收益系统 原帖地址:https://www.fmz.com/bbs-topic-new/262 没错!你听的没错是30行代码!仅仅30行小编我习惯先通篇来看看 代 ...

  2. Django中的cookie和session实现

    cookie from django.shortcuts import render, HttpResponse, redirect # 此装饰器的作用就是讲所有没有cookie验证的页面都需要验证后 ...

  3. 进击的Python【第十五章】:Web前端基础之DOM

    进击的Python[第十五章]:Web前端基础之DOM 简介:文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示 ...

  4. [Usaco2017 Feb]Why Did the Cow Cross the Road I (Gold)

    Description 有一幅n*n的方格图,n <=100,每个点上有一个值. 从(1,1)出发,走到(n,n),只能走上下左右. 每走一步花费t,每走三步需要花费走完三步后到达格子的值. 求 ...

  5. 洛谷 P1816 忠诚

    https://www.luogu.org/problemnew/show/1816 st表模板 #include<cstdio> #include<algorithm> us ...

  6. java getDocumentBase() 得到的文件夹路径

    参考一个百度知道上的回答 举例说来,假设你的项目文件是xx,而这个xx文件夹是在D盘下的yy文件夹里,即项目文件的完整路径D:\yy\xx,则编译运行文件后,在xx文件夹里会产生名为build的文件夹 ...

  7. D. Artsem and Saunders 数学题

    http://codeforces.com/contest/765/problem/D 这题的化简,不能乱带入,因为复合函数的带入,往往要严格根据他们的定义域的 题目要求出下面两个函数 g[h(x)] ...

  8. AJPFX:学习JAVA程序员两个必会的冒泡和选择排序

    * 数组排序(冒泡排序)* * 冒泡排序: 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处* * 选择排序 : 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现 ...

  9. 平衡图片负载,提升web站点访问体验

    最近给分公司做官方网站,内网测试一切ok,发布至云端后,体验惊人——公司外网网速渣渣(十几k~几十k),更加要命的是,网站的高清图,根本就加载不出来,几秒,十几秒过去了,仍然在转圈圈,如下图... 于 ...

  10. ViewPager讲解以及ViewFlipper

    1.加入ViewPager最好导入<android.support.v4.view.ViewPager>兼容低版本 2.将布局转换为View的方法 3.适配器类型 PagerAdapter ...