当前位置:网站首页>Introduction to base ring tree
Introduction to base ring tree
2022-07-25 05:07:00 【You a yours I wa mine】
Base ring tree :
It's a kind of n A little bit n side , A graph in which only one ring exists is called a base ring tree ( Ring tree )
There are three categories :
Undirected tree
Out-Tree ( Each point has only one entry edge )

Introverted tree ( There is only one edge out of each point )

The basic idea :
1、 Deep search for ring
2、 Broken into trees , Carry out dp
example : The knight
Ideas :
1、 For each base ring tree, disconnect any edge on the ring , Make a tree for the two endpoints of this side respectively dp, Answer twice max
2、 Add up the of each base ring tree max

Code:
#include<bits/stdc++.h>
#define bug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
const int N = 1e6 + 100;
const int mod = 1e9 + 7;
typedef long long ll;
int n,cnt,r1,r2;
int val[N],head[N];
bool vis[N];
struct Node {
int to,nxt;
} e[N];
ll dp[N][2],res;
void add(int u,int v) {
++cnt;
e[cnt] = {
v,head[u]};
head[u] = cnt;
}
void Find(int u,int rt) {
vis[u] = 1;
for(int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if(v==rt) {
r1=v;
r2=u;
return;
}
if(!vis[v]) Find(v,rt);
}
}
ll dfs(int u,int rt) {
dp[u][0] = 0;
dp[u][1] = val[u];
for(int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if(v==rt) continue;
dfs(v,rt);
dp[u][0] += max(dp[v][0],dp[v][1]);
dp[u][1] += dp[v][0];
}
return dp[u][0];
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
int w,u;
scanf("%d%d",&val[i],&u);
add(u,i);
}
for(int i=1; i<=n; i++) {
if(!vis[i]) {
r1=r2=0;
Find(i,i);
if(r1) {
ll t1 = dfs(r1,r1);
ll t2 = dfs(r2,r2);
//cout<<t1<<" "<<t2<<endl;
res += max(t1,t2);
}
}
}
printf("%lld",res);
return 0;
}
/* 3 10 2 20 3 30 1 */
Writing is not good For reference only
Source of resources : Dong Xiao algorithm
边栏推荐
- [no title] 1
- Execution process of NPM run XX
- Novel capture practice
- Ora-01460: conversion request cannot be implemented or unreasonable
- rhcsa暑假第二天
- 如何判断是否遭到DDOS攻击
- The third question of force deduction -- the longest substring without repeated characters
- Now the operator wants to check the answer details of all user questions from Zhejiang University. Please take out the corresponding data
- Tiny-emitter.js: a small event subscription and Publishing Library
- Event cycle mechanism browser nodejs async await execution sequence promise execution sequence interview questions
猜你喜欢

panda3d 键盘移动场景

2022-7-15 summary

Ora-01460: conversion request cannot be implemented or unreasonable

Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column
![[CTF learning] steganography set in CTF -- picture steganography](/img/32/2da78bd5866cfab9ee64dfcb1c1204.png)
[CTF learning] steganography set in CTF -- picture steganography

If you don't know these 20 classic redis interview questions, don't go to the interview!

rhcsa暑假第三天

Implementation principle of epoll

The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition writeup

The third question of force deduction -- the longest substring without repeated characters
随机推荐
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
How to test data in the process of data warehouse migration?
服务器防护的七个建议
Druid connection pool - strong self-study from 0. Those who don't understand Druid can click in. If you know not to click in, you will think I'm wordy
Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column
Matter's Unified Smart Home connection standard enables local automatic interaction between smart devices
Forwarding and sharing function of wechat applet
Ownership in rust -- introduction of rust language Xiaobai 11
使用getifaddrs获取本机网口IP地址
Gbase 8A about no suitable driver
"Niuke | daily question" inverse Polish expression
DOM processing in ahooks
Gradle test and idea test
Tiny-emitter.js: a small event subscription and Publishing Library
When image component in wechat applet is used as background picture
Go language function
Idea2021 installation
Li Kou 731. My schedule II
Learn to use PHP to get the URL address link after resetting