首页 > 其他分享 >2022 CCPC广州 C Customs Controls 2

2022 CCPC广州 C Customs Controls 2

时间:2022-11-14 20:45:26浏览次数:71  
标签:int top CCPC Controls cin 2022 include Customs

Customs Controls 2

并查集 + 拓扑

看了题解之后补的,题解写挺好的

考虑到 \(1\) 距离相等的点进行并查集合并(指向同一个点的点,到 \(1\) 的距离相等),缩点后重新建边,判断是否有环,若有说明不成立

成立之后拓扑处理当前点的最大深度,这个深度就代表 \(1\) 到该点(缩点后的)的距离

接下来就好办了,有边 u -> v 的话,代表 \(val_v = dep_v - dep_u\),枚举所有边算一下就行了

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
vector<int>top;

int query(int x)
{
    return x == top[x] ? x : top[x] = query(top[x]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n, m;
        cin >> n >> m;
        vector<vector<int>>gra(n + 1), pre(n + 1), E(n + 1);
        top.resize(n + 1);
        for(int i=0; i<=n; i++) top[i] = i;
        for(int i=0; i<m; i++)
        {
            int a, b;
            cin >> a >> b;
            gra[a].push_back(b);
            pre[b].push_back(a);
        }
        for(int i=1; i<=n; i++)
        {
            if(pre[i].empty()) continue;
            int now = pre[i][0];
            for(int j=1; j<pre[i].size(); j++)
            {
                int a = query(now), b = query(pre[i][j]);
                if(a != b)
                    top[b] = top[a];
            }
        }
        vector<int>in(n + 1, 0), dep(n + 1, 1);
        for(int i=1; i<=n; i++)
        {
            for(int nex : gra[i])
            {
                E[query(i)].push_back(query(nex));
                in[query(nex)]++;
            }
        }
        queue<int>q;
        for(int i=1; i<=n; i++)
        {
            if(i != query(i)) in[i] = -1;
            else if(in[i] == 0) q.push(i);
        }
        while(q.size())
        {
            int now = q.front();
            q.pop();
            for(int nex : E[now])
            {
                if(--in[nex] == 0)
                {
                    q.push(nex);
                    dep[nex] = dep[now] + 1;
                }
            }
        }
        int f = 1;
        for(int i=1; i<=n; i++) if(in[i] > 0) f = 0;
        if(f == 0) {cout << "No\n"; continue;}
        cout << "Yes\n";
        vector<int>ans(n + 1, 1);
        for(int i=2; i<n; i++)
        {
            int nex = pre[i][0];
            ans[i] =  dep[query(i)] - dep[query(nex)];
        }
        for(int i=1; i<=n; i++)
        {
            if(i != 1) cout << " ";
            cout << ans[i];
        }
        cout << "\n";
    }
    return 0;
}

标签:int,top,CCPC,Controls,cin,2022,include,Customs
From: https://www.cnblogs.com/dgsvygd/p/16890334.html

相关文章

  • 2022年度国内主流低代码平台介绍
    随着低代码发展越来越迅速,也出现了很多优秀的低代码平台,企业在做技术选型时难免会觉得眼花缭乱,不知该如何选择;现在就跟小编一起来看一下国内那些优秀的低代码平台吧。让......
  • 51st 2022/11/12 模拟赛总结36
    这次按自己的话来说,不能接受因为和估分差距有点大赛时很开心地以为能A两题,一题50然后爆成120原因:T1的100->20现发现T1是因为没有全取模,很失落其实是因为考试时的一......
  • 20221114_T4B_树形dp换根dp
    题意太冗长了传一张图片自己看吧。题解赛时得分15/100/100赛时写了\(A=0\)的乱搞,没写对但是拿了15pts。首先这个函数是一个增函数,对于power和score两个指标......
  • 20221114-python字符串
    1.字符串定义:    2.字符串的转义符    3.字符串的拼接:      4.字符串的下标:    5.字符串的切片 ......
  • 2022 CCPC 广州站 Alice and Her Lost Cat
    1#include<bits/stdc++.h>2usingnamespacestd;3#definergregister4#definelllonglong5#defineldlongdouble6#defineFOR(i,a,b)for(r......
  • 20221114_T4B_拓扑排序贪心
    题意L国正在举行各种会议,但是可怜的是L国只有一个主持人,每场会议的开始主持人都必须去主持会议,使会议得以开始,在会议开始后主持人可以离开。 主持人不会分身,他在一个时刻......
  • 【2022-11-14】luffy项目实战(七)
    一、短信注册接口user/views.pyclassUserView(ViewSet):@action(methods=['POST'],detail=False)defregister(self,request):info=UserRegiste......
  • 【ECCV2022】AMixer: Adaptive Weight Mixing for Self-Attention Free Vision Transf
    1、Motivation这个论文来自于清华大学鲁继文老师团队,核心是attention和MLP-mixer思想的结合。建议用2分钟时间学习一下谷歌公司的MLP-Mixer「MLP-Mixer:Anall-ML......
  • [已满分在线评测] cmu15445 2022 PROJECT #2 B+Tree Index
    CMU154452022PROJECT#2B+TreeIndex前前言本地测试通过是真的比较简单,因为有数据可以单步debug,很快就能定位错误。但是要通过在线评测还是比较痛苦的,没有数据,没办法......
  • 2022-找工准备-面经积累
    请使用Python实现对数据重复文本的去除,数据是dict的形式,key总计N个,value是字符串,对value中重复的(完全重叠10个字以上)内容进行识别,保留第一次出现该重复文本的内容,去除掉......