Popular Cows

传送门(poj):http://poj.org/problem?id=2186

(bzoj):http://www.lydsy.com/JudgeOnline/problem.php?id=1051

Popular Cows

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 33482   Accepted: 13638

Description

Every cow‘s dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
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

* Line 1: Two space-separated integers, N and M

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow.

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity.

Source

USACO 2003 Fall

【解析】

呵呵哒。在poj上测怎么都是WA,半天错误都没找出来。数组我又开大点也没用。网友提供的十多组数据我都A啊。然后我就去bzoj上测的A了,100ms.

所以下面的代码poj上是过不了的。哼poj浪费我时间。上面两个传送门。

tarjan求强连通分量+缩点。

求出缩点后出度为0的点的个数,如果个数是1,输出这个连通块的牛的个数。否则不存在被所有牛都喜欢的牛。

我欢迎你,你欢迎他,我就欢迎他。

如果有S头牛互相喜欢,如果有一头牛喜欢S头牛中的任意一头,那么那头牛就喜欢这S头牛。

将互相喜欢的牛缩成一个点,如果某一个点的出度为0,那么所有的牛都喜欢它。注意是结果是连通块里牛的个数。

【代码】

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define N 10009
struct Edge
{
    int x,y,next;
    Edge(int x=0,int y=0,int next=0):
        x(x),y(y),next(next){}
}edge[N*5];
int sumedge,n,m,x,y,tim,top,sumclr,sumedge2,js,ans;
int head[N],dfn[N],low[N],Stack[N],color[N],cnt[N],out[N],head2[N];
bool vis[N],instack[N];
void ins(int x,int y)
{
    edge[++sumedge]=Edge(x,y,head[x]);
    head[x]=sumedge;
}
void ins2(int x,int y)
{
    edge[++sumedge2]=Edge(x,y,head2[x]);
    head2[x]=sumedge2;
}
map<int,bool>Map[N];
void tarjan(int x)
{
    dfn[x]=low[x]=++tim;
    vis[x]=1;instack[x]=1;Stack[++top]=x;
    for(int u=head[x];u;u=edge[u].next)
        if(instack[edge[u].y])
        low[x]=min(low[x],dfn[edge[u].y]);
        else
        if(!vis[edge[u].y])
        {
            tarjan(edge[u].y);
            low[x]=min(low[x],low[edge[u].y]);
        }
        else
        {
        }
        if(dfn[x]==low[x])
        {
            sumclr++;
            color[x]=sumclr;
            cnt[sumclr]++;
            while(Stack[top]!=x)
            {
                color[Stack[top]]=sumclr;//染色
                instack[Stack[top]]=0;
                top--;
                cnt[sumclr]++;//记录这个连通块里牛的个数
            }
            instack[x]=0;
            top--;
        }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        ins(x,y);
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])tarjan(i);
        top=0;
    }
    for(int i=1;i<=n;i++)
        for(int u=head[i];u;u=edge[u].next)
            if(color[i]!=color[edge[u].y])
             if(Map[color[i]].find(color[edge[u].y])==Map[color[i]].end())    //缩点
            {
                Map[color[i]][color[edge[u].y]]=true;
                ins2(color[i],color[edge[u].y]);
                out[color[i]]++;
            }
            int cnnt=0;
    for(int i=1;i<=sumclr;i++)
    {
        if(out[i]==0)//度为0意味着所有牛都欢迎他
        {
            js++;
            ans=i;
            cnnt+=cnt[i];//加上这个连通块里牛的个数
        }
    }
    if(js>1||js==0)puts("0");//如果有大于1个的度为0的点,说明这个图不是连通的。
    else
    printf("%d",cnnt);
    return 0;
}
时间: 2024-12-14 18:16:29

Popular Cows的相关文章

POJ 2186 Popular Cows(Targin缩点)

传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 12921 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <=

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

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M &l

【图论】Popular Cows

[POJ2186]Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34752   Accepted: 14155 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1

poj2186 Popular Cows --- 强连通

给一个有向图,问有多少结点是其他所有结点都可以到达的. 等价于,在一个有向无环图上,找出度为0 的结点,如果出度为0的结点只有一个,那么这个就是答案,如果大于1个,则答案是0. 这题有环,所以先缩点.求唯一出度为0的强连通分量. #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<iostream> #define inf 0x3f3f3f3f

POJ2186 Popular Cows【Kosaraju】【强连通分量】

Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24266Accepted: 9954 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 5

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

这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定意味着v可达u.    相互可达则属于同一个强连通分量    (Strongly Connected Component, SCC) §有向图和它的转置的强连通分量相同 §所有SCC构成一个DAG(有向无环图) dfn[u]为节点u搜索的次序编号(时间戳),即首次访问u的时间 low[u]为u或u的

POJ - 2186 - Popular Cows (tarjan)

Popular Cows 题目传送:Popular Cows 思路:tarjan算法求强连通分量 AC代码: #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <strin

POJ 2186 Popular Cows

Popular Cows Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2186 64-bit integer IO format: %lld      Java class name: Main Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <

POJ 2186 Popular Cows (强联通)

id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23819   Accepted: 9767 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <=