[树] cf 698B D Fix a Tree

本文介绍了一种算法,用于判断给定节点集是否构成有根树,并通过调整最少数量的节点来修正非树结构,使之成为一棵有根树。文章详细阐述了算法原理,并提供了具体的实现代码。

题意

    给定每个节点的父亲,判断是否为一棵有根树;若不是,改变最少的节点的父亲,使其成为有根树。(节点规模2*105)。

题目分析

    比赛时一脸懵逼,后来看了大神代码——本题就是“见招拆招”:从叶子(尚未访问过的节点)往上找其最远的祖先,若能找到根,则合题;若找到环或找到与树不连通的祖先,则将祖先的父亲指向根。
    代码中dep[i]代表i是第几批被访问的。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=2e5+5;
int a[maxn];
int root=0;
int dep[maxn]={0};
int cnt=0;
int ans;
void work(int x)
{
    dep[x]=++cnt;
    while(dep[a[x]]==0)
        x=a[x],dep[x]=cnt;
    if(dep[a[x]]==cnt)//祖先是当前批次,则有可能是环、不连通
    {
        if(root==0) root=x;
        if(a[x]!=root)//祖先是环或不连通
            a[x]=root,ans++;
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        if(a[i]==i) root=i;
    for(int i=1;i<=n;i++)
        if(dep[i]==0) work(i);
    cout<<ans<<endl;
    for(int i=1;i<n;i++) printf("%d ",a[i]);cout<<a[n]<<endl;
    return 0;
}
这是操作记录,按说明没有成功 JiuJue@DESKTOP-5ER8D0A MINGW64 /d/wwwRoot/SHARE/D-JingWaiDianShang/uni-app (master) $ git revert --no-commit b12ce7b 143c352 8eaea60 Auto-merging config/app.js CONFLICT (content): Merge conflict in config/app.js Auto-merging pages/users/login/index.vue CONFLICT (content): Merge conflict in pages/users/login/index.vue CONFLICT (modify/delete): pages/users/order_confirm/index.vue deleted in parent of b12ce7b (代码提交) and modified in HEAD. Version HEAD of pages/users/order_confirm/index.vue left in tree. CONFLICT (modify/delete): static/html/pc.html deleted in parent of b12ce7b (代码提交) and modified in HEAD. Version HEAD of static/html/pc.html left in tree. error: could not revert b12ce7b... 代码提交 hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: Disable this message with "git config advice.mergeConflict false" JiuJue@DESKTOP-5ER8D0A MINGW64 /d/wwwRoot/SHARE/D-JingWaiDianShang/uni-app (master|REVERTING) $ git commit -m "撤销 08acc0c 之后的所有提交" error: Committing is not possible because you have unmerged files. hint: Fix them up in the work tree, and then use 'git add/rm <file>' hint: as appropriate to mark resolution and make a commit. fatal: Exiting because of an unresolved conflict. U config/app.js U pages/users/login/index.vue U pages/users/order_confirm/index.vue U static/html/pc.html JiuJue@DESKTOP-5ER8D0A MINGW64 /d/wwwRoot/SHARE/D-JingWaiDianShang/uni-app (master|REVERTING) $ git push origin master Everything up-to-date JiuJue@DESKTOP-5ER8D0A MINGW64 /d/wwwRoot/SHARE/D-JingWaiDianShang/uni-app (master|REVERTING) $ JiuJue@DESKTOP-5ER8D0A MINGW64 /d/wwwRoot/SHARE/D-JingWaiDianShang/uni-app (master|REVERTING) $ JiuJue@DESKTOP-5ER8D0A MINGW64 /d/wwwRoot/SHARE/D-JingWaiDianShang/uni-app (master|REVERTING) $ git log --oneline 8eaea60 (HEAD -> master, origin/master) 修改googel登录 143c352 代码修改提交 b12ce7b 代码提交 08acc0c fix: 佣金界面 07d2c67 fix: 佣金界面 bdf7b8e fix: 佣金界面 0987ad2 fix: 积分商品兑换取消注释 f69164e fix: 全局修改货币标识 f2d4406 fix: 接入paypal a4e1679 fix: 商品跳转链接 90ccade fix: 商品详情页布局 0528b9d fix: 商品详情页布局 507c453 fix: 页面调整 5ecc127 fix: 页面语言调整 a601cf8 fix: 首页商品 42be322 fix: 移除 30a509a fix: 分类页面 617dd9d fix: 分类页面 813aba6 fix: 添加uviewui fbcb681 fix: first commit
03-09
Step 30/37 : RUN apt-get install -y --no-install-recommends ca-certificates ---> Running in 5a1e49788109 Reading package lists... Building dependency tree... Reading state information... The following packages were automatically installed and are no longer required: cron fontconfig-config fonts-dejavu-core iso-codes java-common kmod libapt-inst2.0 libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libfontconfig1 libjpeg-turbo8 libjpeg8 libkmod2 liblcms2-2 libpcsclite1 librhash0 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxi6 libxrender1 libxtst6 powermgmt-base python-apt-common python-six python3-apt python3-dbus python3-gi python3-software-properties unattended-upgrades x11-common Use 'apt autoremove' to remove them. The following additional packages will be installed: openssl The following NEW packages will be installed: ca-certificates openssl 0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded. Need to get 765 kB of archives. After this operation, 1650 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 openssl amd64 1.1.1-1ubuntu2.1~18.04.23 [614 kB] Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 openssl amd64 1.1.1-1ubuntu2.1~18.04.23 [614 kB] Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 openssl amd64 1.1.1-1ubuntu2.1~18.04.23 [614 kB] Err:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 openssl amd64 1.1.1-1ubuntu2.1~18.04.23 Hash Sum mismatch Hashes of expected file: - SHA512:06975f094e038408f56301275c03d0c19cf66b130586bc34df659b191bae83657d624a20981c0d87781eeb56c783383c185fa77ddd82d3cd4e0821ea4fb56fc7 - SHA256:859c21a32a5101cb151d560333d79a1bc3b66fd22f7c68d16b64365910f33c5e - SHA1:962a82f4a332df08e2c41e6904d5d7c4461f2953 [weak] - MD5Sum:12e2125bf1230e7b954b5e4e38e3f788 [weak] - Filesize:613880 [weak] Hashes of received file: - SHA512:3244e83ae15ef90ade25d2e83896b61c048b3969de1f1f5cf65d558b627a84a3874c9c79d7068158762c292af806548cc0f6b468787718d13f540bc977a7e411 - SHA256:4d924393b476e7fb846f51cfe17256c824ab47f0389d89167a1ce5e216620d49 - SHA1:3a1c4a4fb458449a9fb4fd3325efc983563dc71a [weak] - MD5Sum:475230f513a29237551d470d2bf73614 [weak] - Filesize:613880 [weak] Last modification reported: Tue, 30 May 2023 14:23:37 +0000 Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 ca-certificates all 20230311ubuntu0.18.04.1 [151 kB] Fetched 739 kB in 5s (141 kB/s) E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/openssl_1.1.1-1ubuntu2.1~18.04.23_amd64.deb Hash Sum mismatch Hashes of expected file: - SHA512:06975f094e038408f56301275c03d0c19cf66b130586bc34df659b191bae83657d624a20981c0d87781eeb56c783383c185fa77ddd82d3cd4e0821ea4fb56fc7 - SHA256:859c21a32a5101cb151d560333d79a1bc3b66fd22f7c68d16b64365910f33c5e - SHA1:962a82f4a332df08e2c41e6904d5d7c4461f2953 [weak] - MD5Sum:12e2125bf1230e7b954b5e4e38e3f788 [weak] - Filesize:613880 [weak] Hashes of received file: - SHA512:3244e83ae15ef90ade25d2e83896b61c048b3969de1f1f5cf65d558b627a84a3874c9c79d7068158762c292af806548cc0f6b468787718d13f540bc977a7e411 - SHA256:4d924393b476e7fb846f51cfe17256c824ab47f0389d89167a1ce5e216620d49 - SHA1:3a1c4a4fb458449a9fb4fd3325efc983563dc71a [weak] - MD5Sum:475230f513a29237551d470d2bf73614 [weak] - Filesize:613880 [weak] Last modification reported: Tue, 30 May 2023 14:23:37 +0000 E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? The command '/bin/sh -c apt-get install -y --no-install-recommends ca-certificates' returned a non-zero code: 100
最新发布
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值