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

  1. 4
    1 5
    5 2
    3 7
    7 3

output

  1. 6

input

  1. 5
    1 10
    2 10
    3 10
    4 10
    5 5

output

  1. 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(仅有两种情况——都在原位或者都在理想位置)

  1. #include<stdio.h>
  2. #define M 1000000007
  3. #define go(i,a,b) for(int i=a;i<=b;i++)
  4. const int N=200004;
  5. long long ans=1;int n,fa[N],Type[N],siz[N],u,v,U,V;
  6. int find(int x){return fa[x]=x==fa[x]?x:find(fa[x]);}
  7. int main()
  8. {
  9. scanf("%d",&n);n<<=1;
  10. go(i,1,n)fa[i]=i,siz[i]=1;
  11. go(i,1,n/2)
  12. {
  13. scanf("%d%d",&u,&v);
  14. U=find(u),V=find(v);
  15. if(u==v){Type[U]=1;continue;}
  16. if(U==V){Type[U]=2;continue;}
  17. Type[U]|=Type[V];siz[fa[V]=U]+=siz[V];
  18. }
  19. go(i,1,n)if(i==find(i))ans=ans*(Type[i]?Type[i]:siz[i])%M;
  20. printf("%d\n",(ans%M+M)%M);return 0;
  21. }//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. SHOPEX快递单号查询插件圆通V8.2专版

    SHOPEX快递物流单号查询插件特色 本SHOPEX快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世 ...

  2. ruby Logger日志

    1.logger创建 # 输出到标准输出 logger = Logger.new(STDERR) logger = Logger.new(STDOUT) # 输出到指定文件 logger = Logg ...

  3. Qt之pro文件解析

    在我们创建Qt工程项目时,Qt Creator总会创建一个.pro文件,我们称.pro文件为Qt的工程管理文件.一个工程项目可以包含一个或多个.pro文件.理解和掌握pro文件的用法,将有利于Qt开发 ...

  4. JAVA 基础编程练习题

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

  5. Ganglia3.1.7安装与配置(收录)

    一.所需要软件 二.安装过程     1.Ganglia运行平台的安装     2.Ganglia依赖库的安装     3.RRDTool的安装     4.Ganglia的安装 (包括使用yum方式 ...

  6. axios应用

    Skip to content     Features Business Explore Marketplace Pricing Sign in or Sign up     Watch929 St ...

  7. PADS9.5打开Altium designer09的原理图

    1. 打开PADS Logic原理图工具,文件---导入 2. 选择Protel DXP这个选项,找到相应的文件即可打开.

  8. springmvc基础篇—处理图片静态资源文件

    当我们在web.xml中对DispatcherServlet的过滤设置为/ 的时候,表示对所有的路径进行拦截过滤,那么不可避免的就会产生一个问题,那就是像图片这种静态资源文件我明明引用路径有,但就是加 ...

  9. LeetCode - 67. Add Binary(4ms)

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  10. 修复 Ubuntu 中“Unable to lock the administration directory (/var/lib/dpkg/)”

    在 Ubuntu 或者它的衍生版如 Linux Mint(我已经作为日常工作使用的系统)中使用 apt-get 命令或者其相对更新的APT 管理工具时,你可能会在命令行中看到一个 unable to ...