#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; // 最大重み一般マッチング #include #include using namespace boost; typedef property> EdgeProperty; typedef adjacency_list my_graph; void No() { puts("-1"); exit(0); } int main() { int N, M; cin >> N >> M; constexpr int inf = 1 << 20; vector dist(N, vector(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 vs; for (int i = 0, ci; cin >> ci, i < N; ++i) { if (ci) vs.push_back(i); } const int n_vertices = vs.size(); graph_traits::vertex_iterator vi, vi_end; 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); } } graph_traits::vertex_descriptor mate1(n_vertices); maximum_weighted_matching(g, &mate1); auto sz = matching_size(g, &mate1); int ret = matching_weight_sum(g, &mate1); if (sz * 2 < n_vertices) No(); ret = N * vs.size() / 2 - ret; cout << ret << endl; }