Submission #363126

# Submission time Handle Problem Language Result Execution time Memory
363126 2021-02-05T05:46:11 Z ijxjdjd Beads and wires (APIO14_beads) C++14
0 / 100
12 ms 14580 KB
#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)

#define f first
#define s second
using namespace std;

using ll = long long;

const int MAXN = (int)(2e5) + 5;
vector<pair<int, int>> adj[MAXN];
int dp[MAXN][2];
multiset<int> trans[MAXN];
int sm[MAXN];
void dfs(int n, int p, int w) {
    int allSum = 0;
    for (auto& e : adj[n]) {
        if (e.f != p) {
            dfs(e.f, n, e.s);
            trans[n].insert(e.s + dp[e.f][0] - dp[e.f][1]);
            allSum += dp[e.f][1];
        }
    }
    sm[n] = allSum;
    dp[n][0] = allSum;
    dp[n][1] = allSum + (trans[n].size() > 0 ? max(0, *trans[n].rbegin() + w): 0);
}

int ans = 0;
void sol(int n, int p) {
    ans = max(dp[n][0], ans);
    if (trans[n].size() >= 2) {
        ans = max(ans, dp[n][0] + *trans[n].rbegin() + *next(trans[n].rbegin()));
    }
    ans = max(ans, dp[n][0]);
    for (auto e : adj[n]) {
        if (e.f != p) {
            int orin0 = dp[n][0];
            int orin1 = dp[n][1];
            int orinsm = sm[n];
            trans[n].erase(trans[n].find(e.s + dp[e.f][0] - dp[e.f][1]));
            sm[n] -= dp[e.f][1];
            dp[n][0] = sm[n];
            dp[n][1] = sm[n] + (trans[n].size() > 0 ? max(0, *trans[n].rbegin() + e.s): 0);
            int orie0 = dp[e.f][0];
            int orie1 = dp[e.f][1];
            int oriesm = sm[e.f];
            sm[e.f] += dp[n][1];
            dp[e.f][0] = sm[e.f];
            dp[e.f][1] = sm[e.f] + (trans[e.f].size() > 0 ? max(0, *trans[e.f].rbegin() + e.s): 0);
            trans[e.f].insert(e.s + dp[n][0] - dp[n][1]);
            sol(e.f, n);
            trans[e.f].erase(trans[n].find(e.s + dp[n][0] - dp[n][1]));
            sm[n] = orinsm;
            dp[n][0] = orin0;
            dp[n][1] = orin1;
            dp[e.f][0] = orie0;
            dp[e.f][1] = orie1;
            sm[e.f] = oriesm;
            trans[n].insert(e.s + dp[e.f][0] - dp[e.f][1]);
        }
    }
}
int main() {
    freopen("input.in", "r", stdin);
	cin.tie(0);
	cin.sync_with_stdio(0);
	int N;
	cin >> N;
	FR(i, N-1) {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
	}
	dfs(0, 0, 0);
	sol(0, 0);
	cout << ans << '\n';
	return 0;
}

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   66 |     freopen("input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 14580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 14580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 14580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 14580 KB Output isn't correct
2 Halted 0 ms 0 KB -