Submission #546681

# Submission time Handle Problem Language Result Execution time Memory
546681 2022-04-08T07:00:01 Z djs100201 Beads and wires (APIO14_beads) C++17
0 / 100
3 ms 5040 KB
#include<bits/stdc++.h>
#define double long double
//T.erase(unique(T.begin(),T.end()),T.end());
//written by djs100201
#define all(v) v.begin(),v.end()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const ll n_ = 2e5 + 100, inf = (ll)1e9 * (ll)1e9 + 7, mod = 1e9 + 7, sqrtN = 320;

ll dy[4] = { -1,0,1,0 }, dx[4] = { 0,1,0,-1 };
ll n, m, k, tc = 1, a, b, c, d, sum, x, y, z, base, ans, q;
ll gcd(ll x, ll y) {
	if (!y)return x;
	return gcd(y, x % y);
}
vector<P>v[n_];
ll dp[n_][2][2];
void dfs(ll x, ll par, ll cost) {
	ll val1 = 0, val2 = -inf;
	ll t1 = -inf, t2 = -inf, t12 = -inf, t21 = -inf, t22 = -inf;
	ll a1 = -inf, a2 = -inf;
	ll sum1 = 0, sum2 = -inf;
	priority_queue<ll>pq;
	for (auto nxt : v[x]) {
		if (nxt.first == par)continue;
		dfs(nxt.first, x, nxt.second);
		ll val = max(dp[nxt.first][0][0], dp[nxt.first][1][0]);
		dp[x][0][0] += val;
		dp[x][1][0] += val;

		//sum1 += val;
		//sum1은 
		//dp[x][0][1] += val;


		//val = dp[nxt.first][0][0];
		ll c1 = dp[nxt.first][0][1] + nxt.second - val;
		ll c2 = dp[nxt.first][0][0] + nxt.second - val;
		pq.push(c2);
		sum1 = max(sum1, t1 + c2);
		sum1 = max(sum1, t2 + c2);
		sum1 = max(sum1, t2 + c1);
		sum1 = max(sum1, dp[nxt.first][0][1] - val);
		t1 = max(t1, c1);
		t2 = max(t2, c2);
		dp[x][0][1] += val;


		val = max(dp[nxt.first][0][0], dp[nxt.first][1][0]);
		dp[x][1][1] += val;
		c1 = max(dp[nxt.first][0][1], dp[nxt.first][1][1]) - val;
		c2 = dp[nxt.first][0][0] + nxt.second - val;
		sum2 = max(sum2, dp[nxt.first][0][1] + nxt.second - val);
		sum2 = max(sum2, a1 + c2);
		sum2 = max(sum2, a2 + c1);
		a1 = max(a1, c1);
		a2 = max(a2, c2);
	}
	if (pq.size()) {
		ll val = pq.top();
		dp[x][1][0] += val + cost;
	}
	else dp[x][1][0] = -inf;

	dp[x][0][1] += sum1;
	dp[x][1][1] += sum2 + cost;
}
void solve() {
	cin >> n;
	for (int i = 1; i < n; i++) {
		cin >> a >> b >> c;
		v[a].push_back({ b,c });
		v[b].push_back({ a,c });
	}
	dfs(1, 0, -inf);
	cout << max(dp[1][0][0], dp[1][0][1]);
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	//cin >> tc;
	while (tc--)solve();
}

Compilation message

beads.cpp: In function 'void dfs(ll, ll, ll)':
beads.cpp:22:5: warning: unused variable 'val1' [-Wunused-variable]
   22 |  ll val1 = 0, val2 = -inf;
      |     ^~~~
beads.cpp:22:15: warning: unused variable 'val2' [-Wunused-variable]
   22 |  ll val1 = 0, val2 = -inf;
      |               ^~~~
beads.cpp:23:27: warning: unused variable 't12' [-Wunused-variable]
   23 |  ll t1 = -inf, t2 = -inf, t12 = -inf, t21 = -inf, t22 = -inf;
      |                           ^~~
beads.cpp:23:39: warning: unused variable 't21' [-Wunused-variable]
   23 |  ll t1 = -inf, t2 = -inf, t12 = -inf, t21 = -inf, t22 = -inf;
      |                                       ^~~
beads.cpp:23:51: warning: unused variable 't22' [-Wunused-variable]
   23 |  ll t1 = -inf, t2 = -inf, t12 = -inf, t21 = -inf, t22 = -inf;
      |                                                   ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -