結果
問題 | No.364 門松木 |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-05-03 05:46:42 |
言語 | D (dmd 2.107.1) |
結果 |
AC
|
実行時間 | 88 ms / 3,000 ms |
コード長 | 2,907 bytes |
コンパイル時間 | 765 ms |
コンパイル使用メモリ | 119,856 KB |
実行使用メモリ | 28,168 KB |
最終ジャッジ日時 | 2023-09-04 01:02:13 |
合計ジャッジ時間 | 5,365 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,380 KB |
testcase_01 | AC | 1 ms
4,376 KB |
testcase_02 | AC | 1 ms
4,376 KB |
testcase_03 | AC | 1 ms
4,380 KB |
testcase_04 | AC | 1 ms
4,380 KB |
testcase_05 | AC | 1 ms
4,376 KB |
testcase_06 | AC | 1 ms
4,380 KB |
testcase_07 | AC | 1 ms
4,380 KB |
testcase_08 | AC | 80 ms
12,332 KB |
testcase_09 | AC | 80 ms
12,632 KB |
testcase_10 | AC | 78 ms
12,392 KB |
testcase_11 | AC | 79 ms
11,848 KB |
testcase_12 | AC | 74 ms
12,668 KB |
testcase_13 | AC | 68 ms
13,112 KB |
testcase_14 | AC | 66 ms
11,412 KB |
testcase_15 | AC | 67 ms
12,704 KB |
testcase_16 | AC | 68 ms
13,172 KB |
testcase_17 | AC | 80 ms
12,388 KB |
testcase_18 | AC | 81 ms
12,264 KB |
testcase_19 | AC | 82 ms
14,216 KB |
testcase_20 | AC | 79 ms
11,924 KB |
testcase_21 | AC | 78 ms
12,032 KB |
testcase_22 | AC | 58 ms
11,348 KB |
testcase_23 | AC | 82 ms
15,172 KB |
testcase_24 | AC | 85 ms
12,012 KB |
testcase_25 | AC | 82 ms
15,064 KB |
testcase_26 | AC | 87 ms
13,972 KB |
testcase_27 | AC | 81 ms
13,436 KB |
testcase_28 | AC | 88 ms
11,688 KB |
testcase_29 | AC | 86 ms
15,756 KB |
testcase_30 | AC | 84 ms
17,064 KB |
testcase_31 | AC | 2 ms
4,376 KB |
testcase_32 | AC | 2 ms
4,376 KB |
testcase_33 | AC | 84 ms
28,168 KB |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int N; int[] A; int[] X, Y; int[][] G; long[][] dp; void dfs(int u, int p) { foreach (v; G[u]) { if (v != p) { dfs(v, u); } } auto pss = new Tuple!(int, int)[][2]; foreach (v; G[u]) { if (v != p) { if (A[v] < A[u]) { pss[0] ~= tuple(A[v], v); } else if (A[v] > A[u]) { pss[1] ~= tuple(A[v], v); } } } foreach (h; 0 .. 2) { pss[h].sort; } auto psLens = pss.map!(ps => cast(int)(ps.length)).array; // not using p foreach (h; 0 .. 2) { long sum, cnt; for (int i = 0, j; i < psLens[h]; i = j) { long mx; for (; j < psLens[h] && pss[h][i][0] == pss[h][j][0]; ++j) { chmax(mx, dp[1][pss[h][j][1]]); } sum += mx; ++cnt; } chmax(dp[0][u], sum + cnt * (cnt - 1) / 2); } // using p if (p != -1) { const h = (A[p] < A[u]) ? 0 : 1; long sum, cnt = 1; for (int i = 0, j; i < psLens[h]; i = j) { long mx; for (; j < psLens[h] && pss[h][i][0] == pss[h][j][0]; ++j) { chmax(mx, dp[1][pss[h][j][1]]); } if (A[p] != pss[h][i][0]) { sum += mx; ++cnt; } } dp[1][u] = sum + cnt * (cnt - 1) / 2; } } void main() { try { for (; ; ) { N = readInt(); A = new int[N]; foreach (u; 0 .. N) { A[u] = readInt(); } X = new int[N - 1]; Y = new int[N - 1]; foreach (i; 0 .. N - 1) { X[i] = readInt() - 1; Y[i] = readInt() - 1; } G = new int[][N]; foreach (i; 0 .. N - 1) { G[X[i]] ~= Y[i]; G[Y[i]] ~= X[i]; } dp = new long[][](2, N); dfs(0, -1); const ans = dp[0].maxElement; writeln(ans); } } catch (EOFException e) { } }