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<iostream>
#include<cstdio>
#include<cstring> #define N 100007
#define mod 1000000007
#define ll long long using namespace std;
ll n,m,ans,cnt;
ll fa[N],siz[N],num[N];
bool vis[N]; inline ll read()
{
ll x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';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=;
n=read();m=read();
for(ll i=;i<=n;i++) fa[i]=i,siz[i]=;
for(ll i=;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=;i<=n;i++)
{
ll now=find(i);
if(vis[now]) continue;vis[now]=;
if(num[now]>) continue;
if(num[now]==) ans=(ans*)%mod;
if(!num[now]) ans=(ans*siz[now])%mod;
}
printf("%lld\n",ans%mod);
return ;
}
 

P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)的更多相关文章

  1. P3043 [USACO12JAN]牛联盟Bovine Alliance——并查集

    题目描述 给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数. (友情提示:每个点只能分到一条边,中文翻译有问题,英文原版有这样一句:The cows ...

  2. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  3. [USACO12JAN]牛联盟Bovine Alliance

    传送门:https://www.luogu.org/problemnew/show/P3043 其实这道题十分简单..看到大佬们在用tarjan缩点,并查集合并.... 蒟蒻渣渣禹都不会. 渣渣禹发现 ...

  4. P3043 [USACO12JAN]牛联盟(并查集+数学)

    (m<n<=1e5,有重边) 题目表述有问题..... 给定一张图(不一定联通),每条边可以选择连接的两个点之一,剩余的点可以自己成对,问方案数. 一开始是真的被吓到了....觉得可写性极 ...

  5. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  6. BZOJ4998星球联盟——LCT+并查集(LCT动态维护边双连通分量)

    题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成 联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两 ...

  7. 【bzoj4998】星球联盟 LCT+并查集

    题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...

  8. BZOJ1051:受欢迎的牛(并查集 / Tarjan)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8161  Solved: 4460 Description ...

  9. 牛客练习赛16 C 任意点【并查集/DFS/建图模型】

    链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...

随机推荐

  1. 调用系统文件管理器选择图片,调用系统裁剪AIP对图片处理,显示裁剪之后的图片

    package com.pingyijinren.test; import android.annotation.TargetApi; import android.app.Notification; ...

  2. Ubuntu系统备份工具大全(官方整理推荐)

    其实官方在系统备份这块已经有Wiki整理和收集各类实用的工具.以下是翻译自官方Wiki的部分文档: 备份工具  wiki文档实用程序 工具 界面 格式类型 Raw/File 支持 远程 增量 差异 自 ...

  3. JSP处理XML数据

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/xml-data.html: 当通过HTTP发送XML数据时,使用JSP处理传入和传出的XML文件是有意义 ...

  4. 如何探测浏览器是否开启js功能

    <body> ... ... <script type="text/javascript"> <!-- document.write("He ...

  5. javascript statically scope

    在javascript 里面, 函数中使用的未定义的变量,会默认变为全局的变量. 而通过 var 这个关键字定义的变量,就是局部变量. As far as the output is concerne ...

  6. Spring Boot中微信全局token的缓存实现

    为什么要缓存token? 这里的token指的是微信JSAPI中基础支持的ACCESS_TOKEN,并非网页授权ACCESS_TOKEN.网页授权Token每天的调用次数没有限制,不需要缓存. 接口 ...

  7. iOS8使用TouchID

    iOS8新增了LocalAuthentication框架,用于TouchID的授权使用.亲測,眼下须要用户的设备支持指纹识别并已设置锁屏,并且实际測试过程中反馈比較慢.不能直接跟第三方账号passwo ...

  8. leetcode笔记:Search in Rotated Sorted Array

    一.题目描写叙述 二.解题技巧 因为这道题出现了旋转的情况,即比第一个元素小的元素可能出如今数值的后半段或者不出现. 因此.能够考虑採用变种的二分查找,即在比較中间元素与目标之前,先比較第一个元素与目 ...

  9. java图片处理工具之-ImageMagick+jmagick(二)

    简单的图片处理測试类: public class ImageUtil { static{           System.setProperty("jmagick.systemclassl ...

  10. [项目机会]使用lync的统一通信平台

    [项目内容]基于微软的lync或者其他思科.腾讯等公司技术,建立一个员工统一的通讯平台,可以随时的保持员工之间沟通. [项目价值] 1.保持项目团队成员之间的随时随地高效的电话或者视频会议: 2.集成 ...