Submission #949854

#TimeUsernameProblemLanguageResultExecution timeMemory
949854RifalElection Campaign (JOI15_election_campaign)C++14
10 / 100
74 ms26448 KiB
#include <bits/stdc++.h> #include <fstream> #define endl '\n' #define mod 1000000007 #define INF 1000000000 #define INF2 1000000000000000000 ///#define cin fin ///#define cout fout using namespace std; double const EPS = 1e-14; typedef long long ll; ///ofstream fout("herding.out"); ///ifstream fin("herding.in"); const int N = 1e5 + 5; vector<int> v[N]; pair<int,int> inter[N]; ll ans = 0; int dep[N]; int lca[N][31]; int tim = 1; struct Plan { int a, b; ll c; }; void dfs(int s, int p) { for(int i = 1; (1<<i) <= dep[s]; i++) { lca[s][i] = lca[lca[s][i-1]][i-1]; } inter[s].first = tim; tim++; for(auto i : v[s]) { if(i != p) { dep[i] = dep[s]+1; lca[i][0] = s; dfs(i,s); } } inter[s].second = tim; tim++; } int fin(int x, int y) { if(dep[x] < dep[y]) swap(x,y); int pow2 = 0; while(dep[x]-dep[y] > 0) { if(((dep[x]-dep[y])&(1<<pow2)) > 0) { x = lca[x][pow2]; } pow2++; } while(x != y) { pow2 = 30; while(lca[x][pow2] == lca[y][pow2] && pow2 != 0) pow2--; x = lca[x][pow2]; y = lca[y][pow2]; } return x; } bool check(pair<int,int> x, pair<int,int> y) { int nod1 = x.first, nod2 = x.second, nod3 = y.first, nod4 = y.second; if(inter[nod1].first <= inter[nod3].first && inter[nod1].second >= inter[nod3].second && inter[nod2].first >= inter[nod3].first && inter[nod2].second <= inter[nod3].second) return true; else if(inter[nod1].first <= inter[nod4].first && inter[nod1].second >= inter[nod4].second && inter[nod2].first >= inter[nod4].first && inter[nod2].second <= inter[nod4].second) return true; else if(inter[nod3].first <= inter[nod1].first && inter[nod3].second >= inter[nod1].second && inter[nod4].first >= inter[nod1].first && inter[nod4].second <= inter[nod1].second) return true; else if(inter[nod3].first <= inter[nod2].first && inter[nod3].second >= inter[nod2].second && inter[nod4].first >= inter[nod2].first && inter[nod4].second <= inter[nod2].second) return true; return false; } int main() { ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0); int n; cin >> n; for(int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } int m; cin >> m; Plan arr[m]; for(int i = 0; i < m; i++) { cin >> arr[i].a >> arr[i].b >> arr[i].c; } dfs(1,0); for(int i = 0; i < (1<<m); i++) { vector<int> cur; ll sum2 = 0; for(int j = 0; j < m; j++) { if((i&(1<<j)) > 0) { cur.push_back(j); sum2 += arr[j].c; } } bool ok = true; ll sum = 0; for(int j = 0; j < cur.size(); j++) { int nod1 = fin(arr[cur[j]].a,arr[cur[j]].b); for(int z = j+1; z < cur.size(); z++) { int nod2 = fin(arr[cur[z]].a,arr[cur[z]].b); if(check({nod1,arr[cur[j]].a},{nod2,arr[cur[z]].a}) || check({nod1,arr[cur[j]].b},{nod2,arr[cur[z]].a}) || check({nod1,arr[cur[j]].a},{nod2,arr[cur[z]].b}) || check({nod1,arr[cur[j]].b},{nod2,arr[cur[z]].b})) { ok = false; break; } } if(ok) sum += arr[cur[j]].c; else break; } if(ok) { // cout << i << ' ' << sum << 'd' << endl; ans = max(ans,sum); } } cout << ans << endl; return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j = 0; j < cur.size(); j++) {
      |                        ~~^~~~~~~~~~~~
election_campaign.cpp:91:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |             for(int z = j+1; z < cur.size(); z++) {
      |                              ~~^~~~~~~~~~~~
#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...