题目地址:CF1139C Edgy Trees

红黑树

\(ans\) 应该等于总数(\(n^k\))减去不含黑色边的序列数量

不含黑色边就意味着一个序列只能在一个红色联通块中

一个红色联通块中的序列数量应该是点数的 \(k\) 次方

求联通块用dfs用并查集都可以

然后快速幂一下再一减就是 \(ans\)

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 6, P = 1e9 + 7;
int n, k, v[N], cnt;
vector<int> e[N];

inline int ksm(int a, int b) {
    int c = 1;
    while (b) {
        if (b & 1) c = (ll)c * a % P;
        a = (ll)a * a % P;
        b >>= 1;
    }
    return c;
}

void dfs(int x) {
    v[x] = 1;
    ++cnt;
    for (unsigned int i = 0; i < e[x].size(); i++) {
        int y = e[x][i];
        if (v[y]) continue;
        dfs(y);
    }
}

int main() {
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int x, y, z;
        scanf("%d %d %d", &x, &y, &z);
        if (z) continue;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    int ans = ksm(n, k);
    for (int i = 1; i <= n; i++)
        if (!v[i]) {
            cnt = 0;
            dfs(i);
            ans = (ans - ksm(cnt, k) + P) % P;
        }
    cout << ans << endl;
    return 0;
}

CF1139C Edgy Trees的更多相关文章

  1. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. C. Edgy Trees

    链接 [https://codeforces.com/contest/1139/problem/C] 题意 给你n个点,n-1个边,无向的.有red和black的. k表示经过这k个点.可以跨其他点 ...

  3. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  4. Codeforces Round #548 (Div. 2) C. Edgy Trees

    You are given a tree (a connected undirected graph without cycles) of 

  5. Codeforces Round #548 C. Edgy Trees

    题面: 传送门 题目描述: 给出有n个节点的树,整数k.题目要求找长度为k,符合规则(good序列)的"点序列"(由节点构成的序列)个数有多少?规则如下: 1.走一条出发点为a1, ...

  6. CodeForces Round #548 Div2

    http://codeforces.com/contest/1139 A. Even Substrings You are given a string s=s1s2…sns=s1s2…sn of l ...

  7. Codeforces Round #548

    没打,简单补档 C.Edgy Trees 容斥,把黑边断掉数联通块,每个联通块贡献$siz^k$ #include<cstdio> #include<cstring> #inc ...

  8. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  9. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

随机推荐

  1. Oracle 执行计划(三)-------表连接方式

    SQL FOR TESTING: create table qcb_student_test( student_id number, student_name varchar2(20), studen ...

  2. 移动端无限滚动 TScroll.vue组件

    // 先看使用TScroll.vue的几个demo 1.https://sorrowx.github.io/TScroll/#/ 2. https://sorrowx.github.io/TScrol ...

  3. 使用Github生成燃尽图

    经过一晚上折腾,终于算是把linux上成功生成了我们团队项目的燃尽图,效果还是不错,在过程中又发现了另一种生成燃尽图的方式,也是基于一个开源项目. 1.准备: 首先你的项目一定要有milestone. ...

  4. (转)JMeter学习逻辑控制器

    JMeter中的Logic Controller用于为Test Plan中的节点添加逻辑控制器. JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行过程中节点 ...

  5. 使用FaceNet 图像相识度对比

    1. 模型结构:

  6. 前端jquery 获取select多选的值

    当select设置属性multiple='multiple'时, option就可以多选了  那么我们如何获取所有被选中的option的值呢? 首先说明: $('select[name="m ...

  7. Word Representations 词向量

    常用的词向量方法word2vec. 一.Word2vec 1.参考资料: 1.1) 总览 https://zhuanlan.zhihu.com/p/26306795 1.2) 基础篇:  深度学习wo ...

  8. Nginx 11阶段的顺序处理

    L49

  9. luogu P1613 跑路

    一开始看这道题时,发现是最短路,可是搜的又是倍增的题无可分说这是倍增+最短路 但是Dijkstra,SPFA我又不熟,可是看了数据范围心中萌生一种用Floyd做的方法 不扯了 先设一个三维bool数组 ...

  10. jdk8在windows及linux环境下安装

    jdk下载 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 目前大部分公司内部使用的还是jdk8,大部 ...