Submission #720700

#TimeUsernameProblemLanguageResultExecution timeMemory
720700joelgun14Race (IOI11_race)C++17
0 / 100
9 ms18260 KiB
#include "race.h" #include <bits/stdc++.h> #define ll long long using namespace std; int l; int res = 1e9; const int lim = 2e5 + 5; int depth[lim]; bool vis[lim]; vector<pair<int, int>> edges[lim]; struct disjoint_set { int h[lim], sz[lim], d[lim]; ll shift[lim]; // fi -> distance // se -> depth // cari depth terkecil set<pair<int, int>> a[lim]; disjoint_set() { memset(h, -1, sizeof(h)); memset(sz, 0, sizeof(sz)); memset(shift, 0, sizeof(shift)); memset(d, -1, sizeof(d)); } int fh(int nd) { return h[nd] == -1 ? nd : h[nd] = fh(h[nd]); } bool merge(int x, int y) { x = fh(x), y = fh(y); if(x != y) { if(sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; h[y] = x; if(a[x].size() < a[y].size()) { swap(a[x], a[y]); } d[x] = min(d[x], d[y]); if(d[x] == -1) assert(false); for(auto i : a[y]) { // coba cari di a[x] yg panjangnya pas int target = l - i.first - shift[x] - shift[y]; // cari target di a[x]; if(a[x].lower_bound(make_pair(target, 0)) != a[x].end() && (*a[x].lower_bound(make_pair(target, 0))).first == target) { res = min(res, (*a[x].lower_bound(make_pair(target, 0))).second + i.second - 2 * depth[x]); } } for(auto i : a[y]) { auto tmp = i; tmp.first += shift[y] - shift[x]; a[x].insert(tmp); } } return x != y; } }; disjoint_set dsu; void dfs(int nd = 0) { vis[nd] = 1; dsu.a[nd].insert(make_pair(0, depth[nd])); dsu.d[nd] = depth[nd]; for(auto i : edges[nd]) { if(!vis[i.first]) { depth[i.first] = depth[nd] + 1; dfs(i.first); dsu.shift[dsu.fh(i.first)] += i.second; dsu.merge(i.first, nd); } } } int best_path(int N, int K, int H[][2], int L[]) { l = K; for(int i = 0; i < N - 1; ++i) { edges[H[i][0]].push_back(make_pair(H[i][1], L[i])); edges[H[i][1]].push_back(make_pair(H[i][0], L[i])); } dfs(); if(res == 1e9) return -1; else return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...