結果
問題 | No.2642 Don't cut line! |
ユーザー | GOTKAKO |
提出日時 | 2024-02-20 23:00:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 136 ms / 4,000 ms |
コード長 | 4,129 bytes |
コンパイル時間 | 2,757 ms |
コンパイル使用メモリ | 231,900 KB |
実行使用メモリ | 31,984 KB |
最終ジャッジ日時 | 2024-09-29 03:55:09 |
合計ジャッジ時間 | 6,545 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 130 ms
29,684 KB |
testcase_02 | AC | 136 ms
31,984 KB |
testcase_03 | AC | 133 ms
31,092 KB |
testcase_04 | AC | 132 ms
30,704 KB |
testcase_05 | AC | 130 ms
29,888 KB |
testcase_06 | AC | 43 ms
6,816 KB |
testcase_07 | AC | 44 ms
6,820 KB |
testcase_08 | AC | 43 ms
6,820 KB |
testcase_09 | AC | 42 ms
6,816 KB |
testcase_10 | AC | 41 ms
6,816 KB |
testcase_11 | AC | 42 ms
6,816 KB |
testcase_12 | AC | 44 ms
6,816 KB |
testcase_13 | AC | 41 ms
6,820 KB |
testcase_14 | AC | 44 ms
6,816 KB |
testcase_15 | AC | 46 ms
6,816 KB |
testcase_16 | AC | 72 ms
11,008 KB |
testcase_17 | AC | 100 ms
26,844 KB |
testcase_18 | AC | 103 ms
27,252 KB |
testcase_19 | AC | 71 ms
21,672 KB |
testcase_20 | AC | 47 ms
11,136 KB |
testcase_21 | AC | 42 ms
6,816 KB |
testcase_22 | AC | 48 ms
9,088 KB |
testcase_23 | AC | 116 ms
30,148 KB |
testcase_24 | AC | 47 ms
13,264 KB |
testcase_25 | AC | 47 ms
12,288 KB |
testcase_26 | AC | 44 ms
7,168 KB |
testcase_27 | AC | 69 ms
20,816 KB |
testcase_28 | AC | 110 ms
27,984 KB |
testcase_29 | AC | 49 ms
8,576 KB |
testcase_30 | AC | 49 ms
11,008 KB |
testcase_31 | AC | 98 ms
23,720 KB |
testcase_32 | AC | 49 ms
10,112 KB |
testcase_33 | AC | 2 ms
6,816 KB |
testcase_34 | AC | 2 ms
6,820 KB |
testcase_35 | AC | 2 ms
6,816 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; class UnionFind{ public: vector<int> par,siz; void make(int N){ par.resize(N,-1); siz.resize(N,1); } int root(int x){ if(par.at(x) == -1) return x; else return par.at(x) = root(par.at(x)); } void unite(int u, int v){ u = root(u),v = root(v); if(u == v) return; if(siz.at(u) < siz.at(v)) swap(u,v); par.at(v) = u; siz.at(u) += siz.at(v); } bool issame(int u, int v){ if(root(u) == root(v)) return true; else return false; } }; class LowestCommonAncestor{ public: vector<vector<int>> par; vector<int> dist; using SS = int; vector<vector<SS>> dist2; SS op(SS a, SS b){return max(a,b);} SS e(){return 0;} void make(vector<vector<pair<int,SS>>> &Graph,int root){ int N = Graph.size(),K = 1; while((1<<K) < N) K++; par.resize(K,vector<int>(N,-1)); dist2.resize(K,vector<SS>(N,e())); dist.resize(N,-1); dfs(Graph,root,-1,0); for(int i=1; i<K; i++){ for(int k=0; k<N; k++){ if(par.at(i-1).at(k) == -1) par.at(i).at(k) = -1,dist2.at(i).at(k) = dist2.at(i-1).at(k); else{ par.at(i).at(k) = par.at(i-1).at(par.at(i-1).at(k)); dist2.at(i).at(k) = op(dist2.at(i-1).at(k),dist2.at(i-1).at(par.at(i-1).at(k))); } } } }; void dfs(vector<vector<pair<int,SS>>> &Graph,int pos,int back,int d){ dist.at(pos) = d; par.at(0).at(pos) = back; for(auto [to,w] : Graph.at(pos)){ if(to == back) continue; dist2.at(0).at(to) = op(dist2.at(0).at(to),w); dfs(Graph,to,pos,d+1); } } int LCA(int u,int v){ if(dist.at(u) < dist.at(v)) swap(u,v); int K = par.size(),diff = dist.at(u)-dist.at(v); for(int i=0; i<K; i++){ if(!(diff&(1<<i))) continue; u = par.at(i).at(u),diff -= 1<<i; } if(u == v) return u; for(int i=K-1; i>=0; i--){ if(par.at(i).at(u) == par.at(i).at(v)) continue; u = par.at(i).at(u),v = par.at(i).at(v); } return par.at(0).at(u); } int twodist(int u,int v){ int lca = LCA(u,v); return dist.at(u)+dist.at(v)-2*dist.at(lca); } SS twodist2(int u,int v){ SS costu = e(),costv = e(); if(dist.at(u) < dist.at(v)) swap(u,v); int K = par.size(),diff = dist.at(u)-dist.at(v); for(int i=0; i<K; i++){ if(!(diff&(1<<i))) continue; costu = op(costu,dist2.at(i).at(u)); u = par.at(i).at(u); diff -= 1<<i; } if(u == v) return op(costu,costv); for(int i=K-1; i>=0; i--){ if(par.at(i).at(u) == par.at(i).at(v)) continue; costu = op(costu,dist2.at(i).at(u)),costv = op(costv,dist2.at(i).at(v)); u = par.at(i).at(u),v = par.at(i).at(v); } costu = op(costu,dist2.at(0).at(u)),costv = op(costv,dist2.at(0).at(v)); return op(costu,costv); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long N,K,C; cin >> N >> K >> C; vector<tuple<int,int,int,int>> WPUV(K); for(auto &[w,p,u,v] : WPUV) cin >> u >> v >> w >> p,u--,v--; sort(WPUV.begin(),WPUV.end()); int answer = 0; long long cost = 0; UnionFind UF; UF.make(N); vector<vector<pair<int,int>>> Graph(N); for(int i=0; i<K; i++){ auto[w,p,u,v] = WPUV.at(i); if(UF.issame(u,v)) continue; UF.unite(u,v); cost += w; answer = max(answer,p); Graph.at(u).push_back({v,w}); Graph.at(v).push_back({u,w}); } if(cost > C){cout << -1 << endl; return 0;} LowestCommonAncestor Z; Z.make(Graph,0); for(int i=0; i<K; i++){ auto[w,p,u,v] = WPUV.at(i); if(p <= answer) continue; int maxw = Z.twodist2(u,v); if(cost+w-maxw <= C) answer = p; } cout << answer << endl; }