icpc 46 上海站 g题

树,dp,组合数

#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
const int N = 1e5 + 7;
const int mod = 998244353;
typedef long long LL;
LL fact[N];

vector<int> son[N];
int fa[N], vis[N];

void TreeBuild(int x) {
	for (int i = 0; i < son[x].size(); i++) {
		int t = son[x][i];
		if (t == fa[x]) {
			swap(son[x][i],son[x][son[x].size()-1]);
			son[x].pop_back();
			if(son[x].size() == i)
				continue;
			t = son[x][i];
		}
		fa[t] = x;
		TreeBuild(t);
	}

}

long long ans = 1;
bool dfs(int root) {
	long long cnt = 0;
	//鍚戜笅鎼滅储
	for(int i = 0 ; i < son[root].size() ; i++) {
		if(dfs(son[root][i]))
			cnt++;
	}
	if(cnt==0)
		return 1;
	//璁$畻
	if(cnt%2) {
		ans = (ans*fact[cnt])%mod;
		return 0;
	} else {
		ans = (ans*fact[cnt-1])%mod;
		return 1;
	}
}
int main() {
	ios::sync_with_stdio(0);
	fact[0] = 1, fact[1] = 1;
	for (int i = 2; i < N; i++)
		fact[i] = (fact[i - 2] * i) % mod;
	int m;
	cin >> m;
	for (int i = 1; i < m; i++) {
		int s, t;
		cin >> s >> t;
		son[s].push_back(t);
		son[t].push_back(s);
	}
	TreeBuild(1);
	dfs(1);
	cout << ans << endl;
}
posted @ 2022-03-22 14:29  seekerHeron  阅读(42)  评论(0)    收藏  举报