传送门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的更多相关文章

  1. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  2. P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  3. P3043 [USACO12JAN]牛联盟Bovine Alliance——并查集

    题目描述 给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数. (友情提示:每个点只能分到一条边,中文翻译有问题,英文原版有这样一句:The cows ...

  4. P3043 [USACO12JAN]牛联盟(并查集+数学)

    (m<n<=1e5,有重边) 题目表述有问题..... 给定一张图(不一定联通),每条边可以选择连接的两个点之一,剩余的点可以自己成对,问方案数. 一开始是真的被吓到了....觉得可写性极 ...

  5. 洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery

    P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroider ...

  6. bzoj2582 [Usaco2012Jan]Bovine Alliance

    [Usaco2012Jan]Bovine Alliance Time Limit: 2 Sec Memory Limit: 128 MB Description Bessie and her bovi ...

  7. bzoj258 [USACO 2012 Jan Gold] Bovine Alliance【巧妙】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=111 传送门2:http://www.lydsy.com/JudgeOn ...

  8. 微软加入字节码联盟,进一步开发支持Blazor 的WebAssembly技术

    字节码联盟 (Bytecode Alliance)宣布已正式成为 501(c)(3) 非营利组织,参与组建的企业/组织包括 Fastly.英特尔.Mozilla 和微软,此外还邀请到了 Arm.DFI ...

  9. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

随机推荐

  1. 【HTML】三种方法使HTML单页面输入密码才能访问

    方法一 <script type="text/javascript"> function password() { var testV = 1; var pass1 = ...

  2. 【Python】BMI指数 计算器

    身体质量指数 (Body Mass Index, 简称BMI), 亦称克托莱指数, 是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准.BMI 值超标,意味着你必须减肥了. 在线版:https: ...

  3. spring的IOC过程剖析

    先看下类的关系图,看图说话

  4. 变色html css js

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  5. 使用命令把SpringBoot项目打包成可运行的jar包(简洁,操作性强)

    前几天接到一个需求,要把原系统中一个数据处理小功能搬出原系统,拉出来单独做一个SpringBoot项目,然后打成jar包扔到Windows服务器上运行,这样数据处理的时候如果遇到堵塞就不至于整个系统都 ...

  6. jenkins常用

    记最精简版的启动:gradle集成springboot+vue 安装jenkins,安装待推荐插件 服务器安装gradle 服务器安装npm 配置git仓库,配置git开发者账号等 配置gradle构 ...

  7. Yii2.0 高级版修改默认访问控制器

    frontend->config->main-local.php $config = [ 'defaultRoute' => 'index/index',//修改默认访问控制器 'c ...

  8. resize2fs: 报错

    报错如下 [root@localhost ~]# resize2fs /dev/mapper/centos-root resize2fs (-Dec-) resize2fs: Bad magic nu ...

  9. springboot+jwt

    大概了解下SpringMVC和jwt,百度 代码: 1.整体框架 2.controller package com.yiyezhiqiu.jwt.jwt.controller; import com. ...

  10. [Note]后缀数组

    后缀数组 代码 void rsort() { for (int i = 1; i <= m; ++i) tax[i] = 0; for (int i = 1; i <= n; ++i) + ...