题解:P12683 【MX-J15-T3】叉叉学习与自我和解
感觉这题目主要难度就是要看懂。
题目大意
一张包含
你会选中尽量少的边。可以证明,你最终会选中
在已知选中的
题目解法
先看样例
先看每个点道第
我们要保证每个节点从
每条边都可以添加或不添加,答案就是
用这种方式算样例
答案就是
参考代码:
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, mod = 1e9 + 7;
int n, u, v, dep[N], cnt[N]; ll ans;
vector<int> g[N];
void dfs(int u, int f){
dep[u] = dep[f] + 1;
for(int v: g[u]){
if(v == f) continue;
dfs(v, u);
}
}
int qpow(int x, ll y){
int res = 1;
while(y){
if(y&1) res = (ll)res * x % mod;
y >>= 1;
x = (ll)x * x % mod;
}
return res;
}
int main(){
cin >> n;
for(int i = 1; i < n; i++){
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, n);
for(int i = 0; i < n; i++) cnt[dep[i]]++;
for(int i = 1; i <= n; i++){
ans += (ll)cnt[i]*(cnt[i]-1)/2;
if(i > 1) ans += (ll)cnt[i] * (cnt[i-1]-1);
}
cout << qpow(2, ans);
return 0;
}