POJ 2186 Popular Cows(强连通)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 30999 | Accepted: 12580 |
Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
struct Node
{
int to,next;
}edge1[N*],edge2[N*];
//edge1用来存原图G,edge2用来存GT,即逆图
//都是用邻接表来储存的图,
int head1[N];//原图的邻接表的头结点
int head2[N];//逆图的邻接表的头结点
int mark1[N],mark2[N];
int tot1,tot2;
int cnt1,cnt2,st[N],belong[N];
int num,setNum[N];
struct Edge
{
int l,r;
}e[N*];//储存边 void add(int a,int b)//添加边a->b,在原图和逆图都需要添加
{
edge1[tot1].to=b;edge1[tot1].next=head1[a];head1[a]=tot1++;//邻接表存的原图
edge2[tot2].to=a;edge2[tot2].next=head2[b];head2[b]=tot2++;//邻接表存的逆图
}
void DFS1(int x)//深度搜索原图
{
mark1[x]=;
for(int i=head1[x];i!=-;i=edge1[i].next)
if(!mark1[edge1[i].to]) DFS1(edge1[i].to);
st[cnt1++]=x;//st数组是按照完成时间从小到大排序的
}
void DFS2(int x)//深度搜索逆图
{
mark2[x]=;
num++;
belong[x]=cnt2;
for(int i=head2[x];i!=-;i=edge2[i].next)
if(!mark2[edge2[i].to]) DFS2(edge2[i].to);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot1=tot2=;
for(int i=;i<=n;i++)//初始化
{
head1[i]=head2[i]=-;
mark1[i]=mark2[i]=;
}
for(int i=;i<=m;i++)
{
int w,v;
scanf("%d%d",&w,&v);
e[i].l=w;e[i].r=v;//储存边
add(w,v);//建立邻接表
}
cnt1=cnt2=;
for(int i=;i<=n;i++)
{
if(!mark1[i])DFS1(i);
}
for(int i=cnt1-;i>=;i--)
{
if(!mark2[st[i]])
{
num=;
DFS2(st[i]);
setNum[cnt2++]=num;
}
}
int de[N];//计算出度
memset(de,,sizeof(de));
for(int i=;i<=m;i++)//计算各个DAG图的出度
{
if(belong[e[i].l]!=belong[e[i].r])//原图的边不属于同一连通分支
de[belong[e[i].l]]++;
} //计算DAG出度为0的个数
int cnt=,res;
for(int i=;i<cnt2;i++)
if(!de[i]){cnt++;res=i;}
if(cnt>) printf("0\n");
else printf("%d\n",setNum[res]);
}
return ;
}
kosaraju
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int n,m,cnt,tim,top,cut;
int head[N],dfn[N],low[N],stack1[N];
int num[N],du[N],vis[N];
struct man {
int to,next;
} edg[M];
void init() {
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(du,,sizeof(du));
cnt=;
tim=;
top=;
cut=;
}
void add(int u,int v) {
edg[cnt].to=v;
edg[cnt].next=head[u];
head[u]=cnt;
cnt++;
}
void dfs(int u,int fa) {
dfn[u]=tim;
low[u]=tim++;
vis[u]=;
stack1[top++]=u;
for(int i=head[u]; i!=-; i=edg[i].next) {
int v=edg[i].to;
if(!vis[v]) {
dfs(v,u);
low[u]=min(low[u],low[v]);
} else low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]) {
cut++;
while(top>&&stack1[top]!=u) {
top--;
vis[stack1[top]]=;
num[stack1[top]]=cut;
}
}
}
int main() {
int u,v;
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=; i<m; i++) {
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=; i<=n; i++) {
if(!vis[i])dfs(i,);
}
for(int i=; i<=n; i++) {
for(int j=head[i]; j!=-; j=edg[j].next) {
if(num[i]!=num[edg[j].to])du[num[i]]++;
}
}
int sum=,x;
for(int i=; i<=cut; i++) {
if(!du[i])sum++,x=i;
}
if(sum==) {
sum=;
for(int i=; i<=n; i++) {
if(num[i]==x)sum++;
}
cout<<sum<<endl;
} else puts("");
}
return ;
}
POJ 2186 Popular Cows(强连通)的更多相关文章
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- POJ 2186 Popular Cows --强连通分量
题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...
- POJ 2186 Popular Cows 强连通分量模板
题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...
- 强连通分量分解 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 【强连通分量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: 31815 Accepted: 12927 De ...
随机推荐
- android textview 跑马灯
<TextView android:layout_width="match_parent" android:layout_height="48dp" an ...
- 将table导出为Excel的标准无乱码写法
导出为Excel有很多种写法,对于一些复杂的格式,笔者喜欢在后台先拼成一个<table>,再使用Response输出. 如果数据中包含中文或者一些特殊字符,可很多不规范的写法都会导致页面乱 ...
- [rfc3261]sip - via header
在很多情况下,sip并非直达目标主机的,而是要经过很多中间节点服务器.在request消息中,via头域表示当前已走过的节点(每经过一个节点,添加一个via头):在response消息中,via头域表 ...
- PHP四舍五入精确小数位及取整
php中取小数位的函数有sprintf,ceil,floor,round等等函数来实现四舍五入,下面我们就一起来看看具体的实例吧. 本篇文章将使用php对数字进行四舍五入保留N位小数,以及使用 ...
- @font-face usage
If you haven’t been living in a cave for the past few months, you will have heard lots of talk about ...
- 数据结构 《6》----堆 ( Heap )
Practival Problems: a. Construct a Huffman code b. Compute the sum of a large set of floating point ...
- Android与服务器http连接模块代码
package com.example.httpdemo2; import java.io.BufferedReader; import java.io.IOException; import jav ...
- WinFrm窗体的传值方式
比较简单的方法: 一:1.定义两个窗体 2.在父窗体中加入子窗体的属性 public ChildFrm ChildFrm { get; set; } 3.加载的时候: private void Par ...
- Bash简介
Bash(GNU bourne-Again Shell)是一个为GNU计划编写的Unix shell,它是很多Linux平台默认的使用的shell. shell是一个命令解析器,是介于操作系统内核与用 ...
- java作业3
Java字段初始化的规律: 静态初始化生成实例之后(就是new之后)变成你赋给它的值 ,先执行静态初始化,如果没有实例化,按照初始化块和构造方法在程序中出现的顺序执行. 当多个类之间有继承关系时,创建 ...