cf698B Fix a Tree

本文深入探讨了如何通过一种特定算法构建最小生成树,重点介绍了避免边形成环的策略,以及如何在森林中选择合适的根节点进行重新赋值的过程。代码示例使用C++实现,展示了Union-Find数据结构在解决此类问题中的应用。

链接

点击跳转

题解

考虑这么做:
如果这条边加入之后会成环,那我就先不要这条边,否则就要这条边

这样最后建出来是一个森林

之前被我不要的 p i p_i pi,现在要考虑重新赋值

如果原先就存在 r = p r r=p_r r=pr,那么我现在可以直接把所有待连的 i i i都连到 r r r

如果不存在这样的点,我现在需要选一个根节点,选谁呢?强行选 1 1 1会有问题,应该找第一个 p i p_i pi被我舍去的 i i i

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
int n, p[maxn], root, cnt;
struct UnionFind
{
	int f[maxn];
	void init(int n)
	{
		for(auto i=1;i<=n;i++)f[i]=i;
	}
	int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
	void merge(int x, int y){f[find(x)]=find(y);}
}uf;
int main()
{
    int i, j, ans=0;
    n=read();
    rep(i,n)
    {
        p[i]=read();
        if(p[i]==i)root=i;
    }
    uf.init(n);
    rep(i,n)
    {
        if(uf.find(i)==uf.find(p[i]) and i!=root)
        {
            ans++;
            if(root==0)
            {
                if(i==p[i])ans--;
                root=i;
                p[i]=i;
            }
            else p[i]=root;
        }
        uf.merge(i,p[i]);
    }
    printf("%d\n",ans);
    rep(i,n)printf("%d ",p[i]);
    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
Step 30/37 : RUN apt-get install -y --no-install-recommends ca-certificates ---> Running in 1b841472bb50 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] 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:48882a35624b7f1b28c5c1ade44e1b15a3b9ac7334ddc6e18d7424dcceb6679059645b921cf244e1a8cd2fe05a093e6474992dce663ee8a7502daeeb630ac6f3 - SHA256:8033999f07d063ef91aa52170155cacab851911e5ee4768d8cee9259c124ee32 - SHA1:b3583ea00d33ea635287970cbc68b21d252829fa [weak] - MD5Sum:f2e301c5dc8e9aa2f220983fa1397653 [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] Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 ca-certificates all 20230311ubuntu0.18.04.1 [151 kB] Err:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 ca-certificates all 20230311ubuntu0.18.04.1 Hash Sum mismatch Hashes of expected file: - SHA512:2d8d1ed53ba88541bb997350a512e8512d39f00751abd22676b3c612f61c9054cbe3b714208e26a0daf3bc7deb3ba2ca7a6fb5815ae9adec171e37c6405f63c3 - SHA256:ebab7dae205c7ad4a6d12cada2b611896ebc0d8b0477d96d2659b9959e5f4703 - SHA1:9b8cb7bf1441668f55e2a1c0f8f9aa0520a1b561 [weak] - MD5Sum:cb8559351ed2e1c519e865ee3fb16525 [weak] - Filesize:150728 [weak] Hashes of received file: - SHA512:d09788d19a0476ef7f11d900c370817dc6b895bb736783b48082a98c4423db35d6cc7dfd470bde285447fb2b2e92f4415a655817eedb29bda74b817a15df3f58 - SHA256:0a0965604c598734f254a9889e918ebfc65d5270454d23c217bd75f4d472a6fa - SHA1:80858f2adcfa3d3ed59261884f37ecafea81fd49 [weak] - MD5Sum:3c9beb91aa15735d6429970302d850bd [weak] - Filesize:150728 [weak] Last modification reported: Wed, 24 May 2023 14:23:36 +0000 Fetched 419 kB in 9s (46.5 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:48882a35624b7f1b28c5c1ade44e1b15a3b9ac7334ddc6e18d7424dcceb6679059645b921cf244e1a8cd2fe05a093e6474992dce663ee8a7502daeeb630ac6f3 - SHA256:8033999f07d063ef91aa52170155cacab851911e5ee4768d8cee9259c124ee32 - SHA1:b3583ea00d33ea635287970cbc68b21d252829fa [weak] - MD5Sum:f2e301c5dc8e9aa2f220983fa1397653 [weak] - Filesize:613880 [weak] Last modification reported: Tue, 30 May 2023 14:23:37 +0000 E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/c/ca-certificates/ca-certificates_20230311ubuntu0.18.04.1_all.deb Hash Sum mismatch Hashes of expected file: - SHA512:2d8d1ed53ba88541bb997350a512e8512d39f00751abd22676b3c612f61c9054cbe3b714208e26a0daf3bc7deb3ba2ca7a6fb5815ae9adec171e37c6405f63c3 - SHA256:ebab7dae205c7ad4a6d12cada2b611896ebc0d8b0477d96d2659b9959e5f4703 - SHA1:9b8cb7bf1441668f55e2a1c0f8f9aa0520a1b561 [weak] - MD5Sum:cb8559351ed2e1c519e865ee3fb16525 [weak] - Filesize:150728 [weak] Hashes of received file: - SHA512:d09788d19a0476ef7f11d900c370817dc6b895bb736783b48082a98c4423db35d6cc7dfd470bde285447fb2b2e92f4415a655817eedb29bda74b817a15df3f58 - SHA256:0a0965604c598734f254a9889e918ebfc65d5270454d23c217bd75f4d472a6fa - SHA1:80858f2adcfa3d3ed59261884f37ecafea81fd49 [weak] - MD5Sum:3c9beb91aa15735d6429970302d850bd [weak] - Filesize:150728 [weak] Last modification reported: Wed, 24 May 2023 14:23:36 +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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值