Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 37083   Accepted: 15104

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

题:A欢迎B B欢迎C 则A也欢迎C 问 有多少只牛被出自己之外的所有的牛欢迎
强联通图进行缩点  当然联通块只能有一个,否则输出0
缩点之后 出度为0的点的个数就是答案
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const ll mod=1e9+;
int dfn[N];
int low[N];
int vis[N];
int head[N];
int cnt;
stack<int>st;
int belong[N];
struct node{
int to,next;
}edge[N<<];
int num[N];
int t,tot;
int in[N];
int out[N];
void init(){
memset(head,-,sizeof(head));
memset(low,,sizeof(low));
memset(belong,-,sizeof(belong));
memset(vis,,sizeof(vis));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(num,,sizeof(num));
tot=t=;
cnt=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void tarjan(int u){
low[u]=dfn[u]=++t;
vis[u]=;
st.push(u);
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(dfn[v]==){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
int vv;
cnt++;
do{
vv=st.top();
st.pop();
belong[vv]=cnt;
num[cnt]++;
vis[vv]=;
}while(vv!=u);
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=;i<=n;i++){
if(dfn[i]==)tarjan(i);
}
for(int i=;i<=n;i++){
for(int j=head[i];j!=-;j=edge[j].next){
int v=edge[j].to;
if(belong[i]!=belong[v]){
in[belong[v]]++;
out[belong[i]]++;
}
}
}
int flag=;
int ans=;
//cout<<cnt<<endl;
for(int i=;i<=cnt;i++){
if(out[i]==){
flag++;ans=ans+num[i];
}
}
if(flag==){
cout<<ans<<endl;
}
else{
cout<<<<endl;
}
}
}

poj 2186(tarjan+缩点)的更多相关文章

  1. POJ 2186 tarjan+缩点 基础题

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37111   Accepted: 15124 De ...

  2. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

  3. poj 2186 (强连通缩点)

    题意:有N只奶牛,奶牛有自己认为最受欢迎的奶牛.奶牛们的这种“认为”是单向可传递的,当A认为B最受欢迎(B不一定认为A最受欢迎),且B认为C最受欢迎时,A一定也认为C最受欢迎.现在给出M对这样的“认为 ...

  4. POJ 2672 Tarjan + 缩点 + 拓扑思想

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17383 ...

  5. POJ 3694 (tarjan缩点+LCA+并查集)

    好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...

  6. poj 2762(tarjan缩点+判断是否是单链)

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19234 ...

  7. poj 2186 强连通+缩点

    题意:有一群牛,求被所有牛都认可的牛的个数 每个连通分量建一个缩点,出度为零的缩点包含的点的个数即为要求值 如果有多个出度为零的,直接输出零,否则输出那唯一一个出度为零的缩点包含的点的个数 #incl ...

  8. poj 2186 tarjan求强连通分量

    蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...

  9. Countries in War (POJ 3114) Tarjan缩点+最短路

    题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间.   解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...

随机推荐

  1. Yii 2.0版本调试输出SQL语句

    项目是基于框架Yii 2.0开发的. 今天梳理一些数据统计功能代码的时候,想把当前运行的sql语句打印出来,然后放到navicat工具里面运行,并分析一下运行效率和调优方案,之前大部分时候都是写增加. ...

  2. MySQL主从配置详解

    一.mysql主从原理 1. 基本介绍 MySQL 内建的复制功能是构建大型,高性能应用程序的基础.将 MySQL 的 数亿分布到到多个系统上去,这种分步的机制,是通过将 MySQL 的某一台主机的数 ...

  3. 深入React技术栈之setState详解

    抛出问题 class Example extends Component { contructor () { super() this.state = { value: 0, index: 0 } } ...

  4. MySQL-----删

    删 **清空数据库内容** delete from 数据库表名; ----清内容,但是自增列的数不清. truncate table 数据库表名; --------自增列的数也清. **清除指定的行数 ...

  5. POJ-1274The Perfect Stall,二分匹配裸模板题

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23313   Accepted: 103 ...

  6. windows 下 iptables

    windows自带的防火墙就可以. 在命令行方式下输入netsh回车,再输入firewall回车,之后想干什么就干什么. 头一次见对图形化防火墙头晕的...

  7. noip模拟赛 仓库

    分析:非常像货车运输那道题.先求一下最大生成树.求完之后会发现并不好处理.通常这类求生成树的题目不会就分析kruscal算法的性质.每往最大生成树中加一条边,如果配重大于这条边权,那么这条边所连的两个 ...

  8. noip模拟赛 补兵

    分析:比较难想的一道dp题.要想补兵的数量最多,最后每个小兵的血量肯定是呈一个阶梯状的:i,i+1,i+2......i+k.那么记录一下每个血量i离它最近的小兵的血量是多少,记作cur[i].那么把 ...

  9. java中文乱码问题的处理方式

    URL访问java时. 注意: URL: 编码二次 URLEncoder.encode(URLEncoder.encode(searchKey, "utf-8"),"ut ...

  10. zoj3988 Prime Set

    思路不难想到二分图求个最大匹配P,若P>=K,则2*K即可,否则应为P*2+min(K-P,未匹配且有度数不为0的顶点个数s).但坑点在于有1的情况,所以如果直接建二分图去跑最大匹配会因为1的影 ...