zoukankan      html  css  js  c++  java
  • 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom 题解

    每日一题 day11 打卡

    Analysis

    好久没大Tarjan了,练习练习模板。

    只要在Tarjan后扫一遍si数组看是否大于1就好了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define maxn 10000+10
     6 #define maxm 50000+10
     7 using namespace std;
     8 inline int read() 
     9 {
    10     int x=0;
    11     bool f=1;
    12     char c=getchar();
    13     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
    14     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
    15     if(f) return x;
    16     return 0-x;
    17 }
    18 inline void write(int x)
    19 {
    20     if(x<0){putchar('-');x=-x;}
    21     if(x>9)write(x/10);
    22     putchar(x%10+'0');
    23 }
    24 int n,m,cnt,num,top,col,ans;
    25 int head[maxm],dfn[maxn],low[maxn],co[maxn],st[maxn],si[maxn];
    26 struct node{int to,next;}edge[maxm];
    27 inline void add(int x,int y)
    28 {
    29     edge[++cnt].to=y;
    30     edge[cnt].next=head[x];
    31     head[x]=cnt;
    32 }
    33 inline void Tarjan(int u)
    34 {
    35     dfn[u]=low[u]=++num;
    36     st[++top]=u;
    37     for(int i=head[u];i;i=edge[i].next)
    38     {
    39         int v=edge[i].to;
    40         if(!dfn[v])
    41         {
    42             Tarjan(v);
    43             low[u]=min(low[u],low[v]);
    44         }
    45         else if(!co[v])
    46             low[u]=min(low[u],dfn[v]);
    47     }
    48     if(low[u]==dfn[u])
    49     {
    50         co[u]=++col;
    51         while(st[top]!=u)
    52         {
    53             si[col]++;
    54             co[st[top]]++;
    55             --top;
    56         }
    57         si[col]++;
    58         --top;
    59     }
    60 }
    61 int main()
    62 {
    63     n=read();m=read();
    64     for(int i=1;i<=m;i++)
    65     {
    66         int x=read(),y=read();
    67         add(x,y);
    68     }
    69     for(int i=1;i<=n;i++)
    70         if(!dfn[i])
    71             Tarjan(i);
    72     for(int i=1;i<=col;i++)
    73         if(si[i]>1) ans++;
    74     write(ans);
    75     return 0;
    76 }

    请各位大佬斧正(反正我不认识斧正是什么意思)

  • 相关阅读:
    26、面向对象设计模式之单例模式——泛型单例
    Unity 汽车碰撞
    makeObjectsPerformSelector对数组中的对象发送消息执行对象中方法
    NSHashTable NSPointerArray
    webrtc 音频一点相关知识
    记一次ios加急上架经历
    iOS 获取当前正在显示的ViewController
    ios表单上传图片或文件
    https适配
    swift block
  • 原文地址:https://www.cnblogs.com/handsome-zyc/p/11517822.html
Copyright © 2011-2022 走看看