[USACO12JAN]牛联盟Bovine Alliance
传送门:https://www.luogu.org/problemnew/show/P3043
其实这道题十分简单。。看到大佬们在用tarjan缩点,并查集合并。。。。
蒟蒻渣渣禹都不会。
渣渣禹发现,给出的图经过处理之后会出现:
环。
不是环。
不是环的情况我们有n中匹配方式(n为其点的个数)
是环的情况我们只有两种匹配方式,顺时针匹配和逆时针匹配。
所以我们dfs处理出图中有多少个环,和不是环的个数。
ans 初始为1.
遇到环ans乘二,否则ans乘n(n为这个不是环的点的个数)。
结束咯w。
#define B cout << "BreakPoint" << endl;
#define O(x) cout << #x << " " << x << endl;
#define O_(x) cout << #x << " " << x << " ";
#define Msz(x) cout << "Sizeof " << #x << " " << sizeof(x)/1024/1024 << " MB" << endl;
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#define LL long long
#define N 100005
#define p 1000000007
using namespace std;
inline int read() {
int s = 0,w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-')
w = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
int to[2 * N],nxt[2 * N],head[2 * N],vis[N],tot;
int tmp,res;
LL ans = 1;
void add(int u,int vis) {
to[++tot] = vis;
nxt[tot] = head[u];
head[u] = tot;
return ;
}
void dfs(int x) {
vis[x] = 1;
tmp++;
for(int i = head[x]; i; i = nxt[i],res++)
if(!vis[to[i]])
dfs(to[i]);
}
int n,m;
void init() {
n = read(),m = read();
for(int i = 1; i <= m; i++) {
int x = read(),y = read();
add(x,y);
add(y,x);
}
}
void solve() {
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
tmp = res = 0;
dfs(i);
if(tmp > res / 2)
ans = ans * tmp % p;
else
if(tmp == res / 2)
ans = ans * 2 % p;
else ans = 0;
}
}
printf("%lld",ans);
}
int main(){
init();
solve();
return 0;
}
[USACO12JAN]牛联盟Bovine Alliance的更多相关文章
- 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- P3043 [USACO12JAN]牛联盟Bovine Alliance——并查集
题目描述 给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数. (友情提示:每个点只能分到一条边,中文翻译有问题,英文原版有这样一句:The cows ...
- P3043 [USACO12JAN]牛联盟(并查集+数学)
(m<n<=1e5,有重边) 题目表述有问题..... 给定一张图(不一定联通),每条边可以选择连接的两个点之一,剩余的点可以自己成对,问方案数. 一开始是真的被吓到了....觉得可写性极 ...
- 洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery
P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroider ...
- bzoj2582 [Usaco2012Jan]Bovine Alliance
[Usaco2012Jan]Bovine Alliance Time Limit: 2 Sec Memory Limit: 128 MB Description Bessie and her bovi ...
- bzoj258 [USACO 2012 Jan Gold] Bovine Alliance【巧妙】
传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=111 传送门2:http://www.lydsy.com/JudgeOn ...
- 微软加入字节码联盟,进一步开发支持Blazor 的WebAssembly技术
字节码联盟 (Bytecode Alliance)宣布已正式成为 501(c)(3) 非营利组织,参与组建的企业/组织包括 Fastly.英特尔.Mozilla 和微软,此外还邀请到了 Arm.DFI ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
随机推荐
- 简单了解一个WSDL,SOAP的格式
简单了解一个WSDL,SOAP的格式 2016-06-20 09:34:43 树先生i 阅读数 5187 收藏 更多 分类专栏: WebService 版权声明:本文为博主原创文章,遵循CC 4 ...
- C++-POJ1988-Cube Stacking[数据结构][并查集]
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} #include <set> #include <map> #inc ...
- sql根据字符将一行拆成多行
SELECT B.value FROM ( SELECT [value] = CONVERT(xml,'<root><v>' + REPLACE('A,C,D', ',', ' ...
- 题解【BZOJ4145】「AMPPZ2014」The Prices
题目描述 你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\),在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i] ...
- Pacemaker+ISCSI实现Apache高可用-配置
一.配置文件系统 任意节点用ISCSI的共享磁盘创建LVM node1 pvcreate /dev/sdb vgcreate my_vg /dev/sdb lvcreate -L 1G -n web_ ...
- ansible笔记(1):ansible基本概念
一.基础概念 1.ansible是什么? ansible是一个配置管理工具,是一个自动化运维工具. 2.ansible能做什么? 它可以完成一组批量化的工作任务,或者经常重复性的工作任务.例如:a.在 ...
- LED Decorative Light Manufacturer - Led Wall Lamp Performance Characteristics
LED Decorative Light Manufacturer introduction: LED wall lamp is a light-emitting diode as a ligh ...
- AC3 mantissa quantization and decoding
1.overview 所有的mantissa被quantize到固定精确度的level(有相应的bap标识)上,level小于等于15时,使用symmetric quantization.level大 ...
- 查看Oracle的SID的方式
1 使用组合键“Win + R”打开运行对话框,在输入框中输入 regedit 并回车打开“注册表编辑器”. 2 在“注册表编辑器”对话框,依次展开 HKEY_LOCAL_MACHINE\SOF ...
- Java基础(十二)之包和权限访问
软件包 软件包解决了两个类名字一样的问题.软件包就是一个"文件夹". 包名的命名规范:1.要求所有字母都小写:2.包名一般情况下,是你的域名倒过来写.比如baidu.com,pac ...