結果

問題 No.1640 簡単な色塗り
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-08-07 06:06:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,967 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 179,096 KB
実行使用メモリ 53,724 KB
最終ジャッジ日時 2023-09-12 03:43:04
合計ジャッジ時間 25,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 260 ms
44,812 KB
testcase_05 AC 263 ms
45,320 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 460 ms
27,564 KB
testcase_11 AC 353 ms
23,028 KB
testcase_12 AC 298 ms
20,676 KB
testcase_13 AC 837 ms
41,356 KB
testcase_14 AC 802 ms
39,808 KB
testcase_15 AC 196 ms
16,196 KB
testcase_16 AC 244 ms
18,772 KB
testcase_17 AC 642 ms
34,548 KB
testcase_18 AC 27 ms
5,912 KB
testcase_19 AC 285 ms
20,884 KB
testcase_20 AC 442 ms
26,556 KB
testcase_21 AC 330 ms
22,300 KB
testcase_22 AC 18 ms
5,172 KB
testcase_23 AC 502 ms
28,428 KB
testcase_24 AC 82 ms
10,208 KB
testcase_25 AC 296 ms
20,960 KB
testcase_26 AC 579 ms
31,620 KB
testcase_27 AC 169 ms
15,380 KB
testcase_28 AC 759 ms
40,584 KB
testcase_29 AC 497 ms
31,680 KB
testcase_30 AC 27 ms
10,008 KB
testcase_31 AC 335 ms
53,724 KB
testcase_32 AC 246 ms
42,720 KB
testcase_33 AC 147 ms
29,024 KB
testcase_34 AC 239 ms
39,436 KB
testcase_35 AC 142 ms
29,568 KB
testcase_36 AC 26 ms
9,636 KB
testcase_37 AC 38 ms
11,852 KB
testcase_38 AC 251 ms
45,344 KB
testcase_39 AC 99 ms
22,300 KB
testcase_40 AC 86 ms
20,788 KB
testcase_41 AC 178 ms
35,560 KB
testcase_42 AC 108 ms
23,884 KB
testcase_43 AC 143 ms
26,272 KB
testcase_44 AC 108 ms
23,340 KB
testcase_45 AC 83 ms
18,720 KB
testcase_46 AC 29 ms
10,460 KB
testcase_47 AC 19 ms
7,620 KB
testcase_48 AC 286 ms
46,524 KB
testcase_49 AC 8 ms
5,288 KB
testcase_50 AC 2 ms
4,384 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 591 ms
53,300 KB
testcase_53 TLE -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.08.07 06:06:48
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;







struct Dinic {
private:
    struct edge {
        int to;
        ll cap;
        int rev;
        bool isrev;
        int idx;
    };
    vector< vector< edge > > graph;
    vector< int > min_cost, iter;
    bool bfs(int s, int t) {
        min_cost.assign(graph.size(), -1);
        queue< int > que;
        min_cost[s] = 0;
        que.push(s);
        while (!que.empty() && min_cost[t] == -1) {
            int p = que.front();
            que.pop();
            for (auto &e : graph[p]) {
                if (e.cap > 0 && min_cost[e.to] == -1) {
                    min_cost[e.to] = min_cost[p] + 1;
                    que.push(e.to);
                }
            }
        }
        return min_cost[t] != -1;
    }
    ll dfs(int idx, const int t, ll flow) {
        if (idx == t) return flow;
        for (int &i = iter[idx]; i < graph[idx].size(); i++) {
            edge &e = graph[idx][i];
            if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
                ll d = dfs(e.to, t, min(flow, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    graph[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
public:
    Dinic(int V) : graph(V) {}
    
    void add_edge(int from, int to, ll cap, int idx = -1) {
        graph[from].push_back({to, cap, (int)graph[to].size(), false, idx});
        graph[to].push_back({from, 0, (int)graph[from].size() - 1, true, idx});
    }
    
    ll max_flow(int s, int t) {
        ll flow = 0;
        while (bfs(s, t)) {
            iter.assign(graph.size(), 0);
            ll f = 0;
            while ((f = dfs(s, t, 1e9 + 6)) > 0) flow += f;
        }
        return flow;
    }
    void output() {
        for (int i = 0; i < graph.size(); i++) {
            for (auto &e : graph[i]) {
                if (e.isrev) continue;
                auto &rev_e = graph[e.to][e.rev];
                cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
    }
    int ans(int v) {
        for (auto &e : graph[v]) {
            if (e.isrev) continue;
            auto &rev_e = graph[e.to][e.rev];
            if (rev_e.cap == 1) return e.to;
        }
        return -1;
    }
};
int main() {
    int n;cin >> n;
    vector<int> a(n),b(n);
    Dinic g(n*2+2);
    int s = n*2, t = n*2+1;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        g.add_edge(i,n+a[i]-1,1);
        g.add_edge(i,n+b[i]-1,1);
        g.add_edge(s,i,1);
        g.add_edge(n+i,t,1);
    }
    if (g.max_flow(s,t) != n) {
        puts("No");
    }
    else {
        puts("Yes");
        for (int i = 0; i < n; i++) {
            cout << g.ans(i)-n+1 << endl;
        }
    }
    return 0;
}
0