Submission #978216

#TimeUsernameProblemLanguageResultExecution timeMemory
978216guagua0407Election Campaign (JOI15_election_campaign)C++17
100 / 100
121 ms31400 KiB
//#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> #define f first #define s second #define all(x) x.begin(),x.end() #define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } struct node{ int c1,c2,val; }; const int mxn=1e5+5; vector<int> adj[mxn]; int up[mxn][20]; int depth[mxn]; vector<node> st[mxn]; int L[mxn],R[mxn]; ll bit[mxn]; ll dp[mxn]; int cnt=0; void update(int pos,ll val){ for(;pos<mxn;pos+=(pos&-pos)){ bit[pos]+=val; } } ll query(int pos){ ll ans=0; for(;pos>0;pos-=(pos&-pos)){ ans+=bit[pos]; } return ans; } void dfs(int v,int p=0){ up[v][0]=p; depth[v]=depth[p]+1; L[v]=++cnt; for(auto u:adj[v]){ if(u==p) continue; dfs(u,v); } R[v]=cnt; } int go(int x,int len){ for(int i=0;i<20;i++){ if(len&(1<<i)){ x=up[x][i]; } } return x; } int lca(int a,int b){ if(depth[a]<depth[b]) swap(a,b); int len=depth[a]-depth[b]; a=go(a,len); if(a==b) return a; for(int i=19;i>=0;i--){ int ta=up[a][i]; int tb=up[b][i]; if(ta!=tb){ a=ta; b=tb; } } return up[a][0]; } void dfs2(int v,int p=0){ ll sum=0; for(auto u:adj[v]){ if(u==p) continue; dfs2(u,v); } for(auto u:adj[v]){ if(u==p) continue; update(L[v],dp[u]); update(R[v]+1,-dp[u]); update(L[u],-dp[u]); update(R[u]+1,dp[u]); sum+=dp[u]; } dp[v]=sum; for(auto u:st[v]){ dp[v]=max(dp[v],query(L[u.c1])+query(L[u.c2])-(u.c2!=0?sum:0)+u.val); } //cout<<v<<' '<<dp[v]<<'\n'; } int main() {_ int n; cin>>n; 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(1); for(int j=1;j<20;j++){ for(int i=1;i<=n;i++){ up[i][j]=up[up[i][j-1]][j-1]; } } int q; cin>>q; for(int i=0;i<q;i++){ int a,b,c; cin>>a>>b>>c; int node=lca(a,b); //cout<<node<<' '<<a<<' '<<b<<'\n'; if(depth[a]>depth[b]) swap(a,b); int len=depth[b]-depth[a]; if(a==node){ st[node].push_back({b,0,c}); } else{ st[node].push_back({a,b,c}); } } dfs2(1); cout<<dp[1]<<'\n'; return 0; } //maybe its multiset not set //yeeorz //laborz

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:127:13: warning: unused variable 'len' [-Wunused-variable]
  127 |         int len=depth[b]-depth[a];
      |             ^~~
election_campaign.cpp: In function 'void setIO(std::string)':
election_campaign.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...