C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】
一、题面
二、分析
这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题。首先在建图的时候,只考虑红色路径上的点。为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路径,这样把所有的可能数再减去只走红色路径的数就是最终的答案了。这里要注意的是,如果连通块里只有一个点,那么就是K个点都是这个点的情况,根据题意是不满足的,也要减去。
三、AC代码
#include<bits/stdc++.h> using namespace std; typedef long long LL;
const int MOD = 1e9 + ;
const int MAXN = 1e5 + ;
vector<int> Graph[MAXN];
bool visited[MAXN];
int T, N, K; LL Pow(LL a, LL b)
{
LL ans = ;
while(b)
{
if(b&)
{
ans = ans * a % MOD;
}
a = a * a % MOD;
b >>= ;
}
return ans;
} void DFS(int v)
{
if(visited[v])
return;
visited[v] = ;
T++;
for(auto &itr:Graph[v])
{
DFS(itr);
}
} int main()
{
scanf("%d %d", &N, &K);
memset(visited, , sizeof(visited));
for(int i = ; i < N; i++)
{
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
if(c == )
{ Graph[x].push_back(y);
Graph[y].push_back(x);
}
}
LL Ans = ;
for(int i = ; i <= N; i++)
{
if(visited[i])
continue;
T = ;
DFS(i);
Ans = (Ans + Pow(T, K)) % MOD;
}
Ans = (Pow(N, K) - Ans + MOD) % MOD;
printf("%I64d\n", Ans);
return ;
}
C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】的更多相关文章
- 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 (Div. 2)
layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 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个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...
- Codeforces Round #548 (Div. 2) D 期望dp + 莫比乌斯反演
https://codeforces.com/contest/1139/problem/D 题意 每次从1,m中选一个数加入队列,假如队列的gcd==1停止,问队列长度的期望 题解 概率正着推,期望反 ...
- Codeforces Round #548 (Div. 2) B. Chocolates
You went to the store, selling
- Codeforces Round #548 (Div. 2) A. Even Substrings
You are given a string
随机推荐
- abp CrudAppService 自定义分页、排序
public class GetAllTasksInput : PagedAndSortedResultRequestDto { public TaskState? State { get; set; ...
- WEB开发常见错误-php无法操作数据库
Ubuntu 安装phpmyadmin 和配置 ubuntu 安装 phpmyadmin 两种 : 1: apt-get 安装 然后使用 已有的虚拟主机目录建立软连接 sudo apt-g ...
- jquery从零开始学----选择器
(2011-01-10 21:21:28) 转载▼ 后代选择器: $("mix mix"),当然可以是多个嵌套,但后代选择器可以是深层子代,所以$("mix mix m ...
- docker-compose up启动又停止,需要加tty为true
如果docker-compose.yml如下,则用docker-compose up -d启动起来的容器可能会立即停止. version: '2' services: mir-http-repo: i ...
- linux计划任务(二)
计划任务的授权 1.at任务 /etc/at.allow /etc/at.deny 2.crontab任务 /etc/cron.allow /etc/cron.deny [注:如果allow文件存在, ...
- java并发编程实战:第八章----线程池的使用
一.在任务和执行策略之间隐性耦合 Executor框架将任务的提交和它的执行策略解耦开来.虽然Executor框架为制定和修改执行策略提供了相当大的灵活性,但并非所有的任务都能适用所有的执行策略. 依 ...
- TSQL--验证身份证是否有效
/****** Object: UserDefinedFunction [dbo].[udf_IsvalidIDCard] Script Date: 02/27/2014 16:03:20 ***** ...
- 关于类属性值校验的一点记录 【知识点Attribute】
好久没有进来了,之前励志坚持写博客,记录自己在做代码搬运工这段历程中点滴,可是仅仅只坚持了几天,就放弃了!果然是,世上无难事,只要肯放弃!哈哈……闲话不多说,开始进入正题,给自己留点笔记,避免将来老了 ...
- 学习迭代器实现C#异步编程——仿async/await(一)
.NET 4.5的async/await真是个神奇的东西,巧妙异常以致我不禁对其实现充满好奇,但一直难以窥探其门径.不意间读了此篇强文<Asynchronous Programming in C ...
- mac下wordpress环境搭建
mac下本来就有apache和php,只需要配置以下+安装mysql 1.Apache 预装目录在 /etc/apache2: 默认的网站目录在 /Library/WebServer/Documen ...