이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
// #define int long long
// #define int unsigned long long
#define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>
void open_file(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
// const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 5 * 1e5 + 5;
vector<pii> g[maxN];
map<int, int> dp[maxN];
int n, k, dep[maxN], sum[maxN], ans = inf;
void dfs(int v, int par, int len, int val) {
dep[v] = len; sum[v] = val;
dp[v][sum[v]] = dep[v];
for (pii u : g[v]) {
if (u.ff == par) continue;
dfs(u.ff, v, len + 1, val + u.sc);
if (dp[u.ff].size() > dp[v].size())
swap(dp[u.ff], dp[v]);
for (auto x : dp[u.ff]) {
if (dp[v].find(k + 2 * sum[v] - x.ff) != dp[v].end()) {
ans = min(ans, dp[v][k + 2 * sum[v] - x.ff] + x.sc - 2 * dep[v]);
}
} for (auto x : dp[u.ff]) {
if (dp[v].find(x.ff) == dp[v].end())
dp[v][x.ff] = x.sc;
else dp[v][x.ff] = min(dp[v][x.ff], x.sc);
}
}
}
int best_path(int _n, int _k, int h[][2], int l[]) {
n = _n; k = _k;
for (int i = 0; i < n - 1; i++) {
g[h[i][0]].pb({h[i][1], l[i]});
g[h[i][1]].pb({h[i][0], l[i]});
} dfs(0, -1, 0, 0);
return ans;
}
// int main() {
// int _n, _k;
// cin >> _n >> _k;
// int h[_n][2], l[_n];
// for (int i = 0; i < _n - 1; i++) {
// cin >> h[i][0] >> h[i][1];
// } for (int i = 0; i < _n - 1; i++) cin >> l[i];
// cout << best_path(_n, _k, h, l);
// }
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'void open_file(std::string)':
race.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((filename + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |