結果

問題 No.2236 Lights Out On Simple Graph
ユーザー t98slidert98slider
提出日時 2023-03-03 22:21:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 11,602 bytes
コンパイル時間 2,064 ms
コンパイル使用メモリ 194,848 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-17 23:24:27
合計ジャッジ時間 3,728 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
    }
    //out(hungarian(A));
    out(p.second / 2);
}

/*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;
    }
    int n1 = n / 2, n2 = n - n1;
    vector<ll> dp(1 << n1, INF2), dp2(1 << n2, INF2);
    dp[0] = dp2[0] = 0;
    for(int i = 0; i < (1 << n1); i++){
        if(__builtin_popcount(i) & 1) continue;
        for(int j = 0; j < n1; j++){
            if(i >> j & 1) continue;
            for(int k = j + 1; k < n1; k++){
                if(i >> k & 1) continue;
                chmin(dp[i | (1 << j) | (1 << k)], dp[i] + A[j][k]);
            }
        }
    }
    for(int i = 0; i < (1 << n2); i++){
        if(__builtin_popcount(i) & 1) continue;
        for(int j = 0; j < n2; j++){
            if(i >> j & 1) continue;
            for(int k = j + 1; k < n2; k++){
                if(i >> k & 1) continue;
                chmin(dp2[i | (1 << j) | (1 << k)], dp2[i] + A[j + n1][k + n1]);
            }
        }
    }
    mcf_graph<int, ll> g(n + 2);
    int s = n, t = s + 1;
    for(int i = 0; i < n1; i++){
        for(int j = n1; j < n; j++){
            if(c[i] && c[j] && A[i][j] != INF2) g.add_edge(i, j, 1, A[i][j]);
        }
    }
}*/
0