暴躁蒟蒻在线水题er日记

暴躁蒟蒻在线水题er日记

就此生必不可能学懂环,反正populor cow也就一道模板题,套个模板我还是会的~

题目如下~

描述

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.

输入

    * 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.

输出

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

样例输入


    3 3
    1 2
    2 1
    2 3

样例输出

    1

参考代码

#include<bits/stdc++.h> 
using namespace std;
const int maxn =10001;
int n, m;
int cnt = 0, tnt = 0;
int lin, mm = 0;
int tar[maxn] = {0};
int num[maxn] = {0}, low[maxn] = {0}, dfn[maxn] = {0};
bool vis[maxn] = {false};
bool flag[maxn] = {false};
vector<int> edge1[maxn];
vector<int> zero;
stack<int> s;
set<int> ss[maxn];
 
inline void putit()
{
    int a, b;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; ++i)
    {
        scanf("%d%d", &a, &b); 
        edge1[a].push_back(b);
    }
}
 
void tarjan(int t)
{
    if(dfn[t] == 0)
    {
        tnt++;
        dfn[t] = tnt;
        low[t] = tnt;
        vis[t] = true;
        s.push(t);
    }
    for(int i = 0; i < edge1[t].size(); ++i)
    {
        int now = edge1[t][i];
        if(!vis[now])    tarjan(now);
        low[t] = min(low[now], low[t]);
    }
    if(low[t] == dfn[t])
    {
        cnt++;
        while(!s.empty())
        {
            int now = s.top(); s.pop();
            num[now] = cnt;
            tar[cnt]++;
            if(dfn[now] == low[now])    break;
        }
    }
}
 
 
 
inline void workk()
{
    for(int i = 1; i <= n; ++i)
    {
        for(int j = edge1[i].size() - 1; j >= 0; --j)
        {
            if(num[i] != num[edge1[i][j]])
            {
                flag[num[i]] = true;
                break;
            }
        }
    }
    for(int i = 1; i <= cnt; ++i)
    {
        if(!flag[i])
        {
            mm++;
            lin = i;
            if(mm == 2)
            {
                printf("0");
                exit(0);
            }
        }
    }
    printf("%d ", tar[lin]);
}
 
int main()
{
    putit();
    for(int  i = 1; i <= n; ++i)
        if(!vis[i]) tarjan(i);
    workk();
}

猜你喜欢

转载自blog.csdn.net/qq_42710153/article/details/88072201