【补题日记】[2022牛客暑期多校1]J-Serval and Essay

Pro

https://ac.nowcoder.com/acm/contest/33186/J

Sol

如果一个点可以被染色,当且仅当只有一条入边连向该点。因此如果将入度为1的点预先加入队列,使用类似拓扑的方式对所连节点进行更新,同时更新队列,并用并查集维护所有可以同时被染黑的点。

可以发现最终并查集的形成了以 f a [ i ] = i fa[i]=i fa[i]=i为根的树组成的森林,同一棵树内满足可以同时染色,答案即为森林中树的大小的最大值 。

牛客的机子还是不太稳定,所以下面的代码里加了一点卡常的东西

Code

//By cls1277
#include<bits/stdc++.h>
using namespace std;
typedef int LL;
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define endl '\n'

const LL maxn = 2e5+5;
LL n, fa[maxn];

inline LL read() {
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}

LL find(LL x) {
    while(x!=fa[x]) x = fa[x] = fa[fa[x]];
    return x;
}

unordered_set<LL> e[maxn], pre[maxn];

int main() {
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    #ifdef DEBUG
    freopen("data.txt","r",stdin);
    #endif
    LL t; t=read(); //cin>>t;
    Fo(tt,1,t) {
        // cin>>n;
        n = read();
        vector<LL> sz(n+1, 1);
        vector<bool> vis(n+1, 0);
        // unordered_map<LL, unordered_set<LL>> e, pre;
        Fo(i,1,n) {
            e[i].clear();
            pre[i].clear();
        }
        Fo(i,1,n) {
            fa[i] = i;
            LL x; x=read(); //cin>>x;
            Fo(j,1,x) {
                LL y; y=read(); //cin>>y;
                e[y].insert(i);
                pre[i].insert(y);
            }
        }
        // queue<LL> q;
        vector<LL> q(n+1, 0);
        LL _h=0, _t=-1;
        Fo(i,1,n)
            if(pre[i].size()==1) {
                // q.push(i);
                q[++_t] = i;
                vis[i] = 1;
            }
        while(_h<=_t) {
        // while(!q.empty()) {
            // LL u = q.front(); q.pop();
            LL u = q[_h++];
            LL p = *pre[u].begin();
            // for(auto it:pre[u]) {
            //     p = it;
            //     break;
            // }
            LL p1=find(u), p2=find(p);
            if(p1==p2) continue;
            if(sz[p1]>sz[p2]) swap(p1, p2);
            fa[p1] = p2;
            sz[p2] += sz[p1];
            for(auto it:e[p1]) {
                e[p2].insert(it);
                pre[it].erase(p1);
                pre[it].insert(p2);
                if(pre[it].size()==1&&!vis[it]) {
                    vis[it] = 1;
                    // q.push(it);
                    q[++_t] = it;
                }
            }
        }
        LL ans = 1;
        Fo(i,1,n) ans = max(ans, sz[i]);
        // cout<<"Case #"<<tt<<": "<<ans<<endl;
        printf("Case #%lld: %lld\n", tt, ans) ; 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cls1277

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值