zoukankan      html  css  js  c++  java
  • 2019CCPC秦皇岛 F Forest Program

    队友过的:https://blog.csdn.net/liufengwei1/article/details/101632506

    Forest Program

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 124    Accepted Submission(s): 47


    Problem Description
    The kingdom of Z is fighting against desertification these years since there are plenty of deserts in its wide and huge territory. The deserts are too arid to have rainfall or human habitation, and the only creatures that can live inside the deserts are the cactuses. In this problem, a cactus in desert can be represented by a cactus in graph theory.
    In graph theory, a cactus is a connected undirected graph with no self-loops and no multi-edges, and each edge can only be in at most one simple cycle. While a tree in graph theory is a connected undirected acyclic graph. So here comes the idea: just remove some edges in these cactuses so that the remaining connected components all become trees. After that, the deserts will become forests, which can halt desertification fundamentally.
    Now given an undirected graph with n vertices and m edges satisfying that all connected components are cactuses, you should determine the number of schemes to remove edges in the graph so that the remaining connected components are all trees. Print the answer modulo 998244353.
    Two schemes are considered to be different if and only if the sets of removed edges in two schemes are different.
     
    Input
    The first line contains two non-negative integers n, m (1 ≤ n ≤ 300 000, 0 ≤ m ≤ 500 000), denoting the number of vertices and the number of edges in the given graph.
    Next m lines each contains two positive integers u, v (1 ≤ u, v ≤ n, u = v), denoting that vertices u and v are connected by an undirected edge.
    It is guaranteed that each connected component in input graph is a cactus.
     
    Output
    Output a single line containing a non-negative integer, denoting the answer modulo 998244353.
     
    Sample Input
    3 3 1 2 2 3 3 1 6 6 1 2 2 3 3 1 2 4 4 5 5 2
     
    Sample Output
    7 49
     
    Source

    题解:

    找出所有环,每个环至少选择一条边删掉,那么方案数就是2^size-1,不在环上的边为m条,可以随便删,方案数就是2^resm。

    点双抄一遍就过了,也可以直接dfs

     
    参考代码:
    #include<bits/stdc++.h>
    #define maxl 500010
    using namespace std;
     
    const int mod=998244353;
     
    int n,m,top,cnt,ind,sum,rt,dcccnt;
    vector <int> dcc[maxl];
    long long ans;
    int dfn[maxl],low[maxl],ehead[maxl],s[maxl];
    long long num[maxl];
    bool in[maxl],cut[maxl];
    struct ed
    {
      int to,nxt;
    }e[maxl<<1];
     
    inline void add(int u,int v)
    {
      e[++cnt].to=v;e[cnt].nxt=ehead[u];ehead[u]=cnt;
    }
     
    inline void tarjan(int u)
    {
      dfn[u]=low[u]=++ind;s[++top]=u;
      if(u==rt && ehead[u]==0)
        {
          dcc[++dcccnt].push_back(u);
          return;
        }
      int son=0,v;
      for(int i=ehead[u];i;i=e[i].nxt)
        {
          v=e[i].to;
          if(!dfn[v])
        {
          tarjan(v);
          low[u]=min(low[u],low[v]);
          if(low[v]>=dfn[u])
            {
              son++;
              if(u!=rt || son>1)
            cut[u]=true;
              dcccnt++;
              int d;
              do
            {
              d=s[top--];
              dcc[dcccnt].push_back(d);
            }while(d!=v);
              dcc[dcccnt].push_back(u);
            }
        }
          else
        low[u]=min(low[u],dfn[v]);
        }
    }
     
    inline void prework()
    {
      for(int i=1;i<=dcccnt;i++)
        dcc[i].clear();
      dcccnt=0;
      for(int i=1;i<=n;i++)
        {
          dfn[i]=low[i]=0;in[i]=false;
          ehead[i]=0;cut[i]=false;
        }
      int u,v;cnt=1;
      for(int i=1;i<=m;i++)
        {
          scanf("%d%d",&u,&v);
          add(u,v);add(v,u);
        }
      ind=0;
      for(int i=1;i<=n;i++)
        if(dfn[i]==0)
          {
        rt=i;top=0;
        tarjan(i);
          }
    }
     
    inline void mainwork()
    {
      int resm=m;
      ans=1;
      for(int i=1;i<=dcccnt;i++)
          {
        sum=dcc[i].size();
        if(sum>=3)
          ans=ans*num[sum]%mod,resm-=sum;
          }
      ans=ans*(num[resm]+1)%mod;
    }
     
    inline void print()
    {
      printf("%lld
    ",ans);
    }
     
    int main()
    {
      //freopen("1006.in","r",stdin);
      num[0]=1;
      for(int i=1;i<maxl;i++)
        num[i]=2ll*num[i-1]%mod;
      for(int i=0;i<maxl;i++)
        num[i]=((num[i]-1)%mod+mod)%mod;
      while(~scanf("%d%d",&n,&m))
        {
          prework();
          mainwork();
          print();
        }
      return 0;
    }
    View Code
     
     
  • 相关阅读:
    JWT(JSON WEB TOKEN) / oauth2 / SSL
    Guice 学习
    九 fork/join CompletableFuture
    二 lambda表达式
    IDEA 热部署 + 下载jar包放到maven中
    微服务学习一 微服务session 管理
    一 Optional
    八 线程池(待续)
    七 内置锁 wait notify notifyall; 显示锁 ReentrantLock
    六 多线程问题
  • 原文地址:https://www.cnblogs.com/csushl/p/11604635.html
Copyright © 2011-2022 走看看