이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Om Namah Shivaya
// GM in 108 days
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define endl '\n'
#define pb push_back
#define conts continue
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define ff first
#define ss second
#define ceil2(x,y) ((x+y-1) / (y))
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#ifndef ONLINE_JUDGE
#define debug(x) cout << #x <<" = "; print(x); cout << endl
#else
#define debug(x)
#endif
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
bool iseven(ll n) {if ((n & 1) == 0) return true; return false;}
void print(ll t) {cout << t;}
void print(int t) {cout << t;}
void print(string t) {cout << t;}
void print(char t) {cout << t;}
void print(double t) {cout << t;}
void print(ld t) {cout << t;}
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(pair <T, V> p) {cout << "{"; print(p.ff); cout << ","; print(p.ss); cout << "}";}
template <class T> void print(vector <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(set <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(multiset <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T, class V> void print(map <T, V> v) {cout << "[ "; for (auto i : v) {print(i); cout << " ";} cout << "]";}
template<typename T> void amin(T &a, T b) { a = min(a, b); }
template<typename T> void amax(T &a, T b) { a = max(a, b); }
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const int MOD = 1e9 + 7;
const int maxn = 5e5 + 5;
const int inf1 = 1e9 + 5;
const ll inf2 = ll(1e18) + 5;
#include "factories.h"
vector<int> adj[maxn];
vector<pii> adj2[maxn];
vector<pll> adj3[maxn];
struct lca_algo {
// LCA template (for graphs with 1-based indexing)
int LOG = 1;
vector<int> depth;
vector<vector<int>> up;
lca_algo() {
}
lca_algo(int n) {
lca_init(n);
}
void lca_init(int n) {
while ((1 << LOG) < n) LOG++;
up = vector<vector<int>>(n + 1, vector<int>(LOG, 0));
depth = vector<int>(n + 1);
lca_dfs(0, -1);
}
void lca_dfs(int node, int par) {
trav(child, adj[node]) {
if (child == par) conts;
up[child][0] = node;
rep1(j, LOG - 1) {
up[child][j] = up[up[child][j - 1]][j - 1];
}
depth[child] = depth[node] + 1;
lca_dfs(child, node);
}
}
int lift(int u, int k) {
rep(j, LOG) {
if (k & (1 << j)) {
u = up[u][j];
}
}
return u;
}
int query(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
u = lift(u, k);
if (u == v) return u;
rev(j, LOG - 1, 0) {
if (up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
}
u = up[u][0];
return u;
}
};
template<typename T>
struct segtree {
/*=======================================================*/
struct data {
int mxdepth, node;
};
data neutral = { -1, -1};
void merge(data &curr, data &left, data &right) {
if (left.mxdepth > right.mxdepth) curr = left;
else curr = right;
}
void create(int x, int lx, int rx, T v) {
tr[x] = {v.ff, v.ss};
}
void modify(int x, int lx, int rx, T v) {
tr[x] = {v.ff, v.ss};
}
/*=======================================================*/
int siz = 1;
vector<data> tr;
segtree(int n) {
init(n);
}
segtree() {
};
void init(int n) {
while (siz < n) siz *= 2;
tr.assign(2 * siz, neutral);
}
void build(vector<T> &a, int n, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < n) {
create(x, lx, rx, a[lx]);
}
return;
}
int mid = (lx + rx) / 2;
build(a, n, 2 * x + 1, lx, mid);
build(a, n, 2 * x + 2, mid, rx);
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void build(vector<T> &a, int n) {
build(a, n, 0, 0, siz);
}
void pupd(int i, T v, int x, int lx, int rx) {
if (rx - lx == 1) {
modify(x, lx, rx, v);
return;
}
int mid = (lx + rx) / 2;
if (i < mid) {
pupd(i, v, 2 * x + 1, lx, mid);
}
else {
pupd(i, v, 2 * x + 2, mid, rx);
}
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void pupd(int i, T v) {
pupd(i, v, 0, 0, siz);
}
data query(int l, int r, int x, int lx, int rx) {
if (lx >= r or rx <= l) return neutral;
if (lx >= l and rx <= r) return tr[x];
int mid = (lx + rx) / 2;
data curr;
data left = query(l, r, 2 * x + 1, lx, mid);
data right = query(l, r, 2 * x + 2, mid, rx);
merge(curr, left, right);
return curr;
}
data query(int l, int r) {
return query(l, r + 1, 0, 0, siz);
}
};
vector<int> depth(maxn), tin(maxn), tout(maxn);
vector<ll> dis(maxn), dis2(maxn, inf2), dp1(maxn, inf2), dp2(maxn, inf2);
int timer = 0;
lca_algo LCA;
segtree<pii> st;
vector<bool> vis(maxn), type2(maxn);
void dfs1(int node, int par) {
tin[node] = timer++;
for (auto [child, w] : adj2[node]) {
if (child == par) conts;
depth[child] = depth[node] + 1;
dis[child] = dis[node] + w;
dfs1(child, node);
}
tout[node] = timer++;
}
void dfs2(int node, int par) {
if (type2[node]) dp1[node] = 0;
for (auto [child, w] : adj3[node]) {
if (child == par) conts;
dfs2(child, node);
amin(dp1[node], w + dp1[child]);
}
dp2[node] = dp1[node];
}
void dfs3(int node, int par) {
for (auto [child, w] : adj3[node]) {
if (child == par) conts;
amin(dp2[child], w + dp2[node]);
dfs3(child, node);
}
}
void Init(int n, int a[], int b[], int d[]) {
// editorial approach
rep(i, n - 1) {
int u = a[i], v = b[i], w = d[i];
adj[u].pb(v), adj[v].pb(u);
adj2[u].pb({v, w}), adj2[v].pb({u, w});
}
LCA = lca_algo(n);
st = segtree<pii>(2 * n + 5);
dfs1(0, -1);
}
ll getdis(ll u, ll v) {
ll lca = LCA.query(u, v);
ll res = dis[u] + dis[v] - 2 * dis[lca];
return res;
}
ll Query(int n, int a[], int m, int b[]) {
vector<pii> vals;
rep(i, n) vals.pb({tin[a[i]], a[i]});
rep(i, m) vals.pb({tin[b[i]], b[i]});
sort(all(vals));
rep(i, n + m - 1) {
int u = vals[i].ss, v = vals[i + 1].ss;
int lca = LCA.query(u, v);
vals.pb({tin[lca], lca});
}
sort(all(vals));
auto it = unique(all(vals));
vals.resize(it - vals.begin());
int siz = sz(vals);
rep(i, siz) {
int u = vals[i].ss;
auto [mxdepth, v] = st.query(tout[u] + 1, timer);
if (v != -1) {
ll w = getdis(u, v);
adj3[u].pb({v, w}), adj3[v].pb({u, w});
}
st.pupd(tout[u], {depth[u], u});
}
rep(i, m) type2[b[i]] = 1;
dfs2(a[0], -1);
dfs3(a[0], -1);
ll ans = inf2;
rep(i, n) {
amin(ans, dp2[a[i]]);
}
// resetting values
rep(i, siz) {
int u = vals[i].ss;
st.pupd(tout[u], { -1, -1});
adj3[u].clear();
dp1[u] = dp2[u] = inf2;
}
rep(i, m) type2[b[i]] = 0;
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
factories.cpp: In function 'void usaco(std::string)':
factories.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
factories.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | 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... |