• [HAOI2006]受欢迎的牛


    题目描述

    每头奶牛都梦想成为牛棚里的明星。被所有奶牛喜欢的奶牛就是一头明星奶牛。所有奶

    牛都是自恋狂,每头奶牛总是喜欢自己的。奶牛之间的“喜欢”是可以传递的——如果A喜

    欢B,B喜欢C,那么A也喜欢C。牛栏里共有N 头奶牛,给定一些奶牛之间的爱慕关系,请你

    算出有多少头奶牛可以当明星。

    输入输出格式

    输入格式:

     第一行:两个用空格分开的整数:N和M

     第二行到第M + 1行:每行两个用空格分开的整数:A和B,表示A喜欢B

    输出格式:

     第一行:单独一个整数,表示明星奶牛的数量

    输入输出样例

    输入样例#1:
    3 3
    1 2
    2 1
    2 3
    输出样例#1:
    1

    说明

    只有 3 号奶牛可以做明星

    【数据范围】

    10%的数据N<=20, M<=50

    30%的数据N<=1000,M<=20000

    70%的数据N<=5000,M<=50000

    100%的数据N<=10000,M<=50000

    思路:Tarjan+缩点

    代码实现:

     1 #include<cstdio>
     2 #include<cstring>
     3 const int maxn=1e4+10;
     4 const int maxm=1e5+10;
     5 inline int min_(int x,int y){return x<y?x:y;}
     6 int n,m,pro,ans;
     7 int a,b;
     8 int h[maxn],hs;
     9 int e_q[maxm],e_z[maxm],e_n[maxm];
    10 int dn[maxn],fl[maxn],st[maxn],dns,top;
    11 int color[maxn],num[maxn],cd[maxn],ld[maxn],cs;
    12 bool v[maxn],map[maxn][maxn];
    13 void tarjan(int k){
    14     dn[k]=fl[k]=++dns;
    15     st[++top]=k,v[k]=true;
    16     for(int i=h[k];i;i=e_n[i]){
    17         if(!dn[e_z[i]]){
    18             tarjan(e_z[i]);
    19             fl[k]=min_(fl[k],fl[e_z[i]]);
    20         }
    21         if(v[e_z[i]]) fl[k]=min_(fl[k],dn[e_z[i]]);
    22     }
    23     if(dn[k]==fl[k]){
    24         cs++;
    25         while(st[top+1]!=k){
    26             color[st[top]]=cs;
    27             v[st[top--]]=0;
    28             num[cs]++;
    29         }
    30     }
    31 }
    32 int main(){
    33     freopen("cow.in","r",stdin);
    34     freopen("cow.out","w",stdout);
    35     scanf("%d%d",&n,&m);
    36     for(int i=1;i<=m;i++){
    37         scanf("%d%d",&a,&b);
    38         ++hs,e_q[hs]=a,e_z[hs]=b,e_n[hs]=h[a],h[a]=hs;
    39     }
    40     for(int i=1;i<=n;i++) if(!dn[i]) tarjan(i);
    41     for(int i=1;i<=m;i++) if(color[e_q[i]]!=color[e_z[i]]) ++cd[color[e_q[i]]];
    42     for(int i=1;i<=cs;i++) if(!cd[i]) ans=num[i],pro++;
    43     if(pro==1) printf("%d
    ",ans);
    44     else puts("0");
    45     return 0;
    46 }

    我竟然在递归函数里使用了全局过程变量,而且7A3T。。。然后,就坑的很惨了。

  • 相关阅读:
    hibernate常用配置
    hibernate快速入门
    【转】Struts2中json插件的使用
    【转】Struts2解决表单重复提交问题
    OGNL表示式使用和值栈
    Python就是为了方便生活,比如看VIP电影
    使用python进行面部合成,比PS好用多了
    Python黑科技,教你学会Django系统错误监控
    Python这么厉害的么?一次爬完整站小说
    Linux优化不知如何下手?那你的看看这篇文章了
  • 原文地址:https://www.cnblogs.com/J-william/p/6749944.html
Copyright © 2020-2023  润新知