zoukankan      html  css  js  c++  java
  • Popular Cows poj 2186 tarjan

    题意/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. 

     

    题解/solution

            先跑一遍taijian算法。那么出度为0的强连通分量代表的就是受其他奶牛欢迎的,但是如果出度为0的强连通分量的个数大于1.那么则无解。因为将至少有两个分量里的奶牛互相不喜欢。所以我们的算法就是如果出度为0的强连通分量的个数是1.那么我们算出这里面点的个数就是最后的答案。

     

    代码/Code

    const
      maxE=500001;
      maxV=50001;
    
    type
      arr=record
            x,y,w,next:longint;
          end;
    Var
      n,m,t,ans,t1:longint;
      tu:array [0..maxE] of arr;
      v:array [0..maxV] of Boolean;
      ls,a,ru,cu,low,dfn,f:array [0..maxV] of longint;
    procedure dfs(o:longint);
    Var
      i,j,k:longint;
    begin
      inc(t);
      f[t]:=o;
      v[o]:=true;
      inc(t1);
      low[o]:=t1;
      dfn[o]:=t1;
      i:=ls[o];
      while i<>0 do
        with tu[i] do
          begin
            if dfn[y]=0 then
              begin
                dfs(y);
                if low[o]>low[y] then low[o]:=low[y];
              end else
                if (low[o]>dfn[y]) and (v[y]) then low[o]:=dfn[y];
            i:=next;
          end;
      if low[o]=dfn[o]then
        begin
          ans:=ans+1;
          repeat
            j:=f[t];
            t:=t-1;
            a[j]:=ans;
            v[j]:=false;
          until j=o;
        end;
    end;
    
    procedure init;
    var
      i,k:longint;
    begin
      readln(n,m);
      k:=0;
      ans:=0;
      for i:=1 to m do
        with tu[i] do
          begin
            readln(x,y);
            next:=ls[x];
            ls[x]:=i;
          end;
      for i:=1 to n do
        if a[i]=0 then dfs(i);
    end;
    
    procedure main;
    var
      i,j:longint;
    begin
      for i:=1 to n do
        begin
          j:=ls[i];
          while j<>0 do
            with tu[j] do
              begin
                if a[x]<>a[y]
                  then
                    begin
                      cu[a[x]]:=cu[a[x]]+1;
                      ru[a[y]]:=ru[a[y]]+1;
                    end;
                j:=next;
              end;
        end;
    end;
    
    procedure print;
    var
      i,o,k:longint;
    begin
      k:=0;
      for i:=1 to ans do
        if cu[i]=0 then k:=k+1;
      if k<>1 then
        begin
          write(0);
          halt;
        end;
      k:=0;
      for i:=1 to ans do
        if cu[i]=0 then k:=i;
      o:=0;
      for i:=1 to n do
        if a[i]=k then o:=o+1;
      write(o);
    end;
    
    begin
      init;
      main;
      print;
    end.
    



  • 相关阅读:
    总结6.6 PHP后台登录和文件上传
    【017期】JavaSE面试题(十七):JVM之内存模型
    【016期】JavaSE面试题(十六):反射
    【015期】JavaSE面试题(十五):网络IO流
    【014期】JavaSE面试题(十四):基本IO流
    【013期】JavaSE面试题(十三):多线程(3)
    【012期】JavaSE面试题(十二):多线程(2)
    【011期】JavaSE面试题(十一):多线程(1)
    【010期】JavaSE面试题(十):集合之Map
    【009期】JavaSE面试题(九):集合之Set
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319688.html
Copyright © 2011-2022 走看看