ABC226E - Just one (连通图+思维)

链接

题意:

给出n个点m条无向边,让你构造出每个节点只有一个出度的图,问有多少种?

分析:

开始读错题了。读成每个节点可以有大于等于1的出度
我们知道正确题意之后,进行解读,要求每个节点只有一个出度得知,在一个连通图中边数一定只能有节点数这么多,这样就有两种选择,所以我们就将问题转化成找合法的连通图,所谓合法就是边与连通图中的节点相等,

我选择用并查集维护,并且维护边数点数。

/// 欲戴皇冠,必承其重。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef unsigned long long ull;

#define x first
#define y second
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define debug(x) cout << #x << ": " << x << endl;

const int MOD = 998244353;
const int mod = 998244353;
const int N = 5e5 + 10;
const int dx[] = {0, 1, -1, 0, 0, 0, 0};
const int dy[] = {0, 0, 0, 1, -1, 0, 0};
const int dz[] = {0, 0, 0, 0, 0, 1, -1};
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll n, m,p;
ll a[N],b[N],c[N];
ll ans;
ll f(ll x){
    if(a[x]==x) return x;
    return a[x]=f(a[x]);
}
void solve()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) a[i]=i,b[i]=1,c[i]=1;
    for(int i=1;i<=m;i++){
        ll x,y;
        cin>>x>>y;
        if(x>y) swap(x,y);
        ll fx,fy;
        fx=f(x);
        fy=f(y);
        if(fx==fy){
            b[fx]++;
            continue;
        }else {
            a[fx]=fy;
            c[fy]+=c[fx];
            b[fy]+=b[fx];
        }
    }
    ll ans=1;
    for(int i =1 ;i<=n;i++){
        if(a[i]==i){
            if(b[i]==c[i]+1){
                ans=ans*2%mod;
            }else {
                puts("0");
                return ;
            }
        }
    }
    cout<<ans<<endl;
}    
int main()
{
    ll t = 1;
    ///scanf("%lld", &t);
    while(t--)
    {
        solve();
    }
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值