제출 #42039

#제출 시각아이디문제언어결과실행 시간메모리
42039fest구슬과 끈 (APIO14_beads)C++14
100 / 100
259 ms19676 KiB
// fest
#include <bits/stdc++.h>	

#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)

typedef long long ll;
typedef long double ld;    

using namespace std;

void FREOPEN() {
	#ifdef COMP
		freopen(".in", "r", stdin);
		freopen("1.out", "w", stdout);
	#endif
}                           

inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }             

const int N = 200500, inf = 1e9 * 2, MOD = (int)1e9 + 7;

char CH[N];

const ll INF = 1e18;

const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};

int dp[N][2];

vector<pair<int, int> > g[N];

pair<int, int> opt[N];

int ans;

void dfs(int v, int pr, int last) {
	dp[v][0] = 0;
	dp[v][1] = -inf;
	for (auto i : g[v]) {
		int u = i.F;
		int c = i.S;
		if (u == pr) continue;
		dfs(u, v, c);
		dp[v][0] += max(dp[u][0], dp[u][1]);
	}
	for (auto i : g[v]) {
		int u = i.F;
		int c = i.S;
		if (u == pr) continue;
		if (dp[v][1] < last + (dp[v][0] - max(dp[u][0], dp[u][1])) + c + dp[u][0]) {
			dp[v][1] = last + (dp[v][0] - max(dp[u][0], dp[u][1])) + c + dp[u][0];
			opt[v] = {u, c};
		}
	}
}

void calc(int v, int pr, int last, int dp0, int dp1) {
	ans = max(ans, dp[v][0] + max(dp0, dp1));
	for (auto i : g[v]) {
		int u = i.F;
		int c = i.S;
		if (u == pr) continue;
		int go0 = dp[v][0] - max(dp[u][0], dp[u][1]) + max(dp0, dp1);
		int go1 = -inf;
		if (last) go1 = go0 - max(dp0, dp1) + last + dp0 + c;
		if (opt[v].F != u) {
			go1 = max(go1, go0 - max(dp[opt[v].F][0], dp[opt[v].F][1]) + opt[v].S + c + dp[opt[v].F][0]); 
		}
		else {
			for (auto j : g[v]) {
				int u2 = j.F;
				int c2 = j.S;
				if (u2 == pr || u2 == u) continue;
				go1 = max(go1, go0 - max(dp[u2][0], dp[u2][1]) + c2 + c + dp[u2][0]);
			}
		}
		calc(u, v, c, go0, go1);
	}
}

int main() {
  
	FREOPEN();
	int n;
	cin >> n;
	for (int i = 2; i <= n; i++) {
		int x, y, c;
		scanf("%d %d %d", &x, &y, &c);
		g[x].pb({y, c});
		g[y].pb({x, c});
	}
	dfs(1, 0, 0);
	calc(1, 0, 0, 0, -inf);
	cout << ans;
	return 0;	
}

컴파일 시 표준 에러 (stderr) 메시지

beads.cpp: In function 'int main()':
beads.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &x, &y, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...