Codeforces-Round#548(Div.2)-C-Edgy Trees-快速幂
这个题没想出来,好菜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-快速幂相关推荐
- # 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 ...
- 【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 ...
- 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的路 ...
- Codeforces Round #548 (Div. 2)C. Edgy Trees 并查集
codeforces 1139C 题目链接:http://codeforces.com/contest/1139/problem/C 题意: 给你一个n个结点n-1条边的无向连通图,由红边和黑边组成. ...
- Codeforces Round #548 (Div. 2) C. Edgy Trees(思维+dfs)
题目链接:https://codeforces.com/contest/1139/problem/C 题意是给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak ...
- Codeforces Round #548 (Div. 2) C. Edgy Trees(并查集+快速幂)
思路用并查集统计一个连通块的节点个数,最后用总的减去他,设x是连通块的节点个数,o个联通块 #include<bits/stdc++.h> #define fi first #define ...
- 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 ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】
一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...
- Codeforces Round #548 (Div. 2), problem: (C) Edgy Trees 【并查集+快速幂】
题意 给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路径中至少经过一条黑色的边,问这样的集合有多少个 思路 用并查集统计一个连通块的节点个数,最后用总的减去 ...
- Codeforces Round #162 (Div. 2) B. Roadside Trees (Simplified Edition)
题目:http://codeforces.com/contest/265/problem/B #include <iostream> #include <stdio.h> #i ...
最新文章
- 代码参数里的 payload 究竟是什么意思
- 「GAN优化」如何学会以正确的姿势定量评价你的GAN
- Python 命名空间/名称查询 对效率的影响
- caffe的prototxt文件
- Kubernetes 2018 年度简史
- 工作中的javascript代码收集及整理
- K均值聚类关于初始聚类中心选取的一种改进(python程序)
- leetcode —— 965. 单值二叉树
- 豆瓣python网络数据采集器代理_Python 网络数据采集1
- python 对象是什么概念
- Kano模型思考:以QQ第一版为例,如何确定产品优先级
- shell美元符_Linux Shell中的美元符号$ | 学步园
- 金庸武侠十大外功排行
- 为什么团建这么招人恨
- SAP License:仓库ERP系统
- 有趣的表情包购物网站
- 58同城产品2面面经
- NGNIX在CENTEROS 下部署
- 下载文件同时添加水印文字
- Oracle分区表及分区索引的创建