C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】
一、题面
二、分析
这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题。首先在建图的时候,只考虑红色路径上的点。为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路径,这样把所有的可能数再减去只走红色路径的数就是最终的答案了。这里要注意的是,如果连通块里只有一个点,那么就是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) 【连通块】的更多相关文章
- 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 ...
- Codeforces Round 548 (Div. 2)
layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 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) C dp or 排列组合
https://codeforces.com/contest/1139/problem/C 题意 一颗有n个点的树,需要挑选出k个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...
- 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
随机推荐
- ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法(转)
ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法 分类: android应用开发2013-12-19 09:40 1045人阅读 评论(3) 收藏 举报 AndroidS ...
- Function 对象 & anonymous 匿名函数
functionName = new Function( [argname1, [... argnameN,]] body ); 例子: var say = new Function("na ...
- python 3安装PDFMiner3K
首先确保,你的pyhton是python 3 可在https://www.python.org/downloads/处下载 打开cmd,键入pip3 install pdfminer3k
- Leader/Follower多线程网络模型介绍
之前分享过<轻量级 web server Tornado代码分析>,介绍了目前我们采用nginx + tornado的方式搭建升级.配管.数据中心等各类服务组建客户端迭代体系.最近注意到, ...
- HRBUST1200 装修 2017-03-06 15:41 94人阅读 评论(0) 收藏
装修 hero为了能顺利娶princess ,花了血本,买了个房子,现在决定装修.房子的长度为n米,宽度为3米,现在我们有2种地砖,规格分别是1米×1米,2米×2米,如果要为该教室铺设地砖,请问有几种 ...
- VS2017中对C++的单元测试
安装Visual Studio 2017 由于平时都是用codeblock,因此电脑中没有装VS系列的IDE,就从安装开始吧 最开始安装的时候没有注意什么都没选,安装完了以后根本没有c++的编译器和各 ...
- delphi跨平台SOCKET--System.Net.Socket
delphi跨平台SOCKET--System.Net.Socket 不知始于DELPHI哪一个版本,姑且始于柏林版吧. 基于此单元的TSocket类,大家可以很方便地封装出自己的服务端和客户端的SO ...
- Android-自定义控件-继承View与ViewGroup的初步理解
继承View需要走的流程是: 1.构造实例化, public ChildView(Context context, @Nullable AttributeSet attrs) 2.测量自身的高和宽on ...
- WPF 按名称查找控件
1FrameworkElement类FindName方法 使用过程 1.容器控件.RegisterName("Name",要注册的控件) //注册控件 2.容器控件.FindN ...
- c# in out ref关键字
class in_out_ref { #region in 关键字 delegate void DContravariant<in A>(A argumen); static void o ...