Codeforces Gym 100463E Spies 并查集
Spies
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100463/attachments
Description
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 并查集的更多相关文章
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
- CodeForces 455C Civilization (并查集+树的直径)
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...
- Codeforces 650C Table Compression (并查集)
题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...
- Codeforces 468B Two Sets 并查集
题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...
- CodeForces - 893C Rumor【并查集】
<题目链接> 题目大意: 有n个人,其中有m对朋友,现在你有一个秘密你想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉它的所有朋友(他朋友的朋友也会免费知道),现在 ...
- Gym - 101550A Artwork (并查集在线做法)
题目链接 题意:给你一个n*m的网格图,初始时格点全白,每次可以将一段连续的格点涂黑.求出每次操作之后白色连通块的数量. 看了看网上的题解,基本全是离线的做法.其实这道题是有在线的做法的,利用了对偶图 ...
- CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...
随机推荐
- Delphi 实现16进制转字符串及字符串(中文)转16进制
//-----------------------------------------------//16进制字符转整数,16进制字符与字符串转换中间函数//--------------------- ...
- win下 golang 跨平台编译
mac 下编译其他平台的执行文件方式请参看这篇文章,http://www.cnblogs.com/ghj1976/archive/2013/04/19/3030703.html 本篇文章是win下的 ...
- SynchronousQueue
SynchronousQueue是一个没有数据缓冲的BlockingQueue,生产者线程对其的插入操作put必须等待消费者的移除操作take,反过来也一样. 不像ArrayBlockingQueue ...
- Python开发者最常犯的10个错误
Python是一门简单易学的编程语言,语法简洁而清晰,并且拥有丰富和强大的类库.与其它大多数程序设计语言使用大括号不一样 ,它使用缩进来定义语句块. 在平时的工作中,Python开发者很容易犯一些小错 ...
- Java 断点调试总结
为了准备调试,你需要在代码中设置一个断点先,以便让调试器暂停执行允许你调试,否则,程序会从头执行到尾,你就没有机会调试了. 1. 条件断点 断点大家都比较熟悉,在Eclipse Java 编辑区的行头 ...
- <bgsound>和<embed></embed>标签 頁面插入音樂
■ <BGSOUND> 是用以插入背景音乐,但只适用於 IE,其参数设定不多. <BGSOUND src="liberation.mp3"; autostart= ...
- Tomcat启动报错:Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]
在用MyEclipse做开发,启动Tomcat的时候,控制台老是报错Failed to initialize end point associated with ProtocolHandler [&q ...
- Jquery 操作页面中iframe自动跟随窗口大小变化,而页面不出现滚动条,只在iframe内部出滚动条
很多时候大家需要iframe自适应所加载的页面高度而不要iframe滚动条,但是这次我需要的是页面不需要滚动条而iframe要滚动条,且iframe自动跟随窗口大小变化.自适应页面大小.下面是代码,下 ...
- WinHTTP Web Proxy Auto-Discovery Service
下面是网上搜集的,个人没有做测试,----------------------------- WinHTTP Web Proxy Auto-Discovery Service 服务成功发送一个 开始 ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...