暴躁蒟蒻在线水题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();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值