一、题面

here

二、分析

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

三、AC代码

  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long LL;
  6. const int MOD = 1e9 + ;
  7. const int MAXN = 1e5 + ;
  8. vector<int> Graph[MAXN];
  9. bool visited[MAXN];
  10. int T, N, K;
  11.  
  12. LL Pow(LL a, LL b)
  13. {
  14. LL ans = ;
  15. while(b)
  16. {
  17. if(b&)
  18. {
  19. ans = ans * a % MOD;
  20. }
  21. a = a * a % MOD;
  22. b >>= ;
  23. }
  24. return ans;
  25. }
  26.  
  27. void DFS(int v)
  28. {
  29. if(visited[v])
  30. return;
  31. visited[v] = ;
  32. T++;
  33. for(auto &itr:Graph[v])
  34. {
  35. DFS(itr);
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. scanf("%d %d", &N, &K);
  42. memset(visited, , sizeof(visited));
  43. for(int i = ; i < N; i++)
  44. {
  45. int x, y, c;
  46. scanf("%d %d %d", &x, &y, &c);
  47. if(c == )
  48. {
  49.  
  50. Graph[x].push_back(y);
  51. Graph[y].push_back(x);
  52. }
  53. }
  54. LL Ans = ;
  55. for(int i = ; i <= N; i++)
  56. {
  57. if(visited[i])
  58. continue;
  59. T = ;
  60. DFS(i);
  61. Ans = (Ans + Pow(T, K)) % MOD;
  62. }
  63. Ans = (Pow(N, K) - Ans + MOD) % MOD;
  64. printf("%I64d\n", Ans);
  65. return ;
  66. }

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. ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法(转)

    ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法 分类: android应用开发2013-12-19 09:40 1045人阅读 评论(3) 收藏 举报 AndroidS ...

  2. Function 对象 & anonymous 匿名函数

    functionName = new Function( [argname1, [... argnameN,]] body ); 例子: var say = new Function("na ...

  3. python 3安装PDFMiner3K

    首先确保,你的pyhton是python 3 可在https://www.python.org/downloads/处下载 打开cmd,键入pip3 install pdfminer3k

  4. Leader/Follower多线程网络模型介绍

    之前分享过<轻量级 web server Tornado代码分析>,介绍了目前我们采用nginx + tornado的方式搭建升级.配管.数据中心等各类服务组建客户端迭代体系.最近注意到, ...

  5. HRBUST1200 装修 2017-03-06 15:41 94人阅读 评论(0) 收藏

    装修 hero为了能顺利娶princess ,花了血本,买了个房子,现在决定装修.房子的长度为n米,宽度为3米,现在我们有2种地砖,规格分别是1米×1米,2米×2米,如果要为该教室铺设地砖,请问有几种 ...

  6. VS2017中对C++的单元测试

    安装Visual Studio 2017 由于平时都是用codeblock,因此电脑中没有装VS系列的IDE,就从安装开始吧 最开始安装的时候没有注意什么都没选,安装完了以后根本没有c++的编译器和各 ...

  7. delphi跨平台SOCKET--System.Net.Socket

    delphi跨平台SOCKET--System.Net.Socket 不知始于DELPHI哪一个版本,姑且始于柏林版吧. 基于此单元的TSocket类,大家可以很方便地封装出自己的服务端和客户端的SO ...

  8. Android-自定义控件-继承View与ViewGroup的初步理解

    继承View需要走的流程是: 1.构造实例化, public ChildView(Context context, @Nullable AttributeSet attrs) 2.测量自身的高和宽on ...

  9. WPF 按名称查找控件

    1FrameworkElement类FindName方法 使用过程 1.容器控件.RegisterName("Name",要注册的控件)   //注册控件 2.容器控件.FindN ...

  10. c# in out ref关键字

    class in_out_ref { #region in 关键字 delegate void DContravariant<in A>(A argumen); static void o ...