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.
    



  • 相关阅读:
    mybatis-plus解析
    ybatis中查询出多个以key,value的属性记录,封装成一个map返回的方法
    mybatis-plus分页记坑
    ComponentScan注解的使用
    fastJson序列化
    SpringBoot-RestTemplate测试Controller
    configparser模块
    python 将乱码转为汉字
    1.x 版本Django对应rest_framework版本
    docker容器内执行linux的dmidecode命令
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319688.html
Copyright © 2011-2022 走看看