题目链接:https://codeforces.com/contest/1139/problem/C

题意:给了一棵树,n个点,m条边。让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路径中至少经过一条黑色的边,问这样的集合有多少个。

思路:在长度为k的序列里,由于每个位置有n种可能,所以总的可能数为用总的可能数减去仅经过红边的序列数即可。然后统计只经过红边的节点在同一个连通区域的结果个数,减去输出即可。并查集另开一个c数组存节点个数。

dfs代码:

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define IOS cin.tie(0); ios::sync_with_stdio(0);
const int mod = 1e9+7;
const int N = 1e6+7;
int ant, vis[N];
std::vector< int > v[N];
int dfs(int x)
{vis[x] = 1; ant++;for(int i = 0; i < v[x].size(); i++)if(!vis[v[x][i]]) dfs(v[x][i]);
}
ll poww(ll a, ll b)
{ll ans = 1;while (b){if(b & 1) ans = (ans * a) % mod;a = (a * a) % mod;b >>= 1;}return ans;
}
int main()
{IOS; int n, k;std::cin >> n >> k;for(int i = 1; i < n; i++){int a, b, x;std::cin >> a >> b >> x;if(!x){v[a].push_back(b);v[b].push_back(a);}}ll ans = poww(n, k), cnt = 0;for(int i = 1; i <= n; i++){ant = 0;if(!vis[i])dfs(i), (cnt += poww(ant, k)) %= mod;}std::cout << (ans - cnt + mod) % mod << '\n';
}

并查集代码: 

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define IOS cin.tie(0); ios::sync_with_stdio(0);
const int mod = 1e9+7;
const int N = 1e6+7;
int f[N], c[N];
int findd(int x)
{if(x != f[x]) return f[x] = findd(f[x]);return x;
}
void join(int x, int y)
{int tx = findd(x), ty = findd(y);if(tx != ty) f[tx] = ty, c[ty] += c[tx];
}
ll poww(ll a, ll b)
{ll ans = 1;while (b){if(b & 1) ans = (ans * a) % mod;a = (a * a) % mod;b >>= 1;}return ans;
}
int main()
{IOS; int n, k;std::cin >> n >> k;for(int i = 1; i <= n; i++)f[i] = i, c[i] = 1;for(int i = 1; i < n; i++){int a, b, x;std::cin >> a >> b >> x;if(!x) join(a, b);}ll ans = poww(n, k), cnt = 0;for(int i = 1; i <= n; i++){if(findd(i) == i)(cnt += poww(c[i], k) ) %= mod;}std::cout << (ans - cnt + mod) % mod << '\n';
}

Codeforces Round #548 (Div. 2) C. Edgy Trees(dfs || 并查集)相关推荐

  1. Codeforces Round #548 (Div. 2) C. Edgy Trees(并查集+快速幂)

    思路用并查集统计一个连通块的节点个数,最后用总的减去他,设x是连通块的节点个数,o个联通块 #include<bits/stdc++.h> #define fi first #define ...

  2. # Codeforces Round #548 (Div. 2)C Edgy Trees

    Codeforces Round #548 (Div. 2)C Edgy Trees 题目传送门 You are given a tree (a connected undirected graph ...

  3. 【Codeforces Round #548(Div. 2)】Edgy Trees(数学+bfs求连通块)

    题目链接 C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #548 (Div. 2)C. Edgy Trees 并查集

    codeforces 1139C 题目链接:http://codeforces.com/contest/1139/problem/C 题意: 给你一个n个结点n-1条边的无向连通图,由红边和黑边组成. ...

  5. Codeforces Round #548 (Div. 2) C. Edgy Trees(思维+dfs)

    题目链接:https://codeforces.com/contest/1139/problem/C        题意是给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak ...

  6. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  7. Codeforces Round #218 (Div. 2) D. Vessels(思维 并查集)

    题意:从上到下有n个杯子,编号从1到n.每个杯子有一定体积v[i]. 两种操作:1 x y, 向x水杯倒y水; 2 x, 询问x水杯有多少水. (水杯水溢出会往下流) n,q <= 2e5 思路 ...

  8. C. Edgy Trees---(思维题+并查集的运用)---Codeforces Round #548 (Div. 2)

    Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes 题目链接http://codeforces.c ...

  9. [题解][Codeforces 1139A~1139F]Codeforces Round #548 (Div. 2) 简要题解

    终于 rank < 10 了 题目 洛谷 RemoteJudge A B C D E F Codeforces A B C D E F A 题意 一个长度为 nnn 的数字串 sss 求 sss ...

最新文章

  1. java 偶数求和 数组_JAVA实现幻方
  2. python 装饰器 参数-python函数装饰器之带参数的函数和带参数的装饰器用法示例...
  3. Python 之 使用 PIL 库做图像处理
  4. java 类型转换_java中的基本数据类型的转换
  5. python如何将图片打包进exe里_用python将图片切分为九宫格 并打包成exe可执行文件(附源码)...
  6. js 正则中冒号代表什么_是否还在疑惑Vue.js中组件的data为什么是函数类型而不是对象类型...
  7. paip.提升效率---模块化设计方法V2012.9.15
  8. PC端 二维码/条形码扫描器1.1-支持截图+摄像头+本地图片+扫描枪识别
  9. 3458A数字多用表的4线制电阻测量方法
  10. 一名5年工作经验的程序员应该具备的技能
  11. 【设计模式】之原型模式详解与应用(五)
  12. 条形码转化成二维码_在线条形码生成器
  13. (五)Guarded Suspension模式
  14. 钉钉H5应用后台回调地址设置(服务端代码逻辑编写).NetCore API
  15. 丰富网页摘要——HTML5中的“微数据”(MicroData)
  16. Cobbler 3.x 部署实战
  17. 输入a,b,c三个整数求其中最大值(C语言实现)
  18. display:none和visibility:hidden的区别
  19. 信号完整性入门笔记一-细解为什么低频信号在较短传输线不考虑反射?
  20. python调用cplex_如何用python结合cplex求解混合整数规划问题

热门文章

  1. 【RocketMQ】
  2. MyBatis----回顾mybatis自定义和环境搭建+完善自定义Mybatis的注解开发
  3. 转载:js和as间的交互
  4. mybatis代码自动生成工具之maven插件mybatis-generator-maven-plugin(mybatis逆向工程)
  5. php 五舍六入,四舍五入计算器 四舍五入、四舍六入五取偶(双)算法 - 数学公式 - 房贷计算器...
  6. LabVIEWCNN基础
  7. 0328 - 一日三更
  8. echarts图表x轴基准线(平行y轴)
  9. 深入浅出JS—20 生成器控制函数执行
  10. 关于常用(?)字符串处理函数的合集