C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
2 seconds
256 megabytes
standard input
standard output
You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is colored in either black or red.
You are also given an integer kk. Consider sequences of kk vertices. Let's call a sequence [a1,a2,…,ak][a1,a2,…,ak] good if it satisfies the following criterion:
- We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from a1a1 and ending at akak.
- Start at a1a1, then go to a2a2 using the shortest path between a1a1 and a2a2, then go to a3a3 in a similar way, and so on, until you travel the shortest path between ak−1ak−1 and akak.
- If you walked over at least one black edge during this process, then the sequence is good.

Consider the tree on the picture. If k=3k=3 then the following sequences are good: [1,4,7][1,4,7], [5,5,3][5,5,3] and [2,3,7][2,3,7]. The following sequences are not good: [1,4,6][1,4,6], [5,5,5][5,5,5], [3,7,3][3,7,3].
There are nknk sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo 109+7109+7.
The first line contains two integers nn and kk (2≤n≤1052≤n≤105, 2≤k≤1002≤k≤100), the size of the tree and the length of the vertex sequence.
Each of the next n−1n−1 lines contains three integers uiui, vivi and xixi (1≤ui,vi≤n1≤ui,vi≤n, xi∈{0,1}xi∈{0,1}), where uiui and vivi denote the endpoints of the corresponding edge and xixi is the color of this edge (00 denotes red edge and 11 denotes black edge).
Print the number of good sequences modulo 109+7109+7.
4 4
1 2 1
2 3 1
3 4 1
252
4 6
1 2 0
1 3 0
1 4 0
0
3 5
1 2 1
2 3 0
210
In the first example, all sequences (4444) of length 44 except the following are good:
- [1,1,1,1][1,1,1,1]
- [2,2,2,2][2,2,2,2]
- [3,3,3,3][3,3,3,3]
- [4,4,4,4][4,4,4,4]
In the second example, all edges are red, hence there aren't any good sequences.
这个题目读题读到我绝望,我不太理解这样子的题目。
这个题目我深刻的理解到了两个东西,一个是取模运算,还有一个是并查集求连通块,
这个就是求出有多少个0的连通块,然后用公式求出道路就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
const int mod = 1e9 + 7;
bool vis[maxn];
int f[maxn]; int findx(int x)
{
return f[x] == x ? x : f[x] = findx(f[x]);
}
void unite(int x, int y)
{
x = findx(x);
y = findx(y);
if (x == y) return;
f[x] = y;
} int exa[maxn];
int main()
{
int n, k, num = 0;
cin >> n >> k;
ll ans = 1;
for (int i = 1; i <= k; i++)
{
ans *= n;
ans %= mod;
}
//printf("%lld\n", ans);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i < n; i++)
{
int a, b, x;
scanf("%d%d%d", &a, &b, &x);
if (x) continue;
unite(a, b);
if (vis[a] == 0)
{
vis[a] = 1;
num++;
}
if (vis[b] == 0)
{
vis[b] = 1;
num++;
}
}
for (int i = 1; i <= n; i++) exa[i] = 0;
for (int i = 1; i <= n; i++)
{
if (vis[i] == 0) continue;
int x = findx(i);
exa[x]++;
//printf("exa[%d]=%d %d\n", x, exa[x],i);
}
for (int i = 1; i <= n; i++)
{
ll sum = 1;
if (exa[i] == 0) continue;//printf("%d\n", exa[i]);
for (int j = 1; j <= k; j++)
{
sum *= exa[i];
sum %= mod;
}
//printf("%lld\n", sum);
ans = (ans - sum + mod) % mod;
}
ans = (ans - (n - num) + mod) % mod;
printf("%lld\n", ans);
return 0;
}
C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块的更多相关文章
- C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】
一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...
- Codeforces Round 548 (Div. 2)
layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #548 (Div. 2) C dp or 排列组合
https://codeforces.com/contest/1139/problem/C 题意 一颗有n个点的树,需要挑选出k个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...
- 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) 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
随机推荐
- js 点击文本框,预览选择图片
点击文件选择框,选择图片文件,通过FileReader对象,读取图片文件中的内容,存放于result中,具体代码如下 <input type="file" onchange= ...
- 新浪IP库地址
新浪IP库地址 http://int.dpool.sina.com.cn/iplookup/iplookup.php
- webpack4 系列教程(十二):处理第三方JavaScript库
教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十二):处理第三方 JavaScript 库>原文地址.或者来我的小站看更多内容:godbm ...
- React之todo-list
基于React的一个简单Todo-list 先赌为快:在线DEMO,感觉还不错点一下star -_- ~ 源码地址: 一.已经完成的功能 1.新增选项(默认未完成) 2.完成状态可以切换 3.当前选 ...
- 2018年,JavaScript都经历了什么?
摘要: 对JSer来说,这是很有意思的1年. 本文灵感来自JavaScript Weekly周报,欢迎大家订阅. The State of JavaScript 2018 The State of J ...
- WORLD 目录排版调整
文本如下: ----------------------------------------------------------------- 前言1 简介2 我爱你3 圣灵丹方士大夫4 阿类似的看风 ...
- node(http, url)
一.http 模块 http.js const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; con ...
- nodeJs express mongodb 建站(mac 版)
基本环境 homebrew.node.npm.express.mongodb 1.node .npm : (1)辅助工具:homebrew安装(mac下一个软件管理工具,相当于Red hat的yum, ...
- Django的URL路由系统
一. URL配置 URL配置就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图之间的映射表.你就是以这种方式告诉Django,对于哪个URL调用的这段代码. 基本格式 from ...
- JHipster生成微服务架构的应用栈(五)- 容器编排示例
本系列文章演示如何用JHipster生成一个微服务架构风格的应用栈. 环境需求:安装好JHipster开发环境的CentOS 7.4(参考这里) 应用栈名称:appstack 认证微服务: uaa 业 ...