2019 Qinhuangdao Replenishment

F title

Link: https://codeforces.com/gym/102361/problem/F
side length idea if the ring is k, then the number of puncturing schemes edge ring is 2k-1. If the side-chain length is k, then delete the program chain number of edges is 2k. Multiplied by the number of the program chain program loop is the total number of programs
(not closed before the synchronization T ...)
Code

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring> 
using namespace std;
const int maxn=3e5+10;
const int mod=998244353;
#define ll long long 
int n,m,cnt,tot;
ll ans;
vector<int>G[maxn];
int vis[maxn],deep[maxn];
ll power(ll a,ll b)
{
    ll ans=1,x=a;
    for(;b;b>>=1)
    {
        if(b&1)
        {
            ans=ans*x%mod;
        }
        x=x*x%mod;
    }
    return ans;
}
void dfs(int u,int fa)
{   
    deep[u]=deep[fa]+1;
    for(int i=0;i<G[u].size();i++)
    {
        if(G[u][i]!=fa)
        {
            if(!vis[G[u][i]])
            {
                vis[G[u][i]]=1;
                dfs(G[u][i],u);
            }
            else if(deep[G[u][i]]<deep[u])
            {
                int x=deep[u]-deep[G[u][i]]+1;
                tot-=x;
                ans=ans*(power(2,x)-1)%mod;
            }
        }
        
    }
}
int main()
{
        int u,v;    
        ios::sync_with_stdio(false);
        cin>>n>>m;
        ans=1,tot=m;
        for(int i=1;i<=m;i++)
        {
            int u,v;
            cin>>u>>v;
            G[u].push_back(v);
            G[v].push_back(u); 
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                vis[i]=1;
                dfs(i,i);
            }
        }
        ans=ans*power(2,tot)%mod;
        cout<<ans<<"\n";
    
    return 0;
}

Guess you like

Origin www.cnblogs.com/hh13579/p/11769014.html