Submission #398024

#TimeUsernameProblemLanguageResultExecution timeMemory
398024Charis02Election Campaign (JOI15_election_campaign)C++14
20 / 100
1090 ms41668 KiB
/* https://oj.uz/problem/view/JOI15_election_campaign O(NM) Code */ #include<iostream> #include<vector> #include<string.h> #define ll long long #define pi pair < ll,ll > #define mp(a,b) make_pair(a,b) #define MAX_N 100004 using namespace std; vector < int > graph[MAX_N]; vector < pair < pi,ll > > tours[MAX_N]; ll jump_memo[MAX_N][21]; ll tin[MAX_N],tout[MAX_N],cur_time; ll dp[MAX_N],S[MAX_N]; int n,m; int jump(int x,int i) // find 2^i -th ancestor of x { if(jump_memo[x][i] != -1) return jump_memo[x][i]; return jump_memo[x][i] = jump(jump(x,i-1),i-1); } bool is_ancestor(int x,int y) // returns if x is ancestor of y { return (tin[x] <= tin[y] && tout[x] >= tout[y]); } int get_lca(int x,int y) { if(is_ancestor(x,y)) return x; if(is_ancestor(y,x)) return y; for(int i = 20;i >= 0;i--) { if(!is_ancestor(jump(x,i),y)) x = jump(x,i); } return jump(x,0); } void dfs(int cur,int par) { tin[cur] = cur_time++; jump_memo[cur][0] = par; for(int i = 0;i < graph[cur].size();i++) { int v = graph[cur][i]; if(v == par) continue; dfs(v,cur); } tout[cur] = cur_time++; return; } void initialize() { memset(jump_memo,-1,sizeof jump_memo); memset(dp,-1,sizeof dp); dfs(1,1); return; } void input() { int a,b,c; cin >> n; for(int i = 0;i < n-1;i++) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } initialize(); cin >> m; for(int i = 0;i < m;i++) { cin >> a >> b >> c; tours[get_lca(a,b)].push_back(mp(mp(a,b),c)); } return; } ll go(int cur,int par,int target) { if(cur == target) return S[cur]; for(int i = 0;i < graph[cur].size();i++) { int v = graph[cur][i]; if(is_ancestor(v,target) && is_ancestor(cur,v)) return go(v,cur,target)+S[cur]-dp[v]; } } ll calc(int cur,int par) { if(dp[cur] != -1) return dp[cur]; for(int i = 0;i < graph[cur].size();i++) { int v = graph[cur][i]; if(v==par) continue; S[cur] += calc(v,cur); } dp[cur] = S[cur]; for(int i = 0;i < tours[cur].size();i++) { int x = tours[cur][i].first.first; int y = tours[cur][i].first.second; int C = tours[cur][i].second; // cout << "testing route " << x << " -> " << y << ". lca is " << cur << ". Gain is " << C << ". "; // cout << "to x: " << go(cur,par,x) << " to y: " << go(cur,par,y) << " S: " << S[cur] << " " << go(cur,par,x)+go(cur,par,y) - S[cur] + C << endl; dp[cur] = max(dp[cur],go(cur,par,x)+go(cur,par,y) - S[cur] + C); // S[cur] is counted twice } // cout << "here " << cur << " " << dp[cur] << endl; return dp[cur]; } void solve() { cout << calc(1,1) << endl; } int main() { input(); solve(); return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'long long int go(int, int, int)':
election_campaign.cpp:109:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'long long int calc(int, int)':
election_campaign.cpp:122:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:133:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |     for(int i = 0;i < tours[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'long long int go(int, int, int)':
election_campaign.cpp:115:1: warning: control reaches end of non-void function [-Wreturn-type]
  115 | }
      | ^
#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...