一、题面

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. jvm编译环境搭建 win Vc篇

    /************************************************************** 技术博客 http://www.cnblogs.com/itdef/   ...

  2. SpringMvc 全局异常处理器定义,友好的返回后端错误信息

    import com.google.common.collect.Maps; import org.apache.log4j.Logger; import org.springframework.be ...

  3. iOS Orientation获取

    [iOS Orientation获取] 1.[[UIDevice sharedInstance] orientation] 必须调用beginGeneratingDeviceOrientationNo ...

  4. 使用cmd命令登录mysql数据库时报2013-Lost connection to MYSQL server at 'waiting for initial communication packet',system error:0

    [错误内容]:SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packe ...

  5. [operator]ubuntu + sublime + anaconda 自动补全、指定python版本

    ubuntu .sublime.anaconda都安装好之后,首先要解决的就是自动补全问题 Perference---->Browes Packages --->新建一个Python的文件 ...

  6. linux下的文本操作之 文本查找——grep

    摘要:你有没有这样的应用场景:调试一个程序,出现debug的提示信息,现在你需要定位是哪个文件包含了这个debug信息,也就是说,你需要在一个目录下的多个文件(可能包含子目录)中查找某个字符串的位置: ...

  7. 四则运算生成器(java) 蔡苑菲,陆海燕

    github地址:https://github.com/Nancy0611/Myapp.git 一.项目相关要求 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n 10 将生成10个 ...

  8. mysql查看数据库性能常用命令

    mysql> show global status; 可以列出MySQL服务器运行各种状态值,另外,查询MySQL服务器配置信息语句: mysql> show variables; 一.慢 ...

  9. awk中NF的使用

    统计机器中网络连接各个状态个数 netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'   一下子不明白$NF是什么意 ...

  10. CheckBox使用记录

    页面显示 页面代码 <div> <div><input type="checkbox" value="" class=" ...