Edgy Trees(dfs,并查集,快速幂,树形结构,红黑树)

Edgy Trees
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree (a connected undirected graph without cycles) of n vertices. Each of the n1−1 edges of the tree is colored in either black or red.

You are also given an integer k. Consider sequences of k vertices. Let's call a sequence [a1,a2,,ak][1,2,…,] good if it satisfies the following criterion:

  • We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from a11 and ending at ak.
  • Start at a11, then go to a22 using the shortest path between a1 and a2, then go to a3 in a similar way, and so on, until you travel the shortest path between ak1−1 and ak.
  • If you walked over at least one black edge during this process, then the sequence is good.
 

Consider the tree on the picture. If k=3=3 then the following sequences are good: [1,4,7][5,5,3] and [2,3,7][2,3,7]. The following sequences are not good: [1,4,6][1,4,6], [5,5,5][5,5,5], [3,7,3]

There are nk sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo 109+7109+7.

Input

The first line contains two integers n and k (2n1052≤≤105, 2k1002≤≤100), the size of the tree and the length of the vertex sequence.

Each of the next n1lines contains three integers uivi and xi (1ui,vin1≤,≤, xi{0,1}∈{0,1}), where ui and vi denote the endpoints of the corresponding edge and xi is the color of this edge (00 denotes red edge and 11 denotes black edge).

Output

Print the number of good sequences modulo 109+7109+7.

Examples
input Copy
4 4 1 2 1 2 3 1 3 4 1
output Copy
252
input Copy
4 6 1 2 0 1 3 0
1 4 0
output Copy
0
input Copy
3 5 1 2 1 2 3 0
output Copy
210
Note

In the first example, all sequences (4444) of length 4except the following are good:

  • [1,1,1,1]
  • [2,2,2,2]
  • [3,3,3,3]
  • [4,4,4,4]
复制代码
//https://www.luogu.com.cn/problem/CF1139C //dfs解法 //用dfs记录下来一个连通块的节点数,然后用幂运算算出不合法的方案数 #include<bits/stdc++.h> #define ll long long using namespace std; const int N=1e5+10,mod=1e9+7; int n,k,idx; ll res,num; vector<int>mp[N]; bool vis[N]; //快速幂 // ll qpow(ll x,ll y){ // ll ba=x,ans=1; // while(y){ // if(y&1) ans=(ans*ba)%mod; // ba=(ba*ba)%mod; // y>>=1; // } // return ans; // } //普通幂 ll qpow(int x,int cc) { ll ans=1; for(int i=0;i<cc;i++) ans=(ans*x)%mod; return ans; } void dfs(int x) { vis[x]=true; ++idx; for(auto i:mp[x]) if(!vis[i]) dfs(i); } int main() { std::ios::sync_with_stdio(false); cin>>n>>k; for(int i=1;i<n;i++){ int a,b,c; cin>>a>>b>>c; if(c==0) mp[a].push_back(b), mp[b].push_back(a); } res=qpow(n%mod,k)%mod; for(int i=1;i<=n;i++) if(!vis[i]){ idx=0; dfs(i); num=(num+qpow(idx,k))%mod; } cout<<(res-num+mod)%mod; return 0; }
复制代码
复制代码
//并查集解法: //用并查集来维护是否合法 #include<bits/stdc++.h> #define ll long long using namespace std; const int N=1e5+10,mod=1e9+7; int p[N],n,k,vis[N],cnt,idx,mp[N]; bool st[N]; ll res,num; int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } ll qpow(int x) { ll ans=1; for(int i=0;i<k;i++) ans=(ans*x)%mod; return ans; } int main() { std::ios::sync_with_stdio(false); cin>>n>>k; for(int i=1;i<=n;i++) p[i]=i; for(int i=1;i<=n;i++){ int a,b,c; cin>>a>>b>>c; if(c==0) p[find(a)]=find(b);//如果是红的就加进去 } res=qpow(n%mod); for(int i=1;i<=n;i++) mp[find(i)]++;//新方法注意,如何找出这个点的连通块数量? //很明显是这样找得到啦,建立一个桶; for(int i=1;i<=n;i++) num=(num+qpow(mp[i]))%mod; cout<<(res-num+mod)%mod<<endl; return 0; }
复制代码

 

1|0 


__EOF__

本文作者Sakurajimamai
本文链接https://www.cnblogs.com/o-Sakurajimamai-o/p/17485698.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   o-Sakurajimamai-o  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
-- --
点击右上角即可分享
微信分享提示