【LOJ2250】「ZJOI2017」仙人掌

news/2025/2/12 17:43:35/

【题目链接】

  • 点击打开链接

【思路要点】

  • 考虑给定的图为大小为 N + 1 N+1 N+1 的点的菊花图的情况,记方案数为 d p N dp_N dpN ,考虑最后一个儿子是否连边,则有转移 d p i = d p i − 1 + ( i − 1 ) d p i − 2 dp_i=dp_{i-1}+(i-1)dp_{i-2} dpi=dpi1+(i1)dpi2
  • 断开环边,剩余问题即为如何解决图是一棵树的情况。
  • 注意到由于不允许存在重边,一个合法的加边方案中边的长度 ≥ 2 \geq2 2
  • 考虑对一个合法的加边方案进行如下转化:记一条边 ( x , y ) (x,y) (x,y) 经过了点 x , a , b , c , … , d , e , f , y x,a,b,c,\dots,d,e,f,y x,a,b,c,,d,e,f,y ,则断开 ( x , y ) (x,y) (x,y) ,连接 ( x , b ) , ( a , c ) , … , ( d , f ) , ( e , y ) (x,b),(a,c),\dots,(d,f),(e,y) (x,b),(a,c),,(d,f),(e,y)
  • 得到的连边方案所连的新边长度均为 2 2 2 ,且与将各个点与其邻点看做一个菊花图的连边方案的组合一一对应。
  • 因此,记 d i d_i di i i i 的度数,答案即为 ∏ i = 1 N d p d i \prod_{i=1}^{N}dp_{d_i} i=1Ndpdi
  • 时间复杂度 O ( N + M ) O(N+M) O(N+M)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 5;
const int P = 998244353;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {x = 0; int f = 1;char c = getchar();for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';x *= f;
}
template <typename T> void write(T x) {if (x < 0) x = -x, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {write(x);puts("");
}
int n, m, cnt[MAXN], dp[MAXN];
bool vis[MAXN], ban[MAXN], ins[MAXN], flg;
vector <pair <int, int>> a[MAXN];
void dfs(int pos, int fa, int num) {cnt[pos] = 0;vis[pos] = true;ins[pos] = true;for (auto x : a[pos])if (!vis[x.first]) {dfs(x.first, pos, x.second);cnt[pos] += cnt[x.first];} else if (ins[x.first] && x.first != fa) {cnt[pos]++, cnt[x.first]--;ban[x.second] = true;}ins[pos] = false;if (cnt[pos] >= 2) flg = true;if (cnt[pos] >= 1) ban[num] = true;
}
int main() {int T; read(T);while (T--) {read(n), read(m);for (int i = 1; i <= n; i++) {a[i].clear();vis[i] = false;}for (int i = 1; i <= m; i++) {int x, y; read(x), read(y);a[x].emplace_back(y, i);a[y].emplace_back(x, i);ban[i] = false;}flg = false;dfs(1, 0, 0);if (flg) {puts("0");continue;}dp[0] = dp[1] = 1;for (int i = 2; i <= n; i++)dp[i] = (dp[i - 1] + 1ll * (i - 1) * dp[i - 2]) % P;int ans = 1;for (int i = 1; i <= n; i++) {int cnt = 0;for (auto x : a[i])if (!ban[x.second]) cnt++;ans = 1ll * ans * dp[cnt] % P;}writeln(ans);}return 0;
}

http://www.ppmy.cn/news/142565.html

相关文章

2023年的六一儿童节,愿我们永远热泪盈眶,永远保持童心,致我们终将逝去的青春!

六一儿童节&#xff0c;是一个属于孩子们的节日。在这一天&#xff0c;孩子们可以尽情地玩耍、享受快乐。然而&#xff0c;对于我们这些已经长大的人来说&#xff0c;六一儿童节却意味着一种回忆&#xff0c;一种怀念&#xff0c;一种青春的逝去。 小时候&#xff0c;我们总是…

(纪中)2250. NOIP

(File IO): input:noip.in output:noip.out 时间限制: 1000 ms 空间限制: 262144 KB 具体限制 Goto ProblemSet 题目描述 你知道 N e w O r a n g e I n d u s t r y P a l a t a b l e New Orange Industry Palatable NewOrangeIndustryPalatable公司吗&#xff1f;这是老板 S…

【LeetCode】2250. Count Number of Rectangles Containing Each Point

基本上就是Binary Search 简单二分 因为题目的height只有100种可能&#xff0c;我的naive的想法是统计每个height的width&#xff0c;然后遍历到每个点(x, y)的时候&#xff0c;把height>y的每个width数组都二分查找一遍…… from collections import defaultdict from b…

洛谷P3258BZOJ3631DTOJ2250 [JLOI2014]松鼠的新家

洛谷P3258&&BZOJ3631&&DTOJ2250 [JLOI2014]松鼠的新家 题目题目描述输入格式输出格式样例样例输入样例输出 数据范围与提示 题解 题目 题目描述 松鼠的新家是一棵树&#xff0c;前几天刚刚装修了新家&#xff0c;新家有 n n n个房间&#xff0c;并且有 n − …

系统开发视角下的诊断 ———— 动力系统(P)诊断故障8

文章目录 P22XX 燃油和空气计量和辅助排放控制P23XX 点火系统或失火 P22XX 燃油和空气计量和辅助排放控制 P22XX Fuel and air metering and auxiliary emission controls DTC Number DTC Name Location P2200 NOx Sensor Circuit Bank 1 P2201 NOx Sensor Circuit Range/Perf…

NOIP——jzoj 2250

题目描述 你知道New Orange Industry Palatable公司吗&#xff1f;这是老板Smart为了与苹果公司竞争而新开的一家橘子公司&#xff0c;它的业务是栽培美味的橘子并售卖&#xff0c;公司简称为NOIP。 NOIP公司新推出N1个橘子&#xff0c;每个橘子上都贴有一个标签&#xff0c;其…

poj 2250

题目实际意思&#xff0c;就是找两篇文章中的“最多公共单词”——其实可以把它转换成最长公共子序列问题&#xff0c;只是这里的序列的元素不是字母而是单词罢了。 另外&#xff0c;此题主要是考察LCS的形成路径&#xff0c;因此需要一个数组记录这个路径&#xff0c;这里用的…

poj - 2250 题解

题意&#xff1a;魔改版的LCS问题。序列是单词序列 解法&#xff1a;原版LCS的序列元素是数字&#xff0c;想办法把数字替换成单词就解决了 1 #include<iostream>2 #include<cstdio>3 #include<algorithm>4 #include<cstring>5 #include<string>…