首页 > 其他分享 >题解 ARC171C【Swap on Tree】

题解 ARC171C【Swap on Tree】

时间:2024-02-05 16:22:58浏览次数:31  
标签:ARC171C return int 题解 Tree dp operator Modint friend

每棵子树内只可能有至多一个外来的数,且外来的数是多少并不影响方案数,因此考虑设 \(f_{u,i,0/1}\) 表示考虑以 \(u\) 为根的子树,与 \(u\) 相连的所有边中断了 \(i\) 条,且 \(u\) 与其父亲之间的边有没有断的方案数。设 \(g_{u,c}=\sum f_{u,i,c}\)。

每个节点的初始状态是 \(f_{u,0,0}=1,f_{u,1,1}=[u\ne 1]\)。

枚举 \(u\) 的每个儿子 \(v\),进行如下转移:

\[\begin{aligned} f'_{u,i,0}&\gets f_{u,i,0}\times g_{v,0}+[i>0]\times f_{u,i-1,0}\times i\times g_{v,1}\\ f'_{u,i,1}&\gets f_{u,i,1}\times g_{v,0}+[i>0]\times f_{u,i-1,1}\times i\times g_{v,1} \end{aligned} \]

其中两个转移式的前一项代表不断 \((u,v)\) 这条边,后一项代表断 \((u,v)\) 这条边,乘以 \(i\) 是为了确定与 \(u\) 相连的 \(i\) 条边的断边顺序。

时间复杂度 \(O(n^2)\)。上文 \(f,g,f'\) 分别对应代码中的 \(\textrm{dp},\textrm{sum},\textrm{tmp}\)。

// Problem: C - Swap on Tree
// Contest: AtCoder - AtCoder Regular Contest 171
// URL: https://atcoder.jp/contests/arc171/tasks/arc171_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x, y, z) for(int x = (y); x <= (z); ++x)
#define per(x, y, z) for(int x = (y); x >= (z); --x)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do {freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);} while(false)
#define endl '\n'
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
    uniform_int_distribution<int> dist(L, R);
    return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

template<int mod>
inline unsigned int down(unsigned int x) {
	return x >= mod ? x - mod : x;
}

template<int mod>
struct Modint {
	unsigned int x;
	Modint() = default;
	Modint(unsigned int x) : x(x) {}
	friend istream& operator>>(istream& in, Modint& a) {return in >> a.x;}
	friend ostream& operator<<(ostream& out, Modint a) {return out << a.x;}
	friend Modint operator+(Modint a, Modint b) {return down<mod>(a.x + b.x);}
	friend Modint operator-(Modint a, Modint b) {return down<mod>(a.x - b.x + mod);}
	friend Modint operator*(Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}
	friend Modint operator/(Modint a, Modint b) {return a * ~b;}
	friend Modint operator^(Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}
	friend Modint operator~(Modint a) {return a ^ (mod - 2);}
	friend Modint operator-(Modint a) {return down<mod>(mod - a.x);}
	friend Modint& operator+=(Modint& a, Modint b) {return a = a + b;}
	friend Modint& operator-=(Modint& a, Modint b) {return a = a - b;}
	friend Modint& operator*=(Modint& a, Modint b) {return a = a * b;}
	friend Modint& operator/=(Modint& a, Modint b) {return a = a / b;}
	friend Modint& operator^=(Modint& a, int b) {return a = a ^ b;}
	friend Modint& operator++(Modint& a) {return a += 1;}
	friend Modint operator++(Modint& a, int) {Modint x = a; a += 1; return x;}
	friend Modint& operator--(Modint& a) {return a -= 1;}
	friend Modint operator--(Modint& a, int) {Modint x = a; a -= 1; return x;}
	friend bool operator==(Modint a, Modint b) {return a.x == b.x;}
	friend bool operator!=(Modint a, Modint b) {return !(a == b);}
};

const int N = 3e3 + 5;

typedef Modint<998244353> mint;

int n;
mint dp[N][N][2], tmp[N][2], sum[N][2];
vector<int> e[N];

void dfs(int u, int fa) {
	dp[u][0][0] = 1;
	int deg = 0;
	if(fa) {
		dp[u][1][1] = 1;
		++deg;
	}
	for(int v : e[u]) {
		if(v != fa) {
			dfs(v, u);
			++deg;
			rep(i, 0, deg) {
				tmp[i][0] = dp[u][i][0];
				tmp[i][1] = dp[u][i][1];
				dp[u][i][0] = dp[u][i][1] = 0;
			}
			rep(i, 0, deg) {
				dp[u][i][0] += tmp[i][0] * sum[v][0];
				dp[u][i][1] += tmp[i][1] * sum[v][0];
				if(i > 0) {
					dp[u][i][0] += tmp[i - 1][0] * i * sum[v][1];
					dp[u][i][1] += tmp[i - 1][1] * i * sum[v][1];
				}
			}
		}
	}
	rep(i, 0, deg) {
		sum[u][0] += dp[u][i][0];
		sum[u][1] += dp[u][i][1];
	}
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    rep(i, 1, n - 1) {
    	int u, v;
    	cin >> u >> v;
    	e[u].push_back(v);
    	e[v].push_back(u);
    }
    dfs(1, 0);
    cout << sum[1][0] << endl;
    return 0;
}

标签:ARC171C,return,int,题解,Tree,dp,operator,Modint,friend
From: https://www.cnblogs.com/ruierqwq/p/18008360/arc171c

相关文章

  • 题解 ARC171B【Chmax】
    考察题面中的操作究竟做了什么,不难发现其实是将所有满足\(P_i>i\)的\(i\toP_i\)连边,得到若干条链,然后\(B_i\)即为\(i\)所在链的最后一个节点。显然,存在\(A_i<i\)时无解,存在\(A_i\nei\)但\(A_j=i\)时也无解。否则,每个\(A_i\nei\)的位置填的数都唯一确定......
  • [ABC238F] Two Exams 题解
    题目链接题意有\(N\)个人,有两个\(1\simN\)排列\(P,Q\),在其中选择\(K\)个数,要满足:如果\(P_x<P_y\)且\(Q_x<Q_y\)则不能选了\(y\)而不选\(x\)。思路首先按照\(P\)从小到大排序,这样的话只用考虑\(Q\)。设\(f_{i,j,k}\)表示从前\(i\)个数中选\(j\)个,其中......
  • 【题解】U405180 计算平方和
    \(\mathbf{Part\0}\)目录\(/\\mathbf{Contents}\)\(\mathbf{Part\1}\)题目大意\(/\\mathbf{Item\content}\)\(\mathbf{Part\2}\)题解\(/\\mathbf{Solution}\)\(\mathbf{Part\2.1}\text{C}\)++神奇整数类型之\(\text{__int128}/......
  • [ARC162D] Smallest Vertices 题解
    题目链接点击打开链接题目解法这种树的形态计数题首先可以想到\(prufer\)序列计数,如果没有任何限制,那么方案数为\(\prod\frac{(n-2)!}{deg_i}\),其中\(deg_1=d_1-1,deg_i=d_i(2\lei\len)\)对于每个点分开求贡献考虑这个式子只和点的个数和子树内的\(\sumdeg\)有关......
  • P3604 美好的每一天 题解
    题目链接:美好的每一天经典题,这种字符串重排以后回文,优先考虑异或操作。考虑对一个字符串状态压缩,每个字符表示为\(1<<(c-97)\),然后将它们异或起来就可以得到一个字符串的状态压缩情况。考虑每一位异或情况,如果为偶数个单个字符则为\(0\),奇数则为\(1\),注意可以重排,那么当且仅......
  • 2024牛客寒假算法基础集训营1 J 又鸟之亦心 题解
    Question2024牛客寒假算法基础集训营1J又鸟之亦心Solution挺好的一个题,给了我很多启发显然,先二分最大值\(D\),关键在于\(check\)怎么写考虑到两个人是相对的,第\(i\)次之后肯定有一个人在\(a_i\),具体是谁不重要,也不需要关注是怎么走过来的,我们需要去维护另外一个人可......
  • 2024牛客寒假算法基础集训营1 K 牛镇公务员考试 题解
    Question2024牛客寒假算法基础集训营1K牛镇公务员考试给出一张试卷有\(n\)道单选题,每道题用一个整数\(a_i\)和一个长度为\(5\)的字符串\(s_i\)表示,其中第\(i\)道题的题面为:第\(a_i\)道题的答案是()A.\(s_1\)B.\(s_2\)C.\(s_3\)D.\(s_4\)E.\(s_5\)问:正......
  • [CF383E] Vowels 题解
    [CF383E]Vowels题解思路要求的这个东西一看就没什么性质,考虑枚举所有元音子集。如果我们能够求出\(f_s\)表示\(s\)集合作为元音时有多少个单词至少包含一个元音。难求,正难则反,考虑\(f_s\)表示\(s\)集合作为元音时有多少个单词全都由非元音字母组成,由于对称性,我们只......
  • sourcetree配置多ssh
    Sourcetree-工具-选项-ssh客户端选择OpenSSH输入对应的sshkey,用【;】分隔C:\Users\Javen\.ssh\id_rsa;C:\Users\Javen\.ssh\self_id_rsa.ssh目录创建config文件Hostgithub.comHostNamessh.github.comUsergitPort443Hostwork.github.comHostNamessh.github.com......
  • Luogu「Daily OI Round 3」Simple 题解
    #include<iostream>#include<cstdio>#include<queue>#include<algorithm>#include<cstring>#include<string>#include<string.h>#include<vector>#defineintlonglong#definerep(a,b,c)for(autoa=b;a......