ARC097D Equals
题目
We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs of two integers between 1 and N (inclusive), represented as (x1,y1), (x2,y2), .., (xM,yM). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that pi=i is maximized:
Choose j such that 1 ≤ j ≤ M, and swap pxj and pyj.
Find the maximum possible number of i such that pi=i after operations.
Constraints
- 2 ≤ N ≤ 105
- 1 ≤ M ≤ 105
- p is a permutation of integers from 1 through N.
- 1 ≤ xj,yj ≤ N
- xj ≠ yj
- If i ≠ j, {xi,yi} ≠ {xj,yj}.
- All values in input are integers
Input
Input is given from Standard Input in the following format:
N M
p1 p2 .. pN
x1 y1
x2 y2
:
xM yM
Output
Print the maximum possible number of i such that pi=i after operations.
题目大意
给了一个数组,一堆pair,可随意交换任意次pair对应的下标的数,求数组里面下标等于元素值的最多对数。
分析
将初始位置的下标和元素值连边,然后将能互相交换的下标连边,然后Tarjan缩点,判断下标和元素值是否在同一联通分量中即可,注意让代表下标的点与代表元素值的点错开即可
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define pb push_back
int d[110000];
int belong[210000],cnt,sum,dfn[210000],low[210000],ist[210000];
vector<int>v[200010];
stack<int>a;
void tarjan(int x){
int i,j,k;
dfn[x]=low[x]=++cnt;
a.push(x);
ist[x]=1;
for(i=0;i<v[x].size();i++)
if(!dfn[v[x][i]]){
tarjan(v[x][i]);
low[x]=min(low[x],low[v[x][i]]);
}else if(ist[v[x][i]]){
low[x]=min(low[x],dfn[v[x][i]]);
}
if(low[x]==dfn[x]){
sum++;
while(1){
int u=a.top();
a.pop();
ist[u]=0;
belong[u]=sum;
if(u==x)break;
}
}
}
int main()
{ int n,m,i,j,k,x,y;
cin>>n>>m;
for(i=1;i<=n;i++){
cin>>d[i];
v[i].pb(d[i]+n);
v[d[i]+n].pb(i);
}
for(i=1;i<=m;i++){
cin>>x>>y;
v[x].pb(y);
v[y].pb(x);
}
for(i=1;i<=2*n;i++)
if(!dfn[i])tarjan(i);
int ans=0;
for(i=1;i<=n;i++){
if(belong[i]==belong[i+n])ans++;
}
cout<<ans<<endl;
return 0;
}
ARC097D Equals的更多相关文章
- equals变量在前面或者在后面有什么区别吗?这是一个坑点
我就不废话那么多,直接上代码: package sf.com.mainTest; public class Test { public static void main(String[] args) ...
- How to implement equals() and hashCode() methods in Java[reproduced]
Part I:equals() (javadoc) must define an equivalence relation (it must be reflexive, symmetric, and ...
- 【特种兵系列】String中的==和equals()
1. 小样示例 public static void main(String[] args) { String a = "a" + "b" + 123; Str ...
- (转)浅谈Java中的equals和==
原文地址: http://www.cnblogs.com/dolphin0520/p/3592500.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new S ...
- 浅谈Java中的equals和==(转)
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...
- List<T>Find方法,FindAll方法,Contains方法,Equals方法
假如传入的T是一个类, List<MessageInfos> MessageInfos = new List<MessageInfos>(); MessageInfos= Me ...
- 让代码重构渐行渐远系列(3)——string.Equals取代直接比较与非比较
重构背景及原因 最近由于项目组的人员在不断扩充,导致项目中代码风格各异,大有百花齐放甚至怒放之势.考虑到团队的生存与发展,经过众人多次舌战之后,最终决定项目组根据业务分成几个小分队,以加强团队管理与提 ...
- [java] 更好的书写equals方法-汇率换算器的实现(4)
[java] 更好的书写equals方法-汇率换算器的实现(4) // */ // ]]> [java] 更好的书写equals方法-汇率换算器的实现(4) Table of Content ...
- Equals和ReferenceEquals
稍微分析下一下两个方法的区别: public static bool Equals(object objA, object objB); public static bool ReferenceEqu ...
随机推荐
- Netty入门例子
新建maven项目,添加依赖 <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency&g ...
- POJ 1639 Picnic Planning:最小度限制生成树
题目链接:http://poj.org/problem?id=1639 题意: 给你一个无向图,n个节点,m条边,每条边有边权. 让你求一棵最小生成树,同时保证1号节点的度数<=k. 题解: 最 ...
- angularjs file upload插件使用总结
之前由于项目需要,决定使用angularjs做前端开发,在前两个项目中都有文件上传的功能,因为是刚接触angularjs,所以对一些模块和模块间的依赖不是很了解.都是由其他大神搭好框架,我只做些简单的 ...
- 在javascript中使用replace
javascript中使用replace功能很强大,不仅可以替换为字符串,还可以利用正则表达式,对匹配到的字符串为所欲为的进行操作,首先我们来了解一下replace的基本语法 定义和用法 replac ...
- spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持
spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...
- windows下安装配置nginx
下载nginx-1.0.11.zip, 解压到到nginx目录下 D:\nginx\conf 修改conf下的nginx.conf文件, 默认是80端口,若该端口被占则可以修改 listen 8073 ...
- Wrong Answer,Memory Limit Exceeded
错误原因: 1.在递归的时候,递归函数中忘记加返回return. 1.1做题时遇到一个奇葩错误,把它记到这里,看代码: 代码1:错误,用c++提交wrong answer,但是用g++提交却accep ...
- appium-环境搭建(二)
接着上一篇 1.基本的python环境 2.安装node.js,32位还是64位根据自己的系统来选择 3.安装.net framwork4.5 4.安装appium 安装完成后,运行appium.ex ...
- php 冒泡排序原理
$start = microtime(true); $popArr = array(6,3,23,1,5,100,399,99,66); echo '6,3,23,1,5,100,399,99 ...
- 幻想乡三连C:狂飙突进的幻想乡
题解: 不难发现,对于每一条从$S$到$T$的路径,设其$x.y$的和为$S_x.S_y$,其对答案的贡献是$a\cdot S_x+(1-a)\cdot S_y$,这是一个关于$a$的一次函数.而所有 ...