結果
問題 | No.2642 Don't cut line! |
ユーザー | SSRS |
提出日時 | 2024-02-19 21:58:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 161 ms / 4,000 ms |
コード長 | 2,751 bytes |
コンパイル時間 | 2,627 ms |
コンパイル使用メモリ | 226,912 KB |
実行使用メモリ | 25,580 KB |
最終ジャッジ日時 | 2024-09-29 03:32:12 |
合計ジャッジ時間 | 7,454 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 152 ms
25,456 KB |
testcase_02 | AC | 161 ms
25,580 KB |
testcase_03 | AC | 156 ms
25,452 KB |
testcase_04 | AC | 161 ms
25,452 KB |
testcase_05 | AC | 161 ms
25,452 KB |
testcase_06 | AC | 102 ms
6,816 KB |
testcase_07 | AC | 107 ms
6,820 KB |
testcase_08 | AC | 104 ms
6,820 KB |
testcase_09 | AC | 104 ms
6,816 KB |
testcase_10 | AC | 106 ms
6,820 KB |
testcase_11 | AC | 102 ms
6,820 KB |
testcase_12 | AC | 107 ms
6,820 KB |
testcase_13 | AC | 112 ms
6,820 KB |
testcase_14 | AC | 111 ms
6,820 KB |
testcase_15 | AC | 111 ms
6,816 KB |
testcase_16 | AC | 127 ms
10,752 KB |
testcase_17 | AC | 112 ms
22,512 KB |
testcase_18 | AC | 129 ms
24,124 KB |
testcase_19 | AC | 89 ms
18,744 KB |
testcase_20 | AC | 91 ms
10,496 KB |
testcase_21 | AC | 96 ms
6,816 KB |
testcase_22 | AC | 99 ms
8,704 KB |
testcase_23 | AC | 151 ms
25,236 KB |
testcase_24 | AC | 84 ms
14,068 KB |
testcase_25 | AC | 83 ms
12,800 KB |
testcase_26 | AC | 109 ms
7,680 KB |
testcase_27 | AC | 95 ms
17,576 KB |
testcase_28 | AC | 136 ms
23,560 KB |
testcase_29 | AC | 103 ms
9,088 KB |
testcase_30 | AC | 94 ms
11,768 KB |
testcase_31 | AC | 115 ms
20,464 KB |
testcase_32 | AC | 99 ms
10,752 KB |
testcase_33 | AC | 2 ms
6,816 KB |
testcase_34 | AC | 1 ms
6,820 KB |
testcase_35 | AC | 2 ms
6,820 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; const int LOG = 17; struct unionfind{ vector<int> p; unionfind(int N): p(N, -1){ } int root(int x){ if (p[x] == -1){ return x; } else { p[x] = root(p[x]); return p[x]; } } bool same(int x, int y){ return root(x) == root(y); } void unite(int x, int y){ x = root(x); y = root(y); if (x != y){ p[x] = y; } } }; int main(){ int N, K; long long C; cin >> N >> K >> C; vector<tuple<int, int, int, int>> E(K); for (int i = 0; i < K; i++){ int u, v, w, p; cin >> u >> v >> w >> p; u--; v--; E[i] = make_tuple(w, u, v, p); } sort(E.begin(), E.end()); vector<vector<pair<int, int>>> E2(N); unionfind UF(N); long long S = 0; for (int i = 0; i < K; i++){ int u = get<1>(E[i]); int v = get<2>(E[i]); if (!UF.same(u, v)){ int w = get<0>(E[i]); S += w; UF.unite(u, v); E2[u].push_back(make_pair(w, v)); E2[v].push_back(make_pair(w, u)); } } if (S > C){ cout << -1 << endl; } else { vector<int> p(N, -1); vector<int> pw(N, 0); vector<int> d(N, 0); queue<int> Q; Q.push(0); while (!Q.empty()){ int v = Q.front(); Q.pop(); for (pair<int, int> P : E2[v]){ int w = P.second; if (w != p[v]){ p[w] = v; d[w] = d[v] + 1; pw[w] = P.first; Q.push(w); } } } vector<vector<int>> pp(LOG, vector<int>(N, -1)); vector<vector<int>> bl(LOG, vector<int>(N, 0)); for (int i = 0; i < N; i++){ pp[0][i] = p[i]; bl[0][i] = pw[i]; } for (int i = 0; i < LOG - 1; i++){ for (int j = 0; j < N; j++){ if (pp[i][j] != -1){ pp[i + 1][j] = pp[i][pp[i][j]]; bl[i + 1][j] = max(bl[i][j], bl[i][pp[i][j]]); } } } auto get_max = [&](int u, int v){ if (d[u] > d[v]){ swap(u, v); } int ans = 0; for (int i = 0; i < LOG; i++){ if (((d[v] - d[u]) >> i & 1) == 1){ ans = max(ans, bl[i][v]); v = pp[i][v]; } } if (u == v){ return ans; } for (int i = LOG - 1; i >= 0; i--){ if (pp[i][u] != pp[i][v]){ ans = max(ans, bl[i][u]); ans = max(ans, bl[i][v]); u = pp[i][u]; v = pp[i][v]; } } ans = max(ans, pw[u]); ans = max(ans, pw[v]); return ans; }; int ans = 0; for (int i = 0; i < K; i++){ int w = get<0>(E[i]); int u = get<1>(E[i]); int v = get<2>(E[i]); if (S - get_max(u, v) + w <= C){ ans = max(ans, get<3>(E[i])); } } cout << ans << endl; } }