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

  1. 【CF MEMSQL 3.0 A. Declined Finalists】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. 【CF MEMSQL 3.0 C. Pie Rules】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. 【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 ...

  4. 【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 ...

  5. 【CF edu 27 G. Shortest Path Problem?】

    time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...

  6. 【CF Round 439 E. The Untended Antiquity】

    time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...

  7. 【CF Round 439 C. The Intriguing Obsession】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. 【CF Round 439 B. The Eternal Immortality】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. 【Magicodes.IE 2.0.0-beta1版本发布】已支持数据表格、列筛选器和Sheet拆分

    为了更好的完善Magicodes.IE,春节期间我们会进行一次大的重构.由于精力有限,急缺文档和翻译(将文档翻译为英文文档)支持,诚邀各位加入.同时在功能方便也做了相关规划,有兴趣的朋友可以参与提交P ...

随机推荐

  1. FireDAC内存表

    procedure TForm1.FormCreate(Sender: TObject); Var i:integer; begin // i:=; self.FDMemTable1.FieldDef ...

  2. Hive(1)-基本概念

    一. 什么是Hive Hive:由Facebook开源用于解决海量结构化日志的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. 本 ...

  3. vi-vim常用命令

    vi-vim常用命令 1 简介 在UNIX系统中,创建和修改配置文件.shell脚本.初始化文件.编写程序都离不开VI. 1      vi[1]属于两个主要的UNIX规范:POSIX和单一UNIX规 ...

  4. python数据类型及其特有方法

    一.运算符 in方法 "hello" in "abcdefghijklmnopqrstuvwxyz" "li" in ["gg&q ...

  5. latex02-LaTeX源文件的基本结构

    1.一个latex文件有且仅有一个document环境 %后表示注释 2.latex的导言区用于全局设置. 例如:title\author\date 加入空行是结构更加清晰 为了能正确的使用标题信息, ...

  6. Python3爬虫(四)请求库的使用requests

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.基本用法: 1. 安装: pip install requests 2. 例子: import request ...

  7. python2.7练习小例子(十)

        10):古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?     程序分析:兔子的规律为数列1,1 ...

  8. JQuery中的load()、$.get()和$.post()详解 (转)

    load() 1.载入HTML文档 load()方法是jQuery中最为简单和常用的Ajax方法,能载入远程HTML代码并插入DOM中. 它的结构为: load(url [,data][,callba ...

  9. 12 TCP服务器 进程 线程 非阻塞

    1.单进程服务器 from socket import * serSocket = socket(AF_INET, SOCK_STREAM) # 重复使用绑定的信息 serSocket.setsock ...

  10. 【java并发编程实战】第七章:取消与关闭

    停止线程的几种方式 一般的逻辑停止 public class ThreadInterruptTest { public static volatile boolean cancel = true; p ...