Submission #670823

# Submission time Handle Problem Language Result Execution time Memory
670823 2022-12-10T18:07:49 Z Arnch Beads and wires (APIO14_beads) C++17
0 / 100
2 ms 2664 KB
#include<bits/stdc++.h>
using namespace std;
 
#define Sz(x) (x).size()
#define All(x) (x).begin(), (x).end()
 
const int N = 1e5 + 10;
 
long long dp[N][4];
vector<pair<int, int> > adj[N];
 
void DFS(int x, int p = 0, int cnt = 0) {
    for(auto [j, t] : adj[x]) {
        if(j == p) continue;
        DFS(j, x, t);
    }
    if(Sz(adj[x]) == 1 && x != 1) {
        return;
    }
	vector<long long> vc;
	long long sum = 0;
	for(auto [j, t] : adj[x]) {
		if(j == p) continue;
		sum += dp[j][1];
		vc.push_back(dp[j][2] + t - dp[j][1]);
	}		
	sort(All(vc), greater<long long>());
	dp[x][0] = sum + vc[0] + cnt;
	if(Sz(vc) > 1)
		dp[x][0] = max(dp[x][0], sum + vc[0] + vc[1]);

	if(Sz(vc) > 1)
		dp[x][2] = sum + vc[0] + vc[1];

	dp[x][1] = sum + vc[0] + cnt;

	dp[x][3] = sum;
}
 
int main() {
    int n; cin >>n;
    for(int i = 0; i < n - 1; i++) {
        int u, v, c; cin >>u >>v >>c;
        adj[u].push_back({v, c}), adj[v].push_back({u, c});
    }
 
    DFS(1);

	/*for(int i = 1; i <= n; i++) {
		cout<<"^^" <<i <<' ' <<dp[i][0] <<' ' <<dp[i][1] <<' ' <<dp[i][2] <<' ' <<dp[i][3] <<endl;
	}*/

	cout<<dp[1][2] <<endl;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2664 KB Output is correct
3 Incorrect 1 ms 2584 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2664 KB Output is correct
3 Incorrect 1 ms 2584 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2664 KB Output is correct
3 Incorrect 1 ms 2584 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2664 KB Output is correct
3 Incorrect 1 ms 2584 KB Output isn't correct
4 Halted 0 ms 0 KB -