【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 ...
随机推荐
- Java : java基础(4) 线程
java开启多线程的方式,第一种是新建一个Thread的子类,然后重写它的run()方法就可以,调用类的对象的start()方法,jvm就会新开一个线程执行run()方法. 第二种是类实现Runabl ...
- php-5.6.26源代码 - hash存储结构 - hash算法
// zend_inline_hash_func 实现在文件“php-5.6.26\Zend\zend_hash.h” h = zend_inline_hash_func(arKey, nKeyLen ...
- python应用:经纬度匹配
需要安装第三方包:requests 本次经纬度匹配采用高德地图api,首先将gps坐标转化为高德地图的经纬度坐标,然后再根据转化后的坐标进行匹配. 本次匹配主要是获取距离给定经纬度最近的poi点地址信 ...
- stm32+lwip(四):网页服务器测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- 10---git安装
卸载原来的版本: # 查看版本 git --version # 移除原来的版本 yum remove git 安装依赖库: yum install curl-devel expat-devel get ...
- Odoo8中安装新模块找不到的问题
为了要让系统识别出新的模块,我们需要打开用户的技术特性选项,具体在 左侧栏目->用户->administrator, 将技术特性勾选上,刷新. 然后左侧栏目->模块下面就会 ...
- struts2官方 中文教程 系列七:消息资源文件
介绍 在本教程中,我们将探索使用Struts 2消息资源功能(也称为 resource bundles 资源绑定).消息资源提供了一种简单的方法,可以将文本放在一个视图页面中,通过应用程序,创建表单字 ...
- RTSC和XDCTool的理解
1. 在使用TI的开发工具CCS中,里面有几个重要的概念,一直不太清晰,RTSC是什么,XDCTool是什么?包是什么?包的版本为啥都是4位的(比如mathlib_c66x_3_0_1_1)?star ...
- 理解JAVA常量池
下面是一些String相关的常见问题: String中的final用法和理解final StringBuffer a = new StringBuffer("111");final ...
- Linux上Makefile管理java项目
前面文章讲到了Linux上通过.spec文件与rpmbuild命令将java程序打包为RPM安装包, 现阶段遇到新的需求: 使用Makefile来操纵java的编译.打包 该需求以前面的内容为基础 可 ...