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 ...
随机推荐
- RPC之Thrift学习实战
关于Thrift的学习实战请参考:http://blog.csdn.net/column/details/slimina-thrift.html
- CountDownLatch(闭锁)
一.闭锁(Latch) 闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态.通俗的讲就是,一个闭锁相当于一扇大门,在大门打开之前所有线程都被阻断,一旦大门打开所有线程都 ...
- linux commond
1 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2 ifconfig 3 ping 172.22.14.59 4 ping 1 ...
- 冒泡排序小实例 php
源代码如下,仅用于参考: <?php$a = array(10,2,36,14,10,25,23,85,99,45); for($j=0;$j<9;$j++){ for($i=0;$i&l ...
- Extjs3笔记 fbar
在项目中会遇到在grid右下角显示合计之类的显示. 之前使用过的方式: 1.用width强制顶过去. 2.利用css来控制. 弊端: 利用第一种方式:导致调整窗口大小时会导致合计不能根据窗体的大小变化 ...
- GWT环境搭建--eclipse
上面下来需求,需要用到GWT,以前没接触过,搭个开发环境研究研究 安装软件我放在百度云盘里了(其他版本自己找,我的版本 eclipse4.4 luna gwt2.7) 链接:http://pan.b ...
- linux时间自动同步
1,修正本地时区及ntp服务 #yum -y install ntp#rm -rf /etc/localtime#ln -s /usr/share/zoneinfo/Asia/Shanghai /et ...
- iOS开发——推送证书
(最近准备考试……空闲截图整理成博客)
- NOPI读取EXCEL
public void ReadEXCEL(string filePath) { IWorkbook wk = null; string extension = System.IO.Path.GetE ...
- 【Java咬文嚼字】关键字(一):super和this
这段时间一直在学Java,看了辣么多书以及博客,心痒也是着写写自己的学习心得. 这也算是新手篇:咬文嚼字Java中的关键字. 以关键字为第一篇博文也是考虑再三:1.本人基础也是薄弱 2.集跬步至千里 ...