Skip to content

Commit

Permalink
add F_lzy_x.cpp, the version 1,2,3 have bug, and vesion 4 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
zenyarn committed Oct 28, 2024
1 parent 3679ac1 commit 2d667df
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"typeinfo": "cpp",
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp"
"algorithm": "cpp",
"unordered_map": "cpp"
}
}
4 changes: 2 additions & 2 deletions F.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,14 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13\n"
"25\n"
]
}
],
Expand Down
91 changes: 91 additions & 0 deletions F_lzy_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <iostream>
#include <unordered_map>
#include <vector>
#define int long long
using namespace std;

struct {
vector<int> values;
vector<bool> visited;
vector<int> sub;
vector<int> f;
vector<int> g;

void init(int n) {
values.resize(n + 1, 0);
visited.resize(n + 1, false);
sub.resize(n + 1, 0);
f.resize(n + 1, 0);
g.resize(n + 1, 0);
}
} dp;

int n;
unordered_map<int, vector<int>> graph;

void dfs(int node) {
bool have_son = false;
for(int neighbor : graph[node]) {
if(!dp.visited[neighbor]) {
have_son = true;
break;
}
}

if(!have_son)
return;

dp.visited[node] = true;
for(int neighbor : graph[node]) {
if(!dp.visited[neighbor])
dfs(neighbor);
}

for(int i : graph[node]) {
// if visited neighbot
if(dp.visited[i])
dp.g[node] += dp.sub[i];
}

int maxf = 0;
for(int i : graph[node]) {
//if(dp.visited[i]) {
int tmpf = dp.g[i] + dp.values[i] * dp.values[node];
for(int j : graph[node]) {
// if(dp.visited[j] && j != i)
if(j != i)
tmpf += dp.sub[j];
}
maxf = max(tmpf, maxf);
//}
}
dp.f[node] = maxf;

dp.sub[node] = max(dp.f[node], dp.g[node]);
}

signed main() {
cin>>n;
dp.init(n);

for(int i = 0; i < n - 1; i++) {
int a, b;
cin>>a>>b;
graph[a].push_back(b);
graph[b].push_back(a);
}

for(int i = 1; i <= n; i++)
cin>>dp.values[i];

dfs(1);
cout<<dp.sub[1]<<endl;

for(int i = 1; i <= n; i++) {
cout<<"sub["<<i<<"] = "<<dp.sub[i]<<endl;
cout<<"f["<<i<<"] = "<<dp.f[i]<<endl;
cout<<"g["<<i<<"] = "<<dp.g[i]<<endl;
}

return 0;
}
80 changes: 80 additions & 0 deletions F_lzy_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <unordered_map>
#include <vector>
#define int long long
using namespace std;

struct {
vector<int> values;
vector<bool> visited;
vector<int> sub;
vector<int> f;
vector<int> g;

void init(int n) {
values.resize(n + 1, 0);
visited.resize(n + 1, false);
sub.resize(n + 1, 0);
f.resize(n + 1, 0);
g.resize(n + 1, 0);
}
} dp;

int n;
unordered_map<int, vector<int>> graph;

void dfs(int node) {
dp.visited[node] = true;

// 先处理所有子节点
for (int neighbor : graph[node]) {
if (!dp.visited[neighbor]) {
dfs(neighbor);
}
}

// 计算 g[node]
for (int neighbor : graph[node]) {
if (dp.visited[neighbor]) {
dp.g[node] += dp.sub[neighbor]; // 计算不合并的情况
}
}

// 计算 f[node]
int maxf = 0;
for (int neighbor : graph[node]) {
if (dp.visited[neighbor]) {
int tmpf = dp.g[neighbor] + dp.values[neighbor] * dp.values[node];
for (int other : graph[node]) {
if (other != neighbor && dp.visited[other]) {
tmpf += dp.sub[other]; // 加上其他子树的值
}
}
maxf = max(tmpf, maxf);
}
}
dp.f[node] = maxf;

// 计算 sub[node]
dp.sub[node] = max(dp.f[node], dp.g[node]);
}

signed main() {
cin >> n;
dp.init(n);

for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}

for (int i = 1; i <= n; i++)
cin >> dp.values[i];

dfs(1);
cout << dp.sub[1] << endl;

return 0;
}
88 changes: 88 additions & 0 deletions F_lzy_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;

struct DP {
vector<int> values;
vector<bool> visited;
vector<int> sub;
vector<int> f;
vector<int> g;

void init(int n) {
values.resize(n + 1, 0);
visited.resize(n + 1, false);
sub.resize(n + 1, 0);
f.resize(n + 1, 0);
g.resize(n + 1, 0);
}
} dp;

int n;
unordered_map<int, vector<int>> graph;

void dfs(int node) {
dp.visited[node] = true;

for (int neighbor : graph[node]) {
if (!dp.visited[neighbor]) {
dfs(neighbor);
}
}

int sum_g = 0;
int max_f = 0;

for (int neighbor : graph[node]) {
if (dp.visited[neighbor]) {
sum_g += dp.sub[neighbor];
}
}

dp.g[node] = sum_g;

for (int i : graph[node]) {
if (dp.visited[i]) {
int tmp_f = dp.g[i] + dp.values[node] * dp.values[i];
for (int j : graph[node]) {
if (j != i) {
tmp_f += dp.sub[j];
}
}
max_f = max(tmp_f, max_f);
}
}

dp.f[node] = max_f;
dp.sub[node] = max(dp.f[node], dp.g[node]);
}

signed main() {
cin >> n;
dp.init(n);

for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}

for (int i = 1; i <= n; i++) {
cin >> dp.values[i];
}

dfs(1);
cout << dp.sub[1] << endl;

for (int i = 1; i <= n; i++) {
cout << "sub[" << i << "] = " << dp.sub[i] << endl;
cout << "f[" << i << "] = " << dp.f[i] << endl;
cout << "g[" << i << "] = " << dp.g[i] << endl;
}

return 0;
}
Loading

0 comments on commit 2d667df

Please sign in to comment.