• poj 2186


    poj 2186
    tarjan缩点板子题
    缩完点之后是有向无环图,要想其他点都能到达,那么它的出度为0,不然随意连就会出现环,如果出度为0的点的个数不是1,肯定就凉了。

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #include<ctime>
    #include<set>
    #include<map>
    #include<stack>
    #include<cstring>
    #define inf 2147483647
    #define ls rt<<1
    #define rs rt<<1|1
    #define lson ls,nl,mid,l,r
    #define rson rs,mid+1,nr,l,r
    #define N 100010
    #define For(i,a,b) for(int i=a;i<=b;i++)
    #define p(a) putchar(a)
    #define g() getchar()
    
    using namespace std;
    int n,m,x,y,now;
    int dfn[10010],low[10010],ans[10010];
    int cnt,col,c[10010],d[10010];
    bool vis[10010];
    struct node{
        int n;
        node *next;
    }*e[500100];
    
    stack<int>s;
    
    void in(int &x){
        int y=1;
        char c=g();x=0;
        while(c<'0'||c>'9'){
            if(c=='-')y=-1;
            c=g();
        }
        while(c<='9'&&c>='0'){
            x=(x<<1)+(x<<3)+c-'0';c=g();
        }
        x*=y;
    }
    void o(int x){
        if(x<0){
            p('-');
            x=-x;
        }
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void push(int x,int y){
        node *p;
        p=new node();
        p->n=y;
        if(e[x]==NULL)
            e[x]=p;
        else{
            p->next=e[x]->next;
            e[x]->next=p;
        }
    }
    
    void tarjan(int x){
        dfn[x]=low[x]=++cnt;
        vis[x]=true;
        s.push(x);
        for(node *i=e[x];i;i=i->next){
            if(!dfn[i->n]){
                tarjan(i->n);
                low[x]=min(low[x],low[i->n]);
            }
            else
                if(vis[i->n])
                    low[x]=min(low[x],dfn[i->n]);
        }
    
        if(low[x]==dfn[x]){
            col++;
            do{
                ans[col]++;
                now=s.top();
                c[now]=col;
                s.pop();
                vis[now]=0;
            }while(x!=now);
        }
    }
    
    void dfs(int x){
        vis[x]=true;
        for(node *i=e[x];i;i=i->next){
            if(!vis[i->n]){
                if(c[i->n]!=c[x])
                    d[c[x]]++;
                dfs(i->n);
            }
        }
    }
    
    int main(){
        in(n);in(m);
        For(i,1,m){
            in(x);in(y);
            push(x,y);
        }
        For(i,1,n)
            if(!dfn[i])
                tarjan(i);
    
        For(i,1,n)vis[i]=false;    
    
        For(i,1,n)
            if(!vis[i])
                dfs(i);
        cnt=0;
        For(i,1,col){
            if(d[i]==0){
                x=i;
                cnt++;
            }
            //o(d[i]);p('
    ');
        }
        if(cnt==1)
            o(ans[x]);
        else
            o(0);
        return 0;
    }

    //这个题的代码写得很丑23333333

  • 相关阅读:
    看到关于java资料比较全的,自己收藏
    ie6下pnghack——css方案
    git基本操作
    购物车功能实现
    jquery学习:获取位置position(),offset(),scrollTop困惑
    Datax3.0使用说明
    Scala字符串操作
    Scala中class、object、case class、case object区别
    Scala构造函数
    scala数组操作
  • 原文地址:https://www.cnblogs.com/war1111/p/10342413.html
Copyright © 2020-2023  润新知