Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
来源:互联网 发布:linux查看samba服务 编辑:程序博客网 时间:2024/06/06 01:37
题目链接:http://codeforces.com/contest/699/problem/D
A tree is an undirected connected graph without cycles.
Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent).
Given a sequence p1, p2, ..., pn, one is able to restore a tree:
- There must be exactly one index r that pr = r. A vertex r is a root of the tree.
- For all other n - 1 vertices i, there is an edge between vertex i and vertex pi.
A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid.
You are given a sequence a1, a2, ..., an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
In the first line print the minimum number of elements to change, in order to get a valid sequence.
In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.
42 3 3 4
12 3 4 4
53 2 2 5 3
03 2 2 5 3
82 3 5 4 1 6 6 7
22 3 7 8 1 6 6 7
题意:
有n个节点,给出每个点的父节点。问至少修改多少个节点的父节点,使得这些节点刚好构成一棵树?
题解:
1.经过分析,假设在初始状态有k个集合,那么对于每一个集合,它要么是一棵根节点的父节点为自己的树,要么是一棵根节点的父节点为其子孙的树(虽然不是树,但姑且这么叫,便于理解)。
2.首先用并查集使得每个集合“现出原形”,得到集合后,需要选一个根节点,作为最终树的根节点,父节点为自己的根节点优先被选择,然后再把其他根节点的 父节点改为最终树的根节点。如果选另一种,就需要把它的父节点改为自己,真正作为根节点(这个集合就真真正正是一棵树了)后,再把其他根节点的父节点改为最终树的根节点,这样就多了一次修改。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <sstream>#include <algorithm>using namespace std;#define ms(a, b) memset((a), (b), sizeof(a))typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 200000+10;int fa[maxn], a[maxn], b[maxn];int n;int find(int x) { return fa[x]==x? fa[x]=x:find(fa[x]); }void init(){ scanf("%d",&n); for(int i = 1; i<=n; i++) scanf("%d",&a[i]), b[i] = a[i], fa[i] = i;}void solve(){ for(int i = 1; i<=n; i++) { int u = a[i], v = i; int m1 = find(u); int m2 = find(v); if(m1!=m2) fa[m2] = m1; } int rt = 0; for(int i = 1; i<=n; i++) if(fa[i]==i && a[i]==i) { rt = i; break; } for(int i = 1; i<=n; i++) { if(fa[i]==i) { if(!rt) rt = i, a[i] = i; else a[i] = rt; } } int ans = 0; for(int i = 1; i<=n; i++) if(a[i]!=b[i]) ans++; printf("%d\n",ans); for(int i = 1; i<=n; i++) printf("%d ",a[i]);}int main(){// int T;// scanf("%d",&T);// while(T--) { init(); solve(); } return 0;}
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
- Codeforces Round #363 (Div. 2) D. Fix a Tree (并查集)
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
- Codeforces Round #363 (Div. 2) Problem D - Fix a Tree(并查集 + 构造)
- Codeforces Round #363 (Div. 2) D. Fix a Tree(并查集)
- Codeforces Round #363 (Div. 2)D. Fix a Tree并查集
- Codeforces Round #363 (Div. 2) D. Fix a Tree(并查集)(判断圆环)
- Codeforces Round #363 (Div. 2) D Fix a Tree(并查集)
- Codeforces Round #363 (Div. 2) D. Fix a Tree
- Codeforces Round #363 (Div. 2) D. Fix a Tree
- Codeforces Round #363 (Div. 2) D. Fix a Tree
- Codeforces Round #363 (Div. 2) D. Fix a Tree
- Codeforces Round #363 (Div. 2) D. Fix a Tree
- Codeforces Round #363 (Div. 2)D. Fix a Tree
- Codeforces Round #363 D Fix a Tree(并查集)
- (环问题并查集)Codeforces Round #363 D Fix a Tree
- CodeForces 699D—— Fix a Tree(并查集判断环)
- Codeforces 699D. Fix a Tree【并查集+思维】
- H5基础知识第八课时(flex布局)
- 测试国内开源数据库中间件Mycat和Atlas体会
- 数据仓库-BI商业智能
- 最近使用了asp.net,iis安装
- 项目中二级缓存理解,以hibernate为例
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
- c# 开源框架整理
- 人脸识别之人脸检测
- python中将类传给函数,在函数中修改类的值的问题
- WinForm之选择本地文件
- [每日问答]包含L1正则化项的目标函数如何求导?
- c程序设计语言第二版1.18
- 【php基础班】第6天 padding_margin、CSS背景、CSS Sprite、CSS表格属性、CSS浮动与清除
- java 关于wait 和notify