Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 41771   Accepted: 16955

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. 

Source

 
 
如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。有向图的极大强连通子图,称为强连通分量(strongly connected components)。
 
在此题 要求被其他所有的牛崇拜的牛的数量 例如 
1 崇拜2
2 崇拜3
3 崇拜2
那么答案就是2 ,   2和3马被其它所有的节点崇拜, 是一个强连通分量
 
学习了这个博客

再Tarjan算法中,有如下定义。

DFN[ i ] : 在DFS中该节点被搜索的次序(时间戳)

LOW[ i ] : 为i或i的子树能够追溯到的最早的栈中节点的次序号

当DFN[ i ]==LOW[ i ]时,为i或i的子树可以构成一个强连通分量。

觉得叫color好点 给节点染色 有多少种颜色就有多少个强连通分量 同种颜色的节点处于同一个强连通分量

借这个图说明一下染色

为了打字 以下缩写DFN=LOW为条件

首先DFS 栈里面有1 3 5 6

到第六个节点  已经到达DFS最深处 满足条件 染成棕色 栈里面有1 3 5

返回到5 满足条件 染成橙色 栈里面有1 3

到3

再到4 4可以到1  但是不满足!dfn[v] ,  它满足!color[v], low[id] = min(low[id], dfn[v]); low[4] = 1, 不满足条件 栈里面有 1 3 4

回到3 low[id] = min(low[id], low[v]); low[3] = 1, 不满足条件 栈里面有 1 3 4

到1

再到2 2可以到4  但是不满足!dfn[v] ,  它满足!color[v], low[id] = min(low[id], dfn[v]); low[2] = 5, 不满足条件 栈里面有 1 3 4 2

再回到1 满足条件 把栈里面的都拿出来染成蓝色 完毕

注意要反向建图(是这样子叫么?)

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
int N, M;
using namespace std; const int si = 10010;
vector<int> G[si];
int dfn[si], low[si], color[si], size[si], stk[si];
bool flag[si]; int timelag, colorcnt, stksize;
void tarjan(int id) {
dfn[id] = low[id] = ++timelag;
stk[stksize++] = id;
for (int i = 0; i < G[id].size(); i++) {
int v = G[id][i];
if (!dfn[v]) {//dfn也是vis的标志 是否来过
tarjan(v);
low[id] = min(low[id], low[v]);
}
else if (!color[v]) {
low[id] = min(low[id], dfn[v]);
}
}
if (dfn[id] == low[id]) {//染色
int cnt = 0;
colorcnt++;
while (stksize) {
stksize--;
int x = stk[stksize];
color[x] = colorcnt;
cnt++;
if (x == id) break;
}
size[colorcnt] = cnt;
}
}
int main() {
cin >> N >> M;
while (M--) {
int a, b;
scanf("%d %d", &a, &b);
G[b].push_back(a);//反向建图
}
for (int i = 1; i <= N; i++) {
if (!dfn[i]) tarjan(i);//求强连通分量 dfn也是vis的标志
} for (int i = 1; i <= N; i++) {
for (int j = 0; j < G[i].size(); j++) {
int x = G[i][j];//i到x的边 但是他们不是同一个颜色的 上图的3和5是这种关系
if (color[i] != color[x]) flag[color[x]] = 1;
//x崇拜i 则x所处的强连通分量都崇拜i 所有与x颜色相同的都崇拜i
//但是i不崇拜x 否则i和x是同一个强连通分量同一种颜色了 所以扩大到整个x的颜色
}
}
int num = 0, ans = 0;
for (int i = 1; i <= colorcnt; i++) {
if (flag[i]) continue;
num++;
ans = size[i];
}
if (num != 1) ans = 0;//只会有一个被其它所有牛崇拜的强连通分量
cout << ans << endl;
return 0;
}

2186 Popular Cows的更多相关文章

  1. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  2. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  3. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  4. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  5. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

  6. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

  7. [强连通分量] POJ 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31815   Accepted: 12927 De ...

  8. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

  9. poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

随机推荐

  1. SWUST OJ(1044)

    顺序栈基本操作的实现 #include <iostream> #include <cstdlib> using namespace std; typedef struct st ...

  2. 使用js如何设置、获取盒模型的宽和高

    第一种: dom.style.width/height 这种方法只能获取使用内联样式的元素的宽和高. 第二种: dom.currentStyle.width/height 这种方法获取的是浏览器渲染以 ...

  3. python3.7导入gevent模块报错的解决方案

    最近更新了python解释器3.7 结果安装gevent,在导入gevent之后就报错了,错误信息如下 RuntimeWarning: greenlet.greenlet size changed, ...

  4. PhotoShop常用的功能汇总

    1.将图层变为"智能对象"后如何调整大小? 答: ctrl + T 2.如何对文字添加投影? 答: 点击文字图层,“图层”->"图层样式"->&qu ...

  5. 将VMware虚拟机系统镜像导入到ESXi vSphere

    原因: 公司有一个VMware虚拟机的交叉编译镜像,但主机性能不行,因此需要将镜像导入ESXi vSphere 过程: 1.将WMware虚拟机克隆; 2.将虚拟机的多个磁盘文件合并成一个;(否则vS ...

  6. 针对小程序for循环绑定数据,实现toggle切换效果(交流QQ群:604788754)

    如有更好的方法实现,可以留言或加群交流学习.谢谢(交流QQ群:604788754) WXML: <block wx:for="{{datanum}}" wx:for-inde ...

  7. JavaWeb+SVN+Maven+Tomcat +jenkins实现自动化部署

    网址:https://blog.csdn.net/liyong1028826685/article/details/88289218 在日常开发项目中常见的开发模式是使用代码库来存放我们的项目例如:S ...

  8. Java 访问限制符 在同一包中或在不同包中:使用类创建对象的权限 & 对象访问成员变量与方法的权限 & 继承的权限 & 深入理解protected权限

    一.实例成员与类成员 1. 当类的字节码被加载到内存, 类中类变量.类方法即被分配了相应内存空间.入口地址(所有对象共享). 2. 当该类创建对象后,类中实例变量被分配内存(不同对象的实例变量互不相同 ...

  9. vue+uwsgi+nginx部署路飞学城

    vue+uwsgi+nginx部署路飞学城   有一天,老男孩的苑日天给我发来了两个神秘代码,听说是和mjj的结晶 超哥将这两个代码,放到了一个网站上,大家可以自行下载 路飞学城django代码 ht ...

  10. C语言采用socket实现http post方式上传json数据

    1.按照HTTP协议发送请求: http POST 报文格式 http 报文是面向文本的. 报文分为:请求报文和响应报文 请求报文由:请求行,请求头部,空行和请求数据四个部分组成. <1.请求行 ...