CF1139C Edgy Trees
题目地址:CF1139C Edgy Trees
红黑树
\(ans\) 应该等于总数(\(n^k\))减去不含黑色边的序列数量
不含黑色边就意味着一个序列只能在一个红色联通块中
一个红色联通块中的序列数量应该是点数的 \(k\) 次方
求联通块用dfs用并查集都可以
然后快速幂一下再一减就是 \(ans\)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 6, P = 1e9 + 7;
int n, k, v[N], cnt;
vector<int> e[N];
inline int ksm(int a, int b) {
int c = 1;
while (b) {
if (b & 1) c = (ll)c * a % P;
a = (ll)a * a % P;
b >>= 1;
}
return c;
}
void dfs(int x) {
v[x] = 1;
++cnt;
for (unsigned int i = 0; i < e[x].size(); i++) {
int y = e[x][i];
if (v[y]) continue;
dfs(y);
}
}
int main() {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
if (z) continue;
e[x].push_back(y);
e[y].push_back(x);
}
int ans = ksm(n, k);
for (int i = 1; i <= n; i++)
if (!v[i]) {
cnt = 0;
dfs(i);
ans = (ans - ksm(cnt, k) + P) % P;
}
cout << ans << endl;
return 0;
}
CF1139C Edgy Trees的更多相关文章
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- C. Edgy Trees
链接 [https://codeforces.com/contest/1139/problem/C] 题意 给你n个点,n-1个边,无向的.有red和black的. k表示经过这k个点.可以跨其他点 ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】
一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...
- Codeforces Round #548 (Div. 2) C. Edgy Trees
You are given a tree (a connected undirected graph without cycles) of
- Codeforces Round #548 C. Edgy Trees
题面: 传送门 题目描述: 给出有n个节点的树,整数k.题目要求找长度为k,符合规则(good序列)的"点序列"(由节点构成的序列)个数有多少?规则如下: 1.走一条出发点为a1, ...
- CodeForces Round #548 Div2
http://codeforces.com/contest/1139 A. Even Substrings You are given a string s=s1s2…sns=s1s2…sn of l ...
- Codeforces Round #548
没打,简单补档 C.Edgy Trees 容斥,把黑边断掉数联通块,每个联通块贡献$siz^k$ #include<cstdio> #include<cstring> #inc ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
随机推荐
- python字符串replace失效问题
python字符串replace替换无效 背景 今天想把一个列表中符合条件的元素中 替换部分字符串, 发现怎么替换,改元素还是没有改变,本以为是内存引用的问题后来发现并不然. 经查阅解决 在Pytho ...
- go笔记-值传递、引用传递
eg: func sliceModify(slice []int) { // slice[0] = 88 slice = append(slice, ) } func main() { slice : ...
- 调参必备---GridSearch网格搜索
什么是Grid Search 网格搜索? Grid Search:一种调参手段:穷举搜索:在所有候选的参数选择中,通过循环遍历,尝试每一种可能性,表现最好的参数就是最终的结果.其原理就像是在数组里找最 ...
- heapsort(Java)(最小堆)
public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextI ...
- iso data 聚类算法
isodata算法就是先拟定一个预期类,再选取一些聚类中心,通过不断合并或者分裂聚类,达到分类的目的 关键就是在于,如何分裂,合并 要合并或者分裂 必须要确定一些指标 所以第一步就是要确定 某些指标 ...
- Shell命令-文件压缩解压缩之gzip、zip
文件及内容处理 - gzip.zip 1.gzip:gzip压缩工具 gzip命令的功能说明 gzip 命令用于压缩文件.gzip 是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多出 .gz ...
- 解决SpringSecurity阻止ajax的POST和PUT请求,导致403Forbidden的问题
前言: 最近在整合springboot+springsecurity,在PUT请求的时候出现了403的问题,这里记录一下解决的过程 到Spring的官网去查查SpringSecurity的参考手册看看 ...
- PLSQL:orecal,tnsname简介
导入ORACLE遇到很多问题,学了好多,其中很长时间花在网络配置上,刚开始学,具体原因不知道,先把搜集到的好文章存下来,以后慢慢研究. 监听配置文件 为了使得外部进程 如 CA ...
- 【ARC101F】Robots and Exits 树状数组
题目大意 有 \(n\) 个机器人和 \(m\) 个出口. 这 \(n\) 个机器人的初始位置是 \(a_1,a_2,\ldots,a_n\),这 \(m\) 个出口的位置是 \(b_1,b_2,\l ...
- MT【314】正切比值
(05复旦)已知三角形$\Delta ABC$满足$\tan A:\tan B:\tan C=1:2:3$,求$\dfrac{AC}{AB}$____ 解答:设$x=tan A$,利用恒等式$\tan ...