結果
| 問題 |
No.2642 Don't cut line!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-02-20 15:34:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,991 bytes |
| コンパイル時間 | 1,255 ms |
| コンパイル使用メモリ | 99,432 KB |
| 最終ジャッジ日時 | 2025-02-19 18:08:59 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 RE * 14 |
ソースコード
#include<iostream>
#include<algorithm>
#include<vector>
#include<tuple>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}
struct UnionFind {
private:
vector<int> par, siz;
public:
UnionFind(int n) : par(n, -1), siz(n, 1) {}
int find(int x) {
if (par[x] == -1) return x;
else {
par[x] = find(par[x]);
return par[x];
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return siz[find(x)];
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
};
const ll INF=1LL<<60;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,K,C;
cin>>N>>K>>C;
vector<tuple<ll,int,int,int>>vt;
for(int i=0;i<K;i++){
int u,v,p;
ll w;
cin>>u>>v>>w>>p;
u--;
v--;
vt.push_back(make_tuple(w,u,v,p));
}
UnionFind uf(N);
ll cost=0;
int ans=0;
vector<tuple<ll,int,int,int>>vt2;
vector<vector<pair<int,ll>>>G(N);
sort(all(vt));
for(auto[w,u,v,p]:vt){
if(uf.same(u,v)){
vt2.push_back(make_tuple(w,u,v,p));
}else{
uf.merge(u,v);
cost+=w;
chmax(ans,p);
G[u].push_back(make_pair(v,w));
G[v].push_back(make_pair(u,w));
}
}
if(cost>C){
cout<<-1<<"\n";
return 0;
}
vector<int>D(N);
vector P(20,vector<int>(N,-1));
vector M(20,vector<ll>(N,-INF));
auto dfs=[&](auto dfs,int x,int p,ll w)-> void {
P[0][x]=p;
M[0][x]=w;
for(int i=1;i<20;i++){
if(P[i-1][x]==-1)break;
P[i][x]=P[i-1][P[i-1][x]];
}
for(int i=1;i<20;i++){
if(P[i-1][x]==-1)M[i][x]=M[i-1][x];
else M[i][x]=max(M[i-1][x],M[i-1][P[i-1][x]]);
}
for(auto[i,e]:G[x]){
if(i==p)continue;
D[i]=D[x]+1;
dfs(dfs,i,x,e);
}
};
dfs(dfs,0,-1,-INF);
for(auto[w,u,v,p]:vt2){
if(D[u]>D[v])swap(u,v);
ll mx=-INF;
for(int i=0;i<20;i++){
if((D[v]-D[u])>>i&1){
chmax(mx,M[i][v]);
v=P[i][v];
}
}
if(u!=v){
for(int i=19;i>=0;i--){
if(P[i][u]!=P[i][v]){
chmax(mx,max(M[i][u],M[i][v]));
u=P[i][u];
v=P[i][v];
}
}
}
chmax(mx,max(M[0][u],M[0][v]));
if(cost-mx+w<=C)chmax(ans,p);
}
cout<<ans<<"\n";
}