• POJ1236:Network of Schools——题解


    http://poj.org/problem?id=1236

    首先还是缩点,然后入度为0的点的个数就是你要投文件个数。

    然后我们对于入度和出度为0的点的个数取最大值即为答案。

    (简单证明:入度和出度为0的点可以互相连一下成一个环,但是无法匹配的时候就只能随便连了)

    #include<stack>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    inline int read(){
        int x=0,w=1;char ch=0;
        while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return x*w;
    }
    const int maxn=101;
    int cnt=0,head[maxn]; 
    struct node{
        int w;
        int to;
        int nxt;
    }edge[10001];
    void add(int u,int v){
        cnt++;
        edge[cnt].to=v;
        edge[cnt].nxt=head[u];
        head[u]=cnt;
        return;
    }
    int dfn[maxn]; 
    int low[maxn];
    bool instack[maxn];
    int dis[maxn];
    int indeg[maxn];
    int outdeg[maxn];
    int to[maxn];
    int t=0,l=0;
    stack<int>q;
    void tarjan(int u){
        int v;
        t++;
        dfn[u]=t;
        low[u]=t;
        q.push(u);
        instack[u]=1;
        for(int i=head[u];i!=0;i=edge[i].nxt){
        v=edge[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }else if(instack[v]){
            low[u]=min(low[u],dfn[v]);
        }
        }
        if(low[u]==dfn[u]){
        l++;
        do{
            v=q.top();
            q.pop();
            instack[v]=0;
            to[v]=l;
        }while(v!=u);
        }
        return;
    }
    int main(){
        int n=read();
        for(int i=1;i<=n;i++){
        int a=read();
        while(a){
            add(i,a);
            a=read();
        }
        }
        for(int i=1;i<=n;i++){
        if(!dfn[i]){
            tarjan(i);
        }
        }
        for(int i=1;i<=n;i++){
        for(int j=head[i];j;j=edge[j].nxt){
            int v=to[edge[j].to];
            int u=to[i];
            if(u!=v){
            indeg[v]++;
            outdeg[u]++;
            }
        }
        }
        int ans=0,anss=0;
        for(int i=1;i<=l;i++){
        if(!indeg[i]){
            ans++;
        }
        if(!outdeg[i]){
            anss++;
        }
        }
        if(l!=1)
        printf("%d
    %d
    ",ans,max(ans,anss));
        else
        printf("%d
    %d
    ",1,0);
        return 0;
    }
  • 相关阅读:
    如何卸载Mysql
    netty4.1.32 pipeline的添加顺序和执行顺序
    protobuf 在win10系统如何编译jar包
    java swing 的各种布局layout
    一些大神的代码功底方面的文章
    图解ByteBuffer
    Eclipse 高亮显示选中的相同变量
    Java synchronized详解(java 线程同步)
    一篇非常全面的 《单例模式》 的讲解的文章
    java中ThreadLocalRandom类和Random类的使用
  • 原文地址:https://www.cnblogs.com/luyouqi233/p/7840823.html
Copyright © 2020-2023  润新知