【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
题目链接:
http://codeforces.com/problemset/problem/698/B
http://codeforces.com/problemset/problem/699/D
题目大意:
通过给定当前节点的父亲给你一棵有错的树,可能有多个根和环,输出改成正确的一棵树至少要修改几个节点的父亲和修改后所有点的父亲值
题目思路:
【并查集】【模拟】
用并查集把成环的归在一起(类似强连通分量),然后统计分量数并修改。
第一个出现的当作根,其余的每一块连通分量都去掉一条边改为连接到根上。
//
//by coolxxx
////<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 200004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int root;
int a[N],fa[N];
int zhao(int aa)
{
if(fa[aa]== || fa[aa]==aa)return aa;
return (fa[aa]=zhao(fa[aa]));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,fx,fy;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
while(~scanf("%d",&n))
// while(~scanf("%d",&n))
{
mem(fa,);ans=;root=;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==i)root=i;
fx=zhao(a[i]);
fy=zhao(i);
if(fx!=fy)
fa[fy]=fx;
}
for(i=;i<=n;i++)
{
fa[i]=zhao(i);
if(fa[i]==i)
{
if(!root)
{
ans++;
root=i;
a[i]=i;
}
else if(i!=root)
ans++,a[i]=root;
}
}
printf("%d\n",ans);
for(i=;i<=n;i++)
printf("%d ",a[i]);
puts("");
}
return ;
}
/*
// //
*/
【并查集】【模拟】Codeforces 698B & 699D Fix a Tree的更多相关文章
- 【codeforces 698B】 Fix a Tree
题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 洛谷 - P4997 - 不围棋 - 并查集 - 模拟
https://www.luogu.org/problemnew/show/P4997 首先是改变气的定义,使得容易计算,这个很好理解. 然后使用并查集,因为要维护整个连通块的性质. 最后的难点在于, ...
- Codeforces Round #363 Fix a Tree(树 拓扑排序)
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...
- 【并查集】HDU 1325 Is It A Tree?
推断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no ...
随机推荐
- 10.7 noip模拟试题
楼[问题背景]zhx 为他的妹子造了一幢摩天楼.[问题描述]zhx 有一幢摩天楼. 摩天楼上面有 M 个观光电梯,每个观光电梯被两个整数
- php+正则将字符串中的字母数字和中文分割
原文出处 如果一段字符串中出现字母数字还有中文混排的情况,怎么才能将他们区分开呢,经过一番思索,得到了如下代码,分享给大家 如:$str="php如何将字 符串中322的字母数字sf f45 ...
- Axiom3D学习日记 5.Frame Listeners, and Input Handling
Frame Listeners In Ogre's C++, we would register a class to receive notification before and after a ...
- 前端过滤XSS攻击
日常开发过程中,对于存在用户交互的一些门户网站等,过滤xss攻击是必不可少的. 此处主要记录下我在工作过程中的简单处理方法. 前端过滤XSS攻击, 我这里用的是开源工程 js-xss,官网地址:htt ...
- 从XML文件中获取格式化的文本信息
在FMW的运维过程中,时常需要将中间传输的XML信息转换为excel格式化的问题提交给关联系统人员,现总结三种格式化问题提供方式 一.使用Excel转换 因为从系统中取到的xml文档为中间信息文档,需 ...
- C# RSA
using System; using System.Security.Cryptography; using System.Text; class RSACSPSample { static voi ...
- jquery元素查找方法集锦
jQuery常用的元素查找方法总结 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到 ...
- Fatal error: Class 'ZipArchive' not found的解决办法
今天在Linux底下编写导出EXCEL文件并显示输出时,抛出“ZipArchive library is not enabled” 的异常.而我在本地的windows下的代码则是运行正常的. 原因是: ...
- [HOWTO] Install Sphinx for A Script Pro
Hi, Here's a small howto on installing Sphinx Search (http://sphinxsearch.com/) and configuring it t ...
- TatukGIS-TGIS_Editor.CreateShape
procedure CreateShape(const _layer: TObject; const _ptg: TGIS_Point3D; const _type: TGIS_ShapeType; ...