【CF MEMSQL 3.0 E. Desk Disorder】
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.
How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7.
Input
Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers.
N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk.
Output
Print the number of possible assignments, modulo 1000000007 = 109 + 7.
Examples
input
4
1 5
5 2
3 7
7 3
output
6
input
5
1 10
2 10
3 10
4 10
5 5
output
5
Note
These are the possible assignments for the first example:
- 1 5 3 7
- 1 2 3 7
- 5 2 3 7
- 1 5 7 3
- 1 2 7 3
- 5 2 7 3
【翻译】有2*n个座位,输入每一个人的原位和理想位置,现在需要为每一个人分配一个独一无二的位置使得每个人坐在理想位置或者原位置上。输出方案数(%1000000007)。
题解:
①使用建图分来讨论来转化问题。
②使用有向边从原位指向理想位置:
仅有如下三种情况:
(1)一个自环或者一堆链状结构最终止于一个自环
(2)一根或者一堆链状结构最终止于一个不与其他点和自身成环的点
(3)形成环(非自环)
那么对于(1),ans*=1(只用一种),对于(2)ans*=节点数(任意一个点可以移动一次),
对于(3)ans*=2(仅有两种情况——都在原位或者都在理想位置)
#include<stdio.h>
#define M 1000000007
#define go(i,a,b) for(int i=a;i<=b;i++)
const int N=200004;
long long ans=1;int n,fa[N],Type[N],siz[N],u,v,U,V;
int find(int x){return fa[x]=x==fa[x]?x:find(fa[x]);}
int main()
{
scanf("%d",&n);n<<=1;
go(i,1,n)fa[i]=i,siz[i]=1;
go(i,1,n/2)
{
scanf("%d%d",&u,&v);
U=find(u),V=find(v);
if(u==v){Type[U]=1;continue;}
if(U==V){Type[U]=2;continue;}
Type[U]|=Type[V];siz[fa[V]=U]+=siz[V];
}
go(i,1,n)if(i==find(i))ans=ans*(Type[i]?Type[i]:siz[i])%M;
printf("%d\n",(ans%M+M)%M);return 0;
}//Paul_Guderian
【CF MEMSQL 3.0 E. Desk Disorder】的更多相关文章
- 【CF MEMSQL 3.0 A. Declined Finalists】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF MEMSQL 3.0 C. Pie Rules】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF MEMSQL 3.0 D. Third Month Insanity】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF MEMSQL 3.0 B. Lazy Security Guard】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF edu 27 G. Shortest Path Problem?】
time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...
- 【CF Round 439 E. The Untended Antiquity】
time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...
- 【CF Round 439 C. The Intriguing Obsession】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【CF Round 439 B. The Eternal Immortality】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【Magicodes.IE 2.0.0-beta1版本发布】已支持数据表格、列筛选器和Sheet拆分
为了更好的完善Magicodes.IE,春节期间我们会进行一次大的重构.由于精力有限,急缺文档和翻译(将文档翻译为英文文档)支持,诚邀各位加入.同时在功能方便也做了相关规划,有兴趣的朋友可以参与提交P ...
随机推荐
- 关于Linux中mysql中文乱码
1.SHOW VARIABLES LIKE 'character_set_%';查看编码集 2.编辑/etc/my.cnf文件 加入这个设置 default-character-set=utf8 (这 ...
- 3.从print到I/O
为何对双引号念念不忘? >>> print("hello, world!") hello, world! 平x而论,既然在意双引号的去掉,为何不在意括号的去掉 ...
- Ruby字符串的一些方法
最近因为公司需求开始看ruby,先从ruby的基本数据类型开始看 看到ruby的字符串类型string,发现ruby中的字符串单双引号是不一样的,这点和Python有那么点不一样 主要是我们对字符串进 ...
- python2.7入门---Number(数字)
今天咱们来简单分享一下关于python中的一种数据类型和操作方法.费话不多说哈,咱们直接来进行实践加理论.首先,我们要知道,Python Number 数据类型用于存储数.数据类型是不允许改变 ...
- 链接程序的时候遇到问题:fatal error LNK1104: cannot open file 'rctrl-d.lib'
1.lib库文件没有添加到工程中(工程里面根本就没有这个文件) 2.
- jsp 添加jstl标签
jsp页面中添加下列代码即可使用jstl标签. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=" ...
- C++学习004-Go To 语句使用
C++中,goto语句主要负责语句的跳转,可以用在循环中跳出循环 注意gotu语句是无条件跳转,用的时候一定要谨慎,一定要少 编写环境 Qt 5.7 for(int i = 0;i<100;i+ ...
- ASP NET Core --- HTTP 翻页、过滤、排序
参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/v07647j3zkq.html 翻页, 过滤, 排序等 – 如何传递参数? Query ...
- linux下 su 与 su - 的区别和使用
Linux下su与su -命令的区别 在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用servic ...
- LeetCode 410——分割数组的最大值
1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...