洛谷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.

输入输出样例

输入样例#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} 
#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=1;
bool vis[maxn];
struct node{
    int to,pre;
}e[maxn*2];
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]=1;
    int cnt1=1,cnt2=0;
    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]=1;
                cnt1++;
                q.push(to);
            }
        }
    }
    cnt2/=2;
    if(cnt1==cnt2)ans=(2LL*ans)%mod;
    else if(cnt1-1==cnt2)ans=(1LL*cnt1*ans)%mod;
    else {puts("0");exit(0);}
}
int main(){
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        Insert(x,y);Insert(y,x);
    }
    for(int i=1;i<=n;i++){
        if(!vis[i])bfs(i);
    }
    cout<<ans;
}
 
时间: 2024-11-08 07:48:22

洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance的相关文章

【题解】 P3043 [USACO12JAN]牛联盟Bovine Alliance

题意:现在有$n$个点,$m$条边的图,$1 \leq m \leq n$,让点和其所连边进行配对,一个点或边只能配对一次,所有边必须配对,而点可以无需配对 这题有思维无码量还是比较友好的 现在我们有一张连通图,设它边数为$m$,点数为$n$,显然$m \ge n-1$ 根据题意,一个点只能和一条边配对,自然的若在当前图中$m > n$,则是无解的 接下来进行分类讨论 $m=n$的情况下,有点类似**基环树**的感觉...思考和环有关的性质,若现在有一条环,每个点要和一条边进行匹配,不难发现匹配

[USACO12JAN]牛联盟Bovine Alliance

传送门:https://www.luogu.org/problemnew/show/P3043 其实这道题十分简单..看到大佬们在用tarjan缩点,并查集合并.... 蒟蒻渣渣禹都不会. 渣渣禹发现,给出的图经过处理之后会出现: 环. 不是环. 不是环的情况我们有n中匹配方式(n为其点的个数) 是环的情况我们只有两种匹配方式,顺时针匹配和逆时针匹配. 所以我们dfs处理出图中有多少个环,和不是环的个数. ans 初始为1. 遇到环ans乘二,否则ans乘n(n为这个不是环的点的个数). 结束咯

洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery

P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroidery. Cows embroider a cloth mounted in a circular hoop of integer radius d (1 <= d <= 50,000). They sew N (2 <= N <= 50,000) threads, each in a s

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget

洛谷P3048 [USACO12FEB]牛的IDCow IDs

P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 谁能解释一下这个样例啊.... 题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitiou

洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The cows move from any of the N (1 <= N <= 250)

洛谷 2966 [USACO09DEC]牛收费路径Cow Toll Paths

https://www.luogu.org/problem/show?pid=2966 题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The c

洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom

https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform th

洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon

P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no more 25 of the characters 'a'..'z'. Their cowmunication system, based on mooing, is not very accurate; sometime