Codeforces Round #548 C. Edgy Trees
题面:
题目描述:
- 1.走一条出发点为a1,终点为ak的一条路(允许重复边,重复点)
- 2.从a1开始,通过最短路径走到a2,然后从a2通过最短路径走到a3,以此类推,直到走到终点
- 3.如果在上述过程中,至少经过一条“黑边”,则这个序列是good的
题目分析:


#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 1e5+5;
const int mod = 1e9+7;
int k;
long long n;
vector<int> G[maxn]; //存图
long long cnt[maxn]; //统计每个集合元素个数
int vis[maxn]; //标记/判断i属于哪个集合 void dfs(int u, int num){
if(vis[u]) return;
vis[u] = num; //标记
for(int i = 0; i < G[u].size(); i++){
dfs(G[u][i], num);
}
} int main(){
scanf("%lld %d", &n, &k);
int u, v;
int is_b;
for(int i = 0; i < n-1; i++){
scanf("%d%d%d", &u, &v, &is_b);
if(!is_b){ //如果不是黑边就加入到图中
G[u].push_back(v);
G[v].push_back(u);
}
} for(int i = 1; i <= n; i++){
if(!vis[i]) dfs(i, i);
} for(int i = 1; i <= n; i++){
cnt[vis[i]]++; //统计每个集合的元素个数
} long long bad = 0;
for(int i = 1; i <= n; i++){
long long ans = 1;
if(cnt[i]){ //如果集合存在
for(int j = 1; j <= k; j++){ //算cnt[i]的k次方
ans = ans*cnt[i]%mod;
}
bad = (bad+ans)%mod;
}
} long long all = 1; //算n的k次方
for(int i = 1; i <= k; i++){
all = all*n%mod;
} printf("%lld\n", (all-bad+mod)%mod); //因为all-bad可能为负数(计算时边取模边算), 所以, 要先加个mod再取模
return 0;
}
Codeforces Round #548 C. Edgy Trees的更多相关文章
- Codeforces Round 548 (Div. 2)
layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 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 ...
- 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. 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 (Div. 2) F splay(新坑) + 思维
https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\), ...
- Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理
https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...
- Codeforces Round #548 (Div. 2) C dp or 排列组合
https://codeforces.com/contest/1139/problem/C 题意 一颗有n个点的树,需要挑选出k个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...
随机推荐
- LINUX - 通信
为什么三次握手: 让服务端和客户端都知道,自己的收信能力和发信能力没有问题: 第一次:客户端发给服务端--服务端知道了,自己的收信能力和客户端的发信能力没有问题: 第二次:服务端回复客户端--客户端知 ...
- Redis 哨兵高可用(Sentinel)
哨兵机制是 Redis 高可用中重要的一环,其核心是 通过高可用哨兵集群,监控主从复制的健康状态,并实现自动灾备: 哨兵集群以集群的方式进行部署,这种分布式特性具有以下优点: 避免系统中存在单点,防止 ...
- Redis五大类型及底层实现原理
目录 简单动态字符串链表字典跳跃表整数集合压缩列表对象 对象的类型与编码字符串对象列表对象哈希对象 集合对象有序集合对象类型检查与命令多态内存回收对象共享对象的空转时长 简单动态字符串 导读 Red ...
- Linux bash script regex auto replace
Linux bash script regex auto replace 自动替换 /assets/css/0.styles.96df394b.css => ./assets/css/0.sty ...
- LeetCode 最大收益的分配工作
LeetCode 最大收益的分配工作 工作安排 现在有n位工程师和6项工作(编号为0至5),现在给出每个人能够胜任的工作序号表(用一个字符串表示,比如:045,表示某位工程师能够胜任0号,4号,5号工 ...
- React Hooks: useMemo All In One
React Hooks: useMemo All In One useMemo https://reactjs.org/docs/hooks-reference.html#usememo refs x ...
- WebRTC 信令服务器
WebRTC 信令服务器 node.js & V8 libuv socket.io https://socket.io/ node-static SSR https://github.com/ ...
- Flatten Arrays & flat() & flatMap()
Flatten Arrays & flat() & flatMap() https://alligator.io/js/flat-flatmap/ "use strict&q ...
- react fiber
react fiber https://github.com/acdlite/react-fiber-architecture https://github.com/facebook/react/is ...
- Renice INC:不同颜色的酒帽所代表的意义
酒帽就是酒瓶上方的热缩胶帽/锡帽/蜡封,也就是开瓶前要割掉的那一层保护物,所有的法国酒在酒帽上,都会有一个圆形贴纸,除了有不同颜色外,上面还有一串号码,有可能很多人在喝酒时都不会对这个酒帽有更多的在意 ...