洛谷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; }
相关文章推荐
- 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing
- [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos
- 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
- 搜索-洛谷P3040 [USACO12JAN]贝尔分享Bale Share
- 【USACO12JAN】视频游戏的连击Video Game Combos
- P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
- [USACO12Jan][luogu3041] Video Game Combos [AC自动机+dp]
- 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share
- 洛谷P1561 [USACO12JAN]爬山Mountain Climbing(贪心)
- [USACO12JAN]爬山Mountain Climbing
- [USACO12JAN]Video Game Combos
- [USACO12JAN]Video Game Combos
- P1561 [USACO12JAN]爬山Mountain Climbing
- P3041 [USACO12JAN]视频游戏的连击Video Game Combos
- [USACO 12JAN]Mountain Climbing
- USACO 5.4 Betsy's Tour(暴力)
- 【bzoj1606】 【Usaco2008 Dec】Hay For Sale (背包dp)题解&代码
- [Usaco2011][bzoj2442][洛谷2527]修剪草坪解题报告(dp,贪心,单调队列)
- USACO3.43Electric Fence(pick定理)
- 【bzoj1604】【Usaco2008 Open】Cow Neighborhoods (set+曼哈顿距离性质+并查集)奶牛的邻居