POj 2186 Popular Cows[连通分量]
题目大意:给出N头牛,有M种关系u, v。代表u牛崇拜v牛。要求找出有多少头牛被所有牛崇拜着
题目链接:http://poj.org/problem?id=2186
解题思路:
1>求出强连通分量,标记每头牛属于哪个分量内
2>进行缩点,计算连通分量的个数x,给出的M组关系重新构图。u,v若属不于一个连通分量内out[belong[u]]++;
3>统计x个连通分量出度。如果超过1个的连通分量出度为0,证明两群牛互不崇拜;
如果等于1 答案就是该分量中牛的个数.
代码如下:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 10005
#define M 50005
struct Edge
{
int v, next;
}edge[M]; int node[N], stack[N], instack[N], dfn[N], out[N];
int low[N], belong[N], index, cnt_edge, n, m, cnt_tar, top;
int ee[M][]; void add_Edge(int u, int v)
{
edge[cnt_edge].next=node[u];
edge[cnt_edge].v=v;
node[u]=cnt_edge++;
}
void tarjan(int u)
{
int i, j, v;
dfn[u]=low[u]=++index;
stack[++top]=u;
instack[u]=;
for(i=node[u]; i!=-; i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u], low[v]);
}
else if(instack[v])
low[u]=min(low[u], dfn[v]);
}
if(dfn[u]==low[u])
{
cnt_tar++;
do
{
j=stack[top--];
instack[j]=;
belong[j]=cnt_tar;
}while(j!=u);
} }
void solve()
{
int i;
top=, index=, cnt_tar=;
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
for(i=; i<=n; i++)
if(!dfn[i])
tarjan(i);
}
int main()
{
int i, u, v;
while(scanf("%d%d", &n, &m)!=EOF)
{
cnt_edge=;
memset(node, -, sizeof(node));
for(i=; i<=m; i++)
{
scanf("%d%d", &u, &v);
ee[i][]=u, ee[i][]=v;
add_Edge(u, v);
}
solve();
for(i=; i<=m; i++)
{
int xx=belong[ee[i][]], yy=belong[ee[i][]];
if(xx!=yy)
out[xx]++;
}
int mark=, tot=;
for(i=; i<=cnt_tar; i++)
if(out[i]==)
{
tot++;
mark=i;
}
if(tot>)
printf("0\n");
else if(tot==)
{
tot=;
for(i=; i<=n; i++)
if(belong[i]==mark)
tot++;
printf("%d\n", tot);
}
}
return ;
}
POj 2186 Popular Cows[连通分量]的更多相关文章
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- POJ 2186 Popular Cows(强连通)
Popular Cows Time Limit: 2000MS Memo ...
随机推荐
- always block內省略else所代表的電路
真 OO无双 之 真乱舞书 寫程式是很快樂的一件事 Since Sep.15,2006 (筆記) always block內省略else所代表的電路 (SOC) (Verilog) Abstract在 ...
- 【转载】Git的安装与使用
Git的安装与使用 转载来源:http://www.cnblogs.com/Bonker/p/3441781.html 1,下载git https://code.google.com/p/msysg ...
- css扁平化博客学习总结(一)模块分析
一.模块分析 1.每开发一个项目之前,首先要对项目进行一个大致规划,它到底要做什么功能,它有什么具体需求. 2.所以需要进行模块化分析,把这些东西具象化,把一个问题模块化,对需求有一个宏观的了解. 3 ...
- ArrayUtils用法
/* 1. ArrayUtils.isEmpty(strs) : 判断数组是否为空 , 不为空返回false,为空true */ ArrayUtils.isEmpty(new String[] ...
- JobControl管理多job依赖完整示例
处理 复杂的要求的时候,有时一个mapreduce程序是完成不了的,往往需要多个mapreduce程序,这个时候就要牵扯到各个任务之间的依赖关系,所谓 依赖就是一个MR Job 的处理结果是另外的MR ...
- C#数据等待
1 public bool WaitData() 2 { 3 bool states = false; 4 try 5 { 6 DateTime Get_Time = DateTime.Now; 7 ...
- SQL SERVER 锁定的实例
---实例DB:AdventureWorks2014 --- 创建view DBLocks USE [AdventureWorks2014] GO /****** Object: View [dbo] ...
- Sophos UTM WebAdmin存在未明漏洞
...
- ASP.NET Web Service如何工作(3)
ASP.NET Web Service如何工作(3) [日期:2003-06-26] 来源:CSDN 作者:sunnyzhao(翻译) [字体:大 中 小] 为了使.asmx句柄有可能反串行化SOA ...
- ###STL学习--标准模板库
下面进行STL的学习.希望能了解标准模板库中的常用容器,迭代器,可以自由运用STL以提高编写代码的效率.下面的内容我想以知识点为总结,不再像<Effective C++>那样以章节进行总结 ...