Submission #549539

#TimeUsernameProblemLanguageResultExecution timeMemory
549539SmolBrainFactories (JOI14_factories)C++17
0 / 100
287 ms56744 KiB
// 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<ll> adj[maxn]; vector<pll> adj2[maxn], 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 { // iterative segtree implementation based on this blog: // https://codeforces.com/blog/entry/18051 // thanks to AI.Cash for the educational blog!!! /*=======================================================*/ struct data { ll mxdepth, node; }; data neutral = { -1, -1}; data merge(data &left, data &right) { data curr; if (left.mxdepth > right.mxdepth) curr = left; else curr = right; return curr; } void create(int i, T v) { tr[i] = {v.ff, v.ss}; } void modify(int i, T v) { tr[i] = {v.ff, v.ss}; } /*=======================================================*/ int n; vector<data> tr; segtree() { } segtree(int siz) { init(siz); } void init(int siz) { n = siz; tr.assign(2 * n, neutral); } void build(vector<T> &a, int siz) { rep(i, siz) create(i + n, a[i]); rev(i, n - 1, 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]); } void pupd(int i, T v) { modify(i + n, v); for (i += n; i > 1; i >>= 1) tr[i >> 1] = merge(tr[i], tr[i ^ 1]); } data query(int l, int r) { data resl = neutral, resr = neutral; for (l += n, r += n; l <= r; l >>= 1, r >>= 1) { if (l & 1) resl = merge(resl, tr[l++]); if (!(r & 1)) resr = merge(tr[r--], resr); } return merge(resl, resr); } }; vector<ll> depth(maxn), dis(maxn), tin(maxn), tout(maxn); ll timer = 0; lca_algo LCA; segtree<pll> 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 Init(int n, int a[], int b[], int d[]) { // editorial approach, but i have used dijkstra instead of dp on trees 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<pll>(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<pll> 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) { ll u = vals[i].ss, v = vals[i + 1].ss; ll lca = LCA.query(u, v); vals.pb({tin[lca], lca}); } sort(all(vals)); auto it = unique(all(vals)); vals.resize(it - vals.begin()); ll siz = sz(vals); rep(i, siz) { ll 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}); } priority_queue< pll, vector<pll>, greater<pll> > pq; rep(i, n) pq.push({0, a[i]}); rep(i, m) type2[b[i]] = 1; ll ans = -1; while (!pq.empty()) { auto [cost, node] = pq.top(); pq.pop(); if (vis[node]) conts; vis[node] = 1; if (type2[node]) { ans = cost; break; } for (auto [child, w] : adj3[node]) { pq.push({cost + w, child}); } } // resetting values rep(i, siz) { ll u = vals[i].ss; st.pupd(tout[u], { -1, -1}); adj[u].clear(); vis[u] = 0; } rep(i, m) type2[b[i]] = 0; return ans; }

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...