solution-at-abc133-e

乱搞。

考虑对于每个点,它能染的颜色色的数量的限制是任何两个距离不大于 $2$ 的不同节点。考虑直接按 $\operatorname{dfs}$ 序顺序染色。不用考虑每个点到底染了什么颜色,显然答案至与和它距离不大于 $2$ 的不同节点已有多少个被染了色有关。直接任意钦定一种顺序跑一遍即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 1e5+10;
const int mod = 1e9 + 7;
vector <int> g[MAX];
int n, k;
bool f[MAX];
int hh[MAX];
int ans = 1;

void dfs(int u, int fa){
    int cnt = 0;
    for(int v:g[u]){
        if(f[v])    cnt += hh[v], hh[u]++;
        else{
            continue ;
        } 
    }
    f[u] = 1;
    hh[u] ++;
    ans *= k - cnt;
    ans %= mod;
    for(int v:g[u]){
        if(v == fa) continue;
        dfs(v, u);
        hh[u]++;
    }
}

void solve(){
    n = read(), k = read();
    for(int i = 1; i < n; i++){
        int u = read(), v = read();
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 1);
    write(ans), endl;
}

signed main(){
    int t = 1;
    while(t--)  solve();
    return 0;
}
posted @ 2023-05-12 16:23  WRuperD  阅读(1)  评论(0编辑  收藏  举报  来源

本文作者:DIVMonster

本文链接:https://www.cnblogs.com/guangzan/p/12886111.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

这是一条自定义内容

这是一条自定义内容