Submission #287387

# Submission time Handle Problem Language Result Execution time Memory
287387 2020-08-31T16:36:45 Z Namnamseo Beads and wires (APIO14_beads) C++17
100 / 100
758 ms 76908 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
#define pb push_back
void read(int& x){ scanf("%d",&x); }
template<typename T,typename... Args> void read(T& a,Args&... b){ read(a); read(b...); }

const int max_n = 200010;
const ll inf = 1LL<<60;

vector<pp> edge[max_n];
int par[max_n];

ll   dp [max_n][3][2];
bool chk[max_n][3][2];

int n;

void in(){
	read(n);
	for(int i=1; i<n; ++i){
		int x, y, d; read(x, y, d);
		edge[x].pb({y, d});
		edge[y].pb({x, d});
	}
	for(int i=1; i<=n; ++i) sort(all(edge[i]));
}

vector<int> nin[max_n];
ll osum[max_n];
multiset<ll> mev[max_n];

void dfs(int x){
	for(pp tmp:edge[x]){
		int y=tmp.first;
		if(par[x]!=y) par[y]=x, nin[x].pb(y), dfs(y);
	}
	if(par[x]) nin[x].pb(par[x]);
}

ll f(int, int, int);

int vc[max_n];
int lf[max_n];
int ld[max_n];

int ed(int a, int b){
	return lower_bound(all(edge[a]), pp(b, 0))->second;
}

void fill_val(int p, int from){
	if(lf[p] == from) return;
	if(vc[p] == 0){
		for(pp tmp:edge[p]){
			int x, dst;
			tie(x, dst)=tmp;
			if(x == from){
				ld[p]=dst;
				continue;
			}
			ll ov = max(f(x, p, 0), dst + f(x, p, 1));
			osum[p] += ov;
			mev[p].insert(dst + f(x, p, 0) - ov);
		}
	} else {
		if(lf[p] != -1){
			int x=lf[p], dst=ld[p];
			ll ov = max(f(x, p, 0), dst + f(x, p, 1));
			osum[p] += ov;
			mev[p].insert(dst + f(x, p, 0) - ov);
		}
		if(from != -1){
			int x=from, dst=ed(from, p);
			ll ov = max(f(x, p, 0), dst + f(x, p, 1));
			osum[p] -= ov;
			mev[p].erase(mev[p].find(dst + f(x, p, 0) - ov));
			ld[p]=dst;
		}
	}
	++vc[p];
	lf[p]=from;
}

ll f(int p, int from, int parity){
	int child=(from == -1) ? p : (par[from]==p ? from:p);
	int sec = (from == -1) ? 2 : (p==child);
	ll& mydp = dp[child][sec][parity];
	if(chk[child][sec][parity]) return mydp;
	chk[child][sec][parity]=1;
	mydp = -inf;
    
	fill_val(p, from);
	if(parity == 0){
		mydp = osum[p];
	} else {
		if(mev[p].size())
			mydp = osum[p] + *--mev[p].end();
	}
	
	return mydp;
}

int main()
{
	in(); dfs(1);
	ll ans = 0;
	for(int i=1; i<=n; ++i){
		multiset<ll> diff;
		for(auto tmp:edge[i]){
			int y, d; tie(y, d) = tmp;
			ll v = d+f(y, i, 0);
			v -= max(f(y, i, 0), d+f(y, i, 1));
			diff.insert(v);
			if(diff.size() == 3u) diff.erase(diff.begin());
		}
		ll mv = f(i, -1, 0);
		ans = max(ans, mv);
		if(diff.size() >= 2u){
			auto it=diff.rbegin();
			auto i2=it; ++i2;
			ans = max(ans, mv + *it + *i2);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

beads.cpp: In function 'void read(int&)':
beads.cpp:7:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    7 | void read(int& x){ scanf("%d",&x); }
      |                    ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 14 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 14 ms 19200 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 14 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 14 ms 19200 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 17 ms 19172 KB Output is correct
14 Correct 15 ms 19200 KB Output is correct
15 Correct 15 ms 19192 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 15 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 14 ms 19200 KB Output is correct
21 Correct 13 ms 19200 KB Output is correct
22 Correct 16 ms 19200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 14 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 14 ms 19200 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 17 ms 19172 KB Output is correct
14 Correct 15 ms 19200 KB Output is correct
15 Correct 15 ms 19192 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 15 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 14 ms 19200 KB Output is correct
21 Correct 13 ms 19200 KB Output is correct
22 Correct 16 ms 19200 KB Output is correct
23 Correct 21 ms 20352 KB Output is correct
24 Correct 23 ms 20352 KB Output is correct
25 Correct 21 ms 20356 KB Output is correct
26 Correct 30 ms 21504 KB Output is correct
27 Correct 31 ms 21504 KB Output is correct
28 Correct 31 ms 21752 KB Output is correct
29 Correct 37 ms 21696 KB Output is correct
30 Correct 40 ms 21752 KB Output is correct
31 Correct 32 ms 22272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 14 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 14 ms 19200 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 17 ms 19172 KB Output is correct
14 Correct 15 ms 19200 KB Output is correct
15 Correct 15 ms 19192 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 15 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 14 ms 19200 KB Output is correct
21 Correct 13 ms 19200 KB Output is correct
22 Correct 16 ms 19200 KB Output is correct
23 Correct 21 ms 20352 KB Output is correct
24 Correct 23 ms 20352 KB Output is correct
25 Correct 21 ms 20356 KB Output is correct
26 Correct 30 ms 21504 KB Output is correct
27 Correct 31 ms 21504 KB Output is correct
28 Correct 31 ms 21752 KB Output is correct
29 Correct 37 ms 21696 KB Output is correct
30 Correct 40 ms 21752 KB Output is correct
31 Correct 32 ms 22272 KB Output is correct
32 Correct 143 ms 31352 KB Output is correct
33 Correct 146 ms 31352 KB Output is correct
34 Correct 148 ms 31404 KB Output is correct
35 Correct 709 ms 68344 KB Output is correct
36 Correct 710 ms 68216 KB Output is correct
37 Correct 738 ms 68388 KB Output is correct
38 Correct 630 ms 71396 KB Output is correct
39 Correct 656 ms 71668 KB Output is correct
40 Correct 708 ms 71008 KB Output is correct
41 Correct 758 ms 76908 KB Output is correct