結果

問題 No.2236 Lights Out On Simple Graph
ユーザー hitonanode
提出日時 2023-03-11 10:54:42
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 4,000 ms
コード長 1,660 bytes
コンパイル時間 10,040 ms
コンパイル使用メモリ 450,912 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-18 06:16:31
合計ジャッジ時間 12,722 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
using namespace std;
//
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/maximum_weighted_matching.hpp>
using EdgeProperty = boost::property<boost::edge_weight_t, long long, boost::property<boost::edge_index_t, int>>;
using my_graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeProperty>;
void No() {
puts("-1");
exit(0);
}
int main() {
int N, M;
cin >> N >> M;
constexpr int inf = 1 << 20;
vector dist(N, vector<int>(N, inf));
for (int i = 0; i < N; ++i) dist.at(i).at(i) = 0;
while (M--) {
int a, b;
cin >> a >> b, --a, --b, dist.at(a).at(b) = dist.at(b).at(a) = 1;
}
for (int k = 0; k < N; ++k) for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) dist.at(i).at(j) = min(dist.at(i).at(j), dist.at(i).at(k) + dist.at(k).at(j));
}
vector<int> vs;
for (int i = 0, ci; cin >> ci, i < N; ++i) {
if (ci) vs.push_back(i);
}
const int n_vertices = vs.size();
if (n_vertices % 2) No();
my_graph g(n_vertices);
for (int i = 0; i < n_vertices; ++i) {
for (int j = 0; j < i; ++j) {
if (auto w = dist.at(vs.at(i)).at(vs.at(j)); w < inf) add_edge(i, j, EdgeProperty(N - w), g);
}
}
vector<boost::graph_traits<my_graph>::vertex_descriptor> mate1(n_vertices);
maximum_weighted_matching(g, &mate1[0]);
auto sz = matching_size(g, &mate1[0]);
auto ret = matching_weight_sum(g, &mate1[0]);
if (sz * 2 < n_vertices) No();
cout << N * vs.size() / 2 - ret << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0