一、题面

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. div清空填充

    1.清空 $('#editPrize').empty(); 2.填充 var html= '<span>追加</span>'; $('#editPrize' ).append( ...

  2. php快速获取所有的自定义常量用户常量

    快速获取自定义的常量.用户常量 echo "<pre>"; print_r(get_defined_constants(true)['user']); echo &qu ...

  3. jquery相对定位(包含find的使用 find相当于后代选择器)$("选择器1","选择器2")

  4. 6 scrapy框架之分布式操作

    分布式爬虫 一.redis简单回顾 1.启动redis: mac/linux:   redis-server redis.conf windows: redis-server.exe redis-wi ...

  5. Thrift线程和状态机分析

    目录 目录 1 1. 工作线程和IO线程 1 2. TNonblockingServer::TConnection::transition() 2 3. RPC函数被调用过程 3 4. 管道和任务队列 ...

  6. Tomcat 系统架构与设计模式2

    门面设计模式 门面设计模式在 Tomcat 中有多处使用,在 Request 和 Response 对象封装中.Standard Wrapper 到 ServletConfig 封装中.Applica ...

  7. Android-封装JSON数据(JSON对象/JSON数组)

    Android-封装JSON数据(JSON对象/JSON数组),一般情况下不会在Android端封装JSON的数据,因为封装JSON的数据是在服务器端进行封装了,Android更多的工作是解析(JSO ...

  8. 自己写一个图片按钮(XAML)

    有时需要用三张图片(正常状态,鼠标移上,鼠标按下)来作为一个按钮的样式,虽然这种做法不好,应该用矢量的方式制作样式,但有的时候还是需要这样做的. 每次都修改按钮的样式来实现这个做法,既麻烦又会生成大段 ...

  9. sql2008 获取表结构说明

    SELECT     表名       = case when a.colorder=1 then d.name else '' end,    表说明     = case when a.color ...

  10. C#文字转换语音朗读或保存MP3、WAV等格式

    最近遇到一个需求,需要把文字转换语音,参考很多大佬写的方法,最后经过自己改造实现文字在线朗读.保存MP3.WAV等格式. //需要引用System.Speech程序集 //引用using System ...