B - Popular Cows

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

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

在对图进行建立时。假设用矩阵会导致内存超限。用VEC会使后来的缩点变得麻烦。所以用邻接表是最佳选择:

#include<stdio.h>
#include<stack>;
#include<string.h>
using namespace std;
stack<int>S;
int dfn[10005],map[10005][110],low[10005],instack[10005],belong[10005],out[10005];
int index,bnt;
void tarjan(int i){
dfn[i] = low[i] = ++index;
S.push(i);
instack[i] = 1;
for(int j=1;j<=map[i][0];j++){
int k = map[i][j];
if(!dfn[k]){
tarjan(k);
low[i] = min(low[i],low[k]);
}
else if(instack[k]){
low[i] = min(low[i],dfn[k]);
} }
if(low[i] == dfn[i]){
bnt ++;
int k;
do{
k = S.top();
S.pop();
instack[k] = 0;
belong[k] = bnt;
}while(i!=k);
}
}
int main(){
int m,n,sum;
while(~scanf("%d%d",&n,&m)){
int a,b,j;
index = bnt = sum = 0;
memset(map,0,sizeof(map));
memset(instack,0,sizeof(instack));
memset(out,0,sizeof(out));
memset(dfn,0,sizeof(dfn));
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
map[a][++map[a][0]] = b;
}
for(int i=1;i<=n;i++)
if(!dfn[i]){
tarjan(i);
}
for(int i=1;i<=n;i++){
for(j = 1;j<=map[i][0];j++)
if(belong[i]!=belong[map[i][j]])
out[belong[i]]++;
}
int z=0;
for(int i=1;i<=bnt;i++){
if(out[i]==0){
z++;
for(int j=1;j<=n;j++)
if(belong[j]==i)sum++;
}
}
if(z==1)
printf("%d\n",sum);
else printf("0\n"); }
}

tarjan+缩点的更多相关文章

  1. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  2. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  3. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  4. 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率

    2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1638  Solved: 433[Submit][Statu ...

  5. 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP

    1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 318[Submit][Stat ...

  6. 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit] ...

  7. BZOJ 1051 受欢迎的牛(Tarjan缩点)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4573  Solved: 2428 [Submit][S ...

  8. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

  9. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  10. poj3694(tarjan缩点+lca)

    传送门:Network 题意:给你一个连通图,然后再给你n个询问,每个询问给一个点u,v表示加上u,v之后又多少个桥. 分析:方法(1219ms):用并查集缩点,把不是桥的点缩成一个点,然后全图都是桥 ...

随机推荐

  1. JavaScript函数节流与函数去抖

    介绍 首先解释一下这两个概念: 函数节流(throttle):是让一个函数无法在很短的时间间隔内连续调用,当上一次函数执行后过了规定的时间间隔,才能进行下一次该函数的调用. 函数去抖(debounce ...

  2. cpp check 分析

    1 FileTabCharacterCheck 为什么检查: 因为对于一个TAB而言,所空的空格不定是固定的,如果在机器A上设置了是4个空格,显示正常,而在机器B上阅读,B机器是100个空格为一个TA ...

  3. 回文(manacher)

    裸manacher 我竟然写跪了………… 一个地方(偶数)没写清楚…… 我OOXOXOXOXXOXO #include<cstdio> #include<cstdlib> #i ...

  4. Effective C++ 24,25

    24.在函数重载和设定參数缺省值间要谨慎选择. 获得一种类型的数据的最小值或最大值,对于c中,一般使用在<linits.h>中定义的各种宏如INT_MIN 来进行表示,可是这样无法进行泛型 ...

  5. Swift - 按钮(UIButton)的用法

    1,按钮的创建 (1)按钮有下面四种类型: UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果 UIButtonType.DetailDisc ...

  6. Core Data 和 sqlite3的性能对比【图】3gs,iPhone4,4s,5的性能测试。

    demo 和源码再此下载  :http://download.csdn.net/detail/hherima/5603797

  7. 使用CocoaPods出现 The `master` repo requires CocoaPods 0.32.1 - 问题解决

    近期在使用CocoaPods为project配置第三方类库时出现了例如以下问题: [!] The `master` repo requires CocoaPods 0.32.1 - 明显是由于Coco ...

  8. [Cocos2d-x]Cocos2d-x开发中C++调用Object-c

    2013年12月29日 简介: Cocos2d-x开发过程中,难免需要通过C++调用Object-C

  9. SharePoint 内容部署-PowerShell

    1. 创建一个新的内容部署路径 New-SPContentDeploymentPath –Name "Marketing Internet Content" –SourceSPWe ...

  10. Linux内核升级

    一.测试环境 CentOS6.5 X86 64位 内核版本为 2.6.32 VM 10.07 二.编译内核版本 2.1.kernel 3.2.71 2.2.kernel 3.4.108 2.3.ker ...