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
     
     
  • 相关阅读:
    将aaaa替换成aaba 分类: python 小练习 2013-10-28 18:28 246人阅读 评论(0) 收藏
    使用生成器返回fibs列表 分类: python Module python基础学习 2013-10-28 18:19 283人阅读 评论(0) 收藏
    python中的生成器(generator) 分类: python Module python基础学习 2013-10-28 17:41 310人阅读 评论(0) 收藏
    遇到的问题总结 分类: 问题总结 2013-10-28 17:21 263人阅读 评论(0) 收藏
    win7 下安装ipython 分类: python基础学习 software 2013-10-19 12:23 1383人阅读 评论(0) 收藏
    获取函数中的参数 分类: 正则表达式 2013-10-16 15:14 221人阅读 评论(0) 收藏
    使用termcolor模块 分类: python Module 2013-10-12 14:06 459人阅读 评论(0) 收藏
    Mysql启动失败
    java-抽象类
    java三大特性--多态(1)
  • 原文地址:https://www.cnblogs.com/csushl/p/11604635.html
Copyright © 2011-2022 走看看