zoukankan      html  css  js  c++  java
  • Network of Schools POJ

    #include<string>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <set>
    #include <cstdio>
    #include <iostream>
    using namespace std;
    /*
    第一问:至少多少个点有软件就可以传到所有地方
    第二问:增加多少边,使得任意一个点都能将软件传到所有地方
    第一问:
    以为只要找到入度为0的就好了,但是没考虑不是连通图的时候,另一个连通分量是强连通的,卡死我了
    
    第二位:缩点,然后变成DAG,所以只要让这个DAG变成强连通就好了
    加的边数为max(a,b),a,b,分别为入度和出度为0的点数
    
    
    */
    typedef long long ll;
    const int maxn=210;
    vector<int>G[maxn];
    int low[maxn],dfn[maxn],du[maxn],vis[maxn],sta[maxn],color[maxn],du1[maxn],sum,t,index;
    void tarjan(int u)
    {
        dfn[u]=low[u]=++t;
        sta[++index]=u;
        vis[u]=1;
        int len=G[u].size();
        for(int i=0; i<len; i++)
        {
            int v=G[u][i];
            if(!dfn[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(vis[v])
            {
                low[u]=min(low[u],low[v]);
            }
        }
        if(dfn[u]==low[u])
        {
            ++sum;
            do
            {
                color[sta[index]]=sum;
                vis[sta[index]]=0;
                index--;
            }
            while(sta[index+1]!=u);
        }
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        int a;
        for(int i=0; i<n; i++)
        {
            while(scanf("%d",&a)&&a)
            {
                G[i+1].push_back(a);
                du[a]++;
            }
        }
        for(int i=1; i<=n; i++)
            if(!dfn[i])
                tarjan(i);
        int ans=0;
        for(int i=1; i<=n; i++)
            if(!du[i])
                ans++;
    
        memset(du,0,sizeof du);
        ans=0;
        int ans1=0;
        for(int i=1; i<=n; i++)
        {
            int len=G[i].size();
            for(int j=0; j<len; j++)
            {
                int v=G[i][j];
                if(color[i]!=color[v])
                {
                    du[color[i]]++;
                    du1[color[v]]++;
                }
            }
        }
        for(int i=1; i<=sum; i++)
        {
            if(!du[i])
                ans++;
            if(!du1[i])
                ans1++;
        }
        if(sum==1)
            printf("1
    0
    "); 
        else
        printf("%d
    %d
    ",ans1,max(ans,ans1));
    }
  • 相关阅读:
    Microsoft Updateclient更新
    DataTables warning: table id=dataTable
    BCB使用线程删除目录中的图片
    grep常见使用方法总结
    实战:percona-xtrabackup 2.1.9 for mysql 5.6.19
    代理模式之cglib动态代理
    hello world to php( mac 配置 xmapp virtual host)
    Android开发之AlarmManager具体解释
    linux入门教程(六) Linux文件与目录管理
    linux入门教程(五) Linux系统的远程登录
  • 原文地址:https://www.cnblogs.com/zhangzhenjun/p/12584779.html
Copyright © 2011-2022 走看看