Submission #34132

#TimeUsernameProblemLanguageResultExecution timeMemory
34132DoanPhuDucBeads and wires (APIO14_beads)C++98
57 / 100
1071 ms19044 KiB
#include <bits/stdc++.h> #define FOR(x, a, b) for (int x = a; x <= b; ++x) #define FOD(x, a, b) for (int x = a; x >= b; --x) #define REP(x, a, b) for (int x = a; x < b; ++x) #define DEBUG(X) { cout << #X << " = " << X << endl; } #define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; } #define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; } #define BitCount(x) __builtin_popcount(x) using namespace std; typedef long long LL; typedef pair <int, int> II; const int N = 2e5 + 10; const int INF = 0x3f3f3f3f; int n; int f[N], p[N], a[N]; int dp[N][5]; vector <II> adj[N]; void DFS(int u, int par = -1) { f[u] = -INF; REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == par) continue; f[u] = max(f[u], w); p[v] = u; a[v] = w; DFS(v, u); } } void DP(int u) { REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == p[u]) continue; DP(v); } FOR(i, 0, 4) dp[u][i] = 0; // dp[u][0] int S = 0; REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == p[u]) continue; S += max(dp[v][1], dp[v][3]); } REP(k1, 0, adj[u].size()) { int v1 = adj[u][k1].first, w1 = adj[u][k1].second; if (v1 == p[u]) continue; REP(k2, 0, adj[u].size()) if (k1 != k2) { int v2 = adj[u][k2].first, w2 = adj[u][k2].second; if (v2 == p[u]) continue; int newS = S - max(dp[v1][1], dp[v1][3]) - max(dp[v2][1], dp[v2][3]); dp[u][0] = max(dp[u][0], newS + w1 + w2 + dp[v1][3] + max(dp[v2][0], dp[v2][4])); } } //dp[u][1] REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == p[u]) continue; int newS = S - max(dp[v][1], dp[v][3]); dp[u][1] = max(dp[u][1], newS + dp[v][3] + w + a[u]); } // dp[u][2] REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == p[u]) continue; int newS = S - max(dp[v][1], dp[v][3]); dp[u][2] = max(dp[u][2], newS + max(dp[v][0], dp[v][4]) + w + a[u]); } // dp[u][3] dp[u][3] = S; // dp[u][4] REP(k, 0, adj[u].size()) { int v = adj[u][k].first, w = adj[u][k].second; if (v == p[u]) continue; int newS = S - max(dp[v][1], dp[v][3]); int val = 0; FOR(i, 0, 4) val = max(val, dp[v][i]); dp[u][4] = max(dp[u][4], val + newS); } } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // LOCAL scanf("%d", &n); FOR(i, 1, n - 1) { int u, v, w; scanf("%d%d%d", &u, &v, &w); adj[u].push_back(II(v, w)); adj[v].push_back(II(u, w)); } DFS(1); DP(1); printf("%d", max(dp[1][0], max(dp[1][3], dp[1][4]))); return 0; }

Compilation message (stderr)

beads.cpp: In function 'void DFS(int, int)':
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:27:9:
     REP(k, 0, adj[u].size()) {
         ~~~~~~~~~~~~~~~~~~~             
beads.cpp:27:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^~~
beads.cpp: In function 'void DP(int)':
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:37:9:
     REP(k, 0, adj[u].size()) {
         ~~~~~~~~~~~~~~~~~~~             
beads.cpp:37:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^~~
beads.cpp:38:34: warning: unused variable 'w' [-Wunused-variable]
         int v = adj[u][k].first, w = adj[u][k].second;
                                  ^
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:45:9:
     REP(k, 0, adj[u].size()) {
         ~~~~~~~~~~~~~~~~~~~             
beads.cpp:45:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^~~
beads.cpp:46:34: warning: unused variable 'w' [-Wunused-variable]
         int v = adj[u][k].first, w = adj[u][k].second;
                                  ^
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:50:9:
     REP(k1, 0, adj[u].size()) {
         ~~~~~~~~~~~~~~~~~~~~            
beads.cpp:50:5: note: in expansion of macro 'REP'
     REP(k1, 0, adj[u].size()) {
     ^~~
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:53:13:
         REP(k2, 0, adj[u].size()) if (k1 != k2) {
             ~~~~~~~~~~~~~~~~~~~~        
beads.cpp:53:9: note: in expansion of macro 'REP'
         REP(k2, 0, adj[u].size()) if (k1 != k2) {
         ^~~
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:64:9:
     REP(k, 0, adj[u].size()) {
         ~~~~~~~~~~~~~~~~~~~             
beads.cpp:64:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^~~
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:72:9:
     REP(k, 0, adj[u].size()) {
         ~~~~~~~~~~~~~~~~~~~             
beads.cpp:72:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^~~
beads.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(x, a, b) for (int x = a; x < b; ++x)
beads.cpp:83:9:
     REP(k, 0, adj[u].size()) {
         ~~~~~~~~~~~~~~~~~~~             
beads.cpp:83:5: note: in expansion of macro 'REP'
     REP(k, 0, adj[u].size()) {
     ^~~
beads.cpp:84:34: warning: unused variable 'w' [-Wunused-variable]
         int v = adj[u][k].first, w = adj[u][k].second;
                                  ^
beads.cpp: In function 'int main()':
beads.cpp:98:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
beads.cpp:100:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int u, v, w; scanf("%d%d%d", &u, &v, &w);
                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...