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 是不是在同一集合中. 析:很容易知道是用并 ...
随机推荐
- 基于opencv的人脸检测的web应用
参考资料 https://github.com/bsdnoobz/web-based-face-detect http://opencv-code.com/projects/web-based-int ...
- cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用
cocos2dx的3.x版本已经提供了更好地绑定方式,网上有很多相关的教程,这里给一个链接:http://www.cocoachina.com/bbs/read.php?tid=196416. 由于目 ...
- Java学习日志-01-Hello World
1.安装JDK1.7 2.安装eclipse 3.eclipse上写第一个java程序-hello world 先建工程,再建包,养成良好的习惯,然后新建类 若不先建立包,可能会提示"The ...
- focus与定位
<html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...
- XML转换为Map通用算法实现 Java版本(Stax实现)
目前项目中需要将XML转换为Map,下面给出了自己的代码实现. 后续将为大家提供Dom版本的实现. 请各路大神给予各种优良实现. 场景: 在项目中需要解析XML文本字符串,需要将XML文本字符串映射为 ...
- 使用MockMvc测试Spring mvc Controller
概述 对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便 ...
- javascript压缩图片无刷新上传
首先要说明,该方式我没有实现成功,主要问题点是压缩后能拿到压缩后图片地址,确不知道怎么上传上去,如果有哪位大牛可以指点一二,万分感谢. 图片压缩代码: compress: function(sourc ...
- PhysX
[PhysX] 1.施加力: ))) { //施加一个力,X轴方向力度为1000,Y轴方向力度为1000 addFrceObj.rigidbody.AddForce (, , ); } ))) { / ...
- JSON如何序列图片
图片采用JSON格式传输的话,需要把图片转成Base64格式. 首先把图片转成流:Image1.Picture.Graphic.SaveToStream(ss);然后编码成base64格式的:Enco ...
- CCF 201312-5 I’m stuck! (暴力,BFS)
问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七个字符中的一个,分别表示如下意思: '#': 任何时候玩家都不能移动到此方格 ...