https://www.zybuluo.com/ysner/note/1329304

题面

有一张\(n\)点\(m\)边的、不一定联通的无向图。

如果选了一条边,就不能选其两个端点。

现在同时选点和边,那么最多能够选的点边数量和为多少。

同时,回答使点边数最大的方案数。

  • \(n\leq10^5,m\leq3*10^5\)

解析

设答案为\(ans\),方案数为\(tot\)。

讨论一下联通块的形态:

  • \(m=n-1\):\(ans=n,tot=1\)
  • \(m=n\):\(ans=m\),环中\(tot=2\),树的部分\(tot\)值可以通过\(dp\)求出
  • \(m>n\):\(ans=m\),强连通分量\(tot=1\),树的部分\(tot\)值可以通过\(dp\)求出

树的部分的\(dp\):

设\(dp[i][0/1]\)表示统计到\(i\)号点,选不选该点的方案数。

然后从儿子转移,讨论一下就行。

综上,其实把强联通分量缩点后直接树形\(DP\)就行了。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
#define re register
#define il inline
#define fp(i,a,b) for(re int i=a;i<=b;++i)
#define fq(i,a,b) for(re int i=a;i>=b;--i)
using namespace std;
const int N=5e5+100,mod=998244353;
int n,m,h[N],cnt=1,ans,dfn[N],low[N],sta[N],top,tot,sz[N],bl[N],scc,Esz[N],f[2][N],g[2][N];
bool vis[N];
struct dat{int u,v;}a[N<<1];
struct Edge{int to,nxt;}e[N<<1];
il void add(re int u,re int v)
{
e[++cnt]=(Edge){v,h[u]};h[u]=cnt;
e[++cnt]=(Edge){u,h[v]};h[v]=cnt;
}
il ll gi()
{
re ll x=0,t=1;
re char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
il void Tarjan(re int u,re int las)
{
dfn[u]=low[u]=++tot;sta[++top]=u;vis[u]=1;
re int v;
for(re int i=h[u];i+1;i=e[i].nxt)
if((i^1)^las)
{
re int v=e[i].to;
if(!dfn[v]) Tarjan(v,i),low[u]=min(low[u],low[v]);
else if(vis[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
++scc;
do{v=sta[top--];vis[v]=0;++sz[scc];bl[v]=scc;}while(u^v);
}
}
il void dfs(re int u)
{
f[0][u]=sz[u];f[1][u]=Esz[u];g[0][u]=g[1][u]=1;vis[u]=1;
for(re int i=h[u];i+1;i=e[i].nxt)
{
re int v=e[i].to;
if(vis[v]) continue;
dfs(v);
if(f[0][v]>f[1][v]) f[0][u]+=f[0][v],g[0][u]=1ll*g[0][u]*g[0][v]%mod;
if(f[0][v]==f[1][v]) f[0][u]+=f[0][v],g[0][u]=1ll*g[0][u]*(g[0][v]+g[1][v])%mod;
if(f[0][v]<f[1][v]) f[0][u]+=f[1][v],g[0][u]=1ll*g[0][u]*g[1][v]%mod;
if(f[0][v]>f[1][v]+1) f[1][u]+=f[0][v],g[1][u]=1ll*g[1][u]*g[0][v]%mod;
if(f[0][v]==f[1][v]+1) f[1][u]+=f[0][v],g[1][u]=1ll*g[1][u]*(g[0][v]+g[1][v])%mod;
if(f[0][v]<f[1][v]+1) f[1][u]+=f[1][v]+1,g[1][u]=1ll*g[1][u]*g[1][v]%mod;
}
}
int main()
{
memset(h,-1,sizeof(h));
n=gi();m=gi();
fp(i,1,m) a[i].u=gi(),a[i].v=gi(),add(a[i].u,a[i].v);
fp(i,1,n) if(!dfn[i]) Tarjan(i,0);
memset(h,-1,sizeof(h));cnt=0;
fp(i,1,m)
{
re int u=a[i].u,v=a[i].v;
if(bl[u]^bl[v]) add(bl[u],bl[v]);
else ++Esz[bl[u]];
}
n=scc;tot=1;
fp(i,1,n)
if(!vis[i])
{
dfs(i);
if(f[0][i]<f[1][i]) ans+=f[1][i],tot=1ll*tot*g[1][i]%mod;
if(f[0][i]==f[1][i]) ans+=f[1][i],tot=1ll*tot*(g[0][i]+g[1][i])%mod;
if(f[0][i]>f[1][i]) ans+=f[0][i],tot=1ll*tot*g[0][i]%mod;
}
printf("%d\n%d\n",ans,tot);
return 0;
}

[noip模拟赛]小U的女装的更多相关文章

  1. noip模拟赛 小Y的问题

    [问题描述]有个孩子叫小 Y,一天,小 Y 拿到了一个包含 n 个点和 n-1 条边的无向连通图, 图中的点用 1~n 的整数编号.小 Y 突发奇想,想要数出图中有多少个“Y 字形”.一个“Y 字形” ...

  2. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  3. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  4. CH Round #55 - Streaming #6 (NOIP模拟赛day2)

    A.九九归一 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2355%20-%20Streaming%20%236%20(NOIP模拟赛day2)/九九归一 题 ...

  5. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)

    A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...

  6. 10.16 NOIP模拟赛

    目录 2018.10.16 NOIP模拟赛 A 购物shop B 期望exp(DP 期望 按位计算) C 魔法迷宫maze(状压 暴力) 考试代码 C 2018.10.16 NOIP模拟赛 时间:2h ...

  7. Nescafe #29 NOIP模拟赛

    Nescafe #29 NOIP模拟赛 不知道这种题发出来算不算侵权...毕竟有的题在$bz$上是权限题,但是在$vijos$似乎又有原题...如果这算是侵权的话请联系我,我会尽快删除,谢谢~ 今天开 ...

  8. NOI.AC NOIP模拟赛 第六场 游记

    NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...

  9. NOI.AC NOIP模拟赛 第一场 补记

    NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...

随机推荐

  1. 【multimap的应用】D. Array Division

    http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include ...

  2. SpringData JPA进阶查询—JPQL/原生SQL查询、分页处理、部分字段映射查询

    上一篇介绍了入门基础篇SpringDataJPA访问数据库.本篇介绍SpringDataJPA进一步的定制化查询,使用JPQL或者SQL进行查询.部分字段映射.分页等.本文尽量以简单的建模与代码进行展 ...

  3. THUWC2018 暴力+爆炸记

    Day 0 没有Day0. Day 1 签到然后去宿舍,环境还行,比某偏远山区要强多了,不过这热水有点难拿??看RP有遇到煮好水的饮水机就拿,没有就苟矿泉水. 中午,那个餐还是挺好吃的,不过餐费40就 ...

  4. windows 下安装Apache httpd 只需三步

    1.下载 Apache 官网地址:http://httpd.apache.org/docs/current/platform/windows.html#down 找到这个, 看到这几个选项: Apac ...

  5. Object_C 定义全局宏的颜色时,报“Expected identifier”的错误

    在定义全局颜色宏的时候,为了整齐把空格删了,写在了同一行里,调用的时候,出错提示“Expected identifier”,如下: 如果宏定义如上那样的话,在调用的时候,会出现如下的问题: 百思不得解 ...

  6. uva 1364

    刘书上例题 #include <cstdio> #include <cstdlib> #include <cmath> #include <set> # ...

  7. 一处折腾笔记:Android内嵌html5加入原生微信分享的解决的方法

    有一段时间没有瞎折腾了. 这周一刚上班萌主过来反映说:微信里面打开聚客宝.分享功能是能够的(这里是用微信自身的js-sdk实现的).可是在android应用里面打开点击就没反应了:接下来狡猾的丁丁在产 ...

  8. sqlzoo练习答案--SELECT within SELECT Tutorial

    This tutorial looks at how we can use SELECT statements within SELECT statements to perform more com ...

  9. jsoncpp的api简要说明

    1  jsoncpp的api简要说明 1,解析(json字符串转为对象) std::string strDataJson; Json::Reader JReader; Json::Value JObj ...

  10. js 时钟特效

      时钟特效 CreateTime--2018年2月24日15:11:23 Author:Marydon 实现方式:都是基于HTML5的canvas标签实现的 款式一 借助jQuery插件实现 < ...