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. C语言字符串操作函数

    1.函数名: stpcpy  功  能: 拷贝一个字符串到另一个  用  法: char *stpcpy(char *destin, char *source);  程序例: #include < ...

  2. vc 在edit控件中动态插入数据滚动显示

    内存从网上论坛摘抄整理 思路:给控件设置多行属性,设置垂直滚动条,Auto Vscroll设置为true,放入文本后把插入点设置到末尾 pEdit->LineScroll(pEdit->G ...

  3. 细说UI线程和Windows消息队列

    在 Windows应用程序中,窗体是由一种称为“ UI线程( User Interface Thread)”的特殊类型的线程创建的. 首先, UI线程是一种“线程”,所以它具有一个线程应该具有的所有特 ...

  4. oracle系统包——dbms_random用法及order by 小结(转)

    dbms_random是一个可以生成随机数值或者字符串的程序包. 这个包有initialize().seed().terminate().value().normal().random().strin ...

  5. MySQL内存表的特性与使用介绍 -- 简明现代魔法

    MySQL内存表的特性与使用介绍 -- 简明现代魔法 MySQL内存表的特性与使用介绍

  6. 与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)

    原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页][源码下载] 与众不同 windows phone (22 ...

  7. shell 调试

    感觉编写shell在查找错误的过程中,很让你崩溃,还好shell也提供了一些调试的方式: 语法检查      -n选项做语法检查,而不执行脚本      sh -n script_name.sh 启动 ...

  8. hdu2159 Fate 二维背包

    #include <cstring> #include <string> #include <cstdio> #include <cmath> #inc ...

  9. UIButton return(textField textView)

    首先设置TextField 或 TextView的 delegate /////UITextView - (BOOL)textView:(UITextView *)textView shouldCha ...

  10. U盘1G变8M解决的方法

    本人曾有一个大小为1G的纽曼U盘,在一年前不幸中毒,格式化之后就仅仅剩8M了,然后再也无法正常格式化.尽管仅仅有8M,但总认为扔了可惜,于是乎,就一直束之高阁.昨天突然心血来潮,决定再试一试,纯粹是死 ...