結果
問題 | No.2642 Don't cut line! |
ユーザー | tnakao0123 |
提出日時 | 2024-02-21 10:41:15 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 97 ms / 4,000 ms |
コード長 | 3,786 bytes |
コンパイル時間 | 1,108 ms |
コンパイル使用メモリ | 112,548 KB |
実行使用メモリ | 25,880 KB |
最終ジャッジ日時 | 2024-09-29 04:12:51 |
合計ジャッジ時間 | 4,204 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
11,844 KB |
testcase_01 | AC | 97 ms
25,752 KB |
testcase_02 | AC | 94 ms
25,860 KB |
testcase_03 | AC | 95 ms
25,648 KB |
testcase_04 | AC | 92 ms
25,880 KB |
testcase_05 | AC | 92 ms
25,824 KB |
testcase_06 | AC | 39 ms
12,780 KB |
testcase_07 | AC | 39 ms
10,160 KB |
testcase_08 | AC | 37 ms
9,968 KB |
testcase_09 | AC | 38 ms
9,888 KB |
testcase_10 | AC | 40 ms
13,244 KB |
testcase_11 | AC | 38 ms
11,648 KB |
testcase_12 | AC | 38 ms
10,412 KB |
testcase_13 | AC | 38 ms
10,180 KB |
testcase_14 | AC | 38 ms
10,032 KB |
testcase_15 | AC | 39 ms
12,908 KB |
testcase_16 | AC | 60 ms
12,356 KB |
testcase_17 | AC | 76 ms
25,052 KB |
testcase_18 | AC | 84 ms
25,296 KB |
testcase_19 | AC | 59 ms
23,792 KB |
testcase_20 | AC | 41 ms
15,780 KB |
testcase_21 | AC | 38 ms
9,032 KB |
testcase_22 | AC | 42 ms
10,816 KB |
testcase_23 | AC | 96 ms
25,584 KB |
testcase_24 | AC | 42 ms
18,572 KB |
testcase_25 | AC | 45 ms
18,124 KB |
testcase_26 | AC | 46 ms
13,348 KB |
testcase_27 | AC | 60 ms
21,328 KB |
testcase_28 | AC | 82 ms
25,208 KB |
testcase_29 | AC | 42 ms
17,792 KB |
testcase_30 | AC | 41 ms
18,136 KB |
testcase_31 | AC | 67 ms
24,368 KB |
testcase_32 | AC | 42 ms
15,880 KB |
testcase_33 | AC | 3 ms
8,200 KB |
testcase_34 | AC | 3 ms
8,216 KB |
testcase_35 | AC | 2 ms
7,748 KB |
コンパイルメッセージ
main.cpp: In function 'int main()': main.cpp:138:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 138 | auto [w, p, u, v] = es[i]; | ^ main.cpp:157:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 157 | auto [v, w] = nbru[cis[u]++]; | ^ main.cpp:181:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 181 | auto [w, p, u, v] = es[i]; | ^
ソースコード
/* -*- coding: utf-8 -*- * * 2642.cc: No.2642 Don't cut line! - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> #include<tuple> using namespace std; /* constant */ const int MAX_N = 100000; const int BN = 17; const int MAX_K = 100000; const int MAX_P = 100000; const int INF = 1 << 30; const long long LINF = 1LL << 60; /* typedef */ typedef long long ll; typedef queue<int> qi; typedef pair<int,int> pii; typedef vector<pii> vpii; typedef tuple<int,int,int,int> tp4; struct UFT { vector<int> links, ranks, sizes; UFT() {} void init(int n) { links.resize(n); for (int i = 0; i < n; i++) links[i] = i; ranks.assign(n, 1); sizes.assign(n, 1); } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ tp4 es[MAX_K]; bool used[MAX_K]; UFT uft; vpii nbrs[MAX_N]; int ps[MAX_N][BN], pws[MAX_N][BN], ds[MAX_N], cis[MAX_N]; /* subroutines */ int find_maxw(int u, int v) { if (ds[u] > ds[v]) swap(u, v); int maxw = 0; for (int i = BN - 1; i >= 0; i--) if (((ds[v] - ds[u]) >> i) & 1) { maxw = max(maxw, pws[v][i]); v = ps[v][i]; } if (u == v) return maxw; for(int i = BN - 1; i >= 0; i--) if (ps[u][i] != ps[v][i]) { maxw = max(maxw, max(pws[u][i], pws[v][i])); u = ps[u][i], v = ps[v][i]; } return max(maxw, max(pws[u][0], pws[v][0])); } /* main */ int main() { int n, k; ll c; scanf("%d%d%lld", &n, &k, &c); for (int i = 0; i < k; i++) { int u, v, w, p; scanf("%d%d%d%d", &u, &v, &w, &p); u--, v--; es[i] = {w, p, u, v}; } sort(es, es + k); uft.init(n); int maxp = 0; ll wsum = 0; for (int i = 0; i < k; i++) { auto [w, p, u, v] = es[i]; if (! uft.same(u, v)) { used[i] = true; uft.merge(u, v); maxp = max(maxp, p); wsum += w; nbrs[u].push_back({v, w}); nbrs[v].push_back({u, w}); } } if (wsum > c) { puts("-1"); return 0; } //printf("maxp=%d, wsum=%lld\n", maxp, wsum); ps[0][0] = -1, pws[0][0] = 0, ds[0] = 0; for (int u = 0; u >= 0;) { auto &nbru = nbrs[u]; int up = ps[u][0]; if (cis[u] < nbru.size()) { auto [v, w] = nbru[cis[u]++]; if (v != up) { ps[v][0] = u, pws[v][0] = w, ds[v] = ds[u] + 1; u = v; } } else u = up; } for (int i = 0; i < BN - 1; i++) for (int u = 0; u < n; u++) { if (ps[u][i] >= 0) { ps[u][i + 1] = ps[ps[u][i]][i]; pws[u][i + 1] = max(pws[u][i], pws[ps[u][i]][i]); } else { ps[u][i + 1] = -1; pws[u][i + 1] = 0; } } for (int i = 0; i < k; i++) if (! used[i]) { auto [w, p, u, v] = es[i]; if (maxp < p) { int mw = find_maxw(u, v); if (wsum + w - mw <= c) maxp = p; //printf("%d,%d: w=%d, p=%d :wsum %lld->%lld\n", //u, v, w, p, wsum, wsum + w - mw); } } printf("%d\n", maxp); return 0; }