首页 > 其他分享 >D. Sasha and a Walk in the City

D. Sasha and a Walk in the City

时间:2024-02-16 18:34:01浏览次数:31  
标签:City 子树 Walk int 路径 Sasha test intersections city

D. Sasha and a Walk in the City

Sasha wants to take a walk with his girlfriend in the city. The city consists of $n$ intersections, numbered from $1$ to $n$. Some of them are connected by roads, and from any intersection, there is exactly one simple path$^{\dagger}$ to any other intersection. In other words, the intersections and the roads between them form a tree.

Some of the intersections are considered dangerous. Since it is unsafe to walk alone in the city, Sasha does not want to visit three or more dangerous intersections during the walk.

Sasha calls a set of intersections good if the following condition is satisfied:

  • If in the city only the intersections contained in this set are dangerous, then any simple path in the city contains no more than two dangerous intersections.

However, Sasha does not know which intersections are dangerous, so he is interested in the number of different good sets of intersections in the city. Since this number can be very large, output it modulo $998\,244\,353$.

$^{\dagger}$A simple path is a path that passes through each intersection at most once.

Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \leq 3 \cdot 10^5$) — the number of intersections in the city.

The next $(n - 1)$ lines describe the roads. The $i$-th line contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$, $u_i \ne v_i$) — the numbers of the intersections connected by the $i$-th road.

It is guaranteed that these roads form a tree.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

Output

For each test case, output a single integer — the number of good sets of intersections modulo $998\,244\,353$.

Example

input

4
3
1 3
3 2
4
3 4
2 3
3 1
5
1 2
3 4
5 1
2 3
4
1 2
2 3
3 4

output

7
12
16
11

Note

In the first test case, there are $2^3 = 8$ sets of intersections. All of them are good, except for the set $\{1, 2, 3\}$, because if intersections $1, 2$, and $3$ are dangerous, then the simple path $1 - 2 - 3$ contains $3$ dangerous intersections. Thus, there are $7$ good sets.

In the second test case, there are $2^4 = 16$ sets of intersections. Among them, the sets $\{1, 2, 3, 4\}$, $\{1, 2, 3\}$, $\{1, 3, 4\}$, $\{2, 3, 4\}$ are not good. Thus, there are a total of $12$ good sets. The city layout is shown below:

 

解题思路

  在以 $u$ 为根节点的子树中,其子节点为 $v_i$,现在假设已经求得子树 $v_i$ 中合法点集的数量,考虑如何推出子树 $u$ 的合法点集的数量。对于子树 $u$ 中的两个不同节点 $x$ 与 $y$,如果来自同一个子树 $v_i$,则其简单路径仍是合法的,不会受到 $u$ 和其他子节点子树的影响。如果分别来自两个不同 $v_i$ 的子树,其简单路径必然经过最近公共祖先 $u$,此时为了保证路径是合法的,那么 $x$ 到 $u$ 的路径中最多有 $2$ 个被选择的点,$y$ 到 $u$ 的路径中最多有 $2$ 个被选择的点,并且 $x$ 到 $y$ 的路径中最多有 $2$ 个被选择的点。因此我们关心的是子树里每个节点到根节点的路径中,被选择的点最多的那条里有多少个。

  定义 $f(u,0/1/2)$ 表示以 $u$ 为根的子树中,且 $u$ 到每个节点路径中最多有 $0/1/2$ 个被选择的点时,(子树内任意简单路径)合法点集的数量。

  对于 $f(u,0)$,即子树 $u$ 内任何节点都不会被选,显然有 $f(u,0) = 1$。

  对于 $f(u,1)$,如果不选 $u$,那么每个子树 $v_i$ 内的点到 $v_i$ 的路径中既可以有 $0$ 个或 $1$ 个被选择的点(都能保证经过 $u$ 的简单路径中被选择的点不超过 $2$ 个),那么方案数就是 $\left( \prod{f(v_i,0)+f(v_i,1)} \right) - 1$,减 $1$ 指减去均取 $f(v_i,0)$ 的情况,因为要保证到 $u$ 的路径中最多有 $1$ 个被选择的点。如果选 $u$,那么每个子树 $v_i$ 中不能再选择点,只有 $\prod{f(v_i,0)} = 1$ 种方案。因此 $f(u,1) = \prod{f(v_i,0)+f(v_i,1)} - 1 + 1$。

  对于 $f(u,1)$,如果不选 $u$,那么必须恰好有一个子树 $v_i$ 是 $f(v_i,2)$ 的状态,方案数就是 $\sum{f(v_i,2)}$。如果选 $u$,也是必须恰好有一个子树 $v_i$ 是 $f(v_i,1)$ 的状态,方案数就是 $\sum{f(v_i,1)}$。因此 $f(u,2) = \sum{f(v_i,2)+f(v_i,1)}$。

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

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 3e5 + 10, M = N * 2, mod = 998244353;

int h[N], e[M], ne[M], idx;
int f[N][3];

void add(int u, int v) {
    e[idx] = v, ne[idx] = h[u], h[u] = idx++;
}

void dfs(int u, int pre) {
    f[u][0] = f[u][1] = 1;
    f[u][2] = 0;
    for (int i = h[u]; i != -1; i = ne[i]) {
        if (e[i] != pre) {
            dfs(e[i], u);
            f[u][1] = 1ll * f[u][1] * (f[e[i]][0] + f[e[i]][1]) % mod;
            f[u][2] = ((LL)f[u][2] + f[e[i]][2] + f[e[i]][1]) % mod;
        }
    }
}

void solve() {
    int n;
    scanf("%d", &n);
    memset(h, -1, n + 10 << 2);
    idx = 0;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        add(u, v), add(v, u);
    }
    dfs(1, -1);
    printf("%d\n", ((LL)f[1][0] + f[1][1] + f[1][2]) % mod);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  codeforces round #926 (div2) F+A-D 思路分享:https://www.bilibili.com/video/BV1VH4y1a7DP/

标签:City,子树,Walk,int,路径,Sasha,test,intersections,city
From: https://www.cnblogs.com/onlyblues/p/18017370

相关文章

  • 10分钟3个步骤集成使用SkyWalking
    随着业务发展壮大,微服务越来越多,调用链路越来越复杂,需要快速建立链路跟踪系统,以及建立系统的可观测性,以便快速了解系统的整体运行情况。此时就非常推荐SkyWalking了,SkyWalking不仅仅是一款链路跟踪工具,还可以作为一个系统监控工具,还具有告警功能。使用简便、上手又快。真可谓快、......
  • SP277 CTGAME - City Game 题解
    题目传送门前置知识单调栈解法令\(f_{i,j}(1\lei\len,1\lej\lem)\)表示从\((1,j)\)到\((i,j)\)中以\((i,j)\)结尾的均为F的子串长度,即\((i,j)\)上面可延伸的最大距离(子矩形的长)。用单调栈的第一维存储子矩形的长,第二维存储子矩形的宽。考虑依次枚举每一......
  • Windows Dependency Walker & Dumpbin
    *[Windows查看exe依赖的dll的方法-知乎](https://zhuanlan.zhihu.com/p/395557318#%E6%96%B9%E6%B3%95%E4%B8%80%EF%BC%9ALucasg/Dependencies%EF%BC%88%E5%BC%80%E6%BA%90%E7%89%88%E7%9A%84%E7%8E%B0%E4%BB%A3%20Dependency%20Walker%EF%BC%89)*[DUMPBIN工具的使用-z......
  • CF327E Axis Walking
    AxisWalkingLuoguCF327E题目描述给你一个长度为\(n(1\len\le24)\)的正整数序列\(S\),再有\(k(0\lek\le2)\)个正整数。求有多少种\(S\)的排列方式使得其前缀和不会成为那\(k\)个数里的任意一个。答案对\(10^9+7\)取模。Solution挺简单的一个状压DP。考虑......
  • idea easyCode插件与velocity语法
    1,idea安装easyCode插件2,设置模板easyCode的教程:https://gitee.com/makejava/EasyCode/wikiseasyCode会有默认的字段类型的对应关系,也可以根据需要自己修改 下面是我自己写的一套(适用于mybatisPlus)##导入宏定义$!define##保存文件(宏定义)#save("/entity",".java")##......
  • Skywalking 钉钉告警
     1、添加钉钉告警  2、修改配置文件 /usr/local/apache-skywalking-apm-bin/config/ alarm-settings.yml修改  alarm-settings.yml告警文件 添加在 文档尾部(注意格式)dingtalkHooks:textTemplate:|-{"msgtype":"text","text":{"......
  • P3081 [USACO13MAR] Hill Walk G
    原题面:Luogu知识点:扫描线,李超树。被离散化鲨了哈哈,一个人交了三页半,感谢大神的提交记录救我一命呜呜:https://www.luogu.com.cn/record/123948215。简述给定平面坐标系上的\(n\)条线段,每条线段的两个端点为\((x_1,y_1)\),\((x_2,y_2)\)且满足\(x_1<x_2\),\(y_1<y_2\),且有......
  • Star Walk和Solar Walk
    StarWalk和SolarWalk是两款由VitoTechnology开发的天文应用程序,它们都提供了关于天体和宇宙的信息,但在功能和重点上有所不同。StarWalk是一款天文学教育应用程序,主要关注天空观测和天体识别。它提供了一个实时的星空地图,可以显示当前的天体位置、星座、行星、卫星、彗星等。......
  • 【Centos】Centos 7.6 Skywalking 9.2.0,自监控
    1  前言今儿试试 Skywalking自监控。2 安装步骤2.1 下载open-telemetry地址:https://hub.nuaa.cf/open-telemetry/opentelemetry-collector-releases/releases/,我下载的是0.89.0版本的哈:2.2 安装open-telemetryrpm-ivhotelcol_0.89.0_linux_amd64.rpm2.......
  • SkyWalking服务监控简单配置【Windows版本】
    SkyWalking是什么skywalking是一个可观测性分析平台和应用性能管理系统专为微服务、云原生架构和基于容器(Docker、K8s、Mesos)架构而设计。下载官网:https://skywalking.apache.org/下载地址:https://skywalking.apache.org/downloads/中文文档:https://skyapm.github.io/doc......