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. MFC界面相关(彩色工具栏)

    MFC工具栏控件 创建工具栏步骤: (1)在Resource View中插入新toolbar (2)在toolbar上双击增加按钮,更改ID为ID_BUTTON,编译后在resource.h中即可看到 ...

  2. HDU4939Stupid Tower Defense (有思想的dp)

    Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...

  3. address_space 从哪里来

    address_space 从哪里来 这两天想弄清楚linux的内存分配,忽然看到了address_space,就想弄明白. 整个内核就见到 address_space(1)和address_spac ...

  4. 使用Xcode无法发布程序(Archive按钮一直为灰色不可点击)

    问题现象:想在Xcode中把代码编译发布成ipa程序,但“Product”->“Archive”按钮一直不可使用.   解决办法:目前的运行配置是使用模拟器,改成“iOS Device”即可   ...

  5. JQuery 事件与动画

    第一大部分 提纲 事件与动画 一.事件 1.在JavaScript语法中的事件,把onxxxxx中的on去掉,就是JQuery中的事件. onclick - click ondblclick - db ...

  6. Linux目录结构和常用命令

    源地址:http://www.cnblogs.com/JCSU/articles/2770249.html 一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/ ...

  7. js 常用正则表达式分析详解

    1.整数或者小数:/^((0{1}|[1-9]{1}[0-9]+)\.{1}[0-9]+|[1-9]{1}[0-9]*|0)$/ 分析:分类讨论,如果是小数,则有两种形式   0.111对应的是 0{ ...

  8. sqlserver自学笔记之的流程控制语句

    1.使用if else语句 ①在学生表中,查找名字为王刚的同学,如果存在,显示该同学的信息,否则显示查无此人 if exists(select sno from student where sname ...

  9. cocos2dx之lua项目开发中MVC框架的简单应用

    **************************************************************************** 时间:2015-03-31 作者:Sharin ...

  10. objective-c 中数据类型之六 数值类(NSValue)

    // NSValue能够将c类型转换为Objective-C对象,如NSRange,CGPoint.CGSize,CGRect,CGVector,UIEdgeInsets,UIOffset NSRan ...