博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
阅读量:5111 次
发布时间:2019-06-13

本文共 3125 字,大约阅读时间需要 10 分钟。

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.

 

输入输出样例

输入样例#1: 
5 4 1 2 3 2 4 5 4 5
输出样例#1: 
6

说明

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}
/*可以并查集维护可以发现,某个联通快出现大于等于2个环,一定无法分配。有解要么一个环,要么没有环。一个环时答案等于点数乘2(顺时针或逆时针)。没有环是树,对于一个n个点的树,方案一定有n种(不连某个点)。*/#include
#include
#include
#define N 100007#define mod 1000000007#define ll long longusing namespace std;ll n,m,ans,cnt;ll fa[N],siz[N],num[N];bool vis[N];inline ll read(){ ll x=0,f=1;char c=getchar(); while(c>'9'||c<'0'){
if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f;}ll find(ll x){
return x==fa[x]?x:fa[x]=find(fa[x]);}void merge(ll x,ll y){ fa[y]=x; siz[x]+=siz[y];num[x]+=num[y];}int main(){ ll x,y;ans=1; n=read();m=read(); for(ll i=1;i<=n;i++) fa[i]=i,siz[i]=1; for(ll i=1;i<=m;i++) { x=read();y=read(); ll r1=find(x),r2=find(y); if(r1!=r2) merge(r1,r2); else num[r1]++; } for(ll i=1;i<=n;i++) { ll now=find(i); if(vis[now]) continue;vis[now]=1; if(num[now]>2) continue; if(num[now]==1) ans=(ans*2)%mod; if(!num[now]) ans=(ans*siz[now])%mod; } printf("%lld\n",ans%mod); return 0;}

 

 

转载于:https://www.cnblogs.com/L-Memory/p/7757968.html

你可能感兴趣的文章
回调没用,加上iframe提交表单
查看>>
(安卓)一般安卓开始界面 Loding 跳转 实例 ---亲测!
查看>>
Mysql 索引优化 - 1
查看>>
LeetCode(3) || Median of Two Sorted Arrays
查看>>
大话文本检测经典模型:EAST
查看>>
待整理
查看>>
一次动态sql查询订单数据的设计
查看>>
C# 类(10) 抽象类.
查看>>
Vue_(组件通讯)子组件向父组件传值
查看>>
jvm参数
查看>>
我对前端MVC的理解
查看>>
Silverlight实用窍门系列:19.Silverlight调用webservice上传多个文件【附带源码实例】...
查看>>
2016.3.31考试心得
查看>>
mmap和MappedByteBuffer
查看>>
Linux的基本操作
查看>>
转-求解最大连续子数组的算法
查看>>
对数器的使用
查看>>
【ASP.NET】演绎GridView基本操作事件
查看>>
ubuntu无法解析主机错误与解决的方法
查看>>
尚学堂Java面试题整理
查看>>