快手第四题 好序列

直接统计不好序列  路径全为红即为不好序列
每个点记录子节点里有几个点通过不好路径相连
考虑每棵子树对答案的贡献即可
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
struct node
{
    int to;
    int c;
    node(int _to,int _c):to(_to),c(_c){}
};
struct node2
{
    int to;
    int c;
    int num;
     node2(int _to,int _c,int _num):to(_to),c(_c),num(_num){}
};
vector<node>g[100010];
vector<node2>G[100010];
bool vis[100010];
void build(int root){
    if(vis[root]==1) return;
    vis[root]=1;
    for(int i=0;i<g[root].size();i++){
        if(vis[g[root][i].to]==0){
            G[root].push_back(node2(g[root][i].to,g[root][i].c,0));
            build(g[root][i].to);
        }
    }
}
int num[100010],ans=0;
int fast(int a,int b)
{
    int ans=1;
    while(b)
    {
        if(b%2==1) ans=1LL*ans*a%mod;
        b/=2;
        a=1LL*a*a%mod;
    }
    return ans;
}
int n,k,l,r,c;
void solve(int root)
{
    num[root]=1;
    for(int i=0;i<G[root].size();i++){
        solve(G[root][i].to);
        if(G[root][i].c==0) {
                num[root]+=num[G[root][i].to];
                ans=(ans+mod-fast(num[G[root][i].to],k))%mod;
        }
    }
    ans=(ans+fast(num[root],k))%mod;
}
int main()
{
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    cin>>n>>k;
    for(int i=0;i<n-1;i++){
        scanf("%d %d %d",&l,&r,&c);
        g[l].push_back(node(r,c));
        g[r].push_back(node(l,c));
    }
    build(1);
    solve(1);
    cout<<(fast(n,k)+mod-ans)%mod<<endl;
}


#快手##笔试题目##题解#
全部评论
点赞 回复 分享
发布于 2019-08-25 20:27
tql
点赞 回复 分享
发布于 2019-08-25 18:36
求教通过了吗,我也统计不好的,用的并查集加快速幂,百分之零
点赞 回复 分享
发布于 2019-08-25 18:34
我想知道这道题到底啥意思。。。
点赞 回复 分享
发布于 2019-08-25 18:33
点赞 回复 分享
发布于 2019-08-25 18:32
大佬!
点赞 回复 分享
发布于 2019-08-25 18:31
大佬。
点赞 回复 分享
发布于 2019-08-25 18:30

相关推荐

我看标题以为40W,我觉得最差也得40k,点进去一看40块。你永远想不到客户的预算有多低...&nbsp;要求&ldquo;前端使用vue+element开发一个pc端宠物网站和vue+vant开发一个移动端网站,类型是宠物网站的。客户预算40&rdquo;&nbsp;全网最受欢迎的嵌入式面经面经一共32篇文章,12w+字数,包含全部最新的面试必问考点,4.7w+同学学习,2800+订阅,非常适合在找工作面经薄弱的同学,3000+订阅还会涨价,提前订阅提前享受,持续更新中。原帖链接:https://www.nowcoder.com/creation/manager/columnDetail/MJNwoMc
野猪不是猪🐗:哎呀,看来这位客户预算确实挺让人意外的呢!不过,别灰心,有时候客户的预算有限,但项目完成后说不定能带来意想不到的收获呢!😊 至于你提到的嵌入式面经,听起来好像很棒的样子!如果你对求职有帮助,那确实值得订阅学习哦!悄悄告诉你,点击我的头像,我们可以私信聊聊更多求职经验和技巧哦~🎉 对了,你对Vue和Element/Vant的开发有什么疑问或者想要分享的经验吗?我们可以一起探讨一下~😉
点赞 评论 收藏
分享
03-28 19:11
铜陵学院 C++
有礼貌的山羊追赶太阳:太典了,连笔试都没有开始就因为HC满了而结束了,而且还卡你不让你再投其他部门的。
点赞 评论 收藏
分享
评论
2
15
分享

创作者周榜

更多
牛客网
牛客企业服务