# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1128281 | hainam2k9 | Election Campaign (JOI15_election_campaign) | C++20 | 1095 ms | 23592 KiB |
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
int n,m,x,y,z,dp[MAXN],par[MAXN],depth[MAXN];
vector<int> adj[MAXN],vertices[MAXN];
vector<pair<pair<int,int>,int>> g[MAXN];
void dfs(int u){
vertices[depth[u]].pb(u);
for(int v : adj[u])
if(v!=par[u]) par[v]=u, depth[v]=depth[u]+1, dfs(v);
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<n; ++i)
cin >> x >> y, adj[x].pb(y), adj[y].pb(x);
dfs(1);
cin >> m;
for(int i = 1; i<=m; ++i){
cin >> x >> y >> z;
if(depth[x]<depth[y]) swap(x,y);
int tmpX=x, tmpY=y;
while(depth[tmpX]!=depth[tmpY]) tmpX=par[tmpX];
while(tmpX!=tmpY) tmpX=par[tmpX], tmpY=par[tmpY];
g[tmpX].pb(mp(x,y),z);
}
for(int i = n-1; i>=0; --i)
for(int u : vertices[i]){
for(int v : adj[u])
if(v!=par[u]) dp[u]=max(dp[u],dp[v]);
for(pair<pair<int,int>,int> p : g[u]){
int x=p.fi.fi, y=p.fi.se, cur=p.se;
for(int v : adj[x])
if(v!=par[x]) cur+=dp[v];
while(par[x]!=u){
for(int v : adj[par[x]])
if(v!=x&&v!=par[par[x]]) cur+=dp[v];
x=par[x];
}
if(y!=u){
for(int v : adj[y])
if(v!=par[y]) cur+=dp[v];
while(par[y]!=u){
for(int v : adj[par[y]])
if(v!=y&&v!=par[par[y]]) cur+=dp[v];
y=par[y];
}
}
for(int v : adj[u])
if(v!=x&&v!=y&&v!=par[u]) cur+=dp[v];
dp[u]=max(dp[u],cur);
}
}
cout << dp[1];
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |