[On] [FIG communication block] Usaco2012Jan. Bovine Alliance

Description [title]

[洛谷P3043 Usaco2012Jan. Bovine Alliance]

(https://www.luogu.org/problemnew/show/P3043)

【analysis】

  • The meaning of problems: given a number n of edges FIG point m, the current packet and edges, and each edge adjacent to only one of the two points in the group, and a plurality of edge points not a group, but a set of points may be used alone, the number of program packets Q
  • Popular understood that: either a group for each side, and a point adjacent to it, or a single point.
  • Each communication block independent analysis, each provided with a communication block n points, m edges obviously $ m> = n-1 $
  • If $ m> n $: there must be an independent edge, no solution
  • If $ m = n $: ring tree, two cases
  • If $ m = n-1 $: tree, there are n (i.e. each isolated point)
  • As for how to find the communication block? Disjoint-set ......

    [Code]

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+10;
const ll mod=1000000007;
int fa[N],b[N],c[N],n,m,w[N];
ll ans=1;
inline int find(int x){
    if(fa[x]==x) return x;
    return find(fa[x]);
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) fa[i]=i,b[i]=1;
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        int fu=find(u),fv=find(v);
        if(fu!=fv){
            fa[fu]=fv;
            b[fv]+=b[fu];
            b[fu]=0;
            c[fv]+=c[fu]+1;
        }
        else c[fv]++;
    }
    for(int i=1;i<=n;i++){
        if(fa[i]!=i) continue;
        if(c[i]>b[i]){
            ans=0;
            break;
        }
        if(c[i]==b[i]) ans=ans*2%mod;
        if(c[i]==b[i]-1) ans=ans*b[i]%mod;
    }
    cout<<ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/JRTQX/p/11067919.html