zoukankan      html  css  js  c++  java
  • poj1236(强连通缩点)

    传送门:Network of Schools

    题意:一些学校联接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它应支援的学校名单(A学校支援学校B,并不表示B学校一定支援学校A)。当某校获得一个新软件时,无论是直接获得还是通过网络获得,该校都应立即将这个软件通过网络传送给它应支援的学校。因此,一个新软件若想让所有联接在网络上的学校都能使用,只需将其提供给一些学校即可。第一问:至少需要多少份软件,才能使得所有学校都能拥有软件;第二问:如果只用一份软件,那么需要添加多少条变,使得所有学校都能拥有软件。

    分析:一个强连通分量中必定能相互连通,肯定能共享一个软件,因此第一问只需求入度为0的强连通分量个数即可。第二问求需要添加多少条变,使得整个图都成为一个强连通,即任意两个学校都可到达,那么取入度为0的个数a和出度为0的个数b中的最大值,因为强连通分量中必定不会有出度为0或入度为0的点,因此首先用边连接入度和出度为0的点,等其中一个完后再任意连接边把出度为0或入度为0的点补完。

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 100000000
    #define inf 0x3f3f3f3f
    #define eps 1e-6
    #define N 110
    #define FILL(a,b) (memset(a,b,sizeof(a)))
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define PII pair<int,int>
    using namespace std;
    struct edge
    {
        int v,next;
        edge(){}
        edge(int v,int next):v(v),next(next){}
    }e[N*N];
    int n,scc,step,top,tot;
    int head[N],dfn[N],low[N],belong[N],Stack[N];
    int in[N],out[N];
    bool instack[N];
    void init()
    {
        tot=0;step=0;scc=0;top=0;
        FILL(head,-1);FILL(dfn,0);
        FILL(low,0);FILL(instack,false);
        FILL(in,0);FILL(out,0);
    }
    void addedge(int u,int v)
    {
        e[tot]=edge(v,head[u]);
        head[u]=tot++;
    }
    void tarjan(int u)
    {
        int v;
        dfn[u]=low[u]=++step;
        Stack[top++]=u;
        instack[u]=true;
        for(int i=head[u];~i;i=e[i].next)
        {
            v=e[i].v;
            if(!dfn[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(instack[v])
            {
                low[u]=min(low[u],dfn[v]);
            }
        }
        if(dfn[u]==low[u])
        {
            scc++;
            do
            {
                v=Stack[--top];
                instack[v]=false;
                belong[v]=scc;
            }while(v!=u);
        }
    }
    void solve()
    {
        for(int i=1;i<=n;i++)
            if(!dfn[i])tarjan(i);
        if(scc==1)
        {
            printf("1
    0
    ");
            return;
        }
        for(int u=1;u<=n;u++)
        {
            for(int i=head[u];~i;i=e[i].next)
            {
                int v=e[i].v;
                if(belong[v]!=belong[u])
                {
                    out[belong[u]]++;
                    in[belong[v]]++;
                }
            }
        }
        int a=0,b=0;
        for(int i=1;i<=scc;i++)
        {
            if(!in[i])a++;
            if(!out[i])b++;
        }
        printf("%d
    %d
    ",a,max(a,b));
    }
    int main()
    {
        int u;
        while(scanf("%d",&n)>0)
        {
            init();
            for(int i=1;i<=n;i++)
            {
                while(scanf("%d",&u)&&u)
                    addedge(i,u);
            }
            solve();
        }
    }
    View Code
  • 相关阅读:
    django加载静态文件
    计算机网络-划分子网
    接口定义一个Kye.保证其安全性
    GridView中几个显示数据时! 数据停靠(靠左 or 居中)的问题!
    数据库SQL Case...when...then...end的用法!
    利用jQuery发送ajax异步请求
    利用索引进行数据查询优化(转载!)
    身份证的合法验证
    DataTable判断列是否为空!(实用)
    窗体美化,IrisSkin2.dll的使用!
  • 原文地址:https://www.cnblogs.com/lienus/p/4278692.html
Copyright © 2011-2022 走看看