链接:http://poj.org/problem?id=2186

题意:给你N个点,然后在给你N条有向边,然后让你找出这样的点S,S满足条件图上任意一点都能到达S。

要想满足任意一点都能到达,首先满足图连通,然后满足将图缩点后形成一个棵树,树的特征是可以有且只有一个点的出度为0。

然后代码如下:

 #include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stack> using namespace std;
const int maxn = ; struct edge
{
int u,v,val;
};
int dfn[],low[],belong[],inst[];
vector<edge> edges;
vector<int>g[maxn];
stack<int>st;
int bcnt,cnt,time; void init(int n)
{
int i;
for(i =;i <= n;i++)
g[i].clear(); edges.clear();
time = ;bcnt = cnt = ;
return ;
}
void addedge(int u,int v,int val)
{
edges.push_back((edge){u,v,});
g[u].push_back(cnt);
cnt++; return ;
}
void tarjan(int i)
{
int j;
dfn[i] = low[i] = ++time;
inst[i] = ;
st.push(i); for(j = ;j < g[i].size();j++)
{
edge e;
e = edges[g[i][j]];
int v;
v = e.v;
if(!dfn[v])
{
tarjan(v);
low[i] = min(low[i],low[v]);
}
else if(inst[v] && dfn[v] < low[i])
low[i] = dfn[v];
}
if(dfn[i] == low[i])
{
//cout<<i<<"****"<<endl;
bcnt++;
do
{
j = st.top();
st.pop();
inst[j] = ;
belong[j] = bcnt;
//printf("%d *",j);
}
while(j != i);
//puts("");
} }
void tarjans(int n)
{
int i;
bcnt = time = ;
while(!st.empty())st.pop();
memset(dfn,,sizeof(dfn)); memset(inst,,sizeof(inst));
memset(belong,,sizeof(belong));
for(i = ;i <= n;i++)
if(!dfn[i])tarjan(i);
}
int main()
{
int n,m;
//freopen("in.txt","r",stdin);
while(~scanf("%d %d",&n,&m))
{
int a,b,i;
init(n);
for(i = ;i < m;i++)
{
scanf("%d %d",&a,&b);
addedge(a,b,); }
tarjans(n);
//cout<<bcnt<<endl;
int leap = ,flag = ;
int bcnt0,j;
int outbc[];
memset(outbc,,sizeof(outbc));
for(i = ;i <= n;i++)
{
for(j = ;j < g[i].size();j++)
{
int v;
v = edges[g[i][j]].v;
if(belong[i] != belong[v]){
if(outbc[belong[i]] == )
leap++;
outbc[belong[i]] = ;
}
}
}
if(leap == bcnt-)
{
int ans;
ans = ;
for(i = ;i < bcnt+;i++)
{
if(outbc[i] == )
break;
}
for(j = ;j <= n;j++)
if(belong[j] == i)
ans++; printf("%d\n",ans);
}
else
puts(""); }
return ;
}

POJ2186 POPULAR COW的更多相关文章

  1. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

  2. POJ2186 Popular Cows

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  3. POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Des ...

  4. 强连通分量tarjan缩点——POJ2186 Popular Cows

    这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...

  5. 【Tarjan缩点】POJ2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35644   Accepted: 14532 De ...

  6. poj2186 Popular Cows 题解——S.B.S.

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29642   Accepted: 11996 De ...

  7. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

  8. POJ2186 Popular Cows 强连通分量tarjan

    做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的A->B ...

  9. POJ-2186 Popular Cows,tarjan缩点找出度为0的点。

    Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...

随机推荐

  1. xml string 相互转换

    一.使用最原始的javax.xml.parsers,标准的jdk api // 字符串转XMLString xmlStr = /"....../";StringReader sr ...

  2. 翻译 - 元编程动态方法之public_send

    李哲 - MAY 20, 2015 原文地址:Metaprogramming Dynamic Methods: Using Public_send 作者:Friends of The Web的开发者V ...

  3. php laravel 安装

    windows环境尝试学习一下laravel 1.因为SAE的php版本为5.3,因此最高只能支持到Laravel4.1.x.(Laravel4.2用到了php5.4的trait特性) 以4.1为主. ...

  4. 【linux】文字提取

    提取IP地址: 方法①: ifconfig eth3|grep Bcast|cut -d ":" -f2|cut -d " " -f1 ifconfig: 显示 ...

  5. POJ 1995

    #include <iostream> using namespace std; long long power(long long a, long long b, long long m ...

  6. java基础知识回顾之javaIO类---BufferedReader和BufferedWriter

    使用了装饰设计模式:此类的设计是为了提高流操作数据的效率.思想就是定义容器将数据进行临时存储,对于缓冲区对象,其实就是将这个容器进行了分装,并提供了更高效的操作方法. BufferReader: pa ...

  7. C#反射技术的相关使用方法

    1.获取同一程序集的类型实例 无参数构造函数 Type t=Type.GetType("AppCode.Employee"); object emp=t.Assembly.Crea ...

  8. 关于playmaker play animation出现警告 The AnimationClip 'xxx' used by the Animati ...

    转载自网络: 出现这个提示: The AnimationClip 'xxx' used by the Animation component 'xxx' must be marked as Legac ...

  9. NIM博弈的必胜取法

    #include<stdio.h> ; int a[Max]; int main() { int i,n,ans; int tmp; while(scanf("%d", ...

  10. windows条件下,Ping加上时间戳,并保存到文件,适用于测试网络

    在c盘下面新建文件 ping.vbs 在 ping.vbs中输入代码如下: Dim args, flag, unsuccOut args="" otherout="&qu ...