当前位置:网站首页>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
边栏推荐
- 小说抓取实战
- 2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
- [sht30 temperature and humidity display based on STM32F103]
- Ora-01460: conversion request cannot be implemented or unreasonable
- Unity LOD
- [small program practice] first day
- MCU experiment record
- Blog Description & message board
- STM32 Development Notes 119: what macros are required to enable FPU?
- Ownership in rust -- introduction of rust language Xiaobai 11
猜你喜欢

Teach you three ways to optimize the performance from 20s to 500ms

Execution process of NPM run XX

Delivery practice of private PAAS platform based on cloud native

The strongest JVM in the whole network is coming!

JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
搭建私有CA服务器

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

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

推荐系统-协同过滤在Spark中的实现

The second day of rhcsa summer vacation
随机推荐
深入掌握Pod
Web: compiling big refactoring from 10 to 1
China trifluoroethanol industry research and investment forecast report (2022 Edition)
Baklib: share some methods about building enterprise knowledge management (km)
Introduction to kubernetes
Learn to use PHP to get the URL address link after resetting
Compile ue5.0
[sht30 temperature and humidity display based on STM32F103]
Sword finger offer II 012. the sum of the left and right subarrays is equal
panda3d 键盘移动场景
How to judge whether it is attacked by DDoS
Small case of data analysis: visualize recruitment data and view the most needed technologies in the field~
2022-7-15 summary
Which website is suitable for physical server
Valley p2420 let's XOR solution
Tiny-emitter.js: a small event subscription and Publishing Library
rhcsa暑假第二天
[wechat applet] label (86/100)
[CTF learning] steganography set in CTF -- picture steganography
Gradle test and idea test