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 ...
随机推荐
- 自定义cursor
cursor: url('绝对路径/big.cur'),auto; //通用方式
- 使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout
贴出布局代码: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" and ...
- main函数参数的使用
int main(int argc, char * argv[]) argc: argument count argv:argument vector 其中, char * argv[] 指针数组 c ...
- 类似github的框架
github是程序员经常上的网站,但如果是在一家苦逼不能访问外网的公司,那不能把自己的代码托管在github上绝对是一件非常痛苦的事情.如果想要在公司内网也可以用github托管自己的代码,那就要自己 ...
- linux 用户、组,修改文件权限
文件权限 -rwxrw-r‐-1 root root 1213 Feb 2 09:39 abc - 10个字符确定不同用户能对文件干什么 - 第一个字符代表文件(-).目录(d),链接(l) - 其余 ...
- android studio只能全部提示设置
- 2013年9月份第1周51Aspx源码发布详情
大型B2B家具门户网源码 2013-9-6 [VS2008]功能描述: 1.门户信息管理 安全取数据即使数据库连接中断不会报错 2.稳定性 每句代码经过3次以上检查.此网站还在运营3年了,没有出过问 ...
- C++ Primer---- 奇怪的 protected 成员
protected 成员在 C++ Primer 第四版中有如下描述: 可以认为 protected 标号是 private 和 public 的混合: 1. 像 private 成员一样, prot ...
- Design Patterns---- Strategy 模式
设计模式:可复用面向对象软件的基础 书中对 Strategy 模式的定义如下: 定义了一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法可独立于它的用户而变化. 案例:设计一个商 ...
- Git ~ 添加远程仓库 ~Git
现在的情景是 , 你已经在本地创建了一个Git仓库后 , 又想在 Github 创建一个Git 仓库并且让这两个仓库进行远程同步 , 这样Github 上的仓库既可以作为备份 ,有可以让其他人通过仓库 ...