bzoj2582 [Usaco2012Jan]Bovine Alliance
[Usaco2012Jan]Bovine Alliance
Time Limit: 2 Sec Memory Limit: 128 MB
Description
Bessie and her bovine pals from nearby farms have finally decided that they are going to start connecting their farms together by trails in an effort to form an alliance against the farmers. The cows in each of the N (1 <= N <= 100,000) farms were initially instructed to build a trail to exactly one other farm, for a total of N trails. However months into the project only M (1 <= M < N) of these trails had actually been built. Arguments between the farms over which farms already built a trail now threaten to split apart the cow alliance. To ease tension, Bessie wishes to calculate how many ways the M trails that exist so far could have been built. For example, if there is a trail connecting farms 3 and 4, then one possibility is that farm 3 built the trail, and the other possibility is that farm 4 built the trail. Help Bessie by calculating the number of different assignments of trails to the farms that built them, modulo 1,000,000,007. Two assignments are considered different if there is at least one trail built by a different farm in each assignment.
给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数。
Input
Line 1: Two space-separated integers N and M
Lines 2..1+M: Line i+1 describes the ith trail. Each line contains two space-separated integers u_i and v_i (1 <= u_i, v_i <= N, u_i != v_i) describing the pair of farms connected by the trail. Note that there can be two trails between the same pair of farms.
Output
- Line 1: A single line containing the number of assignments of trails to farms, taken modulo 1,000,000,007. If no assignment satisfies the above conditions output 0.
Sample Input
5 4
1 2
3 2
4 5
4 5
Sample Output
6
HINT
OUTPUT DETAILS: There are 6 possible assignments. Letting {a,b,c,d} mean that farm 1 builds trail a, farm 2 builds trail b, farm 3 builds trail c, and farm 4 builds trail d, the assignments are: {2, 3, 4, 5} {2, 3, 5, 4} {1, 3, 4, 5} {1, 3, 5, 4} {1, 2, 4, 5} {1, 2, 5, 4}
大概就是分情况讨论一下;
首先很多个联通块就用乘法原理就好了
每一个联通块中,如果边数 > 点数,那直接就凉了,对吧。
两个刚好相等的时候就是一个环,环可以顺带很多条链,但是只有一个中心环。。因为环可以正反转,所以是 2
如果是一颗树,你可以把那个倒霉的点找出来,因为一旦你决定了那个点没有边,你蝴蝶效应其他就都定下来了。。。所以贡献是点数
统计答案即可。。。
```c++
include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, mod = 1e9 + 7;
int n, m, fa[maxn], size[maxn], p[maxn];
set s;
set::iterator iter;
long long ans = 1;
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 find(int t){return fa[t] == t ? t : (fa[t] = find(fa[t]));}
int main()
{
n = read(); m = read();
for(int i = 1; i <= n; ++i) fa[i] = i, p[i] = 1;
for(int a, b, A, B, i = 1; i <= m; ++i){
a = read(); b = read();
A = find(a); B = find(b); size[A]++;
if(A == B) continue;
if(A > B) swap(A, B);
fa[B] = A; size[A] += size[B]; p[A] += p[B];
}
for(int i = 1; i <= n; ++i) s.insert(find(i));
for(iter = s.begin(); iter != s.end(); ++iter){
int now = *iter;
if(size[now] > p[now]){cout << 0; return 0;}
if(size[now] == p[now]) ans = ans * 2 % mod;
if(size[now] < p[now]) ans = ans * p[now] % mod;
}
cout << ans;
return 0;
}
bzoj2582 [Usaco2012Jan]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 ...
- bzoj258 [USACO 2012 Jan Gold] Bovine Alliance【巧妙】
传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=111 传送门2:http://www.lydsy.com/JudgeOn ...
- P3043 [USACO12JAN]牛联盟Bovine Alliance——并查集
题目描述 给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数. (友情提示:每个点只能分到一条边,中文翻译有问题,英文原版有这样一句:The cows ...
- [USACO12JAN]牛联盟Bovine Alliance
传送门:https://www.luogu.org/problemnew/show/P3043 其实这道题十分简单..看到大佬们在用tarjan缩点,并查集合并.... 蒟蒻渣渣禹都不会. 渣渣禹发现 ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- POJ - 2183 Bovine Math Geniuses
“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数.注意到仔细看题,这种题一定要细心! POJ - 2183 Bovine Math Geniuses Ti ...
- POJ 3047 Bovine Birthday 日期定周求 泽勒公式
标题来源:POJ 3047 Bovine Birthday 意甲冠军:.. . 思考:式 适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2* ...
随机推荐
- Python-编码这趟浑水
最近听Alex讲到python编码,还特意用博客讲解,觉得问题严重了,于是翻看各种博客,先简单的对编码错误做一个总结,其他的后续慢慢补上,还得上班.还得学习.还得写博客?感觉有点吃不消了.各位大神不喜 ...
- 一、WebApi模型验证实践项目使用
一.启语 前面我们说到,模型验证的原理(包含1.项目创建,2.模型创建,3.走通测试模型验证,4.在过滤器中处理返回json格式(非控制器内))-完全是新手理解使用的,新番理解 通常情况下,对于那些经 ...
- rabbitmq3.7集群搭建实战
环境: 3台 centos7.4rabbitmq3.7erlang 22 1. 有几种方式安装,这里使用的yum安装(官方推荐)2. 使用rabbitmq时需要安装erlang,在各个节点上使用vim ...
- python常用函数 P
popleft(iterable) 对应pop,左侧弹出,队列适用. 例子: permutations(iterable, int) itertools的permutations方法可以产生集合的所有 ...
- ppt怎么制作抖音快手快闪效果的倒计时动画?
ppt怎么制作快闪效果的倒计时动画? 1.首先,我们新建一个ppt,如下图: 2.然后我们在ppt中插入一个文本,文本内容为3,如下图: 3.然后我们将我们的文本设置为“Arial Black”,如下 ...
- python基础:3.高级运算符
1.异或运算 十进制的异或运算,先转成二进制进行异或,按位进行比较,对应位置相同则为0,对应位置不同则为1,,再从异或结果转成十进制. python中: 1 ^ 1 = 0 1 ^ 2 = 3 1 ^ ...
- Redis Key过期事件
解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态,如果未支付,则进行处理但是key过期了redis有通知吗?答案是肯定 ...
- 项目中有 xxxx 不能被json序列化
遇到这类问题 ,首先断点调试,看看要序列化的值 是一个什么类型的值 查看值得数据类型 在将值转化成可以被json序列化的对象 此时即可解决问题 如遇到 requests.post() 朝一个url发 ...
- 20175120彭宇辰 《Java程序设计》第十一周学习总结
教材内容总结 第十三章 Java网络编程 一.URL类 一个URL对象包含的三个基本信息:协议.地址和资源. -协议:必须是URL对象所在的Java虚拟机支持的协议,常用的有:Http.Ftp.Fil ...
- hdu 5564 Clarke and digits
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5564 ------------------------------------------------ ...