-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add F_lzy_x.cpp, the version 1,2,3 have bug, and vesion 4 fixed
- Loading branch information
Showing
6 changed files
with
378 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.