当前位置:网站首页>GYM103660E. Disjoint path on tree count
GYM103660E. Disjoint path on tree count
2022-07-19 15:11:00 【HeartFireY】
GYM103660E.Disjoint Path On Tree
Topic ideas
Given a tree , Find the logarithm of disjoint paths on the tree .
Excuse me : Disjoint path logarithm = Total path logarithm - Intersection path logarithm
Consider how to calculate the logarithm of intersecting paths : Enumeration points i i i As a path L C A LCA LCA, Then the number of intersection schemes at this time is i i i And L C A LCA LCA be not in i i i The number of paths × $LCA by by by i The number of paths + The number of paths + The number of paths +LCA All in All in All in i$ Path logarithm of .
Then count directly on the tree and then exclude .
Code
#include <bits/stdc++.h>
#pragma gcc optimize(2)
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
vector<signed> g[N];
int sz[N], ans = 0;
int n = 0;
void dfs(int u, int fa){
int res = 0;
sz[u] = 0;
for(auto v : g[u]){
if(v == fa) continue;
dfs(v, u);
(res += sz[v] * sz[u]) %= MOD;
sz[u] += sz[v];
}
sz[u]++;
int t = (res + sz[u]) % MOD;
(ans += ((n - sz[u]) * sz[u] % MOD * t % MOD)) %= MOD;
(ans += (t * (t + 1) / 2) % MOD) %= MOD;
}
inline void solve(){
cin >> n; ans = 0;
for(int i = 1; i <= n; i++) g[i].clear();
for(int i = 1; i <= n - 1; i++){
int u, v; cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, 0);
int path = ((n * (n + 1)) / 2) % MOD, all = ((path * (path + 1)) / 2) % MOD;
ans = ((all - ans) % MOD + MOD) % MOD;
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
猜你喜欢
1、DBMS基本概念
Leetcode 1275. 找出井字棋的获胜者
SQL wrong questions set of Niuke brush questions
Li Hongyi machine learning -- return to July 13, 2022
Leetcode 1275. 找出井字棋的獲勝者
Icml2022 | geometric multimodal comparative representation learning
Leetcode 1296. Divide the array into a set of consecutive numbers (solved)
【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
High performance pxie data preprocessing board based on kinex ultrascale series FPGA (ku060 +fmc sub card interface)
Classification of interrupts
随机推荐
Learning records [email protected] Moveactivityidto task fallback special case analysis
Mongodb partition cluster construction
PCIe Cameralink signal generator (Cameralink image analog source)
kube-proxy & Service & Endpoint
【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
ICML2022 | 几何多模态对比表示学习
Summary of the third week of summer vacation
人脸技术:不清楚人照片修复成高质量高清晰图像框架(附源代码下载)
Field programmable logic gate array FPGA
Icml2022 | geometric multimodal comparative representation learning
Force deduction 912 sorting array notes
买股票开户应该选哪个证券公司?什么证券公司是更安全的
[Axi] interpret the additional signals of the Axi protocol (QoS signal, region signal, and user signal)
[code attached] how to realize handwritten digit recognition with hog+svm
UVA - 12096 The SetStack Computer
ZABBIX realizes the monitoring of redis
长安链学习研究-存储分析wal机制
Leetcode 1275. Find out the winner of tic tac toe
SQL wrong questions set of Niuke brush questions
Oracle - 锁