您的位置:首页 > 其它

洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

2017-11-09 19:30 363 查看

P3043[USACO12JAN]牛联盟BovineAlliance

题目描述

Bessieandherbovinepalsfromnearbyfarmshavefinallydecidedthattheyaregoingtostartconnectingtheirfarmstogetherbytrailsinanefforttoformanallianceagainstthefarmers.ThecowsineachoftheN(1<=N<=100,000)farmswereinitiallyinstructedtobuildatrailtoexactlyoneotherfarm,foratotalofNtrails.HowevermonthsintotheprojectonlyM(1<=M<N)ofthesetrailshadactuallybeenbuilt.

Argumentsbetweenthefarmsoverwhichfarmsalreadybuiltatrailnowthreatentosplitapartthecowalliance.Toeasetension,BessiewishestocalculatehowmanywaystheMtrailsthatexistsofarcouldhavebeenbuilt.Forexample,ifthereisatrailconnectingfarms3and4,thenonepossibilityisthatfarm3builtthetrail,andtheotherpossibilityisthatfarm4builtthetrail.HelpBessiebycalculatingthenumberofdifferentassignmentsoftrailstothefarmsthatbuiltthem,modulo1,000,000,007.Twoassignmentsareconsidereddifferentifthereisatleastonetrailbuiltbyadifferentfarmineachassignment.

给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数。

输入输出格式

输入格式:

Line1:Twospace-separatedintegersNandM

Lines2..1+M:Linei+1describestheithtrail.Eachlinecontainstwospace-separatedintegersu_iandv_i(1<=u_i,v_i<=N,u_i!=v_i)describingthepairoffarmsconnectedbythetrail.

输出格式:

Line1:Asinglelinecontainingthenumberofassignmentsoftrailstofarms,takenmodulo1,000,000,007.Ifnoassignmentsatisfiestheaboveconditionsoutput0.

输入输出样例

输入样例#1:复制
54
12
32
45
45


输出样例#1:复制
6


说明

Notethattherecanbetwotrailsbetweenthesamepairoffarms.

Thereare6possibleassignments.Letting{a,b,c,d}meanthatfarm1buildstraila,farm2buildstrailb,farm3buildstrailc,andfarm4buildstraild,theassignmentsare:

{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>
#definemaxn100010
#definemod1000000007
usingnamespacestd;
intn,m,num,head[maxn];
longlongans=1;
boolvis[maxn];
structnode{
intto,pre;
}e[maxn*2];
voidInsert(intfrom,intto){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
voidbfs(ints){
queue<int>q;
q.push(s);vis[s]=1;
intcnt1=1,cnt2=0;
while(!q.empty()){
intnow=q.front();q.pop();
for(inti=head[now];i;i=e[i].pre){
intto=e[i].to;
cnt2++;
if(!vis[to]){
vis[to]=1;
cnt1++;
q.push(to);
}
}
}
cnt2/=2;
if(cnt1==cnt2)ans=(2LL*ans)%mod;
elseif(cnt1-1==cnt2)ans=(1LL*cnt1*ans)%mod;
else{puts("0");exit(0);}
}
intmain(){
scanf("%d%d",&n,&m);
intx,y;
for(inti=1;i<=m;i++){
scanf("%d%d",&x,&y);
Insert(x,y);Insert(y,x);
}
for(inti=1;i<=n;i++){
if(!vis[i])bfs(i);
}
cout<<ans;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: