CodeForces 698B Fix a Tree

时间:2022-12-27 20:21:33

并查集,构造。

先看一下图的特殊性,按照这种输入方式,一个点的入度最多只有$1$,因此,问题不会特别复杂,画画图就能知道了。

如果给出的序列中已经存在$a[i]=i$,那么随便取一个$a[i]=i$的$i$作为$root$,剩下的每一条边$a[i] \to i$,可以用并查集来处理,如果发现某条边$a[i] \to i$加入前$a[i]$与$i$已经在同一集合中,说明再加$a[i] \to i$会导致成环,因此将$i$的$father$改成$root$即可,并将$i$与$root$合并。

如果给出的序列中不存在$a[i]=i$,和上面的处理方法类似,唯一不同的是:找到第一条不能加入的边$a[i] \to i$,将$i$的$father$改为$i$,并且$root$设置为$i$,之后出现不能加入的边和上面处理方法一样。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
int f[maxn],a[maxn],n; int Find(int x)
{
if(x!=f[x]) f[x]=Find(f[x]);
return f[x];
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int root=-; for(int i=;i<=n;i++) if(a[i]==i) root=i;
int num=; for(int i=;i<=n;i++) f[i]=i; for(int i=; i<=n; i++)
{
if(i==root) continue;
int fx=Find(i),fy=Find(a[i]);
if(fx!=fy) f[fx]=fy;
else
{
if(root==-) a[i]=i, root=i, num++;
else
{
a[i]=root, num++;
fx=Find(root); fy=Find(i);
f[fx]=fy;
}
}
}
printf("%d\n",num);
for(int i=; i<=n; i++) printf("%d ",a[i]);
printf("\n");
return ;
}

CodeForces 698B Fix a Tree的更多相关文章

  1. CodeForces 698B Fix a Tree (并查集应用)

    当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...

  2. Codeforces Round &num;363 &lpar;Div&period; 2&rpar; 698B Fix a Tree

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes     A tree is an und ...

  3. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  4. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  5. Codeforces Round &num;363 &lpar;Div&period; 2&rpar; D&period; Fix a Tree —— 并查集

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

  6. Codeforces Round &num;363 &lpar;Div&period; 2&rpar;D&period; Fix a Tree&lpar;并查集&rpar;

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces Fix a Tree

    Fix a Tree time limit per test2 seconds A tree is an undirected connected graph without cycles. Let' ...

  8. Codeforces 461B Appleman and Tree&lpar;木dp&rpar;

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  9. Fix a Tree

    Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...

随机推荐

  1. &period;net 实现上传文件分割,断点续传上传文件

    一 介绍 断点续传搜索大部分都是下载的断点续传,涉及到HTTP协议1.1的Range和Content-Range头. 来个简单的介绍 所谓断点续传,也就是要从文件已经下载的地方开始继续下载.在以前版本 ...

  2. VMWare虚拟机下RedHat 9&period;0linux的网络设置

    VMWare虚拟机下安装的RedHat 9.0 linux有三种方式实现上网,桥接.nat.host-only.本来想用桥接方式的,可总是因为配置网络出现问题而不能上网,还把 sygate4.5(代理 ...

  3. (续篇3):飞测独家のJmeter秘籍,限量发放

    好东西,分享大家,自上次分享出来fiddler导出jmx格式V4.0版本对外公开后,收到一些反馈,我们利用工作之余时间继续优化,现在一个比较稳定的版本出炉,分享给大伙,我们一起来看看. 特性说明: 版 ...

  4. 【正则】精通JS正则表达式,没消化 信息量太大,好文

    http://www.jb51.net/article/25313.htm 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用 ...

  5. URAL1658&period; Sum of Digits&lpar;DP&rpar;

    链接 这题卡了挺久了 昨天试着用类似dfs的方法直接TLE在第二组 看了下题解,,发现s1,s2的范围是个幌子..100位最大的s1900 s28100 觉得s1s2太大不敢开二维.. 这样就简单了 ...

  6. Application的多种值判断测试程序

    Application.Contents.Remove("FriendLink") Response.Write("Application.Contents.Remove ...

  7. Python动态绑定属性slots的使用

    当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.废话不多说,我们看一个例子: class Person(object): pass ...

  8. Service Fabric部署笔记

    使用 x509 证书时报错 Thumbprint contains invalid characters 原因是在 windows 证书管理器复制指纹的时候,在指纹字符串开头有不显示的字符,粘贴到 j ...

  9. torch&period;linspace,unsqueeze&lpar;&rpar;以及squeeze&lpar;&rpar;函数

    1.torch.linspace(start,end,steps=100,dtype) 作用是返回一个一维的tensor(张量),其中dtype是返回的数据类型. import torch print ...

  10. &lpar;11&rpar;Python包