zoukankan      html  css  js  c++  java
  • 【luogu2746】 [USACO5.3]校园网Network of Schools [tarjan 缩点]

    P2746 [USACO5.3]校园网Network of Schools

    任务a:找有多少个入度为0的点

    任务b:找出出度为0的个数和入度为0点个数中的较大数

    在一个出度为0和另一入度为0的点间连一条边 就可以同时解决两个点 故找出其中较大数

    要注意最终缩为一个强连通时要特判

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define ll long long
     4 const int N=100+5;
     5 int n,in[N],out[N],ans1=0,ans2=0;
     6 int dfn[N],bl[N],low[N],inst[N],sum[N],idx=0,Bcnt=0;
     7 stack<int>s;
     8 template<class t>void rd(t &x){
     9     x=0;int w=0;char ch=0;
    10     while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    11     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    12     x=w?-x:x;
    13 }
    14 
    15 int tot=0,head[N];
    16 struct edge{
    17     int u,v,nxt;
    18 }e[N*N];
    19 void add(int u,int v){
    20     e[++tot]=(edge){u,v,head[u]};head[u]=tot;
    21 }
    22 
    23 void tarjan(int u){
    24     dfn[u]=low[u]=++idx;
    25     inst[u]=1,s.push(u);
    26     for(int i=head[u],v;i;i=e[i].nxt){
    27         v=e[i].v;
    28         if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
    29         else if(inst[v]&&dfn[v]<low[u]) low[u]=dfn[v];
    30     }
    31     if(dfn[u]==low[u]){
    32         ++Bcnt;
    33         int v;
    34         do{
    35             v=s.top();s.pop();
    36             bl[v]=Bcnt;
    37             ++sum[Bcnt],inst[v]=0;
    38         }while(u!=v);
    39     }
    40 }
    41 
    42 int main(){
    43     freopen("in.txt","r",stdin);
    44     rd(n);
    45     for(int i=1;i<=n;++i){
    46         int v;rd(v);
    47         while(v) add(i,v),rd(v);
    48     }
    49     memset(dfn,0,sizeof(dfn));
    50     for(int i=1;i<=n;++i)
    51     if(!dfn[i]) tarjan(i);
    52     for(int i=1;i<=tot;++i)
    53     if(bl[e[i].u]!=bl[e[i].v]) ++in[bl[e[i].v]],++out[bl[e[i].u]];
    54     for(int i=1;i<=Bcnt;++i){
    55         if(!in[i]) ++ans1;
    56         if(!out[i]) ++ans2;
    57     }
    58     printf("%d
    %d",ans1,Bcnt!=1?max(ans1,ans2):0);
    59     return 0;
    60 }
  • 相关阅读:
    做人方法论之---三省身
    大脑的作用
    SpEL 和 jquery 有点像
    Bootstrap 栅格系统
    JS 详解 Cookie、 LocalStorage 与 SessionStorage
    深度学习 机器学习 人工智能
    信息熵是怎样炼成的 | 纪念信息论之父香农
    最小熵原理(一):无监督学习的原理
    思维的基本形式、信息量与熵
    思维的本质是信息处理的过程
  • 原文地址:https://www.cnblogs.com/lxyyyy/p/11155240.html
Copyright © 2011-2022 走看看