BZOJ 1051: [HAOI2006]受欢迎的牛 缩点

1051: [HAOI2006]受欢迎的牛

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

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

Description

每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头牛被所有的牛认为是受欢迎的。

Under two situations the player could score one point.

⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

⋅2. Ignoring the buoys and relying on dogfighting to get point. If you and your opponent meet in the same position, you can try to fight with your opponent to score one point. For the proposal of game balance, two players are not allowed to fight before buoy #2 is touched by anybody.

There are three types of players.

Speeder: As a player specializing in high speed movement, he/she tries to avoid dogfighting while attempting to gain points by touching buoys.
Fighter: As a player specializing in dogfighting, he/she always tries to fight with the opponent to score points. Since a fighter is slower than a speeder, it's difficult for him/her to score points by touching buoys when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.

There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting. Since Asuka is slower than Shion, she decides to fight with Shion for only one time during the match. It is also assumed that if Asuka and Shion touch the buoy in the same time, the point will be given to Asuka and Asuka could also fight with Shion at the buoy. We assume that in such scenario, the dogfighting must happen after the buoy is touched by Asuka or Shion.

The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

Input

第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可能出现多个A,B)

 

Output

一个数,即有多少头牛被所有的牛认为是受欢迎的。

 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

HINT

 

题意

 

题解:

缩点,然后看出度为0 的点是否只有一个,如果只有一个就输出这个点的大小

否则就是0

代码

 

#include<iostream>
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
#define maxn 10005
vector<int> E[maxn];
vector<int> rE[maxn];
int belong[maxn];
int vis[maxn];
int cl[maxn];
int cnt = 0;
int siz[maxn];
int kis[maxn];
void dfs1(int x)
{
    vis[x]=1;
    for(int i=0;i<E[x].size();i++)
    {
        int e = E[x][i];
        if(vis[e])continue;
        dfs1(e);
    }
    cl[++cnt]=x;
}
void dfs2(int x)
{
    vis[x]=1;
    belong[x]=cnt;
    siz[cnt]++;
    for(int i=0;i<rE[x].size();i++)
    {
        int e = rE[x][i];
        if(vis[e])continue;
        dfs2(e);
    }
}
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        E[x].push_back(y);
        rE[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
            dfs1(i);
    }
    memset(vis,0,sizeof(vis));
    cnt = 0;
    for(int i=n;i>=1;i--)
    {
        if(!vis[cl[i]])
        {
            cnt++;
            dfs2(cl[i]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<E[i].size();j++)
        {
            if(belong[i]==belong[E[i][j]])
                continue;
            kis[belong[i]]++;
        }
    }
    int ans1=0,ans2=0;
    for(int i=1;i<=cnt;i++)
    {
        if(kis[i]==0)
        {
            ans1++;
            ans2=i;
        }
    }
    if(ans1==1)
        printf("%d\n",siz[ans2]);
    else
        puts("0");
}

 

posted @ 2015-11-05 21:59  qscqesze  阅读(223)  评论(0编辑  收藏  举报