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. sublime搜索和替换--正则

    Search and Replace Sublime Text features two main types of search: Search - Single File Search - Mul ...

  2. Python+Django+SAE系列教程15-----输出非HTML内容(图片/PDF)

    一个Django视图函数 必须 接受一个HttpRequest 实例作为它的第一个參数 返回一个HttpResponse 实例 从一个视图返回一个非HTML 内容的关键是在构造一个 HttpRespo ...

  3. 可以根据柜子内表取出所有的柜子信息的BAPI函数

    DATA: gt_hunumbers TYPE STANDARD TABLE OF bapihunumber,      gt_huitem  TYPE STANDARD TABLE OF bapih ...

  4. phpStorm 新建文件SVN不提交的解决的方法

    phpStorm中新建文件夹,可是打开文件夹.却没有提交到SVN.导致每次都必须手动增加.假设新增的文件夹或者文件较多文件夹较深,easy遗漏.(default7#zbphp.com) 解决的方法: ...

  5. Swift - 自定义单元格实现微信聊天界面

    1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示 ...

  6. 第四章 Spring与JDBC的整合

    这里选择的是mysql数据库. 4.1引入aop.tx的命名空间 为了事务配置的需要,我们引入aop.tx的命名空间 <?xml version="1.0" encoding ...

  7. linux cent os putty 问题彻底解决办法

    出现乱码的根本原因: linux系统和putty使用的编码格式不一致. 解决办法: 1.首先使用命令查看linux当前使用的是什么编码格式 echo $LANG 返回的结果有如下几种情况:1)zh_C ...

  8. [Android学习笔记]Android调试

    Eclipse Debug 快捷键: [Ctrl + Shift + B]: 添加/取消断点 [F5]:进入方法中 [F6]:单步执行 [F7]:执行完毕此方法 [F8]:继续执行,直接跳到下一个断点 ...

  9. groovy : 正則表達式

    groovy 正則表達式 企图模仿Perl 的语法,结果是我试用后.发现没法提取匹配的字符串. 还是直接引用 java.util.regex  负责对字符序列进行正則表達式匹配 先转载水木清华上的样例 ...

  10. hive字符串函数

    1. 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length('abcedfg') f ...