洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance
P3043 [USACO12JAN]牛联盟Bovine Alliance
题目描述
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条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数。
输入输出格式
输入格式:
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.
输出格式:
- 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.
输入输出样例
说明
Note that there can be two trails between the same pair of farms.
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}
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstdlib>
#define maxn 100010
#define mod 1000000007
using namespace std;
int n,m,num,head[maxn];
long long ans=;
bool vis[maxn];
struct node{
int to,pre;
}e[maxn*];
void Insert(int from,int to){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
void bfs(int s){
queue<int>q;
q.push(s);vis[s]=;
int cnt1=,cnt2=;
while(!q.empty()){
int now=q.front();q.pop();
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
cnt2++;
if(!vis[to]){
vis[to]=;
cnt1++;
q.push(to);
}
}
}
cnt2/=;
if(cnt1==cnt2)ans=(2LL*ans)%mod;
else if(cnt1-==cnt2)ans=(1LL*cnt1*ans)%mod;
else {puts("");exit();}
}
int main(){
scanf("%d%d",&n,&m);
int x,y;
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
Insert(x,y);Insert(y,x);
}
for(int i=;i<=n;i++){
if(!vis[i])bfs(i);
}
cout<<ans;
}
洛谷P3043 [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——并查集
题目描述 给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数. (友情提示:每个点只能分到一条边,中文翻译有问题,英文原版有这样一句:The cows ...
- [USACO12JAN]牛联盟Bovine Alliance
传送门:https://www.luogu.org/problemnew/show/P3043 其实这道题十分简单..看到大佬们在用tarjan缩点,并查集合并.... 蒟蒻渣渣禹都不会. 渣渣禹发现 ...
- 洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery
P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroider ...
- P3043 [USACO12JAN]牛联盟(并查集+数学)
(m<n<=1e5,有重边) 题目表述有问题..... 给定一张图(不一定联通),每条边可以选择连接的两个点之一,剩余的点可以自己成对,问方案数. 一开始是真的被吓到了....觉得可写性极 ...
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
- 洛谷P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...
- 洛谷 P3048 [USACO12FEB]牛的IDCow IDs
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...
- 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
随机推荐
- Hibernate学习---第十二节:Hibernate之锁机制&乐观锁实现
1.悲观锁 它指的是对数据被外界修改保持保守态度,因些,在整个数据处理过程中,将数据牌锁定状态.悲观锁的实现,往往依靠数据库提供的锁机制(也只有数据库层的锁机制才能保证数据访问的排他性,否则,即使在本 ...
- AngularJS学习笔记(二) 表单验证案例(ng-repeat/filter)
这一节相对来说需要理解的东西不是太多,记住了那些api就行了. 还是一个案例(同样来自miaov),一个表单验证,先上代码,然后再对对应的内容进行解释. <!DOCTYPE html> & ...
- Appium-appium日志分析
查看日志是很重要的一部分,我们在编辑器上测试代码时可以直接进行调试,但持续集成时程序自动运行,如果出现bug,只能通过日志来定位代码位置.appium日志主要分为三部分. 1. 准备阶段,包含了app ...
- 简单使用XStream
Stream1.什么作用 *可以把JavaBean转换为(序列化为)xml2.XStream的jar *核心jar:xstream-1.4.7.jar *必须依赖包:xpp3_min-1.1.4c(X ...
- linux下导入导出oracle的dmp文件
1.导出dmp件 命令:exp QGTG/\"QGTG@orcl\" file=/usr/fuck.dmp exp QGTG/\"QGTG@orcl\" fil ...
- FFMPEG基于内存的转码实例——输入输出视频均在内存
我在6月份写了篇文章<FFMPEG基于内存的转码实例>,讲如何把视频转码后放到内存,然后通过网络发送出去.但该文章只完成了一半,即输入的数据依然是从磁盘文件中读取.在实际应用中,有很多数据 ...
- HTML5视音频标签参考
本文将介绍HTML5中的视音频标签和对应的DOM对象.是相关资料的中文化版本,可以作为编写相关应用的简易中文参考手册. 一些约定 所有浏览器:指支持HTML5的常见桌面浏览器,包括IE9+.Firef ...
- AtCoder Grand Contest 015 题解
A - A+...+B Problem 常识 Problem Statement Snuke has N integers. Among them, the smallest is A, and th ...
- 移动端H5 button 默认事件
button 在移动端下会有自带的默认事件,如果不处理的话,点击按钮的时候会有自动刷新页面的效果,原因就是button的默认事件没有阻止. 所以在点击事件里面要加上 e.preventDefault( ...
- dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型
概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺点以及使用场景,最终结合本身项目的实际情况选择了使用dubbox作为rpc基础服务框架.下面就简单介绍一下RPC框架技术选型的过 ...