首页 > 其他分享 >G. Counting Graphs

G. Counting Graphs

时间:2023-08-08 23:22:40浏览次数:47  
标签:sz le 10 int tree Graphs 权值 Counting

G. Counting Graphs

Given a tree consisting of $n$ vertices. A tree is a connected undirected graph without cycles. Each edge of the tree has its weight, $w_i$.

Your task is to count the number of different graphs that satisfy all four conditions:

  1. The graph does not have self-loops and multiple edges.
  2. The weights on the edges of the graph are integers and do not exceed $S$.
  3. The graph has exactly one minimum spanning tree.
  4. The minimum spanning tree of the graph is the given tree.

Two graphs are considered different if their sets of edges are different, taking into account the weights of the edges.

The answer can be large, output it modulo $998244353$.

Input

The first line contains an integer $t$ ($1\le t\le 10^4$) — the number of test cases.

The first line of each test case contains two integers $n$ and $S$ ($2 \le n \le 2 \cdot 10^5, 1\le S\le 10^9$) — the number of vertices and the upper bound of the weights.

Then follow $n-1$ lines describing the tree, the $i$-th line contains three integers $u_i$, $v_i$, and $w_i$ ($1\le u_i,v_i\le n, u_i \ne v_i, 1\le w_i\le S$) — an edge in the tree with weight $w_i$.

It is guaranteed that the sum of $n$ for all tests does not exceed $2\cdot 10^5$.

Output

For each test, output the number of different graphs that satisfy the conditions, modulo $998244353$.

Example

input

4
2 5
1 2 4
4 5
1 2 2
2 3 4
3 4 3
5 6
1 2 3
1 3 2
3 4 6
3 5 1
10 200
1 2 3
2 3 33
3 4 200
1 5 132
5 6 1
5 7 29
7 8 187
7 9 20
7 10 4

output

1
8
80
650867886

Note

In the first sample, there is only one graph, which is the given tree.

In the second samle, the given tree looks like this:

All possible graphs for the second sample are shown below, the minimum spanning tree is highlighted in red:

 

解题思路

  问题就是让我们在原有的树中加一些额外的边,这些边不能是自环或重边,且边的权值不超过$S$,然后对构造的图求最小生成树,问有多少个这样的图所得到的最小生成树就是原来的树?

  先考虑加一条边,选择两个节点$u$和$v$相连,当然$u$和$v$不能直接相邻否则就会有重边。此时就会形成一个环,假设在$u$和$v$相连前,从$u$到$v$的简单路径中最大的权值为$P(u,v)$,根据最小生成树的性质,这条新边的权值应该在$[P(u,v)+1, S]$这个区间内,这样构造的新图的最小生成树才是原来的那棵树。否则在这个环中就至少存在一条边的权值比新边大或相等,那么保留新边并选择一条权重比新边大或相等的边删去,得到一棵新树,可以知道这棵树的权重比原来的小或相等,最小生成树就肯定不是原来那棵树了。事实上只要新加的边满足上面的条件,那么构造的新图的最小生成树就一定是原来的树,因此加边及赋权值的行为可以看作是相互独立的。

  由于加的是无向边,为了避免重复这里规定从编号小的节点向编号大的节点连一条边,由于加边是独立的,结合乘法原理,那么满足要求的图的数量就是$\prod\limits_{1\le u < v \le n}{} (S-P(u,v)+1)$,这其中考虑了某两个点不连边的情况,因此任意两个点间连边的情况就有$S-P(u,v)+1$种(当然还要保证$u$和$v$不直接相连)。可以发现如果直接暴力求的话时间复杂度至少是$O(n^2 \log{n})$,这里不好直接优化(因为要双重循环枚举$u$和$v$),因此尝试能不能用另外的方法求出这个值。

  可以发现任意两点$u$和$v$连边后赋值的受限取决于原树中这两点简单路径上的最大权值$P(u,v)$,在$u$和$v$相连后,与原树形成的环就一定包含这条权值为$P(u,v)$的边,因此可以根据$P(u,v)$来对所有的新边进行分类,即根据形成的环所包含的最大权值的边进行分类。那么我们可以先对原树中的边按照权值从小到大排序,并且把原树中的所有边删除,然后再从小到大依次枚举每一条边并重新加入到树中。假设当前枚举到的第$i$条边$(u_i, v_i, w_i)$,若要以这条边作为环中最大权值的边,那么很明显要包含这条边,且其他边的权值不能超过$w_i$。由于我们是从小到大往树中加边,那么我们就可以从与$u_i$连通的点集,以及与$v_i$连通的点集中分别选出一个点,然后这两个点相连形成新边(根据树的性质当枚举到第$i$条边时$u_i$和$v_i$不连通)。假设与$u_i$连通的点集大小为${sz}_{u_{i}}$,与$v_i$连通的点集大小为${sz}_{v_{i}}$,那么根据乘法原理在形成的环中最大权值为$w_i$的新边数量就是${sz}_{u_{i}} \cdot {sz}_{v_{i}} - 1$,减$1$是删去$u_i$和$v_i$这两个直接相连的点所构成的新边。又由于这${sz}_{u_{i}} \cdot {sz}_{v_{i}} - 1$条新边中每条边都有$S-w_i+1$种选择,由独立性因此一共有${(S-w_i+1)^{{sz}_{u_{i}} \cdot {sz}_{v_{i}} - 1}}$种情况。

  可能你会问如果环中权值最大的边有多条怎么办?上面的做法会不会重复统计?答案是不会的,如果存在多条取值最大的边,那么我们就只认在排序后最靠前的边作为唯一的权值最大的边,当枚举完这条边后,这条边两个端点所在的连通块就会合并成一个,而对应的新边的两个点往后就不会再被枚举到(因为每次都是从两个不同的连通块中选点)。

  再考虑完所有不同$P(u,v)$的情况,那么最终的答案就是$$\prod\limits_{i=1}^{n-1}{{(S-w_i+1)^{{sz}_{u_{i}} \cdot {sz}_{v_{i}} - 1}}}$$

  AC代码如下,时间复杂度为$O(n \log{n})$:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10, mod = 998244353;
 7 
 8 struct Node {
 9     int v, w, wt;
10 }e[N];
11 int fa[N], sz[N];
12 
13 int find(int x) {
14     return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
15 }
16 
17 int qmi(int a, LL k) {
18     int ret = 1;
19     while (k) {
20         if (k & 1) ret = 1ll * ret * a % mod;
21         a = 1ll * a * a % mod;
22         k >>= 1;
23     }
24     return ret;
25 }
26 
27 void solve() {
28     int n, m;
29     scanf("%d %d", &n, &m);
30     for (int i = 0; i < n - 1; i++) {
31         scanf("%d %d %d", &e[i].v, &e[i].w, &e[i].wt);
32     }
33     sort(e, e + n - 1, [&](Node &a, Node &b) {
34         return a.wt < b.wt;
35     });
36     for (int i = 1; i <= n; i++) {
37         fa[i] = i;
38         sz[i] = 1;
39     }
40     int ret = 1;
41     for (int i = 0; i < n - 1; i++) {
42         int x = find(e[i].v), y = find(e[i].w);
43         ret = 1ll * ret * qmi(m - e[i].wt + 1, (1ll * sz[x] * sz[y] - 1)) % mod;
44         sz[y] += sz[x];
45         fa[x] = y;
46     }
47     printf("%d\n", ret);
48 }
49 
50 int main() {
51     int t;
52     scanf("%d", &t);
53     while (t--) {
54         solve();
55     }
56     
57     return 0;
58 }

 

参考资料

  Codeforces Round 891 (Div. 3) Editorial:https://codeforces.com/blog/entry/119134

标签:sz,le,10,int,tree,Graphs,权值,Counting
From: https://www.cnblogs.com/onlyblues/p/17615283.html

相关文章

  • Atcoder ABC259H Yet Another Path Counting
    首先可以想到有组合数的方法:令起点为\((x1,y1)\),终点为\((x2,y2)\),则路径方案数就为\(\binom{x2+y2-x1-y1}{x2-x1}\),这样设有\(k\)个相同颜色的点,时间复杂度就为\(O(k^2)\)。再考虑到还有\(\text{DP}\)方法:令\(f_{x,y}\)为走到\((x,y)\)的方案数,不限制......
  • [ARC143B] Counting Grids 题解
    CountingGrids题目大意将\(1\simn^2\)填入\(n\timesn\)的网格\(A\)中,对于每个格子满足以下条件之一:该列中存在大于它的数。该行中存在小于它的数。求方案数。思路分析首先有一个比较显然的结论:对于一个不合法的方案,有且仅有一个数不满足任何一个条件。考虑......
  • Query2box Reasoning over Knowledge Graphs in Vector Space using Box Embeddings
    目录概符号说明Query2Box代码RenH.,HuW.andLeskovecJ.Query2box:Reasoningoverknowledgegraphsinvectorspaceusingboxembeddings.ICLR,2020.概Boxembedding用于查询判断,和我想的那个有很大差别啊.我对这方面不是很了解,只能记录个大概.符号说明......
  • R语言代做编程辅导ASSIGNMENT FOUR - RANDOM GRAPHS(附答案)
    全文链接:https://tecdat.cn/?p=33183PROBLEM1)CreatingRandomAdjacencyMatricesScriptName:adjMatrixInput:n...Thenumberofverticesinthegraphp...Probablitytwoverticesareconnectedplot...whetherornotthematrixshouldbeplottedasagraph......
  • PAT-甲级-1004 Counting Leaves C++
    Afamilyhierarchyisusuallypresentedbyapedigreetree.Yourjobistocountthosefamilymemberswhohavenochild.InputSpecification:Eachinputfilecontainsonetestcase.Eachcasestartswithalinecontaining 0<N<100,thenumberofnode......
  • Arrangement排列•Combination组合•Counting计数•Binomial Theorem二项式定理
    符号C-Combination组合数[1]A-Arrangement(旧教材为P-Permutation)N-Number元素的总个数(自然数集合).M-参与选择的元素个数(M不大于N,两者都是自然数集合).!-Factorial阶乘.Arrangement排列与Combination组合:注意:n,m都是自然数,且m<=n,下同.排列的定义:从n......
  • NC24048 [USACO 2017 Jan P]Promotion Counting
    题目链接题目题目描述Thecowshaveonceagaintriedtoformastartupcompany,failingtorememberfrompastexperiencethatcowsmaketerriblemanagers!Thecows,convenientlynumbered1…N(\(1\leqN\leq100,000\)),organizethecompanyasatree,withco......
  • Understanding JavaScript Garbage Collection: Dive into Reference Counting and Ma
    JavaScript,theprogramminglanguageoftheweb,isoftenpraisedforitsabilitytohandlememorymanagementautomatically.TheJavaScriptengine'sgarbagecollectorplaysapivotalroleinthisprocess.Today,we'lltakeadeepdiveintotwom......
  • Counting principle and Program Testing
    Referencehttps://www.shuxuele.com/data/basic-counting-principle.html基本计数原理若有m个方法去做一件事,及n个方法去做另一件事,则有m×n个方法去做这两件事。应用时需要注意这个原理只适合在所有选择都是独立时才适用。如果是做了一个选择会影响另一个选择就不适合用。......
  • Luogu P3605 [USACO17JAN]Promotion Counting P
    [USACO17JAN]PromotionCountingP题目描述Thecowshaveonceagaintriedtoformastartupcompany,failingtorememberfrompastexperiencethatcowsmaketerriblemanagers!Thecows,convenientlynumbered\(1\ldotsN\)(\(1\leqN\leq100,000\)),o......