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 是不是在同一集合中. 析:很容易知道是用并 ...
随机推荐
- Learning Vector
题意: 给出n组x,y增量,从(0,0)开始以x,y坐标增加后等到的终点坐标,可以构成一个面积,再以这个终点为起点再增加,以此类推,使用增量顺序不同,得到的面积不,求用k组增量能得到的最大的面积. 分 ...
- 【转】linux之ln命令
转自:http://www.cnblogs.com/peida/archive/2012/12/11/2812294.html ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位 ...
- C++学习之路--类的构建以及数据转换存储
注意理解下面的代码,数据的处理与转换. 头文件: #ifndef STACK_H #define STACK_H class Stack { struct Link { void* data; Lin ...
- php 的简单易用的调式方法,打印方法
简单的调试方法:echo, print_r, var_dump, exit, debug_backtrace(), debug_print_backtrace(), gettype(), get_cl ...
- 各种边框样式。。本以为border不是这么用的。
关于文本框样式□ 文本框样式 □ 显示虚线边框的文本框(IE5.5才可看到效果) <INPUT style="border-width: 1px,1px,1px,1px;border ...
- Jquery 等待ajax返回数据loading控件ShowLoading组件
1.意义 开发项目中,前台的页面要发请求到服务器,服务器响应请求返回数据到前台,这段时间,有可能因为返回的数据量较大导致前台页面出现短暂性的等待,此时如果用户因不知情而乱点击有可能造成逻辑混乱,所以此 ...
- SharePoint咨询师之路:设计之前的那些事三:体系结构
提示:本系列只是一个学习笔记系列,大部分内容都可以从微软官方网站找到,本人只是按照自己的学习路径来学习和呈现这些知识.有些内容是自己的经验和积累,如果有不当之处,请指正. 咨询师更多的时候是解决方案提 ...
- 实体框架 (EF) 入门 => 四、CodeFirst 枚举支持
当使用 Code First 开发时,通常是从编写用来定义概念(域)模型的 .NET Framework 类开始. 插入记录没有为 Budget 赋值. 数值类型默认值为0,数据库中都为not nul ...
- Hadoop MapReduce概念学习系列之mr程序组件全貌(二十)
其实啊,spilt是,控制Apache Hadoop Mapreduce的map并发任务数,详细见http://www.cnblogs.com/zlslch/p/5713652.html map,是m ...
- 深刻理解Python中的元类metaclass(转)
本文由 伯乐在线 - bigship 翻译 英文出处:stackoverflow 译文:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上很热 ...