QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#155400#2169. 'S No Problemwarwolf#WA 2ms6256kbC++232.3kb2023-09-01 16:30:242023-09-01 16:30:25

Judging History

你现在查看的是最新测评结果

  • [2023-09-01 16:30:25]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6256kb
  • [2023-09-01 16:30:24]
  • 提交

answer

#include <bits/stdc++.h>
#define maxn 100086

using namespace std;

int n;
vector<pair<int, int> > v[maxn];
int ans, f[maxn], g[maxn], h[maxn];

void dfs(int i, int fa){
	int mx = 0, id = 0, sx = 0;
	for(auto it : v[i]){
		int to = it.first, co = it.second;
		if(to == fa) continue;
		dfs(to, i);
		if(g[to] >= mx) sx = mx, mx = g[to], id = to;
		else sx = max(sx, g[to]); 
	}
	f[i] = mx;
	int fmx = 0, hmx = 0, gmx = 0;
	for(auto it : v[i]){
		int to = it.first, co = it.second;
		if(to == fa) continue;
		ans = max(ans, g[i] + g[to]);
		g[i] = max(g[i], g[to]);
		int fval = f[to] + co;
		int hval = h[to] + co;
		ans = max(ans, fval + hmx);
		ans = max(ans, fmx + hval);
		gmx = max(gmx, hmx + hval);
		fmx = max(fmx, fval);
		hmx = max(hmx, hval);
	}
	g[i] = max(g[i], gmx);
	for(auto it : v[i]){
		int to = it.first, co = it.second;
		if(to == fa) continue;
		int fval = f[to] + co + (id == to ? sx : mx);
		int hval = h[to] + co;
		f[i] = max(f[i], fval);
		h[i] = max(h[i], hval);
	}
	vector<pair<int, int> > a, b;
	for(auto it : v[i]){
		int to = it.first, co = it.second;
		if(to == fa) continue;
		h[to] += co;
		a.push_back({h[to], to});
		b.push_back({g[to], to});
	}
	sort(a.begin(), a.end(), greater<pair<int, int> >());
	sort(b.begin(), b.end(), greater<pair<int, int> >());
	set<int> st;
	vector<int> w;
	for(int j = 0;j < min((int)a.size(), 6);j++) st.insert(a[j].second);
	for(int j = 0;j < min((int)b.size(), 6);j++) st.insert(b[j].second);
	for(auto x : st) w.push_back(x);
	for(int i = 0;i < w.size();i++) for(int j = 0;j < i;j++) for(int k = 0;k < j;k++){
		ans = max(ans, h[w[i]] + h[w[j]] + g[w[k]]);
		ans = max(ans, h[w[i]] + g[w[j]] + h[w[k]]);
		ans = max(ans, g[w[i]] + h[w[j]] + h[w[k]]);
	}
	for(int i = 0;i < w.size();i++) for(int j = 0;j < i;j++){
		ans = max(ans, h[w[i]] + g[w[j]]);
		ans = max(ans, g[w[i]] + h[w[j]]);
	}
//	printf("%d %d %d %d--\n", i, f[i], g[i], h[i]);
}

int main(){
	scanf("%d", &n);
	for(int i = 1;i < n;i++){
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		v[x].push_back({y, z});
		v[y].push_back({x, z});
	}
	dfs(1, 0);
	ans = max(ans, g[1]);
//	printf("%d--\n", ans);
	ans = -ans;
	for(int i = 1;i <= n;i++) for(auto it : v[i]) ans += it.second;
	printf("%d", ans);
}
/*
6
1 2 1
1 3 1
3 4 1
3 5 1
1 6 1

6
1 2 10
2 3 10
1 4 1
4 5 10
4 6 10
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 6256kb

input:

5
1 2 1
1 3 2
1 4 3
1 5 4

output:

13

result:

wrong answer 1st lines differ - expected: '10', found: '13'