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个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...
随机推荐
- Win7下安装IIS
安装IIS 1.控制面板 --> 程序 --> 卸载程序,进入"程序与功能". 2.进入"打开或关闭Window功能". 3.找到"Int ...
- Java 并发机制底层实现 —— volatile 原理、synchronize 锁优化机制
本书部分摘自<Java 并发编程的艺术> 概述 相信大家都很熟悉如何使用 Java 编写处理并发的代码,也知道 Java 代码在编译后变成 Class 字节码,字节码被类加载器加载到 JV ...
- codeforces 6D
D. Lizards and Basements 2 time limit per test 2 seconds memory limit per test 64 megabytes input st ...
- ACM ICPC 2017 Warmup Contest 1 D
Daydreaming Stockbroker Gina Reed, the famous stockbroker, is having a slow day at work, and between ...
- Docker--Image and Container
2.1 深入探讨Image 说白了,image就是由一层一层的layer组成的. 2.1.1 官方image https://github.com/docker-library mysql http ...
- very useful English Acronyms in Programming for Programmer
very useful English Acronyms in Programming for Programmer alias / shorthand / acronyms 别名 / 简写 / 缩略 ...
- css var & auto width css triangle
css var & auto width css triangle https://codepen.io/xgqfrms/pen/PooeEbd css var https://codepen ...
- NGK全球启动大会圆满落幕
加州时间2020年11月25日,NGK全球启动大会在美国硅谷圆满落幕.本次NGK全球启动大会为NGK正式在全球上线拉开了序幕. 百余位受邀嘉宾出席了本次NGK全球启动大会,其中包括NGK创始人.星盟投 ...
- FreeRTOS操作系统最全面使用指南
FreeRTOS操作系统最全面使用指南 1 FreeRTOS操作系统功能 作为一个轻量级的操作系统,FreeRTOS提供的功能包括:任务管理.时间管理.信号量.消息队列.内存管理.记录功能等,可基本满 ...
- 微信小程序(八)-项目实例(原生框架 MINA转云开发)==03-云开发-数据库
云数据库 云数据库开发文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database.html 1.新建云数据库( ...