B - Edgy Trees CodeForces - 1139C(并查集)

C. Edgy Trees
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a tree (a connected undirected graph without cycles) of n vertices. Each of the n−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] good if it satisfies the following criterion:

We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from a1 and ending at ak.
Start at a1, then go to a2 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 ak−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 then the following sequences are good: [1,4,7], [5,5,3] and [2,3,7]. The following sequences are not good: [1,4,6], [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+7.

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

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

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

Examples
inputCopy
4 4
1 2 1
2 3 1
3 4 1
outputCopy
252
inputCopy
4 6
1 2 0
1 3 0
1 4 0
outputCopy
0
inputCopy
3 5
1 2 1
2 3 0
outputCopy
210
Note
In the first example, all sequences (44) of length 4 except the following are good:

[1,1,1,1]
[2,2,2,2]
[3,3,3,3]
[4,4,4,4]
In the second example, all edges are red, hence there aren’t any good sequences.

题意:给你一棵树,有红色的边和黑色的边,问你长度为k,且经过至少一个黑边的路径总数有多少条,同一个点可以经过多次;
思路:很显然直接去求每一个种可能必然会超时,所以必须使用它的反面,至少经过一个黑边的反面就是全是红边,所以只要统计全是红色的联通块的个数,以及连通块由多少个点构成就行了,最后用n^k-每个连通块个数的k次方就是答案了;
代码如下:

#include<bits/stdc++.h>
#define LL long long
#define Max 100005
#define Mod 1e9+7
const LL mod=1e9+7;
const LL inf=0x3f3f3f3f;
using namespace std;
LL ksm(LL a,LL n){//快速幂
    LL ans=1;
    while(n){
        if(n&1)
            ans=ans*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return ans;
}
LL n,k,x,y,c;
LL pre[Max],large[Max];
void creat()
{
    for(int i=0;i<=n;i++){
        pre[i]=i;
        large[i]=1;//初始化每个连通块个数为1个
    }
}
int Find(LL x){
    return x==pre[x]?x:pre[x]=Find(pre[x]);
}
int main()
{
    scanf("%lld%lld",&n,&k);
    creat();
    LL ans=0,cc=0;
    for(int i=1;i<=n-1;i++){
        scanf("%lld%lld%lld",&x,&y,&c);
        if(c)
            continue;
        LL l,r;
        l=Find(x),r=Find(y);
        pre[l]=r;large[r]+=large[l];//将x,y连接成一个连通块后要把个数合并
    }
    for(LL i=1;i<=n;i++){
        if(pre[i]==i){
            ans=(ans+ksm(large[i],k))%mod;//计算答案
        }
    }
    printf("%lld\n",(ksm(n,k)-ans+mod)%mod);//输出一定要加上mod再取余mod,因为可能出现负数
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Gee_Zer/article/details/89134079