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

题目链接:

https://www.luogu.org/problemnew/show/P2746

题意:

给定n个点,第i+1行的是第i个点连向的点,也就是给了一个有向图,问最少需要选定几个点,通过这些选定的点,可以到达图中任何点,第二问,再添几条边可以使整个图成为一个连通图,即任意两点可达;

分析:

显然应先对整个图缩点,第一问的结果应该等于,整个图缩点后,入度为0的强连通分量的个数,对于第二问,即答案是出度为0强连通分量数目,与入度为0强连通分量数目,二者较大的一个;

代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;

#define  inf 0x7f7f7f7f
#define  maxn 10011
#define  N 200050

typedef long long ll;
typedef struct{
    int u,v,next,w;
}Edge;
Edge e[maxn];
int cnt,head[maxn];

inline void add(int u,int v){
    e[cnt].u=u;
    e[cnt].v=v;
    //e[cnt].w=w;
    // e[cnt].f=f;
    e[cnt].next=head[u];
    head[u]=cnt++;
    //e[cnt].u=v;
//    e[cnt].v=u;
//    e[cnt].w=0;
//    e[cnt].f=-f;
//    e[cnt].next=head[v];
//    head[v]=cnt++;
}
inline void write(int x)
{
     if(x<0)
        putchar('-'),x=-x;
     if(x>9)
        write(x/10);
     putchar(x%10+'0');
}

inline int read()
{
    int x = 0;
    int f = 1;
    char c = getchar();
    while (c<'0' || c>'9'){
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0'&&c <= '9'){
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x*f;
}

//struct splay_Tree{
//    int ch[N][2],f[N],cnt[N],size[N],n,root,key[N];
//    splay_Tree(){
//        memset(ch,0,sizeof(ch));
//        memset(f,0,sizeof(f));
//        memset(cnt,0,sizeof(cnt));
//        memset(size,0,sizeof(size));
//        n=0;
//    }
//    int get(int x) {
//        return ch[f[x]][1]==x;
//    }
//    void update(int x) {
//        if(x){
//            size[x]=cnt[x];
//            if(ch[x][0])size[x]+=size[ch[x][0]];
//            if(ch[x][1])size[x]+=size[ch[x][1]];
//        }
//    }
//    void rotate(int x){
//        int y=f[x],z=f[y];
//        int kind=get(x);
//        ch[y][kind]=ch[x][!kind],f[ch[y][kind]]=y;
//        ch[x][!kind]=y,f[y]=x;
//        if(z){
//            ch[z][ch[z][1]==y]=x;
//        }
//        //更新顺序不能反
//        update(y),update(x);
//    }
//    void splay(int x) {
//        for (int fa; fa = f[x]; rotate(x)) {
//            if (f[fa])
//                rotate((get(x) == get(fa) ? fa : x));
//        }
//        root = x;
//    }
//    /*void insert(int x) {
//        if(root==0){
//            n++;
//            size[n]=cnt[n]=1;
//            ch[n][0]=ch[n][1]=f[n]=0;
//            root=n;key[n]=x;
//            return ;
//        }
//        int now=root,fa=0;
//        while(1){
//            if(key[now]==x){
//                cnt[now]++;
//                update(now);
//                update(fa);
//                splay(now);
//                return ;
//            }
//        }
//    }*/
//};

int t,res,n,dfn[110],low[110],_time,vis[110],out[110],in[110],scc[110];
stack<int >s;
void tarjan(int x){
    dfn[x]=low[x]=++_time;
    s.push(x);
    vis[x]=1;
    for(int i=head[x];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(!dfn[v]){
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(vis[v]) low[x]=min(dfn[v],low[x]);
    }
    if(dfn[x]==low[x]){
        res++;
        //cout<<res<<endl;
        do{
            t=s.top();s.pop();
            vis[t]=0;
            scc[t]=res;
        }while(x!=t);
    }
}
int main() {
    cin>>n;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++){
        int t;
        while(cin>>t&&t!=0){
            add(i,t);
        }
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i])
            tarjan(i);
    }
    //cout<<res<<endl;
    if(res==1){
        cout<<1<<endl<<0<<endl;
        return 0;
    }
    for(int i=1;i<=n;i++){
        for(int j=head[i];j!=-1;j=e[j].next){
            int u=e[j].u,v=e[j].v;
            if(scc[u]!=scc[v]){
                out[scc[u]]++;
                in[scc[v]]++;
            }
        }
    }
    int a=0,b=0;
    for(int i=1;i<=res;i++){
        if (!out[i])a++;
        if (!in[i])b++;
    }
    cout<<b<<endl<<max(a,b)<<endl;
    return 0;
}

(仅供个人理解)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值