[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 路面修整 一开始没有想到离散化.然后离散化之后就 ...
随机推荐
- Application Comparison Of LED Holiday Light And Traditional Incandescent Lights
Obviously, LED holiday lights are leading the competition in economical Christmas decorations, but t ...
- 【Python】字符串的格式化
一一对应 符号要用英文半角形式
- jQuery尺寸
jQuery 尺寸 jQuery width() 和 height() 方法 width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包 ...
- C#设置一个控件可以鼠标拖动
C#设置一个控件可以鼠标拖动: 新建一个C#项目, 创建一个label控件, 设置label的鼠标按下和抬起事件分别为:label1_MouseDown和label1_MouseUp. 对代码进行如下 ...
- 三剑客-sed awk
目录 1.sed 2.awk 1.sed //sed命令 #语法说明:命令 参数 条件+处理= (指令) 处理文件信息 字符流编辑工具(行编辑工具)==按照每行中的字符进行处理操作 ). 擅长对行进 ...
- OpenCV离散傅里叶变换
离散傅里叶变换 作用:得到图像中几何结构信息 结论:傅里叶变换后的白色部分(即幅度较大的低频部分),表示的是图像中慢变化的特性,或者说是灰度变化缓慢的特性(低频部分). 傅里叶变换后的黑色部分(即幅度 ...
- mysql之路4
MYSQL之约束 2.主键约束 349行cnname换成cname
- Linux - Shell - cut: 低配 awk
概述 简述 shell 命令行工具 cut 背景 偶尔需要用 awk 来筛选特定的列 awk 很是强大 但是强大的背后, 却伴随着复杂 其实同样的功能, awk 也没有复杂多少 如果是 简单的任务, ...
- Jenkins Pipeline waitForQualityGate pending 超时
请参考链接:http://www.iotxing.com/2019/06/12/258/jenkins-pipeline-waitforqualitygate%E8%B6%85%E6%97%B6/ 主 ...
- Is It Good To Use LED Wall Light In Household Space?
Wall lamps are mostly used for local lighting, can play a very decorative effect, improve the visual ...