結果

問題 No.630 門松グラフ
ユーザー maimai
提出日時 2017-06-06 10:00:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 10,445 bytes
コンパイル時間 4,248 ms
コンパイル使用メモリ 245,680 KB
実行使用メモリ 16,968 KB
最終ジャッジ日時 2024-10-08 14:13:01
合計ジャッジ時間 9,919 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function '{anonymous}::MaiScanner& {anonymous}::MaiScanner::operator>>(std::string&)':
main.cpp:77:9: warning: no return statement in function returning non-void [-Wreturn-type]
   77 |         }
      |         ^
main.cpp: At global scope:
main.cpp:298:17: warning: overflow in conversion from 'double' to 'int' changes value from '5.0e+15' to '2147483647' [-Woverflow]
  298 | const int inf = 5e15;
      |                 ^~~~

ソースコード

diff #

//
// assert付きwriter解
//

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include "bits/stdc++.h" // define macro "/D__MAI"

using namespace std;
typedef long long int ll;

#define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;
#define debugm(m) printf("L%d %s is..\n",__LINE__,#m);for(auto v:m){for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) printf("L%d %s is => ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;
#define debugaa(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[x][y]<<" ";}cout<<endl;}
#define debugaar(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout<<endl;}
#define ALL(v) (v).begin(),(v).end()
#define repeat(l) for(auto cnt=0;cnt<(l);++cnt)
#define iterate(b,e) for(auto cnt=(b);cnt!=(e);++cnt)
#define MD 1000000007ll
#define PI 3.1415926535897932384626433832795
template<typename T1, typename T2>
ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << "(" << p.first << ":" << p.second << ")"; return o; }

#define TIME chrono::system_clock::now()
#define MILLISEC(t) (chrono::duration_cast<chrono::milliseconds>(t).count())
namespace {
    std::chrono::system_clock::time_point ttt;
    void tic() { ttt = TIME; }
    void toc() { fprintf(stderr, "TIME : %lldms\n", MILLISEC(TIME - ttt)); }
    std::chrono::system_clock::time_point tle = TIME;
#ifdef __MAI
    void safe_tle(int msec) { assert(MILLISEC(TIME - tle) < msec); }
#else
#define safe_tle(k) ;
#endif
}


#ifdef __MAI
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
#ifdef __VSCC
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#endif
namespace {
#define isvisiablechar(c) (0x21<=(c)&&(c)<=0x7E)
    class MaiScanner {
    public:
        template<typename T>
        void input_integer(T& var) {
            var = 0;
            T sign = 1;
            int cc = getchar_unlocked();
            for (; cc<'0' || '9'<cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc&&cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var*sign;
        }
        inline int c() { return getchar_unlocked(); }
        inline MaiScanner& operator>>(int& var) {
            input_integer<int>(var);
            return *this;
        }
        inline MaiScanner& operator>>(long long& var) {
            input_integer<long long>(var);
            return *this;
        }
        inline MaiScanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiablechar(cc); cc = getchar_unlocked());
            for (; isvisiablechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
        }
    };
}
MaiScanner scanner;

class Flow {
public:
    size_t n;
    struct Arrow {
        int from, to;
        int w_max;
        int cap;

        Arrow(int from = 0, int to = 0, int w = 1) :from(from), to(to), w_max(w), cap(w) {}
        bool operator<(const Arrow& a) const { return (from<a.from) | (to<a.to) | (w_max<a.w_max) | (cap<a.cap); }
        bool operator==(const Arrow& a) const { return (from == a.from) && (to == a.to) && (w_max == a.w_max) && (cap == a.cap); }
    };
    vector<vector<int>> vertex_to;
    vector<vector<int>> vertex_from;
    vector<Arrow> arrow;

    Flow(int n, int m = 5010) :n(n), vertex_to(n), vertex_from(n) { arrow.reserve(m); }

    void connect(int from, int to, int left) {
        vertex_to[from].push_back(arrow.size()); // toto
        vertex_from[to].push_back(arrow.size()); // fromfrom
        arrow.emplace_back(from, to, left);
    }
    size_t degree(int v) {
        return vertex_to[v].size() + vertex_from[v].size();
    }
    size_t degree_in(int v) {
        return vertex_from[v].size();
    }
    size_t degree_out(int v) {
        return vertex_to[v].size();
    }
};


int _dinic_path_dfs(Flow& flow, vector<int>& result, vector<int>& flag, const vector<int>& dist, int u, int i_sink, int mini) {
    // TODO: 経路再利用
    if (i_sink == u) return mini;
    if (flag[u]) return -1;
    flag[u] = true;

    int sumw = 0;
    bool term = true;
    for (int e : flow.vertex_to[u]) {
        Flow::Arrow& a = flow.arrow[e];
        if (a.w_max > 0 && dist[u]>dist[a.to]) {
            int w;
            if (mini < 0)
                w = a.w_max;
            else
                w = min(a.w_max, mini);

            w = _dinic_path_dfs(flow, result, flag, dist, a.to, i_sink, w);
            if (w == -1) continue;
            a.w_max -= w;
            result[a.to] += w;

            sumw += w;
            mini -= w;
            term = false;
            flag[u] = false; // TODO: 末尾では? 

            if (mini == 0) return term ? -1 : sumw;
        }
    }
    for (int e : flow.vertex_from[u]) {
        Flow::Arrow& a = flow.arrow[e];
        if (a.cap>a.w_max && dist[u]>dist[a.from]) {
            int w;
            if (mini < 0)
                w = a.cap - a.w_max;
            else
                w = min(a.cap - a.w_max, mini);

            w = _dinic_path_dfs(flow, result, flag, dist, a.from, i_sink, w);
            if (w == -1) continue;
            a.w_max += w;
            result[a.to] -= w;

            sumw += w;
            mini -= w;
            term = false;
            flag[u] = false;
            if (mini == 0) return term ? -1 : sumw;
        }
    }
    return term ? -1 : sumw;
}

// flowは書き換えられる.
void dinic(Flow &flow, vector<int>& result, int i_source, int i_sink) {
    assert(i_source != i_sink);

    result.resize(flow.n);

    int distbegin = 0;
    vector<int> dist(flow.n);
    queue<int> q;
    vector<int> flag(flow.n);
    for (int distbegin = 0; ; distbegin += flow.n) {

        q.emplace(i_sink); // bfsはsinkからsourceへの距離を計算.
        dist[i_sink] = distbegin + 1;
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int ie : flow.vertex_from[v]) {
                const Flow::Arrow& e = flow.arrow[ie];
                if (0<e.w_max && dist[e.from] <= distbegin) {
                    dist[e.from] = dist[v] + 1;
                    q.emplace(e.from);
                }
            }
            for (int ie : flow.vertex_to[v]) {
                const Flow::Arrow& e = flow.arrow[ie];
                if (e.w_max<e.cap && dist[e.to] <= distbegin) {
                    dist[e.to] = dist[v] + 1;
                    q.emplace(e.to);
                }
            }
        }
        //debugv(dist);
        fill(ALL(flag), false);

        if (dist[i_source] <= distbegin) {
            break;
        }
        else {
            result[i_source] += _dinic_path_dfs(flow, result, flag, dist, i_source, i_sink, -1);
        }
    }

}

// ----------------------------------------------
// ソルバーここまで.
// ----------------------------------------------

inline bool kadomatu(int a, int b, int c) {
    return a != b&&b != c&&c != a && ((a<b && c<b) || (a>b && c>b));
}

class GraphE {
public:
    size_t n;
    struct Edge {
        int u, v;
        Edge(int from = 0, int to = 0) :u(from), v(to) {}

        int to(int _v) { return _v == v ? u : v; }
    };
    vector<vector<int>> vertex_to;
    vector<Edge> edge;

    GraphE(int n, int m = 5010) :n(n), vertex_to(n) { edge.reserve(m); }

    void connect(int from, int to) {
        vertex_to[from].push_back(edge.size()); // toto
        vertex_to[to].push_back(edge.size()); // fromfrom
        edge.emplace_back(from, to);
    }
    size_t degree(int v) {
        return vertex_to[v].size();
    }
    void resize(size_t _n) {
        n = _n;
        vertex_to.resize(_n);
    }
};

void checkinput(GraphE& g, vector<int>& vertices){
    set<int> parts;
    repeat(g.n) {
        const vector<int>& edges = g.vertex_to[cnt];
        int size = edges.size();

        for (int i = 0; i < (size - 1); ++i) {
            auto e1 = g.edge[edges[i]];
            for (int j = i + 1; j < size; ++j) {
                auto e2 = g.edge[edges[j]];
                bool b = false;

                if (e1.u == e2.u) {
                    b|= (kadomatu(vertices[e1.v], vertices[e1.u], vertices[e2.v]));
                }
                else if (e1.u == e2.v) {
                    b|= (kadomatu(vertices[e1.v], vertices[e1.u], vertices[e2.u]));
                }
                else if (e1.v == e2.u) {
                    b|= (kadomatu(vertices[e1.u], vertices[e1.v], vertices[e2.v]));
                }
                else if (e1.v == e2.v) {
                    b|= (kadomatu(vertices[e1.u], vertices[e1.v], vertices[e2.u]));
                }
                assert(b);
                parts.insert(e1.u);
                parts.insert(e1.v);
                parts.insert(e2.u);
                parts.insert(e2.v);
            }
        }
    }
    assert(parts.size() == g.n);
    
    set<pair<int,int>> s;
    for (auto e : g.edge){
        s.insert(make_pair(e.u, e.v));
    }
    assert(s.size() == g.edge.size());
}


// ----------------------------------------------
// 本題ここから
// ----------------------------------------------

const int inf = 5e15;

int main() {
    int n, m;

    scanner >> n >> m;

    vector<int> cost(n);
    Flow graph(n + 2);
    const int source = n;
    const int sink = n + 1;
    vector<int> classify(n);
    
    GraphE checker(n);

    repeat(n) {
        scanner >> cost[cnt];
    }
    repeat(m) {
        int u, v;
        scanner >> u >> v;
        assert(u < v);
        --u; --v;
        if (cost[u] > cost[v]) swap(u, v);
        
        // 未だ source->u の辺を張っていないならば
        if (!classify[u]) {
            classify[u] = 1;
            graph.connect(source, u, cost[u]);
        }
        // 未だ v->sink の辺を張っていないならば
        if (!classify[v]) {
            classify[v] = 2;
            graph.connect(v, sink, cost[v]);
        }
        graph.connect(u, v, inf);
        
        checker.connect(u, v);
    }
    checkinput(checker, cost);

    // sourceからsinkにフローを流す.
    // result[i]は,頂点iに流れた流量が格納される.
    vector<int> result;
    dinic(graph, result, source, sink);

    cout << result[sink] << endl;

    return 0;
}
0