Submission #565160

#TimeUsernameProblemLanguageResultExecution timeMemory
565160SSRSElection Campaign (JOI15_election_campaign)C++14
0 / 100
1085 ms11436 KiB
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<vector<pair<int, int>>> E(N); for (int i = 0; i < N - 1; i++){ int X, Y; cin >> X >> Y; X--; Y--; E[X].push_back(make_pair(i, Y)); E[Y].push_back(make_pair(i, X)); } int M; cin >> M; vector<int> A(M), B(M), C(M); for (int i = 0; i < M; i++){ cin >> A[i] >> B[i] >> C[i]; A[i]--; B[i]--; } vector<int> S(N - 1, 0); for (int i = 0; i < M; i++){ vector<int> p(N, -1); vector<int> e(N); queue<int> Q; Q.push(A[i]); while (!Q.empty()){ int v = Q.front(); Q.pop(); for (auto P : E[v]){ int w = P.second; if (w != p[v]){ p[w] = v; e[w] = P.first; Q.push(w); } } } int v = B[i]; while (v != A[i]){ S[e[v]] |= 1 << i; v = p[v]; } } vector<vector<bool>> ok(M, vector<bool>(M, true)); for (int i = 0; i < N - 1; i++){ for (int j = 0; j < M; j++){ for (int k = j + 1; k < M; k++){ if ((S[i] >> j & 1) == 1 && (S[i] >> k & 1) == 1){ ok[j][k] = false; } } } } int ans = 0; for (int i = 0; i < (1 << M); i++){ bool ok2 = true; for (int j = 0; j < M; j++){ for (int k = j + 1; k < M; k++){ if ((i >> j & 1) == 1 && (i >> k & 1) == 1 && !ok[j][k]){ ok2 = false; } } } if (ok2){ int sum = 0; for (int j = 0; j < M; j++){ if ((i >> j & 1) == 1){ sum += C[j]; } } ans = max(ans, sum); } } cout << ans << endl; }
#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...