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}\) ...
随机推荐
- [Python] WeChat_Robot
在微信中接入一个聊天机器人 1. WeChat 个人接口itchat 2. 图灵机器人 #-*- coding:utf-8 -*- import itchat import requests apiU ...
- 基于AT UI实现表格的增删改查遇到的坑
基于AT UI实现表格的增删改查遇到的坑 坑一.表格数据加载的渲染报错 报错:Error in render: "TypeError: Cannot read property 'isChe ...
- Python 多进程使用
进程通信 方式一.共享内存(进程安全,效率高) 共享变量:multiprocessing.Value 共享数组:multiprocessing.Array 方式二.Manager对象:list, ...
- rtmp一些状态信息详解-as连接FMS服务器报错状态汇总~~
原地址:http://help.adobe.com/zh_CN/AIR/1.5/jslr/flash/events/NetStatusEvent.html 下表说明了 code 和 level 属性可 ...
- MySQL 执行 'use databases;' 时很慢
问题描述: 就是这么个情况,登录数据库切换库时感觉很卡,需要等待几秒钟. 案例: shell > mysql -uroot -ppassword mysql> use databases; ...
- python中heapq堆的讲解
堆的定义: 堆是一种特殊的数据结构,它的通常的表示是它的根结点的值最大或者是最小. python中heapq的使用 列出一些常见的用法: heap = []#建立一个常见的堆 heappush(hea ...
- python时间戳、日期、时间转换
1.str转时间戳 # 字符类型的时间 tss1 = '2013-10-10 23:40:00' # 转为时间数组 timeArray = time.strptime(tss1, "%Y-% ...
- 从零玩转JavaWeb系列7web服务器-----get与post的区别
总结get与post的区别 get参数通过url传递,post放在request body中. get请求在url中传递的参数是有长度限制的,而post没有. get比post更不安全,因为参数直接暴 ...
- angularjs学习访问服务器(5)
(1) 后台AngularController.java代码 package com.amy.controller; import java.util.ArrayList; import java.u ...
- DataTable相关
设置主键列: this.tableTestData.PrimaryKey = new DataColumn[] { this.tableTestData.Columns[0] };