这个题没想出来,好菜QAQ...

题目要求至少经过一段黑色的边的答案,那么我们可以求总数减去不经过黑色的,这样就是答案了。

那么也就是求出每个只有红色边的连通块中有几个点。

总数就是n的k次方,设连通块有i个点,那么每次减去i的k次方就行了。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <iomanip>
#include <string>using namespace std;typedef long long int ll;
ll n,k,sum;
int u,v,x;
const int maxn=100000+5;
vector<int> edge[maxn];
const int mod=1e9+7;
int vis[maxn];ll quick_m(ll a,ll b){ll res=1ll;while(b){if(b%2) res=res*a%mod;a=a*a%mod;b/=2;}return res;
}void DFS(int x){vis[x]=1;sum++;for(int i=0;i<edge[x].size();i++){int nxt=edge[x][i];if(!vis[nxt]) DFS(nxt);}
}int main(){while(scanf("%I64d%I64d",&n,&k)!=EOF){for(int i=0;i<=n;i++) edge[i].clear();memset(vis,0,sizeof(vis));for(int i=1;i<n;i++){scanf("%d%d%d",&u,&v,&x);if(x==0){//去掉黑色的边edge[u].push_back(v);edge[v].push_back(u);}}ll ans=quick_m(n,k);for(int i=1;i<=n;i++){if(!vis[i]){sum=0;DFS(i);ans=((ans-quick_m(sum,k))%mod+mod)%mod;}}printf("%I64d\n",ans);}return 0;
}

  

转载于:https://www.cnblogs.com/JustDoA/p/10586090.html

Codeforces-Round#548(Div.2)-C-Edgy Trees-快速幂相关推荐

  1. # 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 ...

  2. 【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 ...

  3. Codeforces Round #548 (Div. 2) C. Edgy Trees(dfs || 并查集)

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

  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 #548 (Div. 2) C. Edgy Trees(并查集+快速幂)

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

  7. 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 ...

  8. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  9. Codeforces Round #548 (Div. 2), problem: (C) Edgy Trees 【并查集+快速幂】

    题意 给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路径中至少经过一条黑色的边,问这样的集合有多少个 思路 用并查集统计一个连通块的节点个数,最后用总的减去 ...

  10. Codeforces Round #162 (Div. 2) B. Roadside Trees (Simplified Edition)

    题目:http://codeforces.com/contest/265/problem/B #include <iostream> #include <stdio.h> #i ...

最新文章

  1. 代码参数里的 payload 究竟是什么意思
  2. 「GAN优化」如何学会以正确的姿势定量评价你的GAN
  3. Python 命名空间/名称查询 对效率的影响
  4. caffe的prototxt文件
  5. Kubernetes 2018 年度简史
  6. 工作中的javascript代码收集及整理
  7. K均值聚类关于初始聚类中心选取的一种改进(python程序)
  8. leetcode —— 965. 单值二叉树
  9. 豆瓣python网络数据采集器代理_Python 网络数据采集1
  10. python 对象是什么概念
  11. Kano模型思考:以QQ第一版为例,如何确定产品优先级
  12. shell美元符_Linux Shell中的美元符号$ | 学步园
  13. 金庸武侠十大外功排行
  14. 为什么团建这么招人恨
  15. SAP License:仓库ERP系统
  16. 有趣的表情包购物网站
  17. 58同城产品2面面经
  18. NGNIX在CENTEROS 下部署
  19. 下载文件同时添加水印文字
  20. Oracle分区表及分区索引的创建

热门文章

  1. 云服务器无法访问解决办法
  2. jmeter第21讲 阿里云下载安装JDK并配置环境变量
  3. 开发简单 web 服务程序 cloudgo
  4. 开发运维(DevOps)自动化运维与持续交付企业级实战
  5. 合成大西瓜html源码,合成大西瓜
  6. 聚合支付码 一码支付的大时代到来
  7. 如何用计算机算ln,如何用计算器算LN(对数)?
  8. 小程序-手写签名(附代码)
  9. mathtype向上取整函数
  10. Linux | 云盘扩容后,lsblk显示已增加,df -h显示没变化