当前位置:网站首页>Change DP (ah ah ah)
Change DP (ah ah ah)
2022-04-22 07:30:00 【S atur】

Ideas :
* The first is the conventional dfs Find the sum of the distance from the root node to all other points . If so x Root node ,y by x Immediate child nodes of , that y Subtree pair x The contribution of dis[y]+cnt[y],dis[y] by y To y Is the sum of the distances of all subtree nodes of the root node ,cnt[y] by y The number of all subtree nodes as the root node .
* Then change the root dp Handle . After the root node is removed from x Move to y When ,dp[y] = dp[x]-cnt[y]+(n-cnt[y); Among them -cnt[y] Is to y Extended cnt[y] All paths -1, and +(n-cnt[y]) Yes, from x The extended does not include y The other paths are +1.
Code implementation :
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9+7;
const int N = 1e6+10;
inline void read(int &x){
char t=getchar();
while(!isdigit(t)) t=getchar();
for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}
int n, dis[N], cnt[N], dp[N], vis[N];
vector<int> ve[N];
void dfs(int x){
vis[x] = 1;
int sum = 0;
for(int i = 0; i < ve[x].size(); i ++){
int y = ve[x][i];
if(!vis[y]){
dfs(y);
sum += cnt[y];
dis[x] += dis[y]+cnt[y];
}
}
cnt[x] = sum+1;
}
void DP(int x){
vis[x] = 1;
for(int i = 0; i < ve[x].size(); i ++){
int y = ve[x][i];
if(!vis[y]){
dp[y] = dp[x]-cnt[y]+(n-cnt[y]);
DP(y);
}
}
}
signed main()
{
IOS;
cin >> n;
for(int i = 1; i < n; i ++){
int a, b; cin >> a >> b;
ve[a].push_back(b);
ve[b].push_back(a);
}
me(vis); dfs(1); me(vis);
for(int i = 1; i <= n; i ++)
dp[i] = dis[i];
DP(1);
int ans = inf;
for(int i = 1; i <= n; i ++)
ans = min(ans, dp[i]);
cout << ans << endl;
return 0;
}
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220616060469.html
边栏推荐
猜你喜欢

C language | array

C. Ducky debugging (Game 5 of 2021 training League warm-up training competition)

Redis Advanced

Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)

Leetcode - 1 - (substructure, combination, spiral matrix and full arrangement of tree < Ⅰ Ⅲ >)

HDU Ice_cream‘s world I (并查集判环)

Leetcode - 7 - (nearest common ancestor of binary tree, rotation array, direct of binary tree, next permutation, combined sum)

最小圆覆盖(计算几何基础)

Leetcode - 4 - (longest substring without repeated characters, candy distribution, binary tree traversal)

C.Ducky Debugging(简单判断/签到)(2021年度训练联盟热身训练赛第五场 )
随机推荐
Codeforces Round #776 (Div. 3)
递归反转链表
296 · array de duplication
C language | array
A. Binary seating (the fifth game of 2021 training League warm-up training competition)
A. Alice and Bob (game? Thinking & Violence) (2021 Niuke summer multi school training camp 1)
E.Figure Skating (字符串排序/签到) (2021年度训练联盟热身训练赛第五场 )
并发编程的艺术(2):Synchronized的使用场景
系统日志文件过大优化
1420 · 最小覆盖子串II
Recursive reverse linked list
并发编程的艺术(9):final的使用和原理
Use of ansible
363 · 接雨水
2021学习计划
Codeforces Round #778
Codeforces Round #779 (Div. 2)
838 · the sum of subarrays is K
LeetCode - 6 - (字符串相乘、下一個更大元素<ⅠⅡⅢ>、k個一組翻轉鏈錶)
Leetcode - 1 - (substructure, combination, spiral matrix and full arrangement of tree < Ⅰ Ⅲ >)