传送门

题目

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的更多相关文章

  1. equals变量在前面或者在后面有什么区别吗?这是一个坑点

    我就不废话那么多,直接上代码: package sf.com.mainTest; public class Test { public static void main(String[] args) ...

  2. 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 ...

  3. 【特种兵系列】String中的==和equals()

    1. 小样示例 public static void main(String[] args) { String a = "a" + "b" + 123; Str ...

  4. (转)浅谈Java中的equals和==

    原文地址: http://www.cnblogs.com/dolphin0520/p/3592500.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new S ...

  5. 浅谈Java中的equals和==(转)

    浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...

  6. List<T>Find方法,FindAll方法,Contains方法,Equals方法

    假如传入的T是一个类, List<MessageInfos> MessageInfos = new List<MessageInfos>(); MessageInfos= Me ...

  7. 让代码重构渐行渐远系列(3)——string.Equals取代直接比较与非比较

    重构背景及原因 最近由于项目组的人员在不断扩充,导致项目中代码风格各异,大有百花齐放甚至怒放之势.考虑到团队的生存与发展,经过众人多次舌战之后,最终决定项目组根据业务分成几个小分队,以加强团队管理与提 ...

  8. [java] 更好的书写equals方法-汇率换算器的实现(4)

    [java] 更好的书写equals方法-汇率换算器的实现(4) // */ // ]]>   [java] 更好的书写equals方法-汇率换算器的实现(4) Table of Content ...

  9. Equals和ReferenceEquals

    稍微分析下一下两个方法的区别: public static bool Equals(object objA, object objB); public static bool ReferenceEqu ...

随机推荐

  1. [原创]关于设置linux中vim 显示行号

    1.更改所有账户配置 直接更改/etc/vimrc vim /etc/vimrc 在vimrc文件的最后添加 set nu 即可 wq退出. 这样,不论使用哪个账号登陆,vim打开后都显示行号 2.为 ...

  2. es6对象内函数的两种写法

    es6对象内函数一般有两种写法: var person1 = { name: "p1", sayThis() { console.log(this); } }; var perso ...

  3. 好久没更了,确实太忙了--dedecms篇

    最近写了一个地方新闻网站,可以看看:www.qiluhuabao.com.模仿www.bashan.com写的 用的是dedecms,只听过,没用过.终于在上周来了一个必须用到cms的项目,现学现卖, ...

  4. zTree获取(子)节点

    var ids=[]; ids=getChildren(ids, treeNode);//TreeNode是选中节点,ids是子节点id数组,格式:123,223,4,55 1.获取直接子节点的id ...

  5. 分享知识-快乐自己:mongodb 安装部署(linux)

    1):下载 mongodb 包 [root@admin tools]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6. ...

  6. ZOJ 2724 Windows Message Queue (二叉堆,优先队列)

    思路:用优先队列 priority_queue,简单 两种方式改变队列 的优先级 (默认的是从大到小) #include<iostream> #include<queue> # ...

  7. Git_学习_01_ 常用 Git 命令清单

    我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下 ...

  8. Firefox 不知道如何打开此地址,因为协议 (javascrpit) 未和任何程序关联.

    用火狐打开出现这个错误,360没事:这个是什么原因???怎么解决?代码:<a href="javascrpit:;"onclick="showD('pas','ri ...

  9. 计算机丨浏览器访问出现DNS_PROBE_POSSIBLE解决方法

    方法1.打开命令控制台输入: netsh winsock reset.提示重启,电脑重启后就ok了. 其他方法待续......

  10. IO - 同步,异步,阻塞,非阻塞 (转帖:http://blog.csdn.net/historyasamirror/article/details/5778378)

    同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不同的人给出 ...