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
随机推荐
- 结构型---装饰者模式(Decorator Pattern)
定义 装饰者模式以对客户透明的方式动态地给一个对象附加上更多的责任,装饰者模式相比生成子类可以更灵活地增加功能. Component抽象构件 Component是一个接口或者是抽象 ...
- 禅道导入bugfree 3.0的数据
禅道项目导入bugfree功能只支持到2.0, 官方不提供3.0的导入,只好自己写了一个.因为bugfree 3.0换人开发了,表结构和禅道差别很大,所以,这个工具不是完全转换,一些History表内 ...
- java集合框架-List集合ArrayList和LinkedList详解
List 集合源码剖析 ✅ ArrayList 底层是基于数组,(数组在内存中分配连续的内存空间)是对数组的升级,长度是动态的. 数组默认长度是10,当添加数据超越当前数组长度时,就会进行扩容,扩容长 ...
- JavaScript开发工具简明历史
译者按: JavaScript开发要用到的工具越来越多,越来越复杂,为什么呢?你真的弄明白了吗? 原文: Modern JavaScript Explained For Dinosaurs 为了保证可 ...
- select2 插件编辑时设置默认值
function htDate(selectCustomerId, val) { var customerId = selectCustomerId; var values = val; ajaxJs ...
- Web标准中xhtml规范的内容有哪些
1.所有的标记都必须要有一个相应的结束标记 以前在HTML中,你可以打开许多标签,例如<p>和<li>而不一定写对应的</p>和</li>来关闭它们.但 ...
- HDU1521 排列组合(生成函数 背包)
题意 链接 Sol 可以用生成函数做,也可以用组合数做. 生成函数就是无脑算一下阶乘暴力背包,然后最后再乘上\(M\)的阶乘 组合数的方法就是用类似背包的转移,转移的时候考虑当前放的这几个的方案数即可 ...
- FUNCTIONALITY OF ITEM CATEGORY
Item Category Purpose This wiki page will breify discuss about functionality of Item Category in SAP ...
- WPF:Hyperlink如何绑定数据
<TextBlock> <Hyperlink> <Run Text="{Binding PCFolderPath, Mode=OneWay}"/> ...
- Apache httpd.conf配置文件主要内容解释
1 ServerRoot 配置 ["ServerRoot" 主要用于指定Apache的安装路径,此选项参数值在安装Apache时系统会自动把Apache的路径写入.Windows安 ...