diff --git a/Luogu/P3806/P3806.cpp b/Luogu/P3806/P3806.cpp new file mode 100644 index 00000000..7b422718 --- /dev/null +++ b/Luogu/P3806/P3806.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e4 + 5, + K = 1e8 + 5; + +int n, m, query[N]; +int root, siz[N], max[N]; +int cnt, dist[N]; +bool vis[N], exists[K], ans[N]; +std::vector> g[N]; + +void find(int u, int fa, int tot) { + siz[u] = 1; + max[u] = 0; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (v == fa || vis[v]) continue; + + find(v, u, tot); + + siz[u] += siz[v]; + max[u] = std::max(max[u], siz[v]); + } + + max[u] = std::max(max[u], tot - siz[u]); + + if (max[u] < max[root]) root = u; +} + +void dis(int u, int fa, int sum) { + dist[++cnt] = sum; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (v == fa || vis[v]) continue; + + dis(v, u, sum + w); + } +} + +void calc(int u) { + std::queue q; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (vis[v]) continue; + + cnt = 0; + dis(v, u, w); + + for (int i = 1; i <= cnt; i++) { + for (int j = 1; j <= m; j++) { + if (query[j] >= dist[i]) { + ans[j] |= exists[query[j] - dist[i]]; + } + } + } + + for (int i = 1; i <= cnt; i++) { + q.push(dist[i]); + exists[dist[i]] = true; + } + } + + while (!q.empty()) { + exists[q.front()] = false; + q.pop(); + } +} + +void solve(int u) { + vis[u] = true; + exists[0] = true; + calc(u); + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (vis[v]) continue; + + max[root = 0] = std::numeric_limits::max(); + find(v, 0, siz[v]); + solve(root); + } +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n >> m; + + for (int i = 1, u, v, w; i < n; i++) { + cin >> u >> v >> w; + + g[u].push_back(std::make_pair(v, w)); + g[v].push_back(std::make_pair(u, w)); + } + + for (int i = 1; i <= m; i++) { + cin >> query[i]; + } + + cnt = 0; + max[root = 0] = n; + find(1, 0, n); + solve(root); + + for (int i = 1; i <= m; i++) { + cout << (ans[i] ? "AYE" : "NAY") << endl; + } + + return 0; +} diff --git a/Luogu/P3806/data/P3806_1.in b/Luogu/P3806/data/P3806_1.in new file mode 100644 index 00000000..758e221c --- /dev/null +++ b/Luogu/P3806/data/P3806_1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5ef7efad1de5ec30fa6a1a01926e2f783427b562b597713f64d57a85b4279975 +size 1334 diff --git a/Luogu/P3806/data/P3806_1.out b/Luogu/P3806/data/P3806_1.out new file mode 100644 index 00000000..ad85f7c3 --- /dev/null +++ b/Luogu/P3806/data/P3806_1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3be4c9f44ec460f75dee856bade3faa83f935bdfaf6c59589bd2cc280113d8f6 +size 250 diff --git a/Luogu/P3806/data/P3806_2.in b/Luogu/P3806/data/P3806_2.in new file mode 100644 index 00000000..36a0a0dd --- /dev/null +++ b/Luogu/P3806/data/P3806_2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5c11e8f8312857169cc1cf5e4c43117a7da20528fe3f9f5c947bc55ad377c6ea +size 1433 diff --git a/Luogu/P3806/data/P3806_2.out b/Luogu/P3806/data/P3806_2.out new file mode 100644 index 00000000..b30c2990 --- /dev/null +++ b/Luogu/P3806/data/P3806_2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fc788fb6a6240cc903c6cd8d7c5d3c789832afe839d699635af33f94bb498289 +size 250 diff --git a/Luogu/P3806/data/P3806_7.in b/Luogu/P3806/data/P3806_7.in new file mode 100644 index 00000000..96109de7 --- /dev/null +++ b/Luogu/P3806/data/P3806_7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eec1a9dc4e020ef32459cb2b4aa08646b2bd2d6686adf75002072f9eadf53eb8 +size 144238 diff --git a/Luogu/P3806/data/P3806_7.out b/Luogu/P3806/data/P3806_7.out new file mode 100644 index 00000000..4c3777ef --- /dev/null +++ b/Luogu/P3806/data/P3806_7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1d898d0ce5a2b48d17ff94268a4bb811f2c4e125a1716ad13803f2242d80ac9d +size 500