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
随机推荐
- 编译hadoop的libhdfs.a
进入hadoop-hdfs-project/hadoop-hdfs/src目录,执行cmake以生成Makefile文件. 如果遇到如下的错误: ~/hadoop-2.7.1-src/hadoop-h ...
- SpringMVC源码解析 - HandlerAdater - ModelAndViewContainer上下文容器
HandlerAdapter在处理请求时上下文数据的传递工作是由ModelAndViewContainer负责的. 源码注释是这样描述的: Records model and view related ...
- ZSTU4270 同源数 2017-03-22 14:34 82人阅读 评论(0) 收藏
4270: 同源数 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1284 Solved: 224 Description 如果x和y的质因子集合完 ...
- Windbg and resources leaks in .NET applications 资源汇总
Windows Forms Leaks 1.http://blogs.msdn.com/b/tess/archive/2008/02/04/net-debugging-demos-informatio ...
- django drf django-filter的method过滤
1.View Demo from django.shortcuts import render from rest_framework.views import APIView from rest_f ...
- IOS渠道追踪方式
本文来自网易云社区 作者:马军 IOS,安卓渠道追踪的差异 Google Play国内不可用,国内的安卓 App 分发,都是依托几十个不同的应用市场或发行渠道,如百度.360.腾讯等互联网企业以及小米 ...
- sgi stl内存池实现------源码加翻译
class __default_alloc_template { enum { unit = 8 };//分配单位 后面直接用8代替 enum { max_bytes = 128 };//最大分配字节 ...
- python--内置常用模块(一) collections 队列 time模块 functiontools
一. 模块的简单认识 模块就是我们把装有特定功能的代码进行归类的结果.从代码编写的单位来看我们的程序,从小到大的顺序:一条代码 < 语句块 < 代码块(函数,类) < 模块,我们目前 ...
- python--类的约束,异常处理,MD5加密,日志处理logging模块
1.类的约束 在开发中,如果项目经理需要对类进行约束,可以有两种方式 1. 对子类进行约束 Base: #对子类进行约束,必须重写这个方法 # 在工作中发现了NotImplementedError之后 ...
- 201621123023《Java程序设计》第10周学习总结
一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 二.书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出 ...