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
随机推荐
- JQuery ajax的使用
JQuery 真的是好东西 $.ajax({ type: "post", url: "/DataCheck", ...
- 从零开始学安全(二十六)●利用Nmap目标的本版进行探测
通过对对方电脑的服务探测 对本版较低的服务 或者无补丁的服务 可以直入侵 版本探测 version 后边就是版本
- 构建SpringBoot第一个Demo
使用官方地址生成项目 https://start.spring.io Generate:可以选择Maven或者Gradle构建项目 语言:我想一般都是Java 接下来选择SpringBoot的版本, ...
- 【 js 工具 】如何在Github Pages搭建自己写的页面?
最近发现 github 改版了,已没有像原来的 Launch automatic page generator 这样的按钮等,所以我对我的文章也进行了修正,对于新版来说,步骤更加简单了.欢迎享用. - ...
- JS实现数组去重方法整理
前言 我们先来看下面的例子,当然来源与网络,地址<删除数组中多个不连续的数组元素的正确姿势> 我们现在将数组中所有的‘ a’ 元素删除: var arr = ['a', 'a', 'b', ...
- JS对全角与半角的验证,相互转化以及介绍
1.什么是全角和半角? 全角:是一种电脑字符,是指一个全角字符占用两个标准字符(或两个半角字符)的位置.全角占两个字节. 汉字字符和规定了全角的英文字符及国标GB2312-80中的图形符号和特殊字符都 ...
- window.requestAnimationFrame与Tween.js配合使用实现动画缓动效果
window.requestAnimationFrame 概述 window.requestAnimationFrame()这个方法是用来在页面重绘之前,通知浏览器调用一个指定的函数,以满足开发者操作 ...
- css不受高度限制实现文本超出隐藏并以省略号结束
文本超出省略号显示代码: overflow: hidden; text-overflow:ellipsis; white-space: nowrap;width: 100px; /*宽度做好限制*/ ...
- windows10系统关闭自动更新服务
一.关闭Windows10系统的自动更新服务 1:使用快捷键Win+R,打开运行 2:输入命令:services.msc,打开系统服务界面 找到Windows Update双击 将启动类型改为[禁用] ...
- vue2 设置网页title的问题
好东东,没个标题看着多难受 看到1文章 http://blog.csdn.net/qishuixian/article/details/72912368 推荐使用 vue-wechat-title插 ...