zoukankan      html  css  js  c++  java
  • bzoj1051: [HAOI2006]受欢迎的牛

    题目传送门 又是一波复习题.....

    tarjan缩点后图就不存在环了 寻找每个环的出度 如果一个环被所有点间接或者直接指向并且这个环没有出度那么答案就是这个环上点的个数(当然也只能存在一个环)

    如果存在两个点没有出度 那么就不存在被所有牛喜欢的牛 答案就是0了

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int M=50007;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    int n,m,sum,first[M];
    int color[M],out[M],s[M],num,ans,last;
    int dfn[M],low[M],cnt,st[M],top,book[M];
    struct node{int to,next;}e[M];
    void insert(int a,int b){sum++; e[sum].to=b; e[sum].next=first[a]; first[a]=.sum;}
    void dfs(int x){
        dfn[x]=low[x]=++cnt;
        book[x]=1; 
        st[++top]=x;
        for(int i=first[x];i;i=e[i].next){
            int now=e[i].to;
            if(!dfn[now]){
                dfs(now);
                low[x]=min(low[x],low[now]);
            }
            if(book[now]) low[x]=min(low[x],dfn[now]);
        }
        if(low[x]==dfn[x]){
            s[++num]=1;
            book[x]=0; color[x]=num;
            while(st[top]!=x){
                int now=st[top--];
                book[now]=0;
                color[now]=num;
                s[num]++;
            }
            top--;
        }
    }
    int main()
    {
        int x,y;
        n=read(); m=read();
        for(int i=1;i<=m;i++) x=read(),y=read(),insert(x,y);
        for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i);
        for(int i=1;i<=n;i++)
            for(int j=first[i];j;j=e[j].next)
                if(color[e[j].to]!=color[i]){
                    out[color[i]]=1;
                    break;
                }
        for(int i=1;i<=num;i++) if(!out[i]){
            if(last){
                printf("0
    ");
                return 0;
            }
            last=i;
        }
        printf("%d
    ",s[last]);
        return 0;
    }
    View Code
  • 相关阅读:
    toFixed()与银行家舍入
    VScode链接服务器并配置公钥-SSH Keys
    改造@vue/cli项目为服务端渲染-ServerSideRender
    vue预渲染及其cdn配置
    界面优化--如何提升用户体验(Velocity.js和GSAP)
    eslint配置介绍-如何在uniapp中配置eslint
    babel 的介绍及其配置
    如何为我的VUE项目编写高效的单元测试--Jest
    計算幾何 學習
    Manacher
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7089313.html
Copyright © 2011-2022 走看看