题目链接:http://codeforces.com/problemset/problem/859/E

题目大意:

  有$n$个人,$2n$个座位。

  给出这$n$个人初始的座位,和他们想坐的座位。

  每个人要么坐在原来的位置不动,要么坐到想坐的座位上,但是不能有两个人坐在同一个座位上。

  问你合法的安排座位的方案数。

题解:

考虑把每个人看成边,把每个人想坐的位置连起来,显然我们会得到一个个的联通块

我们设某个联通块的边数为$e$,点数为$v$,那么有$e>=v-1$。

$e>v$的时候显然不存在这样的方案,不过数据好像保证了合法,就随意了

$e=v$时有$2$种方案。考虑环上的一条边(环的长度大于$1$),这条边的方法确定后其他边的放法都确定了

$e=v-1$是有$v$种方案,考虑枚举哪个点不选,其他的边的放法都确定了

考虑并查集维护联通块,对每个联通块记录下$siz$它的大小,$mark$表示这个块的种类($0$表示没有环,$1$表示有自环,$2$表示有环但不是环)

当一个联通块有自环的时候方案肯定是固定的,写一个$unit$函数合并两个联通块即可

#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll; const int N=2e5+;
const int mod=1e9+;
int siz[N],mark[N],fa[N];
inline int read()
{
char ch=getchar();
int s=,f=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();};
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
int find(int x)
{
if (fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
void unit(int a,int b)
{
if (a==b)
{
mark[find(a)]=;//自环
return;
}
a=find(a);b=find(b);
if (a==b)
{
mark[a]=;//非自环的环
return;
}
fa[a]=b;
mark[b]|=mark[a];
siz[b]+=siz[a];
}
int main()
{
int n=read();
for (int i=;i<=n*;i++) fa[i]=i,siz[i]=,mark[i]=;
for (int i=,a,b;i<=n;i++)
{
a=read();b=read();
unit(a,b);
}
ll ans=;
for (int i=;i<=n*;i++)
if (fa[i]==i)
{
if (mark[i]==) ans=1ll*ans*siz[i]%mod;//没环就是树
else if (mark[i]==) ans=ans*%mod;
}
printf("%lld\n",ans);
return ;
}

[codeforces 859 E] Desk Disorder 解题报告 (并查集+思维)的更多相关文章

  1. [JZOJ3385] [NOIP2013模拟] 黑魔法师之门 解题报告(并查集)

    Description 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源.然而在与Violet星球的战争中,由于Z副官的愚蠢,地球的领袖applepi被邪恶的黑魔法师Vani囚禁在了Vi ...

  2. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  3. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  4. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  5. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  6. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  7. Codeforces Round #479 (Div. 3)解题报告

    题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...

  8. codeforces 445B. DZY Loves Chemistry 解题报告

    题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...

  9. Codeforces 437D The Child and Zoo(贪心+并查集)

    题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...

随机推荐

  1. [AHOI 2009] 同类分布

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1799 [算法] 数位DP [代码] #include<bits/stdc++. ...

  2. 【HNOI 2004】宠物收养所

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1208 [算法] 建两棵平衡树维护领养者和宠物的特点值,这两棵平衡树支持 插入删除,查 ...

  3. USACO 1.5 Superprime Rib

    Superprime Rib Butchering Farmer John's cows always yields the best prime rib. You can tell prime ri ...

  4. 10. Regular Expression Matching[H]正则表达式匹配

    题目 Given an input string(s) and a pattern(p), implement regular expression matching with support for ...

  5. POJ 3020 Hungary

    一道建图题-- // by SiriusRen #include <cstdio> #include <cstring> using namespace std; #defin ...

  6. jQuery应用实例2:简单动画

    效果: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  7. PopupWindow实现点击外部不消失

    View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_ip, null); final Po ...

  8. vue-router 设置默认路由

    加入 {path: '/', redirect: 'ratings'},vue 1.0版本版本使用go,但是在2.0中是用router.go(‘/ratings’);会一直刷新

  9. 谈 instanceof 和 typeof 的实现原理

    typeof: js 在底层存储变量的时候,会在变量的机器码的低位1-3位存储其类型信息

  10. [Atcoder Code Festival 2017 Qual A Problem D]Four Coloring

    题目大意:给一个\(n\times m\)的棋盘染四种颜色,要求曼哈顿距离为\\(d\\)的两个点颜色不同.解题思路:把棋盘旋转45°,则\((x,y)<-(x+y,x-y)\).这样就变成了以 ...