Spies

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100463/attachments

Description

In the aftermath of Canada’s annexation of Pittsburgh tensions have been pretty high between Canada and the US. You have personally been hired by the US government to track down a number of Canadian spies. Through CIA intelligence the location of a number of spies is partially known. Specifically the CIA knows there are N (1 ≤ N ≤ 100000) spies and have narrowed down each spy to one or two locations. ”Oh My Golly!” exclaims the president. ”There could be as many as 2N possible arrangments for the spies”! ”Not quite, Mr. President. You see the Canadian government would not waste resources by sending two spies to the same location.” Now the task comes to. You are to compute how many ways the spies could be arranged. As this number could be quite large compute it modulo 1, 000, 000, 007.

Input

Input will consist of multiple test cases. The first line of each case will contain N and M separated by a space (1 ≤ N, M ≤ 100, 000) giving the number of spies and the number of cities respectively. After that will be N lines each containing two integers u and v (0 ≤ u, v < M) indicating the two possible locations of a spy. Interpret lines where u = v as indicating the spy is known to be at location u. The input is terminated with a line containing two zeroes.

Output

For each test case output the case number followed by the number of ways the spies could be arranged modulo 1, 000, 000, 007. Two arrangements are considered different if any spy has a different location. If there is no way to arrange the spies in different cities output 0. Follow the format in the sample output.

Sample Input

4 5 0 1 1 2 3 4 4 3 3 2 0 0 1 1 0 1 0 0

Sample Output

Case 1: 6 Case 2: 0

HINT

题意

有一堆人,每个人可能存在两个地方,每个地方最多一个人,然后问你有多少种情况

题解:

看起来好麻烦,我当成dp搞了好久……Orz

结果是一个并查集就好了,把人都连边处理成链状,然后就吼了!

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int fa[maxn];
bool uni[maxn];
int num[maxn];
int n,m;
int sz[maxn];
int fi(int x)
{
return fa[x]==x?x:fa[x]=fi(fa[x]);
}
void me(int x,int y)
{
int rx=fi(x),ry=fi(y);
uni[rx]|=uni[ry];//如果有一个确定了,那么显然与他合并的都确定了
num[rx]++;
if(x==y)
{
uni[rx]=;
return;
}
if(rx!=ry)
{
sz[rx]+=sz[ry];
num[rx]+=num[ry];
}
fa[ry]=rx;
}
int solve()
{
int res=;
for(int i=;i<m;i++)
{
if(fa[i]==i)
{
if(num[i]>sz[i])
return ;
if(num[i]==sz[i])
res=res*(uni[i]?1ll:2ll)%mod;
else
res=1ll*res*sz[i]%mod;
}
}
return res%mod;
}
int main()
{
int t=;
while(scanf("%d%d",&n,&m)==)
{
t++;
if(n==&&m==)
break;
for(int i=;i<m;i++)
fa[i]=i;
for(int i=;i<m;i++)
sz[i]=;
memset(num,,sizeof(num));
memset(uni,,sizeof(uni));
//上面都是奇怪的初始化
for(int i=;i<=n;i++)
{
int x=read(),y=read();
me(x,y);
}
printf("Case %d: %d\n",t,solve());
}
}

Codeforces Gym 100463E Spies 并查集的更多相关文章

  1. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  2. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  3. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

  4. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  5. Codeforces 650C Table Compression (并查集)

    题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...

  6. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

  7. CodeForces - 893C Rumor【并查集】

    <题目链接> 题目大意: 有n个人,其中有m对朋友,现在你有一个秘密你想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉它的所有朋友(他朋友的朋友也会免费知道),现在 ...

  8. Gym - 101550A Artwork (并查集在线做法)

    题目链接 题意:给你一个n*m的网格图,初始时格点全白,每次可以将一段连续的格点涂黑.求出每次操作之后白色连通块的数量. 看了看网上的题解,基本全是离线的做法.其实这道题是有在线的做法的,利用了对偶图 ...

  9. CodeForces 566D Restructuring Company (并查集+链表)

    题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...

随机推荐

  1. ERROR:The requested URL could not be retrieved解决方法

    ERROR 错误 The requested URL could not be retrieved 您所请求的网址(URL)无法获取 While trying to retrieve the URL: ...

  2. [整] Android Fragment 生命周期图

    1. onAttach ------called once the fragment is associated with its activity 2. onCreate-------called ...

  3. Java并发编程-可重入锁

    可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍可以获取该锁而不受影响.在JAVA环境下 ReentrantLock 和synchronized 都是 可重入锁. publ ...

  4. python+selenium环境搭建

    这里主要基于windows平台. 下载python.http://python.org/getit/ 下载setuptools [python的基础包工具].http://pypi.python.or ...

  5. selenium python (十三)对于分页的处理

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #对于web上分页的功能,一般做如下操作:    #获取总页数    # ...

  6. loadrunner下检查点乱码情况处理

    对于很多用过LR的人来说,乱码一直是很纠结的事情,尤其是对新手来说.网上给的解决方法是在录制的时候勾选UTF-8选项,但是似乎并没有解决. 对于用户名为中文或者检查点为中文的情况,我们又该如何去处理呢 ...

  7. Linux基本命令(7)文件阅读的命令

    文件阅读的命令 命令 功能 head 查看文件的开头部分 tail 查看文件结尾的10行 less less是一个分页工具,它允许一页一页地(或一个屏幕一个屏幕地)查看信息 more more是一个分 ...

  8. Red Hat Linux认证

    想系统的学习一下Linux,了解了一些关于Red Hat Linux认证的信息.整理如下. 当前比较常见的是RHCE认证,即Red Hat Certified Engineer.最高级别的是RHCA ...

  9. MyEclipse10导入工程jsp报错问题

    好多时候,再用myecplise进行项目开发的时候,遇到导入工程的时候,工程内的jsp页面好多都报错.这是什么原因造成的呢?​ 我对于我遇到的问题及解决方法,跟大家分享一下.​ 我的Jsp页面报错的原 ...

  10. [WebService]之Schema

    schema入门 1:schema出现的目的是通过一个更加合理的方式来编写xml文件的限制(以XML语法的方式) 2:schema可以使用命名空间来支持多个名称相同的元素 3:schema可以很好的的 ...