#include <bits/stdc++.h>
#define fi(i, a, b) for( int i = a; i <= b; i++ )
#define fid(i, a, b) for( int i = a; i >= b; i-- )
#define getbit(x, i) ((x>>i)&1)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define st first
#define nd second
#define mp make_pair
#define HTManh ""
#define maxn 100009
#define endl '\n'
using namespace std;
int n;
vector<int> a[100009];
int m;
struct dl
{
int u, v;
int c;
} tv[100009];
bool check()
{
fi(u,1,n) for(int v: a[u]) if (u < v && u != v-1) return 0;
return 1;
}
vector<pii> truyvan[100009];
ll mx[100009];
void sub23()
{
//cout << "gay" << endl;
fi(i,1,m)
{
int u = tv[i].u, v = tv[i].v, c = tv[i].c;
if (u > v) swap(u, v);
truyvan[v].pb({u, c});
}
fi(i,1,n)
{
ll gt = 0;
for(pii tg: truyvan[i])
{
gt = max(gt, mx[tg.st - 1] + tg.nd);
}
mx[i] = max(mx[i-1], gt);
}
cout << mx[n] << endl;
}
int h[100009];
int euler[200009], cnt;
int dau[200009];
void dfs(int u, int pre = 0)
{
euler[++cnt] = u;
dau[u] = cnt;
for(int v: a[u])
{
if (v == pre) continue;
h[v] = h[u] + 1;
dfs(v,u);
euler[++cnt] = u;
}
}
int anc[200009][21];
void build_lca()
{
fi(i,1,cnt) anc[i][0] = euler[i];
fi(k,1,__lg(cnt))
{
fi(i,1,cnt-(1<<k)+1)
{
anc[i][k] = (h[anc[i][k-1]] < h[anc[i+(1<<(k-1))][k-1]]) ? anc[i][k-1] : anc[i+(1<<(k-1))][k-1];
}
}
}
int lca(int u, int v)
{
if (dau[u] > dau[v]) swap(u,v);
int k = __lg(dau[v]-dau[u]+1);
return (h[anc[dau[u]][k]] < h[anc[dau[v]-(1<<k)+1][k]] ? anc[dau[u]][k] : anc[dau[v]-(1<<k)+1][k]);
}
int dis(int u, int v)
{
return h[u] + h[v] - 2*h[lca(u, v)];
}
void sub1()
{
dfs(1);
build_lca();
ll res = 0;
fi(x,1,(1<<m)-1)
{
bool flag = 1;
fi(i,1,m) if (getbit(x,(i-1)))
{
fi(j,1,i-1) if (getbit(x,(j-1)))
{
int dinh1 = lca(tv[i].u, tv[i].v);
int dinh2 = lca(tv[j].u, tv[j].v);
if (dis(tv[i].u, tv[i].v) == dis(tv[i].u, dinh2) + dis(tv[i].v, dinh2)) flag = 0;
if (dis(tv[j].u, tv[j].v) == dis(tv[j].u, dinh1) + dis(tv[j].v, dinh1)) flag = 0;
}
}
if (!flag) continue;
ll kq = 0;
fi(i,1,m) if (getbit(x,(i-1))) kq += tv[i].c;
res = max(res, kq);
}
cout << res << endl;
}
void subac()
{
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
if (fopen(HTManh".inp", "r"))
{
freopen(HTManh".inp", "r", stdin);
freopen(HTManh".out", "w", stdout);
}
cin >> n;
fi(i,1,n-1)
{
int x, y;
cin >> x >> y;
a[x].pb(y);
a[y].pb(x);
}
cin >> m;
fi(i,1,m) cin >> tv[i].u >> tv[i].v >> tv[i].c;
if (check()) sub23();
else
{
if (m <= 15) sub1();
else subac();
}
}
Compilation message (stderr)
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(HTManh".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(HTManh".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |