[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 路面修整 一开始没有想到离散化.然后离散化之后就 ...
随机推荐
- 2019牛客多校第五场 G subsequence 1 dp+组合数学
subsequence 1 题意 给出两个数字串s,t,求s的子序列中在数值上大于t串的数量 分析 数字大于另一个数字,要么位数多,要么位数相同,字典序大,位数多可以很方便地用组合数学来解决,所以只剩 ...
- hrtf virtual surround matlab实现
将5.1 ch的数据经过hrtf处理,然后downmix到2ch,使得2ch的数据有virtual surround的效果. function output = hrir_process(input) ...
- [CF]Round 516
A Make a triangle! 题意:给定三根线段,问最少要延长多少才能拼成一个三角形. 数学题. B Equations of Mathematical Magic 题意:求$a - (a \ ...
- kmp算法散记
1. https://blog.csdn.net/abcjennifer/article/details/5794547 #include<bits/stdc++.h> using nam ...
- 经常犯的错误之递归写不全return
在写递归函数的时候,只在最后一层写return,中间的过程没有return,导致结果的丢失. 举个例子 LL query(LL i, LL k) { if (sum[i] < k) { ; } ...
- oracle中日期的函数的使用
--oracle中trunc()函数的用法 .select trunc(sysdate) from dual --2019-6-18 今天的日期为209-6-18 .select trunc(sysd ...
- 用户 'sa' 登录失败。该用户与可信 SQL Server 连接无关联'。错误代码:18452 解决办法
原文:https://blog.csdn.net/wuxianwei/article/details/6330270 SQLSERVER 2005采用'SQLSERVER身份验证'去登录, 出错的原因 ...
- EAC3 mantissa quantization(VQ & GAQ)
EAC3基于hebap来决定mantissa的quantizer. hebap如下: mantissa 使用VQ(vector quantization) 和GAQ(gain adaptive qua ...
- [AGC027E]ABBreviate
Description AGC027E 给定一个仅由\(AB\)构成的字符串\(S\),给定两个操作,把\(AA\)换成\(B\),和把\(BB\)换成\(A\),问由这个字符串和任意次操作可以得到几 ...
- SaltStack自动化软件简介及安装
==================================================================================================== ...