結果

問題 No.274 The Wall
ユーザー imulanimulan
提出日時 2017-10-17 17:12:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 410 ms / 2,000 ms
コード長 2,937 bytes
コンパイル時間 1,787 ms
コンパイル使用メモリ 179,376 KB
実行使用メモリ 131,924 KB
最終ジャッジ日時 2023-09-04 02:27:51
合計ジャッジ時間 3,806 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 142 ms
69,540 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 410 ms
131,924 KB
testcase_12 AC 20 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 10 ms
4,380 KB
testcase_16 AC 71 ms
26,732 KB
testcase_17 AC 69 ms
26,420 KB
testcase_18 AC 71 ms
27,272 KB
testcase_19 AC 17 ms
4,380 KB
testcase_20 AC 19 ms
4,376 KB
testcase_21 AC 20 ms
4,376 KB
testcase_22 AC 22 ms
4,380 KB
testcase_23 AC 21 ms
4,376 KB
testcase_24 AC 21 ms
4,380 KB
testcase_25 AC 22 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

struct SCC{
    int V;
    vector<vector<int>> G, rG;
    vector<int> vs; // 帰りがけ順の並び
    vector<int> cmp; //属する強連結成分トポロジカル順序
    vector<bool> used;

    SCC(){}
    SCC(int n){
        V = n;
        G = vector<vector<int>>(n);
        rG = vector<vector<int>>(n);
    }

    void add_edge(int from, int to){
        G[from].push_back(to);
        rG[to].push_back(from);
    }

    void dfs(int v){
        used[v] = true;
        rep(i,G[v].size())if(!used[G[v][i]]) dfs(G[v][i]);
        vs.push_back(v);
    }

    void rdfs(int v, int k){
        used[v]=true;
        cmp[v]=k;
        rep(i,rG[v].size())if(!used[rG[v][i]]) rdfs(rG[v][i],k);
    }

    int scc(){
        used = vector<bool>(V,false);
        vs.clear();
        rep(i,V)if(!used[i]) dfs(i);

        used = vector<bool>(V,false);
        cmp = vector<int>(V);
        int num_scc = 0;
        for(int i=vs.size()-1; i>=0; --i)if(!used[vs[i]]) rdfs(vs[i],num_scc++);
        return num_scc;
    }
};

struct TwoSat{
    int v;
    SCC graph;

    // v literals
    // 0~v-1: true
    // v~2v-1: false

    TwoSat(int num_literal){
        v = num_literal;
        graph = SCC(2*v);
    }

    inline int num(int id, bool b){return id+(b?0:v);}

    void add_clause(int x, bool X, int y, bool Y){
        graph.add_edge(num(x,!X), num(y,Y));
        graph.add_edge(num(y,!Y), num(x,X));
    }

    // 割り当てが可能か調べる
    bool calc(){
        graph.scc();
        rep(i,v)if(graph.cmp[i]==graph.cmp[v+i]) return false;
        return true;
    }

    // リテラルの真偽値を返す
    vector<bool> get_literals(){
        assert(calc());
        vector<bool> res(v);
        rep(i,v) res[i] = (graph.cmp[i]>graph.cmp[v+i]);
        return res;
    }
};

using pi = pair<int,int>;
inline pi REV(pi x, int m)
{
    return {m-1-x.se,  m-1-x.fi};
}

inline bool COV(pi x, pi y)
{
    int l = max(x.fi,y.fi);
    int r = min(x.se,y.se);
    return l<=r;
}

int main()
{
    int n,m;
    cin >>n >>m;
    vector<pi> range(n);
    rep(i,n) cin >>range[i].fi >>range[i].se;

    TwoSat solver(n);
    rep(j,n)rep(i,j)
    {
        rep(ii,2)rep(jj,2)
        {
            pi x=range[i], y=range[j];
            if(!ii) x = REV(x,m);
            if(!jj) y = REV(y,m);
            if(COV(x,y)) solver.add_clause(i, !ii, j, !jj);
        }
    }

    cout << (solver.calc()?"YES":"NO") << endl;
    return 0;
}
0