結果

問題 No.2236 Lights Out On Simple Graph
ユーザー t98slidert98slider
提出日時 2023-03-03 22:31:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 10,466 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 203,376 KB
実行使用メモリ 77,940 KB
最終ジャッジ日時 2023-10-18 02:40:41
合計ジャッジ時間 8,230 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define codefor int test;cin>>test;while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
template<class T> istream& operator>>(istream& is, vector<T>& vec) {for(T& x : vec)is >> x;return is;}
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) {if(vec.empty())return os;os << vec[0];for(auto it = vec.begin(); ++it!= vec.end();)os << ' ' << *it;return os;}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){cin >> head;in(tail...);}
void out(){cout << '\n';}
template<class T>void out(const T& a){cout << a << '\n';}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){cout << head << ' ';out(tail...);}
const int INF = 1 << 30;
const long long INF2 = 1ll << 60;
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

struct LCA_tree{
    int _n,MAX_LOG_V,root;
    vector<vector<int>> g;
    vector<vector<int>> parent;
    vector<int> depth;
    LCA_tree() : _n(0) {}
    LCA_tree(int n) : _n(n), g(n),depth(n) {
        MAX_LOG_V = 1;
        while(_n >> MAX_LOG_V) MAX_LOG_V++;
        parent.resize(MAX_LOG_V, vector<int>(_n));
    }
    void merge(int u, int v){
        g[u].push_back(v);
        g[v].push_back(u);
    }
    void dfs(int v,int p,int d){
        parent[0][v]=p;
        depth[v]=d;
        for(int i=0;i<g[v].size();i++){
            if(g[v][i]!=p)dfs(g[v][i],v,d+1);
        }
    }
    void init(int r){
        root=r;
        dfs(root,-1,0);
        for(int j=0;j+1<MAX_LOG_V;j++){
            for(int i=0;i<_n;i++){
                if(parent[j][i]<0)parent[j+1][i]=-1;
                else parent[j+1][i]=parent[j][parent[j][i]];
            }
        }
    }
    int lca(int u,int v){
        if(depth[u]>depth[v])swap(u,v);
        for(int i=0;i<MAX_LOG_V;i++){
            if((depth[v]-depth[u])>>i&1)v=parent[i][v];
        }
        if(u==v)return u;
        for(int i=MAX_LOG_V-1;i>=0;i--){
            if(parent[i][u]!=parent[i][v]){
                u=parent[i][u];
                v=parent[i][v];
            }
        }
        return parent[0][u];
    }
    //パスの辺数
    int dist(int u,int v){
        int lcav=lca(u,v);
        if(lcav==-1)return depth[u]+depth[v];
        return depth[u]+depth[v]-2*depth[lcav];
    }
    //頂点wが頂点u,vのパス上に存在するか
    int on_path(int u,int v,int w){
        return (dist(u,w)+dist(v,w)==dist(u,v));
    }
};


template <class Cap, class Cost> struct mcf_graph {
  public:
    mcf_graph() {}
    mcf_graph(int n) : _n(n), g(n) {}
    int add_edge(int from, int to, Cap cap, Cost cost) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        g[from].push_back(_edge{to, int(g[to].size()), cap, cost});
        g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost});
        return m;
    }
    struct edge {
        int from, to;
        Cap cap, flow;
        Cost cost;
    };
    edge get_edge(int i) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{
            pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost,
        };
    }
    std::vector<edge> edges() {
        int m = int(pos.size());
        std::vector<edge> result(m);
        for (int i = 0; i < m; i++) {
            result[i] = get_edge(i);
        }
        return result;
    }
    std::pair<Cap, Cost> flow(int s, int t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {
        return slope(s, t, flow_limit).back();
    }
    std::vector<std::pair<Cap, Cost>> slope(int s, int t) {
        return slope(s, t, std::numeric_limits<Cap>::max());
    }
    std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);
        std::vector<Cost> dual(_n, 0), dist(_n);
        std::vector<int> pv(_n), pe(_n);
        std::vector<bool> vis(_n);
        auto dual_ref = [&]() {
            std::fill(dist.begin(), dist.end(),
                      std::numeric_limits<Cost>::max());
            std::fill(pv.begin(), pv.end(), -1);
            std::fill(pe.begin(), pe.end(), -1);
            std::fill(vis.begin(), vis.end(), false);
            struct Q {
                Cost key;
                int to;
                bool operator<(Q r) const { return key > r.key; }
            };
            std::priority_queue<Q> que;
            dist[s] = 0;
            que.push(Q{0, s});
            while (!que.empty()) {
                int v = que.top().to;
                que.pop();
                if (vis[v]) continue;
                vis[v] = true;
                if (v == t) break;
                for (int i = 0; i < int(g[v].size()); i++) {
                    auto e = g[v][i];
                    if (vis[e.to] || !e.cap) continue;
                    Cost cost = e.cost - dual[e.to] + dual[v];
                    if (dist[e.to] - dist[v] > cost) {
                        dist[e.to] = dist[v] + cost;
                        pv[e.to] = v;
                        pe[e.to] = i;
                        que.push(Q{dist[e.to], e.to});
                    }
                }
            }
            if (!vis[t]) {
                return false;
            }
            for (int v = 0; v < _n; v++) {
                if (!vis[v]) continue;
                dual[v] -= dist[t] - dist[v];
            }
            return true;
        };
        Cap flow = 0;
        Cost cost = 0, prev_cost = -1;
        std::vector<std::pair<Cap, Cost>> result;
        result.push_back({flow, cost});
        while (flow < flow_limit) {
            if (!dual_ref()) break;
            Cap c = flow_limit - flow;
            for (int v = t; v != s; v = pv[v]) {
                c = std::min(c, g[pv[v]][pe[v]].cap);
            }
            for (int v = t; v != s; v = pv[v]) {
                auto& e = g[pv[v]][pe[v]];
                e.cap -= c;
                g[v][e.rev].cap += c;
            }
            Cost d = -dual[s];
            flow += c;
            cost += c * d;
            if (prev_cost == d) {
                result.pop_back();
            }
            result.push_back({flow, cost});
            prev_cost = cost;
        }
        return result;
    }
    std::vector<Cost> detail_slope(int s, int t){
        std::vector<std::pair<Cap, Cost>> ori = slope(s, t);
        std::vector<Cost> ans(ori.back().first + 1);
        Cap x = 0, nx;
        Cost y = 0, ny;
        for(int i = 1; i < ori.size(); i++){
            std::tie(nx, ny) = ori[i];
            Cost d = (ny - y) / (nx - x);
            while(x != nx){
                ++x, y+= d;
                ans[x] = y;
            }
        }
        return ans;
    }

  private:
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
        Cost cost;
    };
    std::vector<std::pair<int, int>> pos;
    std::vector<std::vector<_edge>> g;
};

template< typename T >
T hungarian(vector<vector< T >> &A) {
  const T infty = numeric_limits< T >::max();
  const int N = (int) A.size();
  const int M = (int) A[0].size();
  vector< int > P(M), way(M);
  vector< T > U(N, 0), V(M, 0), minV;
  vector< bool > used;

  for(int i = 1; i < N; i++) {
    P[0] = i;
    minV.assign(M, infty);
    used.assign(M, false);
    int j0 = 0;
    while(P[j0] != 0) {
      int i0 = P[j0], j1 = 0;
      used[j0] = true;
      T delta = infty;
      for(int j = 1; j < M; j++) {
        if(used[j]) continue;
        T curr = A[i0][j] - U[i0] - V[j];
        if(curr < minV[j]) minV[j] = curr, way[j] = j0;
        if(minV[j] < delta) delta = minV[j], j1 = j;
      }
      for(int j = 0; j < M; j++) {
        if(used[j]) U[P[j]] += delta, V[j] -= delta;
        else minV[j] -= delta;
      }
      j0 = j1;
    }
    do {
      P[j0] = P[way[j0]];
      j0 = way[j0];
    } while(j0 != 0);
  }
  return -V[0];
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    INT(n, m);
    vector<vector<ll>> A(n, vector<ll>(n, INF2));
    int u, v;
    vector<int> c(n);
    for(int i = 0; i < m; i++){
        cin >> u >> v;
        u--, v--;
        A[u][v] = A[v][u] = 1;
    }
    in(c);
    for(int i = 0; i < n; i++) A[i][i] = 0;
    for(int k = 0; k < n; k++){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                chmin(A[i][j], A[i][k] + A[k][j]);
            }
        }
    }
    int cnt = count(all(c), 1);
    if(cnt & 1){
        out(-1);
        return 0;
    }
    //cerr << "enter" << '\n';
    mcf_graph<int, ll> g(2 * n + 2);
    //cnt /= 2;
    //cerr << cnt * 2 << '\n';
    int s = 2 * n, t = s + 1;
    for(int i = 0; i < n; i++){
        if(c[i]){
            g.add_edge(s, i, 1, 0);
            g.add_edge(i + n, t, 1, 0);
        }
        for(int j = 0; j < n; j++){
            if(i == j) continue;
            if(c[i] && c[j]){
                if(A[i][j] == INF2) continue;
                g.add_edge(i, j + n, 1, A[i][j]);
            }
        }
    }
    auto p = g.flow(s, t);
    if(p.first != cnt){
        out(-1);
        return 0;
    }
    ll S = 0;
    for(int i = 0; i < n; i++){
        if(c[i]) S |= 1ll << i;
    }
    map<ll,ll> mp;
    function<ll(ll)> dfs = [&](ll S){
        if(S == 0) return 0ll;
        if(mp.find(S) != mp.end())return mp[S];
        ll res = INF2;
        for(int i = 0; i < n; i++){
            if(~S >> i & 1) continue;
            for(int j = i + 1; j < n; j++){
                if(~S >> j & 1) continue;
                chmin(res, dfs(S ^ (1ll << i) ^ (1ll << j)) + A[i][j]);
            }
            break;
        }
        return mp[S] = res;
    };
    out(dfs(S));
}
0