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个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...
随机推荐
- 重学c#————struct
前言 简单整理一下struct. 正文 struct 对于struct 而言呢,我们往往会拿class作为对比,但是呢,我们在初学阶段用class来替代struct,struct的存在感越来越低了. ...
- React 性能优化 All In One
React 性能优化 All In One Use CSS Variables instead of React Context https://epicreact.dev/css-variables ...
- 启动Turtlesim,输入roscore命令,报错
Error: Traceback (most recent call last): File "/opt/ros/indigo/lib/python2.7/dist-packages/r ...
- Dyno-queues 分布式延迟队列 之 生产消费
Dyno-queues 分布式延迟队列 之 生产消费 目录 Dyno-queues 分布式延迟队列 之 生产消费 0x00 摘要 0x01 前情回顾 1.1 设计目标 1.2 选型思路 0x02 产生 ...
- 手把手教你gitlab汉化
详细教程如下: 一.在Github上 https://gitlab.com/xhang/gitlab/-/tags 下载对应的版本到服务器中 这种-zh结尾的才是汉化包,下载速度可能比较慢,有条件的可 ...
- 翻译:《实用的Python编程》02_03_Formatting
目录 | 上一节 (2.2 容器) | 下一节 (2.4 序列) 2.3 格式化 虽然本节稍微有点离题,但是当处理数据时,通常想要生成结构化的输出(如表格).示例: Name Shares Price ...
- day1 分布式基础概念
1. 分布式:一个业务分拆多个子业务,部署在不同的服务器上集群:同一个业务,部署在多个服务器上节点:集群中的一个服务器 2.远程调用 分布式系统中调用其它主机 springcloud用http+jso ...
- oracle ORA-00257
su - oracle sqlplus /nolog conn / as sysdba select * from v$flash_recovery_area_usage; select sum(pe ...
- IVMS-5000海康平台安装
某学校系统因中勒索病毒重装监控系统平台 一 安装前准备 二 数据库安装 采用免安装版本安装,原先平台采用免安装版mysql 5.6.24 ,学校IT管理员将数据库文件完全复制,用虚拟机将数据库搭 ...
- JAVA基础(二)—— 常用的类与方法
JAVA基础(二)-- 常用的类与方法 1 Math类 abs ceil floor 绝对值 大于等于该浮点数的最小整数 小于等于该浮点数的最大整数 max min round 两参数中较大的 两参数 ...