이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
a %= MOD;
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 1) return 1;
if (a == 0) return b;
return gcd(b%a, a);
}
const int N = 10001;
vii g[N];
int dp[N][4];
vi ans(N);
vi val(N);
//dp[i][0] without edge i, with parent of i
//dp[i][1] with the edge
//dp[i][2-3] from below,
void dfs(int u, int p){
dp[u][0]=0; dp[u][1]=-MOD;
int sz = 0;
for (ii edge : g[u]){
int v = edge.fi; int w = edge.se;
if (v==p) continue;
sz++;
dfs(v,u);
dp[u][0] += max(dp[v][0], dp[v][1]+w);
}
if (sz==0){
return;
}
for (ii edge : g[u]){
int v = edge.fi, w = edge.se;
if (v==p) continue;
int val = dp[v][0]+w - max(dp[v][0], dp[v][1]+w);
dp[u][1] = max(dp[u][1], dp[u][0]+val);
}
}
void dfs2(int u, int p, int W){
multiset<int> st; int sz=0;
for (ii edge : g[u]){
int v = edge.fi; int w = edge.se;
if (v==p) continue;
int val1 = ans[p]+W+w+dp[u][0]-max(dp[v][0], dp[v][1]+w);
if (p!=0) ans[v]=max(ans[v], val1);
val[v]=dp[v][0] + w - max(dp[v][0], dp[v][1]+w);
st.insert(val[v]);
sz++;
}
if (sz<2) return;
for (ii edge : g[u]){
int v = edge.fi; int w =edge.se;
if (v==p) continue;
st.erase(st.find(val[v]));
int val1 = dp[u][0]+val[v]-dp[v][0];
val1 = ans[u] + val1 + *st.rbegin();
ans[v]=max(ans[v], val1);
st.insert(val[v]);
dfs2(v,u,w);
}
}
void solve(){
int n; cin >> n;
FOR(i,0,n-1){
int u,v,w; cin>>u>>v>>w;
g[u].pb({v,w}); g[v].pb({u,w});
}
dfs(1,0);
int answer2 = 0;
FOR(i,1,n+1){
dfs(i,0);
answer2 = max(answer2, dp[i][0]);
}
cout << answer2;
return;
dfs2(1,0,-MOD);
int answer = 0;
FOR(i,1,n+1){
//cout<<ans[i]<<" "<<dp[i][0]<<" "<<dp[i][1]<<'\n';
answer=max(answer, ans[i] + dp[i][0]);
}
cout << answer <<'\n';
return;
}
int main() {
ios::sync_with_stdio(false);
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization ("Ofast")
|
beads.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |