[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 路面修整 一开始没有想到离散化.然后离散化之后就 ...
随机推荐
- python3练习100题——015
原题链接:http://www.runoob.com/python/python-exercise-example15.html 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用 ...
- 使用Limit实现分页
limit语法 #语法 SELECT * FROM table LIMIT stratIndex,pageSize SELECT * FROM table LIMIT 5,10; // 检索记录行 6 ...
- 家庭记账小账本Android studio
经过长达7天多的时间,从Android studio的安装碰到很多的问题,真的很不走运,别人安装Android studio都是一气呵成,我的Android真的没话说 把自己的这些天的开发成果展示一下 ...
- lnmt
目录 1.nginx安装与配置 1.1安装nginx 1.2nginx安装后的配置 2.mysql安装与配置 2.1安装mysql 2.2mysql配置 3.部署tomcat 3.1java环境安装 ...
- MySQL数据库 | 数据表-查询命令详细记录
本篇专门记录数据库增删改查中最常用.花招最多的 查. [文章结构] 一.数据的准备 二.基本的查询功能 三.条件查询 四.查询排序 五.聚合函数 六.分组查询 七.分页查询 八.连接查询 九.子查询 ...
- php设计模式之责任链模式实现举报功能实例代码
html <html> <head> <meta charset="UTF-8"> <title>责任链模式</title&g ...
- UPF set_port_attribute
『set_port_attribute』, 在IEEE 1801-2015 中该命令定义如下,不是所有的工具都支持所有的option: 这个命令用于描述port 在『未知』区域的power 连接情况, ...
- 题解 P2320 【[HNOI2006]鬼谷子的钱袋】
P2320 [HNOI2006]鬼谷子的钱袋 挺有趣的一道题,之所以发这篇题解是因为感觉思路的更清晰一点qwq 此题主要有两种方法: 一.分治思想 例如要凑出1~20,假如我们已经能凑出1~10了,那 ...
- thinkphp中 model(模型)的使用
首先:有三个数据表 现在用命令行建立它们的模型 php think make:model admin/Class php think make:model index/Teacher
- [Vue源码]一起来学Vue双向绑定原理-数据劫持和发布订阅
有一段时间没有更新技术博文了,因为这段时间埋下头来看Vue源码了.本文我们一起通过学习双向绑定原理来分析Vue源码.预计接下来会围绕Vue源码来整理一些文章,如下. 一起来学Vue双向绑定原理-数据劫 ...