結果

問題 No.274 The Wall
ユーザー codershifthcodershifth
提出日時 2016-05-07 14:59:21
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,533 bytes
コンパイル時間 1,768 ms
コンパイル使用メモリ 165,184 KB
実行使用メモリ 169,156 KB
最終ジャッジ日時 2023-07-28 03:42:46
合計ジャッジ時間 5,128 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 149 ms
60,712 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 480 ms
169,156 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 258 ms
90,020 KB
testcase_17 AC 233 ms
82,724 KB
testcase_18 AC 246 ms
86,944 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 23 ms
4,380 KB
testcase_23 AC 23 ms
4,380 KB
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;
class SCC {
public:
    // for input or output data
    std::vector<std::vector<int>> G_;
    std::vector<int> cmp_;

    // for algorithm
    std::vector<std::vector<int>> rG_;
    std::vector<int> vs_;
    std::vector<bool> used_;

    SCC() {}
    SCC(int V) { init(V); }
    void init(int V) {
        G_.resize(V);
        rG_.resize(V);
        used_.resize(V);
        cmp_.resize(V);
    }
    ~SCC() {}
    void add(int from, int to) {
        assert(0 <= from && from <  (int)G_.size());
        assert(0 <= to && to <  (int)rG_.size());
        G_[from].push_back(to);
        rG_[to].push_back(from);
    }
    void dfs(int v) {
        used_[v] = true;
        for (int i = 0; i < (int)G_[v].size(); ++i)
        {
            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;
        for (int i = 0; i < (int)rG_[v].size(); ++i)
        {
            if (!used_[rG_[v][i]])
                rdfs(rG_[v][i], k);
        }
    }
    //
    // O(|V|+|E|)
    //
    int decompose() {
        int V = G_.size();
        std::fill(used_.begin(), used_.end(), false);
        vs_.clear();
        for (int v = 0; v < V; ++v)
            if (!used_[v])
                dfs(v);
        std::fill(used_.begin(), used_.end(), false);
        int k = 0;
        for (int i = vs_.size()-1; i >= 0; i--)
            if (!used_[vs_[i]])
                rdfs(vs_[i], k++);
        return k;
    }
    const std::vector<int> &components() const {
        return cmp_;
    }
};


bool rangeIsCross(int s1, int e1, int s2, int e2)
{
    if (s1 > e1) swap(s1,e1);
    if (s2 > e2) swap(s2,e2);
    return max(s1,s2) <= min(e1,e2);
}

class TheWall
{
public:
    void solve(void)
    {
        int N,M;
        cin>>N>>M;
        vector<pair<int,int>> blks;
        REP(i,N)
        {
            int l,r;
            cin>>l>>r;
            blks.emplace_back(l,r);
        }
#define flip(X) (M-1-(X))
        SCC scc(N*2);

        REP(i,N)
        {
            int l1,r1;
            tie(l1,r1) = blks[i];

            // 対偶も追加
#define Add(X,Y) do { scc.add(X,Y); scc.add((Y+N)%(2*N), (X+N)%(2*N));} while(0)

            REP(j,i)
            {
                int l2,r2;
                tie(l2,r2) = blks[j];
                // 交差しないときに辺を張るのではなくて、
                // 交差するときにそれを回避するための制約として辺を張る
                if (rangeIsCross(l1,r1,l2,r2))
                    Add(i,j+N);
                if (rangeIsCross(l1,r1,flip(l2),flip(r2)))
                    Add(i,j);
                if (rangeIsCross(flip(l1),flip(r1),l2,r2))
                    Add(i+N,j);
                if (rangeIsCross(flip(l1),flip(r1),flip(l2),flip(r2)))
                    Add(i,j+N);
            }
        }
        scc.decompose();
        REP(i,N)
        {
            if ( scc.cmp_[i] == scc.cmp_[i+N] )
            {
                cout<<"NO"<<endl;
                return;
            }
        }
        cout<<"YES"<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new TheWall();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0