p2746校园网Network of Schools

传送门

思路:缩点

所谓接受列表,即说明是有向图。

环内的学校是互通的,对于子任务A,缩点后求入度为0的强联通块数量即可。

题目求的最小发送次数,即考虑最好的情况,发送到一个强联通块后,可以继续向其他快传输,如果入度为0就得重新发送了。

对于子任务B,求max(入度为0的强联通块数,出度为0的强联通块数)即可。

问的是最少加几条有向边,让整张图强联通,如果存在多个强联通块,那么每个块的入度和出度至少其一不为0,否则无法实现任意的传送,所以需要对应的增加出边和入边,

对于出入度都不为0的块,我们顺着它走,肯定走到出度为0的块,这时给出度为0的块连一条边到入度为0的块,可以发现一条边就解决了,两个块的问题。

反着建图,此时入度为出度,出度为入度,同理,给“出度”为0的块连一条边到“入度”为0的块。

综上取max(出度为0块,入度为0块),注意特判一下强联通块数为1时的情况

//tarjan缩点
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
#define IOS ios::sync_with_stdio(false)
#define _for(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define _rep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define mst(v,s) memset(v,s,sizeof(v))
#define pb push_back
#define int long long
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1e5+10;
vector<int > G[N*2];
int n,m;
int x[N],y[N],a[N],f[N],dfn[N],low[N];//dfn第一次访问 low时间戳
int sta[N],pd[N],top;//栈  是否在栈中  栈顶指针
int col[N];//缩点后每个点所在的强联通块
int sum[N];//每个强联通块的权值
int ti,tot,ans;//时间   总的强联通块数量  答案
int in[N],out[N];//入度,出度
void tarjan(int x)
{
    sta[++top]=x;//入栈
    dfn[x]=low[x]=++ti;//按照访问时间给dfn和时间戳赋值
    pd[x]=1;//入栈
    for(int i=0 ;i<G[x].size() ;i++)
    {
        int y = G[x][i];
        if( dfn[y] == 0 )
        {
            tarjan(y);
            low[x] =min(low[x],low[y]);
        }
        else if( pd[y] ) low[x] =min(low[x] ,low[y]);
    }
    if( dfn[x] == low[x])//时间戳不变,说明无法更新了,强联通块到此可以打包
    {
        tot++;
        while( sta[top+1] != x)//如果x还没退栈
        {
            col[sta[top]]=tot;//在x上方的元素打包在一起
            sum[tot] ++;//计算连通块大小
            pd[sta[top--]]=0;//退栈(更新pd 和 stack)
        }
    }
}
void solve()
{
    _for(i,1,n)
    {
        if( !dfn[i]) tarjan(i);
    }
    //缩点后遍历,不重新建图了,数据范围小直接搜
    _for(i,1,n)
    {
        for(int j=0 ;j<G[i].size() ;j++)
        {
            int y = G[i][j];
            if( col[i] != col[y])//不在一个强连通块里
            {
                //统计的是强联通块
                in[col[y]]++;
                out[col[i]]++;
            }
        }

    }
    int temp=0;//入度数为1的块
    if( tot==1 )//特判强联通块数为1
    {
        cout<<1<<endl<<0<<endl;
        return ;
    }
    _for(i,1,tot) if( !in[i] ){ temp++;}//统计入度数为1的块
    _for(i,1,tot) if( !out[i] ) ans++;//统计出度数为1的块
    cout<<temp<<endl;
    cout<<max(temp,ans)<<endl;
}
signed main()
{
    IOS;
    ///!!!
//    freopen("data.txt","r",stdin);
    ///!!!
    cin>>n;
    _for(i,1,n)
    {
        int y;
        while( cin>>y )
        {
            if(y==0) break;
            G[i].pb(y);
        }
    }
    solve();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值