题目链接:http://codeforces.com/contest/859/problem/E

题意:有N个人。2N个座位。现在告诉你这N个人它们现在的座位。以及它们想去的座位。每个人可以去它们想去的座位或者就站在原地不动。新的座位和旧的座位,都不允许一个座位被两个人占据的情况。问你安排的方案数。

解法:对于这N个点,N条边构成的图,我们应该对每个连通块独立计算答案,最后乘起来。如果n个点,n-1条边答案显然为n。如果n个点n条边,会出现一个环,且恰好只有一个环。如果是一个自环,那么答案是1,因为所有人都不能动。如果环的大小>=2的话,答案为2。

维护环直接用并查集即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
const int mod = 1e9+7;
namespace DSU{
int fa[maxn], cycle[maxn], sz[maxn];
void init(){
for(int i=1; i<maxn; i++) fa[i] = i, sz[i] = 1, cycle[i] = 0;
}
int find_set(int x){
if(x == fa[x]) return x;
else return fa[x] = find_set(fa[x]);
}
void union_set(int x, int y){
int fx = find_set(x);
int fy = find_set(y);
if(fx == fy){
cycle[fx] = 1;
}else{
fa[fy] = fx;
sz[fx] += sz[fy];
cycle[fx] |= cycle[fy];
}
}
}
using namespace DSU; int main()
{
int n;
cin >> n;
init();
long long ans = 1;
for(int i=1; i<=n; i++){
int x, y;
cin >> x >> y;
if(x == y){
cycle[find_set(x)] = 2;
continue;
}
union_set(x, y);
}
for(int i=1; i<=2*n; i++){
if(find_set(i) == i){
if(cycle[i] == 1) ans = ans*2%mod;
else{
if(cycle[i] == 0) ans = ans * sz[i]%mod;
}
}
}
cout << ans << endl;
return 0;
}

Codeforces 859E Desk Disorder 并查集找环,乘法原理的更多相关文章

  1. Codeforces 859E Desk Disorder:并查集【两个属性二选一】

    题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的 ...

  2. Codeforces Round #346 (Div. 2) E题 并查集找环

    E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...

  3. cf246 ENew Reform (并查集找环)

    Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each ...

  4. bzoj1116 [POI2008]CLO——并查集找环

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1116 分析性质,只要有环,那么给环定一下向就满足了条件: 环上点的其他边可以指向外面,所以两 ...

  5. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  6. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  7. CodeForces 455C Civilization(并查集+树直径)

    好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环, ...

  8. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  9. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

随机推荐

  1. view的阴影效果shadowColor

    btn.layer.shadowColor = UIColor.blackColor().CGColor btn.layer.shadowOffset = CGSizeMake(5, 5) btn.l ...

  2. Mysql向数据库插入数据时,判断是否存在,若不存在就插入数据

    表中一定要有主键  : select :id,此处的id位置处必须是主键 insert into table_name(id, name, password) select :id, :name, : ...

  3. poj2761 feed the dog

    题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...

  4. Linux内核分析第八周——进程的切换和系统的一般执行过程

    Linux内核分析第八周--进程的切换和系统的一般执行过程 李雪琦+原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/cou ...

  5. Mac OS 装gdb

    1 要求按照mac ports 2 命令:sudo port install gdb 3 安装位置在: /opt/local/bin/ggdb , 注意,ggdb是执行命令

  6. vue入门教程

    vue视频教程(对vue有个概览,要掌握vue-cli的用法,对vue-router,vuex有基本的概念) https://www.imooc.com/learn/1091 1. vue-cli v ...

  7. 【Asp.net入门5-02】创建数据模型和存储库

  8. python 模块之hashlib

    Hashlib模块 Python里面的hashlib模块提供了很多加密的算法,这里介绍一下hashlib的简单使用事例,用hashlib的md5算法加密数据,其他的所有加密算法使用方式上基本类似. h ...

  9. PHP基础知识之————匿名函数(Anonymous functions)

    匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应用的情况. ...

  10. core EFCore 开始尝试

    准备工作: 工程:core + console 引用包: Install-Package Microsoft.EntityFrameworkCore Install-Package Microsoft ...