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. Java : 多态表现:静态绑定与动态绑定(向上转型的运行机制)

    本来想自己写写的,但是看到有人分析的可以说是很清晰了,故转过来. 原文地址:http://www.cnblogs.com/ygj0930/p/6554103.html 一:绑定 把一个方法与其所在的类 ...

  2. python查询mysql数据

    >>>cur.execute("select * from 表名") >>>lines=cur.fetchall() >>>f ...

  3. 静态栈抽象数据类型stack实现

    #include<stdio.h> #include<stdbool.h> #include<stdlib.h> #define MAX_STACK_SIZE 10 ...

  4. mysql5.7.19安装报错 无法定位程序输入点

    https://blog.csdn.net/t876587201/article/details/79503688

  5. 使用apache的ab压力测试时失败请求原因

    只要出现 Failed requests 就会多出现一行要求失败的各原因的数据统计,分别有 Connect, Length,与 Exception 三种,分别代表的意义为:Connect      无 ...

  6. Office使用技巧(不断补充)

    1.word中,第一行后面有很多空格,但把第二行的退到第一行来就删了第一行的字,为什么? 解决办法:应该是下一行开头部分是一个不可拆分的整体,上一行末尾放不下,只能放在下一行.处理方法:格式--段落- ...

  7. 大数据de 2文章

    点击可免费试用网易有数 文章来源:网易有数的搭积木原则阐述 ,经作者文雯授权发布 wo ceceshi 相关文章:[推荐] SpringBoot入门(五)--自定义配置

  8. ios下 active 演示激活

    document.body.addEventListener('touchstart', function () { });

  9. 【赛后补题】(HDU6228) Tree {2017-ACM/ICPC Shenyang Onsite}

    这条题目当时卡了我们半天,于是成功打铁--今天回来一看,mmp,贪心思想怎么这么弱智.....(怪不得场上那么多人A了 题意分析 这里是原题: Tree Time Limit: 2000/1000 M ...

  10. 常用的python模块及安装方法

    adodb:我们领导推荐的数据库连接组件 bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctyp ...