题目描述:

你有一个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 (并查集)的更多相关文章

  1. AtCoder Beginner Contest 161

    比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...

  2. AtCoder Beginner Contest 173 题解

    AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...

  3. AtCoder Beginner Contest 148 题解

    目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...

  4. AtCoder Beginner Contest 238 A - F 题解

    AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...

  5. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  6. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  7. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  8. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  9. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

随机推荐

  1. MessageBox 函数

    函数原型: int WINAPI MessageBox( _In_opt_ HWND hWnd, _In_opt_ LPCTSTR lpText, _In_opt_ LPCTSTR lpCaption ...

  2. WTL教程

    很不错的教程 http://www.yakergong.net/wtl/

  3. 11_java之接口和多态

    01接口的概念 * A:接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来 ...

  4. Rhythmk 一步一步学 JAVA(11)Ibatis 环境配置

    1.项目文件分布. 2.example1.java: package com.rhythmk.example1; import java.io.IOException; import java.io. ...

  5. C#接口的三种实现方式

    转自原文C#接口的三种实现方式 public interface MyInterface { /// 下面三个方法的签名都是 /// .method public hidebysig newslot ...

  6. github提交表情包

    emoji-list emoji表情列表 目录 人物 自然 事物 地点 符号 人物 :bowtie: :bowtie: :smile: :smile: :laughing: :laughing: :b ...

  7. Redis实战——安装问题汇总

    借鉴:https://www.cnblogs.com/liu2-/p/6914159.html 通用方法:迅速查看缺少的包的路径,并安装 yum provides *** 如 yum provides ...

  8. JS倒计时,自动提交表单!

    <form id="frm" action="http://www.baidu.com"> 考试还剩余<div id="time&q ...

  9. 关于 Apache Shiro 详解

    1.1  简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Securi ...

  10. [转]字符集、字符编码、XML中的中文编码

    字符集.字符编码.XML中的中文编码 作为程序员的你是不是对于ASCII .UNICODE.GB2321.UTF-7.UTF-8等等不时出现在你面前的这些有着奇怪意义的词感到很讨厌呢,是不是总觉得好象 ...