diff --git a/S2OJ/1586/1586.cpp b/S2OJ/1586/1586.cpp new file mode 100644 index 00000000..297867fb --- /dev/null +++ b/S2OJ/1586/1586.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int INF = 0x3f3f3f3f; + +class DSU { + private: + std::vector fa; + + public: + DSU(int _n = 0) + : fa(_n) { + std::iota(fa.begin(), fa.end(), 0); + } + + int find(int x) { + return fa[x] == x ? x : fa[x] = find(fa[x]); + } + + bool merge(int x, int y) { + x = find(x); + y = find(y); + + fa[x] = y; + + return x != y; + } +}; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + + cin >> t; + + while (t--) { + int n, m; + + cin >> n >> m; + + std::vector a(n), b(n); + std::vector> edges; + std::vector f(1 << n, false); + + for (int i = 0, u, v, w; i < m; i++) { + cin >> u >> v >> w; + + edges.emplace_back(u - 1, v - 1, w); + } + + for (int i = 0; i < n; i++) { + cin >> a[i] >> b[i]; + } + + auto check = [&](int s) -> bool { + auto in_block = [&](int x) -> bool { + return s >> x & 1; + }; + + int sum = 0, root = 0; + DSU dsu(n); + std::vector> block_edges; + + for (auto& edge : edges) { + int u, v; + + std::tie(u, v, std::ignore) = edge; + + if (in_block(u) && in_block(v)) { + block_edges.emplace_back(edge); + } + } + + std::sort(block_edges.begin(), block_edges.end(), [](auto& lhs, auto& rhs) { + return std::get<2>(lhs) < std::get<2>(rhs); + }); + + for (int i = 0; i < n; i++) { + if (in_block(i)) sum += a[i] - b[i]; + } + + for (auto& edge : block_edges) { + int u, v, w; + + std::tie(u, v, w) = edge; + + if (dsu.merge(u, v)) sum -= w; + } + + for (int i = 0; i < n; i++) { + if (in_block(i) && dsu.find(i) == i) { + root = i; + } + } + + for (int i = 0; i < n; i++) { + if (in_block(i) && dsu.find(i) != root) { + return false; + } + } + + return sum >= 0; + }; + + f[0] = true; + for (int s = 1; s < 1 << n; s++) { + f[s] = check(s); + + for (int i = s; i; i = (i - 1) & s) { + f[s] = f[s] || f[i] && f[s ^ i]; + } + } + + cout << (f[(1 << n) - 1] ? "Yes" : "No") << endl; + } + + return 0; +} diff --git a/S2OJ/1586/data/ex_trans1.ans b/S2OJ/1586/data/ex_trans1.ans new file mode 100644 index 00000000..9ff07567 --- /dev/null +++ b/S2OJ/1586/data/ex_trans1.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c0ad16096fbde1a1d8a06dc33088e1430035c91a772a6e275093bf9d98909dc9 +size 7 diff --git a/S2OJ/1586/data/ex_trans1.in b/S2OJ/1586/data/ex_trans1.in new file mode 100644 index 00000000..745beadb --- /dev/null +++ b/S2OJ/1586/data/ex_trans1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b4f6ff72d0dc591a50e07223687c138da541db61c2c36648c466814b46d66fa7 +size 70 diff --git a/S2OJ/1586/data/ex_trans2.ans b/S2OJ/1586/data/ex_trans2.ans new file mode 100644 index 00000000..de4ec137 --- /dev/null +++ b/S2OJ/1586/data/ex_trans2.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:beba6e067712c15ccc3840937d9790da2be7cc9204f31fc16c0de87d4b58f24e +size 33 diff --git a/S2OJ/1586/data/ex_trans2.in b/S2OJ/1586/data/ex_trans2.in new file mode 100644 index 00000000..f71f7bef --- /dev/null +++ b/S2OJ/1586/data/ex_trans2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eff066f42098fdaba572726fcf3408972a87e3c8491cd7584f4d3391c599fbff +size 7452 diff --git a/S2OJ/1586/data/ex_trans3.ans b/S2OJ/1586/data/ex_trans3.ans new file mode 100644 index 00000000..3b9acd11 --- /dev/null +++ b/S2OJ/1586/data/ex_trans3.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c18306e9f1c60bc6219d938797a5ff7540109aa9a056a634c567a777428edb36 +size 36 diff --git a/S2OJ/1586/data/ex_trans3.in b/S2OJ/1586/data/ex_trans3.in new file mode 100644 index 00000000..15df81ff --- /dev/null +++ b/S2OJ/1586/data/ex_trans3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:23036b50d1437744edf64753b2724a66b9b7649e939954a5b9bb40b00fb5ad46 +size 2844 diff --git a/S2OJ/1586/data/ex_trans4.ans b/S2OJ/1586/data/ex_trans4.ans new file mode 100644 index 00000000..0f98cb7c --- /dev/null +++ b/S2OJ/1586/data/ex_trans4.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ec66decbb1363e1bb3692af8077a65445d2c6173d8e95a39a7c20464b81f9aa1 +size 35 diff --git a/S2OJ/1586/data/ex_trans4.in b/S2OJ/1586/data/ex_trans4.in new file mode 100644 index 00000000..cf5b8eb0 --- /dev/null +++ b/S2OJ/1586/data/ex_trans4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d57b555a250d68a9c7cc52e5322d21659a9fc742b166f8db69132ea3513e25ff +size 10156 diff --git a/S2OJ/1586/data/problem.conf b/S2OJ/1586/data/problem.conf new file mode 100644 index 00000000..888b4bff --- /dev/null +++ b/S2OJ/1586/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6a5b6078e7de2852a4e948b085d9205412084e90d9bda520f4744cb7e4f92d9 +size 179 diff --git a/S2OJ/1586/data/trans1.ans b/S2OJ/1586/data/trans1.ans new file mode 100644 index 00000000..ceae890c --- /dev/null +++ b/S2OJ/1586/data/trans1.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b1168518879afd2a5d4ac1a2c665d3695205e3f38b61cd56e30819ede753b7cf +size 34 diff --git a/S2OJ/1586/data/trans1.in b/S2OJ/1586/data/trans1.in new file mode 100644 index 00000000..d12b04dc --- /dev/null +++ b/S2OJ/1586/data/trans1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0aea7bb4a476d51fe46f42873997cb6652e6eedc33743abc8d5cdf3b47716388 +size 6818 diff --git a/S2OJ/1586/data/trans10.ans b/S2OJ/1586/data/trans10.ans new file mode 100644 index 00000000..9d5171a8 --- /dev/null +++ b/S2OJ/1586/data/trans10.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:50c5290e940a445f65ed389316a116df8145547deab30c8184ee2b1ccfeb7a0c +size 35 diff --git a/S2OJ/1586/data/trans10.in b/S2OJ/1586/data/trans10.in new file mode 100644 index 00000000..c7d16c64 --- /dev/null +++ b/S2OJ/1586/data/trans10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bd8ea0585c1c0f666c9e126cfe074f1f9d937252a7a45cd07b21332b26242475 +size 11118 diff --git a/S2OJ/1586/data/trans2.ans b/S2OJ/1586/data/trans2.ans new file mode 100644 index 00000000..381bef6e --- /dev/null +++ b/S2OJ/1586/data/trans2.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bb86fbab930e374a7447bf44d01756e65691818b6e62184ed1f5f37e623040c8 +size 37 diff --git a/S2OJ/1586/data/trans2.in b/S2OJ/1586/data/trans2.in new file mode 100644 index 00000000..60e4cd2c --- /dev/null +++ b/S2OJ/1586/data/trans2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a749921b10be33d357a19c8e57b065efa2bb55ea9226ea215cf65ce31a92a66d +size 5883 diff --git a/S2OJ/1586/data/trans3.ans b/S2OJ/1586/data/trans3.ans new file mode 100644 index 00000000..33713994 --- /dev/null +++ b/S2OJ/1586/data/trans3.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ec817212b5ea00890339916a5fb9de505261c40b6f9d8ee9458637a8411d95c +size 32 diff --git a/S2OJ/1586/data/trans3.in b/S2OJ/1586/data/trans3.in new file mode 100644 index 00000000..b56a2fd1 --- /dev/null +++ b/S2OJ/1586/data/trans3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d5b73d4392f7548b4bcbfafac95dd767c87daff2a162d1a81bd0b4ab1d4ae880 +size 2642 diff --git a/S2OJ/1586/data/trans4.ans b/S2OJ/1586/data/trans4.ans new file mode 100644 index 00000000..4b4e649b --- /dev/null +++ b/S2OJ/1586/data/trans4.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bbc27c9fecc9f68f9393423d20cb2dfbfd0769a30c918bc85bcc346bed23f018 +size 35 diff --git a/S2OJ/1586/data/trans4.in b/S2OJ/1586/data/trans4.in new file mode 100644 index 00000000..52cea691 --- /dev/null +++ b/S2OJ/1586/data/trans4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cf53e21813497469512fbd8ac576ae24b0d3ce2f8b503797ed1bbe529c07ad82 +size 2842 diff --git a/S2OJ/1586/data/trans5.ans b/S2OJ/1586/data/trans5.ans new file mode 100644 index 00000000..aa6a3132 --- /dev/null +++ b/S2OJ/1586/data/trans5.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1223752cf52276fe492852b8dc4fd25d537754f829c2d01e9229e867ca2f0cc +size 35 diff --git a/S2OJ/1586/data/trans5.in b/S2OJ/1586/data/trans5.in new file mode 100644 index 00000000..3e3ad158 --- /dev/null +++ b/S2OJ/1586/data/trans5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bfbf014dad8909e6460b94bdbad4b206bd660580b8a37a4ef1ba77d7d7e1d4ce +size 2838 diff --git a/S2OJ/1586/data/trans6.ans b/S2OJ/1586/data/trans6.ans new file mode 100644 index 00000000..1d1a0664 --- /dev/null +++ b/S2OJ/1586/data/trans6.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7322e6f9cb28e747900ff9160b4d9505dc4b6ddd76a4769bb37a2fc18b7d6106 +size 36 diff --git a/S2OJ/1586/data/trans6.in b/S2OJ/1586/data/trans6.in new file mode 100644 index 00000000..0c9ca5f0 --- /dev/null +++ b/S2OJ/1586/data/trans6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:481c9baba4b3c945995996483928dc409473232e7d7d676b6fc6c506ad6e76db +size 9059 diff --git a/S2OJ/1586/data/trans7.ans b/S2OJ/1586/data/trans7.ans new file mode 100644 index 00000000..cca159f0 --- /dev/null +++ b/S2OJ/1586/data/trans7.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:db3961f5cead38ae9642645b8e1b55d59efc2dcff680f066138f76375f7baef5 +size 35 diff --git a/S2OJ/1586/data/trans7.in b/S2OJ/1586/data/trans7.in new file mode 100644 index 00000000..9e2d3a61 --- /dev/null +++ b/S2OJ/1586/data/trans7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3f9ca884b00cb9ead3af6e155e2830612853f4e9518e5d82f82e94886bd9b754 +size 10268 diff --git a/S2OJ/1586/data/trans8.ans b/S2OJ/1586/data/trans8.ans new file mode 100644 index 00000000..d7cde703 --- /dev/null +++ b/S2OJ/1586/data/trans8.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2ec744c2478c6e1377d76890ee154127fbc354d146d9aa5831dbd9f075d4b5ee +size 38 diff --git a/S2OJ/1586/data/trans8.in b/S2OJ/1586/data/trans8.in new file mode 100644 index 00000000..fb007930 --- /dev/null +++ b/S2OJ/1586/data/trans8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a41f7b068999d6eac58f8b32482ce0dfae1dbba454ca1d2af2742ab5c9b30a9d +size 9949 diff --git a/S2OJ/1586/data/trans9.ans b/S2OJ/1586/data/trans9.ans new file mode 100644 index 00000000..daa8c27e --- /dev/null +++ b/S2OJ/1586/data/trans9.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:910f7bbce72317f11d4f6cc305458de39b3272b91541611d74071472d03fd909 +size 34 diff --git a/S2OJ/1586/data/trans9.in b/S2OJ/1586/data/trans9.in new file mode 100644 index 00000000..191b0463 --- /dev/null +++ b/S2OJ/1586/data/trans9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab8a59fd92261156614facbb6e2aa7a99d8526a83787b16ae4f53e6eb95592a9 +size 11337