zoukankan      html  css  js  c++  java
  • POJ2186-Tarjan-kosaraju-缩点

    (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

    目录

    题意:传送门

     原题目描述在最下面。
     A认为B优秀,B认为C优秀,则A认为C优秀。问有多少个人被其他所有人认为优秀。

    思路:

    缩点后,求出度为0的连通分量。当且仅当只有一个连通分量出度为0时输出解,否则输出0.

    AC代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #include<cctype>
    #include<string>
    #include<cmath>
    #include<bitset>
    #include<cassert>
    #define mme(a,b) memset((a),(b),sizeof((a)))
    #define fuck(x) cout<<"* "<<x<<"
    "
    #define all(x) (x).begin(),(x).end()
    #define iis std::ios::sync_with_stdio(false)
    using namespace std;
    typedef long long LL;
    const int N = 1e4+5;
    const int M = 1e7+5;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9 + 7;
    int n, m;
    struct lp{
      int v, nex;
    }cw[N*20];
    int head[N],tot;
    int dfn[N],low[N],vis[N],inde;
    int qltNum,qltId[N];
    vector<int> scc[N];
    int stak[N*20],top;
    int out[N];
    void dfs(int u,int Fa){
      dfn[u]=low[u]=++inde;
      vis[u]=1;stak[++top]=u;
      for(int i=head[u];~i;i=cw[i].nex){
        int v = cw[i].v;
        //if(v==Fa)continue;
        if(!dfn[v]){
          dfs(v,u);
          low[u]=min(low[u],low[v]);
        }else if(vis[v]==1) low[u]=min(low[u],dfn[v]);
      }
      if(dfn[u]==low[u]){
        qltNum++;
        int v;
        do{
          v=stak[top--];
          vis[v]=2;
          qltId[v]=qltNum;
        }while(v!=u);
      }
    }
    void tarjan(){
      for(int i=1;i<=n;++i){
        if(!dfn[i])dfs(i,-1);
      }
    }
    inline void work(){
      for(int i=1;i<=n;++i){
        for(int j=head[i];~j;j=cw[j].nex){
          if(qltId[i]!=qltId[cw[j].v]){
            out[qltId[i]]++;
          }
        }
      }
      int num=0,p=1;
      for(int i=1;i<=qltNum;++i){
        if(out[i]==0){
          num++;p=i;
        }
      }
      if(num>1||num==0)printf("0
    ");
      else{
        int ans=0;
        for(int i=1;i<=n;++i){
          if(qltId[i]==p)ans++;
        }
        printf("%d
    ", ans);
      }
    }
    inline void add(int u,int v){
      cw[++tot].v=v;cw[tot].nex=head[u];
      head[u]=tot;
    }
    inline void init(){
      mme(head,-1);mme(dfn,0);mme(low,0);mme(vis,0);
      for(int i=1;i<=n;++i)scc[i].clear();
      qltNum=inde=top=0;
      tot=-1;
    }
    inline void read(){
      for(int i=0,u,v;i<m;++i){
        scanf("%d%d",&u,&v);
        add(u,v);
      }
    }
    int main(){
      while(~scanf("%d%d", &n,&m)){
        init();
        read();
        tarjan();
        work();
      }
      return 0;
    }
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<set>
    #include<string>
    #include<cmath>
    #include<bitset>
    #define mme(a,b) memset((a),(b),sizeof((a)))
    #define precision(x,d) cout<<fixed<<setprecision(d)<<x<<"
    "
    #define iis std::ios::sync_with_stdio(false)
    #define lowbit(x) (x&(-x))
    using namespace std;
    typedef long long LL;
    typedef unsigned long long uLL;
    const int N = 10005;
    const int M = 490005;
    const int INF = 0x3f3f3f3f;
    int n,m,tot;
    vector<int> G[N],GT[N];
    int post[N],vis[N],inde;
    int qltNum,qltId[N];
    int out[N];
    void init(){
      for(int i=1;i<=n;++i){
        G[i].clear();GT[i].clear();
      }
      inde=qltNum=0;
      mme(vis,0);mme(out,0);
    }
    void dfs1(int u){
      int len = G[u].size();
      vis[u]=1;
      for(int i=0;i<len;++i){
        int v = G[u][i];
        if(!vis[v])dfs1(v);
      }
      post[++inde]=u;
    }
    void dfs2(int u){
      int len = GT[u].size();
      vis[u]=0;qltId[u]=qltNum;
      for(int i=0;i<len;++i){
        int v = GT[u][i];
        if(vis[v])dfs2(v);
      }
    }
    void kosaraju(){
      for(int i=1;i<=n;++i){
        if(!vis[i])dfs1(i);
      }
      for(int i=inde;i>0;--i){
        if(vis[post[i]]){
          qltNum++;
          dfs2(post[i]);
        }
      }
      for(int i=1;i<=n;++i){
        int len = G[i].size();
        for(int j=0;j<len;++j){
          int v = G[i][j];
          if(qltId[i]!=qltId[v]){
            out[qltId[i]]++;
          }
        }
      }
      int cnt=0,ans=-1;
      for(int i=1;i<=qltNum;++i){
        if(out[i]==0){
          cnt++;ans=i;
        }
      }
      if(cnt!=1)printf("0
    ");
      else{
        int sum=0;
        for(int i=1;i<=n;++i){
          if(ans==qltId[i]){
            sum++;
          }
        }
        printf("%d
    ", sum);
      }
    }
    int main(){
      while(~scanf("%d%d",&n,&m)){
        init();
        for(int i=0,u,v;i<m;++i){
          scanf("%d%d",&u,&v);
          G[u].push_back(v);
          GT[v].push_back(u);
        }
        kosaraju();
      }
      return 0;
    }
    

    ####原题目描述: Description

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

    • 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.
      Output

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

    3 3
    1 2
    2 1
    2 3
    Sample Output

    1
    Hint

    Cow 3 is the only cow of high popularity.
    Source

    USACO 2003 Fall

  • 相关阅读:
    音乐情感识别
    SoftmaxWithLoss函数和师兄给的loss有哪些区别呢
    内积层和全连接层是一样的
    caffe中的Local Response Normalization (LRN)有什么用,和激活函数区别
    caffe官网的部分翻译及NG的教程
    couldn't import dot_parser
    apt-get -f install
    Spring常用注解总结 hibernate注解
    Set Map List Iterator
    iframe 与frameset
  • 原文地址:https://www.cnblogs.com/Cwolf9/p/9417735.html
Copyright © 2011-2022 走看看