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
随机推荐
- 从零开始学安全(十)●TCP/IP协议栈
局域网靠mac 地址通信
- Java_IO流_抽象类
一. 概念 流动,从一端流向另一端,源头与目的地 以程序为中心,程序与 数组/文件/网络连接/数据库, 二. io流分类 流向:输入流与输出流 数据 :字节楼:二进制,所有文件都可以操作,包括 ...
- youtube link for review STAT(1)
Confidence Interval: https://www.youtube.com/watch?v=bekNKJoxYbQ Introduction to confidence interval ...
- 剑指offer:1.找出数组中重复的数(java版)
数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...
- SQLite: sql script demo
如果有成熟的架构,如何根据数据库关系的表.视图等,进行代码生成架构?减少写代码的时间? -- 考虑主键外键 -- create database geovindu; use geovindu; --2 ...
- vue从入门到进阶:vue-router路由功能(九)
基本使用 html: <script src="https://unpkg.com/vue/dist/vue.js"></script> <scrip ...
- HDU 2586 How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- css不受高度限制实现文本超出隐藏并以省略号结束
文本超出省略号显示代码: overflow: hidden; text-overflow:ellipsis; white-space: nowrap;width: 100px; /*宽度做好限制*/ ...
- CentOS 安装 jdk
1.首下载CentOS对应的jdk压缩包. 2.通过secureCRT工具远程连接目标服务器. 3.通过rz命令上传jdk压缩包到linux服务器. 4.解压缩上传的jdk压缩包 tar -zxvf ...
- 一位ML工程师构建深度神经网络的实用技巧
一位ML工程师构建深度神经网络的实用技巧 https://mp.weixin.qq.com/s/2gKYtona0Z6szsjaj8c9Vg 作者| Matt H/Daniel R 译者| 婉清 编辑 ...