This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
vi adjList[100000];
int parent[100000],depth[100000],size[100000],heavy[100000];
int chain[100000],disc[100000],head[100000],csize[100000];
int num = 0,c = 0;
int doDFS(int u,int p,int d) {
int i;
parent[u] = p,depth[u] = d,size[u] = 1,heavy[u] = -1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
size[u] += doDFS(v,u,d+1);
if ((heavy[u] == -1) || (size[v] > size[heavy[u]])) heavy[u] = v;
}
}
return size[u];
}
int doHLD(int u,int p) {
int i;
chain[u] = c;
disc[u] = num++;
if (csize[c] == 0) head[c] = u;
csize[c]++;
if (heavy[u] != -1) doHLD(heavy[u],u);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if ((v != p) && (v != heavy[u])) c++,doHLD(v,u);
}
return 0;
}
vector<pair<pii,int> > paths[100000];
int dp[100000];
int bit[100001];
int query(int i) {
int sum = 0;
for (i++; i > 0; i -= i & (-i)) sum += bit[i];
return sum;
}
int update(int i,int num,int N) {
for (i++; i <= N; i += i & (-i)) bit[i] += num;
return 0;
}
int doDFS2(int u,int N) {
int i;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != parent[u]) dp[u] += doDFS2(v,N);
}
for (i = 0; i < paths[u].size(); i++) {
int uu = paths[u][i].first.first;
int vv = paths[u][i].first.second;
int s = 0;
while (chain[uu] != chain[vv]) {
if (depth[head[chain[uu]]] < depth[head[chain[vv]]]) swap(uu,vv);
s += query(disc[uu])-query(disc[head[chain[uu]]]-1);
uu = parent[head[chain[uu]]];
}
if (depth[uu] < depth[vv]) s += query(disc[vv])-query(disc[uu]-1);
else s += query(disc[uu])-query(disc[vv]-1);
dp[u] = max(dp[u],s+paths[u][i].second);
}
if (parent[u] != -1) update(disc[parent[u]],dp[u],N);
update(disc[u],-dp[u],N);
return dp[u];
}
int main() {
int i;
int N,M,A,B,C;
scanf("%d",&N);
for (i = 1; i < N; i++) {
scanf("%d %d",&A,&B);
A--,B--;
adjList[A].pb(B);
adjList[B].pb(A);
}
doDFS(0,-1,0);
doHLD(0,-1);
scanf("%d",&M);
for (i = 0; i < M; i++) {
scanf("%d %d %d",&A,&B,&C);
A--,B--;
int u = A,v = B;
while (chain[u] != chain[v]) {
if (depth[head[chain[u]]] < depth[head[chain[v]]]) swap(u,v);
u = parent[head[chain[u]]];
}
if (depth[u] < depth[v]) paths[u].pb(mp(mp(A,B),C));
else paths[v].pb(mp(mp(A,B),C));
}
printf("%d\n",doDFS2(0,N));
return 0;
}
Compilation message (stderr)
election_campaign.cpp: In function 'int doDFS(int, int, int)':
election_campaign.cpp:65:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int doHLD(int, int)':
election_campaign.cpp:81:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int doDFS2(int, int)':
election_campaign.cpp:101:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:105:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < paths[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:125:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
election_campaign.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&A,&B);
~~~~~^~~~~~~~~~~~~~~
election_campaign.cpp:134:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&M);
~~~~~^~~~~~~~~
election_campaign.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&A,&B,&C);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |