一、题面

here

二、分析

这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题。首先在建图的时候,只考虑红色路径上的点。为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路径,这样把所有的可能数再减去只走红色路径的数就是最终的答案了。这里要注意的是,如果连通块里只有一个点,那么就是K个点都是这个点的情况,根据题意是不满足的,也要减去。

三、AC代码

 #include<bits/stdc++.h>

 using namespace std;

 typedef long long LL;
const int MOD = 1e9 + ;
const int MAXN = 1e5 + ;
vector<int> Graph[MAXN];
bool visited[MAXN];
int T, N, K; LL Pow(LL a, LL b)
{
LL ans = ;
while(b)
{
if(b&)
{
ans = ans * a % MOD;
}
a = a * a % MOD;
b >>= ;
}
return ans;
} void DFS(int v)
{
if(visited[v])
return;
visited[v] = ;
T++;
for(auto &itr:Graph[v])
{
DFS(itr);
}
} int main()
{
scanf("%d %d", &N, &K);
memset(visited, , sizeof(visited));
for(int i = ; i < N; i++)
{
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
if(c == )
{ Graph[x].push_back(y);
Graph[y].push_back(x);
}
}
LL Ans = ;
for(int i = ; i <= N; i++)
{
if(visited[i])
continue;
T = ;
DFS(i);
Ans = (Ans + Pow(T, K)) % MOD;
}
Ans = (Pow(N, K) - Ans + MOD) % MOD;
printf("%I64d\n", Ans);
return ;
}

C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】的更多相关文章

  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. Codeforces Round 548 (Div. 2)

    layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...

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

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

  4. Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

    https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\), ...

  5. Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理

    https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...

  6. Codeforces Round #548 (Div. 2) C dp or 排列组合

    https://codeforces.com/contest/1139/problem/C 题意 一颗有n个点的树,需要挑选出k个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...

  7. Codeforces Round #548 (Div. 2) D 期望dp + 莫比乌斯反演

    https://codeforces.com/contest/1139/problem/D 题意 每次从1,m中选一个数加入队列,假如队列的gcd==1停止,问队列长度的期望 题解 概率正着推,期望反 ...

  8. Codeforces Round #548 (Div. 2) B. Chocolates

    You went to the store, selling 

  9. Codeforces Round #548 (Div. 2) A. Even Substrings

    You are given a string 

随机推荐

  1. SpringCloud之自动化配置-config

    编程开发的时候有没有觉得很多配置文件需要维护,比如,修改了数据库连接,所有用到该数据库的服务配置都得替换,是不是超级的麻烦呢 下面,给大家介绍一下Spring提供的配置自动化组件-spring clo ...

  2. maven pom.xml中的 build说明

    在Maven的pom.xml文件中,Build相关配置包含两个部分,一个是<build>,另一个是<reporting>,这里我们只介绍<build>. 1. 在M ...

  3. dojo表格操作的简单示例(建立表格)

    代码示例: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w ...

  4. Jetty 源码分析

    一. 总括      你了解Jetty 吗,就像我们所熟知的Tomcat一样, Jetty是一个免费的开放源码的100%纯Java的Http服务器和Servlet容器. Jetty具备以下特点:   ...

  5. win32多线程-异步(asynchronous) I/O

    I/O设备是个慢速设备,无论打印机.调制解调器,甚至硬盘,与CPU相比都奇慢无比,坐下来干等I/O的完成是一件不甚明智事情. 异步(asynchronous) I/O在win32多线程程序设计中被称为 ...

  6. DRBD+Heartbeat实现自动切换

    1>HeartBeat介绍 Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组件,在 Heartbeat 项 ...

  7. 咏南中间件V10.1更新日志

    咏南中间件V10.1 2016-8-3----------------------------------------------------开始支持DELPHI10.1(BERLIN)增加中间件业务 ...

  8. TSQL--使用CTE完成递归查询

    CREATE TABLE TB001( CategoryId INT PRIMARY KEY, ParentCategoryId INT, CategoryName NVARCHAR(200))GO ...

  9. Ultimate guide to learning AngularJS in one day

    What is AngularJS? Angular is a client-side MVC/MVVM framework built in JavaScript, essential for mo ...

  10. 十二生肖查询网页版制作(php)

    今天无聊做了一个十二生肖查询器: 预览网址效果:http://hongxing01.hktd02u.me48.com/03Sxcx 源代码下载:http://down.51cto.com/data/1 ...