当前位置:网站首页>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
边栏推荐
- Harbor installation
- STM32 Development Notes 118: using CMSIS DSP Library in stm32cube IDE
- Document collaboration tool recommendation
- Luogu p4281 [ahoi2008] emergency gathering / gathering solution
- Learn to use PHP to get the URL address link after resetting
- Interviewer: explain the core principle of ThreadLocal
- Data link layer protocol -- Ethernet protocol
- Introduction to fundamentals of operations research [1]
- Paper:《Peeking Inside the Black Box: Visualizing Statistical Learning with Plots of Individual Condi
- Json.tojsonstring cannot pass Boolean
猜你喜欢

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

Implementation principle of epoll

Data link layer protocol -- Ethernet protocol

Redis的三个必知必会的问题

Pychart configuration pyqt5

When we talk about immutable infrastructure, what are we talking about

Unity LOD

85 distributed project construction
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]
![[sht30 temperature and humidity display based on STM32F103]](/img/43/bbc66ab2d56cfa9dc05d795e8fe456.jpg)
[sht30 temperature and humidity display based on STM32F103]
随机推荐
After watching the latest interview with big manufacturers, these six JVM interview questions were asked
How to ensure data consistency between MySQL and redis?
Why does the official not recommend using UUID as MySQL primary key
panda3d 键盘移动场景
STM32 development note 120: solve the problem that%f in printf cannot be output
[analysis of GPIO register (crl/crh) configuration of STM32]
Introduction to fundamentals of operations research [1]
Novel capture practice
ESWC 2018 | R-GCN:基于图卷积网络的关系数据建模
GDT,LDT,GDTR,LDTR
Redis集群搭建(Windows)
龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
[sht30 temperature and humidity display based on STM32F103]
[wechat applet] label (86/100)
[no title] 1
Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
Token value replacement of burpsuite blasting
Bypass XSS filters in Web Applications
2022-7-13 summary