DP-Algo
This commit is contained in:
50
Algorithm/DP-DynamicProgramming/Tree-DP/树的最大独立集.cpp
Normal file
50
Algorithm/DP-DynamicProgramming/Tree-DP/树的最大独立集.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
void dfs(vector<vector<int>>& d, const vector<vector<int>>& adj_, vector<bool>& vs, int v){
|
||||
d[v][0] = 0;
|
||||
d[v][1] = 1;
|
||||
for(auto x : adj_[v]){
|
||||
if(vs[x] == false) {
|
||||
vs[x] = true;
|
||||
dfs(d, adj_, vs, x);
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
d[v][0] += max(d[x][0], d[x][1]); // <20><>ѡu<D1A1><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ
|
||||
d[v][1] += d[x][0]; // ѡu<D1A1><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>벻ѡ<EBB2BB><D1A1>
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
//ͼ<>Ļ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>vector<6F><72><EFBFBD>ڽӱ<DABD><D3B1><EFBFBD><EFBFBD><EFBFBD>
|
||||
int n;
|
||||
cin >> n;
|
||||
vector<vector<int>> adj(n);
|
||||
vector<vector<int>> dp(n, vector<int>(2, 0));
|
||||
vector<bool> visited(n, false);
|
||||
//vector<vector<int>> choose(n, vector<int>(n, 0));
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>0<EFBFBD><30>ʼ<EFBFBD><CABC>n - 1
|
||||
for(int i = 0; i < n - 1; i++){
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
adj[a].push_back(b);
|
||||
adj[b].push_back(a);
|
||||
}
|
||||
/*
|
||||
for(int l = 2; l <= n; l++){
|
||||
for(int i = 0; i < n - l + 1; i++){
|
||||
int j = i + l - 1;
|
||||
for(int k = i; k < j; k++){
|
||||
if(adj[i][j])
|
||||
d[i][j] = max(dp[i][j], )
|
||||
}
|
||||
}
|
||||
}*/
|
||||
visited[0] = true;
|
||||
dfs(dp, adj, visited, 0); // <20><>0Ϊ<30><CEAA>
|
||||
cout << max(dp[0][0], dp[0][1]) << endl;
|
||||
return 0;
|
||||
}
|
||||
60
Algorithm/DP-DynamicProgramming/Tree-DP/树的直径-最远点对.cpp
Normal file
60
Algorithm/DP-DynamicProgramming/Tree-DP/树的直径-最远点对.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
vector<vector<int>> adj;
|
||||
vector<int> dp;
|
||||
void dfs(int v, int parent){
|
||||
//no return;
|
||||
for(auto x : adj[v]){
|
||||
if(x == parent) continue;
|
||||
dfs(x, v);
|
||||
dp[v] = max(dp[v], dp[x] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin >> n;
|
||||
adj.resize(n, vector<int>());
|
||||
dp.resize(n, 1);
|
||||
vector<bool> visited(n, false);
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>0<EFBFBD><30>ʼ<EFBFBD><CABC>n - 1
|
||||
for(int i = 0; i < n - 1; i++){
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
adj[a].push_back(b);
|
||||
adj[b].push_back(a);
|
||||
}
|
||||
dfs(0, -1);
|
||||
int mmax = 0;
|
||||
if(adj[0].size() == 1) cout << dp[0] << endl;
|
||||
else{
|
||||
vector<int> tmp;
|
||||
for(auto x : adj[0]){
|
||||
tmp.push_back(dp[x]);
|
||||
}
|
||||
sort(tmp.begin(), tmp.end());
|
||||
mmax = *(tmp.end() - 1) + *(tmp.end() - 2);
|
||||
cout << mmax + 1<< endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
7
|
||||
0 1
|
||||
0 2
|
||||
1 3
|
||||
1 4
|
||||
2 5
|
||||
2 6
|
||||
|
||||
8
|
||||
0 1
|
||||
0 2
|
||||
1 3
|
||||
1 4
|
||||
2 5
|
||||
2 6
|
||||
3 7
|
||||
*/
|
||||
Reference in New Issue
Block a user