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
随机推荐
- [angularjs] 前端路由实现单页跳转
代码: <div ng-app="Home"> <div ui-view></div> <div ng-controller=" ...
- 10.QT-定时器
QObject定时器 需要头文件#include <QTimerEvent> 需要函数 int QObject::startTimer(int interval); //启动定时器,并设 ...
- Android Material Design控件使用(二)——FloatButton TextInputEditText TextInputLayout 按钮和输入框
FloatingActionButton 1. 使用FloatingActionButton的情形 FAB代表一个App或一个页面中最主要的操作,如果一个App的每个页面都有FAB,则通常表示该App ...
- Java学习笔记之——内部类
内部类 形式:把一个类定义在一个类的内部. 分为:成员内部类和匿名内部类重点掌握 a) 成员内部类 b) 静态成员内部类 c) 匿名内部类 d) 局部内部类 (1)成员内部类: Java的语言是面向对 ...
- 挑战常规--为什么不应该使用Jsonp进行跨域
常规跨域的方法 常见跨域的方法有: 添加Access-Control-Allow-Origin 后台服务器代理 Jsonp 1.2两种方法都是安全可靠的,3是不安全不可靠的 Json的本质 Json本 ...
- 反向ajax实现原理
留言板显示留言一直刷新,那么实现原理是怎么样的? 一般发送留言就是通过正常的ajax向服务器发送数据,而实时显示留言就需要用到轮询了. 什么是轮询:就是使用定时器,每隔固定的时间从客户端向服务器发起请 ...
- Java 开源博客 Solo 1.9.0 发布 - 新皮肤
这个版本主要是改进了评论模版机制,让大家更方便皮肤制作,并发布了一款新皮肤:9IPHP. Solo 是一款一个命令就能搭建好的 Java 开源博客系统,并内置了 15+ 套精心制作的皮肤.除此之外,S ...
- tomcat闪退解决
异常原因:拷贝了一个tomcat到新机器上,运行startup闪退 解决方法: 1.检查发现当前系统没有安装配置jdk,安装配置后运行仍然闪退 2.在tomcat的启动脚本和关闭脚本中指定JDK和to ...
- loadrunner 脚本优化-集合点设置
脚本优化-集合点设置 by:授客 QQ:1033553122 添加集合点(Insert->Rendezvous) 当一个集合点被插入,VuGen往Vuser脚本中插入一个lr_rendezvou ...
- .net webapi 后台导出excel 申请付款单实例
[HttpGet, AllowAnonymous] public void ExportSettlementPrint(string code) { FinSettlementModel settle ...