bzoj2582 [Usaco2012Jan]Bovine Alliance

本文针对 USACO 2012 January 赛题 Bovine Alliance,探讨了如何通过图论方法解决该问题。主要内容包括:分析题目背景、输入输出格式、算法思路讲解及代码实现细节。

[Usaco2012Jan]Bovine Alliance

Time Limit: 2 Sec Memory Limit: 128 MB

Description

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条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数。

Input

  • 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. Note that there can be two trails between the same pair of farms.

Output

  • 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.

Sample Input

5 4

1 2

3 2

4 5

4 5

Sample Output

6

HINT

OUTPUT DETAILS: 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
如果是一颗树,你可以把那个倒霉的点找出来,因为一旦你决定了那个点没有边,你蝴蝶效应其他就都定下来了。。。所以贡献是点数
统计答案即可。。。


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, mod = 1e9 + 7;
int n, m, fa[maxn], size[maxn], p[maxn];
set<int> s;
set<int>::iterator iter;
long long ans = 1;

inline int read()
{  
   int s = 0, w = 1;  char ch = getchar();  
   while(ch <= '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}  
   while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();  
   return s * w;  
}  

int find(int t){return fa[t] == t ? t : (fa[t] = find(fa[t]));}

int main()
{
    n = read(); m = read();
    for(int i = 1; i <= n; ++i) fa[i] = i, p[i] = 1;
    for(int a, b, A, B, i = 1; i <= m; ++i){
        a = read(); b = read();
        A = find(a); B = find(b); size[A]++;
        if(A == B) continue;
        if(A > B) swap(A, B); 
        fa[B] = A; size[A] += size[B]; p[A] += p[B];
    }
    for(int i = 1; i <= n; ++i) s.insert(find(i));
    for(iter = s.begin(); iter != s.end(); ++iter){
        int now = *iter;
        if(size[now] > p[now]){cout << 0; return 0;}
        if(size[now] == p[now]) ans = ans * 2 % mod;
        if(size[now] < p[now]) ans = ans * p[now] % mod;
    }
    cout << ans;
    return 0;
}

转载于:https://www.cnblogs.com/LLppdd/p/9224641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值