XJOI 3578 排列交换/AtCoder beginner contest 097D equal (并查集)
题目描述:
你有一个1到N的排列P1,P2,P3...PN,还有M对数(x1,y1),(x2,y2),....,(xM,yM),现在你可以选取任意对数,每对数可以选取任意次,然后对选择的某对数(xi,yi)进行操作,操作方式为交换xi,yi两个位置的数。最终你想要Pi=i的位置尽可能多。输出最多可以有多少个这样的位置
输入格式:
第一行输入一个整数N,第二行输入一个整数M
接下来M行每行输入一对数xi,yi
输出格式:
输出一个整数
样例输入1:
5 2
5 3 1 4 2
1 3
5 4
样例输出1:
2
样例输入2:
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
样例输出2:
8
约定:
2<=N<=105,1<=M<=105,xi!=yi
牢骚:emmm,在看到这题的第一秒我整个人就感觉不好了
记得那是我的第一场abc,报完名用fuko大佬的电脑看了开始时间,嗯,九点
后来才知道fuko大佬的电脑是东京时间QAQ
虽然三十分钟AK了,但是因为晚开了一个小时,只有四十多名orz
所以这道D题真的是影响深刻,fuko大佬大概开题后3s就口胡完了标算
是非常中(jian)规(jian)中(dan)矩(dan)的D题
下面进入题解:
考虑如果a-b能互换,b-c能互换,那么a-c也一定能互换,这其实可以扔到并查集里,到时候查询a[i]的位置与i是不是祖先相同就可以了(没错就是这么短)
代码如下:
#include<set>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,m,a[],ans; struct dsu
{
int fa[],rank[]; void init(int n)
{
for(int i=;i<=n;i++)
{
fa[i]=i;
}
} int find(int x)
{
if(fa[x]==x)
{
return x;
}
return fa[x]=find(fa[x]);
} void union_(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)
{
return ;
}
if(rank[fx]<rank[fy])
{
fa[fx]=fy;
}
else
{
fa[fy]=fx;
if(rank[fx]==rank[fy])
{
rank[x]++;
}
}
} int same(int x,int y)
{
return find(x)==find(y);
}
}b; int main()
{
cin>>n>>m;
b.init(n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<=m;i++)
{
int from,to;
scanf("%d%d",&from,&to);
b.union_(from,to);
}
for(int i=;i<=n;i++)
{
if(b.same(a[i],i))
{
ans++;
}
}
printf("%d\n",ans);
}
啊,为什么3-1这么水……
XJOI 3578 排列交换/AtCoder beginner contest 097D equal (并查集)的更多相关文章
- AtCoder Beginner Contest 161
比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- AtCoder Beginner Contest 148 题解
目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...
- AtCoder Beginner Contest 238 A - F 题解
AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
随机推荐
- windows administrator提升system
最近刚好有这个需求,本想开一个super cmd,但是win10上不兼容不太好. 于是使用PsExec来提升system权限. 微软传送地址:https://docs.microsoft.com/en ...
- Python curses getch()
window.getch([y, x]) Get a character. Note that the integer returned does not have to be in ASCII ra ...
- Android屏幕适配方案——基于最小宽度(Smallest-width)限定符
转自:https://www.cnblogs.com/error404/p/3815739.html 一.关于布局适配建议 1.不要使用绝对布局 2.尽量使用match_parent 而不是fill_ ...
- select to object 查询方法
(1)Select() (2)Where() (3)OrderBy()
- ListBox和ComboBox绑定数据简单例子
1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //自定义了Person类(有Name,Age,Heigth等属性) List<Person> per ...
- Quartz.NET文档 入门教程
概述 Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...
- 塔防游戏 Day2
1. 创建炮塔选择的 UI 使用 UI -> Toggle .注意指定同一 group. 2. 创建炮台的数据类 [System.Serializable] // 序列化 public clas ...
- redis 开发与运维 学习心得1
主要是命令相关 第一章 初识Redis 1.redis是基于键值对的NoSQL. 2.redis的值可以是 string, hash, list, set, zset, bitmaps, hyperl ...
- python:ord()和chr()——字符串和ASCll编码转换
ord()函数:字符串——>ascll编码 chr()函数:ascll编码——>字符串 #函数 for i in range(ord('d'),ord('f')+1):#拿到d和f对应的a ...
- Spring、Springboot常用注解:@Qualifier(不定时更新)
1.@Qualifier 出现场景: 老项目中有多个实现类实现同一个接口时,或者一个项目中有多个数据源时,spring容器不知道该注入哪个实现类或者使用哪个数据源,该注解就派上用场. 1)多实现类实现 ...