結果

問題 No.1420 国勢調査 (Easy)
ユーザー outlineoutline
提出日時 2021-03-05 22:45:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,172 bytes
コンパイル時間 1,942 ms
コンパイル使用メモリ 150,520 KB
実行使用メモリ 33,300 KB
最終ジャッジ日時 2024-04-16 10:32:17
合計ジャッジ時間 16,486 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 950 ms
11,468 KB
testcase_03 AC 948 ms
11,456 KB
testcase_04 AC 917 ms
11,520 KB
testcase_05 AC 928 ms
11,524 KB
testcase_06 AC 996 ms
11,488 KB
testcase_07 AC 250 ms
11,540 KB
testcase_08 AC 128 ms
11,616 KB
testcase_09 AC 72 ms
11,520 KB
testcase_10 AC 69 ms
11,648 KB
testcase_11 AC 68 ms
11,776 KB
testcase_12 AC 53 ms
17,500 KB
testcase_13 AC 479 ms
17,368 KB
testcase_14 AC 32 ms
17,536 KB
testcase_15 AC 488 ms
17,280 KB
testcase_16 AC 465 ms
17,512 KB
testcase_17 AC 479 ms
17,372 KB
testcase_18 AC 499 ms
17,536 KB
testcase_19 AC 482 ms
17,364 KB
testcase_20 AC 488 ms
17,368 KB
testcase_21 AC 485 ms
17,536 KB
testcase_22 TLE -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int vs[200005];

struct StronglyConnectedComponents{
    using G = vector<vector<int>>;

    G graph;
    G rev;
    int n;
    int scc_sz = 0;
    int id = 0;

    vector<int> scc_id;
    vector<bool> visited;

    StronglyConnectedComponents() = default;

    StronglyConnectedComponents(int V) : n(V) {
        graph.resize(n);
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
    }

    StronglyConnectedComponents(G& g) : graph(g), n(g.size()) {
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
        
        for(int u = 0; u < n; ++u){
            for(int v : graph[u])   rev[v].push_back(u);
        }
    }

    void resize(int V){
        n = V;
        graph.resize(n);
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
    }

    void add_edge(int u, int v){
        graph[u].emplace_back(v);
        rev[v].emplace_back(u);
    }

    void dfs(int from){
        visited[from] = true;
        for(int to : graph[from]){
            if(!visited[to])  dfs(to);
        }
        vs[id++] = from;
    }

    void rdfs(int from, int k){
        scc_id[from] = k;
        for(int to : rev[from]){
            if(scc_id[to] == -1)    rdfs(to, k);
        }
    }

    void build(){
        for(int v = 0; v < n; ++v){
            if(!visited[v]) dfs(v);
        }
        for(int i = n - 1; i >= 0; --i){
            if(scc_id[vs[i]] == -1){
                rdfs(vs[i], scc_sz++);
            }
        }
        return;
    }

    int size() { return scc_sz; }

    int operator[](int v) { return scc_id[v]; }
};

struct TwoSAT {
    StronglyConnectedComponents scc;
    int n;
    vector<int> sat;

    TwoSAT(int V) : n(V) {
        scc.resize(n * 2);
        sat.resize(n);
    }

    // [false] [true]
    void add_edge(int i, bool f, int j, bool g){
        scc.add_edge(i * 2 + (f ? 0 : 1), j * 2 + (g ? 1 : 0));
        scc.add_edge(j * 2 + (g ? 0 : 1), i * 2 + (f ? 1 : 0));
    }

    // sat[v] = true  -> v = false
    // sat[v] = false -> v = true
    bool satisfiable(){
        scc.build();
        for(int i = 0; i < n; ++i){
            if(scc[i * 2] == scc[i * 2 + 1])    return false;
            sat[i] = scc[i * 2] < scc[i * 2 + 1];
        }
        return true;
    }

    vector<int> get_sat() { return sat; }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    vector<int> A(M), B(M), Y(M);
    for(int i = 0; i < M; ++i){
        cin >> A[i] >> B[i] >> Y[i];
        --A[i], --B[i];
    }

    vector<int> X(N);
    for(int k = 0; k < 30; ++k){
        TwoSAT ts(N);
        for(int i = 0; i < M; ++i){
            if(Y[i] >> k & 1){
                ts.add_edge(A[i], false, B[i], false);
                ts.add_edge(A[i], true, B[i], true);
            }
            else{
                ts.add_edge(A[i], false, B[i], true);
                ts.add_edge(A[i], true, B[i], false);
            }
        }
        if(!ts.satisfiable()){
            cout << -1 << endl;
            return 0;
        }
        for(int i = 0; i < N; ++i){
            if(!ts.sat[i])  X[i] += 1 << k;
        }
    }

    for(int i = 0; i < N; ++i)  cout << X[i] << '\n';

    return 0;
}
0