CodeForces 698B Fix a Tree
并查集,构造。
先看一下图的特殊性,按照这种输入方式,一个点的入度最多只有$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的更多相关文章
- CodeForces 698B Fix a Tree (并查集应用)
当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...
- Codeforces Round #363 (Div. 2) 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 ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Fix a Tree
Fix a Tree time limit per test2 seconds A tree is an undirected connected graph without cycles. Let' ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Fix a Tree
Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...
随机推荐
- Web API 2中的Action Results
[译]Action Results in Web API 2 单击此处查看原文 本文阐述了ASP.NET Web API是如何将controller action的返回值转换为HTTP respons ...
- php集成环境和自己配置的区别,php集成环境、php绿色集成环境、php独立安装版环境这三者的区别
最近有学生问我,直接使用PHP集成环境和我们自己独立安装的php环境有什么不一样吗? 答:PHP集成环境,和自己安装的php环境实际上没啥区别的,只不过大部分的集成环境进行了一些绿化操作,本质上没啥区 ...
- [HMLY]8.Cocoa
cocoa是苹果公司为mac os x所创建的原声面向对象API,是mac os x上五大API之一,其他四个是carbon,posiX,x11,java. 苹果的面向对象开发框架,用来生成 Mac ...
- Xtrabackup 使用stream输出并压缩备份
mysql:5.6.29xtrabackup:2.2.10mysql数据目录:/data/mysqlmysql备份目录:/data/dbbak/full #确保有足够的磁盘空间 1.安装依赖 yum ...
- Android学习笔记(一)Git相关配置及使用
一.配置 打开Git Bash, git config --global user.name "username" git config --global user.email & ...
- socket 心跳包机制
心跳包的发送,通常有两种技术 方法1:应用层自己实现的心跳包 由应用程序自己发送心跳包来检测连接是否正常,大致的方法是:服务器在一个 Timer事件中定时 向客户端发送一个短小精悍的数据包,然后启动 ...
- centos下安装Jenkins轻松搞定
jenkins安装步骤如下: 命令:yum -y list java* yum -y install java-1.7.0-openjdk.x86_64 ...
- udp接收
char receive_buffer[500] = {0}; std::vector<std::string> mysplit(std::string str,std::string p ...
- Sonatype Nexus 服务启动失败问题解决
Sonatype Nexus 服务启动失败问题解决 问题前述: 近日在开发机本机安装了 Oracle 数据库快捷版 11g2 之后,重启电脑后发现本机的maven代理服务无法访问. 现象 通过 Win ...
- Interrupts