답안 #878679

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878679 2023-11-25T04:59:48 Z kh0i Joker (BOI20_joker) C++17
49 / 100
1950 ms 36524 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int N = 2e5 + 3;

int n, m, q, L[N], R[N];
vector<pii> g[N];
pii e[N];

// contain no odd cycle -> bipartite graph

namespace Sub12{
    bool valid_input(){
        return n <= 2000 and m <= 2000 and q <= 2000;
    }

    int vis[N];

    bool dfs(int u, int l, int r, int col){
        bool ok = 1;
        vis[u] = col;
        for(auto [v, id] : g[u]){
            if(l <= id and id <= r)
                continue;
            if(vis[v])
                ok &= (vis[u] != vis[v]);
            else
                ok &= dfs(v, l, r, col ^ 3);
        }
        return ok;
    }

    void solve(){
        for(int i = 1; i <= q; ++i){
            bool bipartite = 1;
            for(int u = 1; u <= n; ++u)
                vis[u] = 0;
            for(int u = 1; u <= n; ++u){
                if(!vis[u])
                    bipartite &= dfs(u, L[i], R[i], 1);
            }
            cout << (bipartite ? "NO\n" : "YES\n");
        }
    }
}

struct DSU{
    bool bipartite = 1;
    vector<pii> p;
    int n;

    void init(int _n){
        n = _n;
        p.resize(n + 2);
        for(int i = 1; i <= n; ++i)
            p[i] = {i, 0};
    }

    pii find_set(int u){
        if(u != p[u].F){
            int parity = p[u].S;
            p[u] = find_set(p[u].F);
            p[u].S ^= parity;
        }
        return p[u];
    }

    void add_edge(int u, int v){
        pii pu = find_set(u);
        u = pu.F;
        pii pv = find_set(v);
        v = pv.F;
        int x = pu.S, y = pv.S;
        if(u == v){
            bipartite &= (x != y);
            return;
        }
        p[v] = {u, x ^ y ^ 1};
    }
};

namespace Sub34{
    bool valid_input(){
        bool ok = 1;
        for(int i = 1; i <= q; ++i)
            ok &= (L[i] <= 200);
        return ok;
    }

    int ans[N];
    vector<pii> d[N];

    void solve(){
        for(int i = 1; i <= q; ++i){
            d[L[i]].push_back({R[i], i});
        }
        for(int i = 1; i <= min(200, m); ++i){
            DSU dsu;
            dsu.init(n);
            for(int j = 1; j < i; ++j){
                dsu.add_edge(e[j].F, e[j].S);
            }
            sort(all(d[i]), [](pii &x, pii &y) -> bool{
                 return x.F < y.F;
            });

            for(int j = m; j >= i; --j){
                while(!d[i].empty() and d[i].back().F == j){
                    ans[d[i].back().S] = dsu.bipartite;
                    d[i].pop_back();
                }
                dsu.add_edge(e[j].F, e[j].S);
            }
        }
        for(int i = 1; i <= q; ++i)
            cout << (ans[i] ? "NO\n" : "YES\n");
    }
}

struct DSU_Rollback{
    struct Data{
        int u, parity_u, sz_u, v, parity_v, sz_v;
        bool bipartite;
        int t;
    };

    bool bipartite;
    vector<pii> p;
    vector<int> sz;
    vector<Data> op;

    void init(int _n){
        bipartite = 1;
        p.resize(n + 2);
        sz.resize(n + 2, 1);
        for(int i = 1; i <= n; ++i)
            p[i] = {i, 0};
    }

    pii find_set(int u){
        if(u == p[u].F)
            return p[u];
        int parity = p[u].S;
        pii x = find_set(p[u].F);
        x.S ^= parity;
        return x;
    }

    void join_sets(int u, int v, int t){
        pii pu = find_set(u), pv = find_set(v);
        u = pu.F, v = pv.F;
        int x = pu.S, y = pv.S;

        if(u == v){
            if(x == y and bipartite){
                op.push_back({u, x, sz[u], v, y, sz[v], bipartite, t});
                bipartite = 0;
            }
            return;
        }
        if(sz[u] < sz[v])
            swap(u, v);
        op.push_back({u, x, sz[u], v, y, sz[v], bipartite, t});
        p[v] = {u, x ^ y ^ 1};
        sz[u] += sz[v];
    }

    void rollback(int t){
        while(!op.empty() and op.back().t > t){
            Data &x = op.back();
            op.pop_back();
            p[x.u] = {x.u, x.parity_u};
            sz[x.u]= x.sz_u;
            p[x.v] = {x.v, x.parity_v};
            sz[x.v] = x.sz_v;
            bipartite = x.bipartite;
        }
    }
};

namespace Sub5{
    bool valid_input(){
        return q <= 2000;
    }

    int B, add_time[N];
    vector<int> blk[N];
    int ans[N];

    void solve(){
        B = m / sqrt(q);
        debug(B);
        for(int i = 1; i <= q; ++i)
            blk[(L[i] + B - 1) / B].push_back(i);

        for(int bl = 1; bl < N; ++bl){
            if(blk[bl].empty())
                continue;
            vector<int> &d = blk[bl];
            sort(all(d), [](int &x, int &y) -> bool {
                 return R[x] < R[y];
            });
            DSU_Rollback dsu;
            dsu.init(n);

            int timer = 0;
            int U = (bl - 1) * B + 1;

            debug(U);

            for(int i = 1; i <= U; ++i){
                dsu.join_sets(e[i].F, e[i].S, ++timer);
                add_time[i] = timer;
                debug(e[i], timer);
            }
            debug(R[d.front()]);
            for(int i = m; i >= R[d.front()]; --i){
                dsu.join_sets(e[i].F, e[i].S, ++timer);
                add_time[i] = timer;
                debug(e[i], timer);
            }

            for(int x : d){
                debug(L[x], R[x], add_time[R[x]]);
                dsu.rollback(add_time[R[x]]);

                int last = timer;
                debug(last);

                for(int i = U + 1; i <= L[x]; ++i){
                    dsu.join_sets(e[i].F, e[i].S, ++timer);
                    debug(e[i], timer);
                }

                debug(dsu.bipartite);

                ans[x] = dsu.bipartite;

                dsu.rollback(last);
                timer = last;
            }

            for(int i = 1; i <= q; ++i)
                cout << (ans[i] ? "NO\n" : "YES\n");
        }
    }
};

void solve(){
    cin >> n >> m >> q;
    for(int i = 1; i <= m; ++i){
        int u, v;
        cin >> u >> v;
        g[u].push_back({v, i});
        g[v].push_back({u, i});
        e[i] = {u, v};
    }
    for(int i = 1; i <= q; ++i)
        cin >> L[i] >> R[i];
    if(Sub12::valid_input())
        Sub12::solve();
    else if(Sub34::valid_input())
        Sub34::solve();
    else if(Sub5::valid_input())
        Sub5::solve();
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "nohome"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}

Compilation message

Joker.cpp: In function 'int32_t main()':
Joker.cpp:292:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  292 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:293:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  293 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 3 ms 19036 KB Output is correct
4 Correct 3 ms 19036 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 18868 KB Output is correct
9 Correct 4 ms 19044 KB Output is correct
10 Correct 5 ms 19032 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19032 KB Output is correct
13 Correct 4 ms 19032 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 4 ms 18868 KB Output is correct
16 Correct 4 ms 19084 KB Output is correct
17 Correct 4 ms 19032 KB Output is correct
18 Correct 6 ms 19036 KB Output is correct
19 Correct 4 ms 19112 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19036 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 4 ms 19036 KB Output is correct
24 Correct 4 ms 19040 KB Output is correct
25 Correct 4 ms 19032 KB Output is correct
26 Correct 4 ms 19036 KB Output is correct
27 Correct 4 ms 19036 KB Output is correct
28 Correct 4 ms 19040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 3 ms 19036 KB Output is correct
4 Correct 3 ms 19036 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 18868 KB Output is correct
9 Correct 4 ms 19044 KB Output is correct
10 Correct 5 ms 19032 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19032 KB Output is correct
13 Correct 4 ms 19032 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 4 ms 18868 KB Output is correct
16 Correct 4 ms 19084 KB Output is correct
17 Correct 4 ms 19032 KB Output is correct
18 Correct 6 ms 19036 KB Output is correct
19 Correct 4 ms 19112 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19036 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 4 ms 19036 KB Output is correct
24 Correct 4 ms 19040 KB Output is correct
25 Correct 4 ms 19032 KB Output is correct
26 Correct 4 ms 19036 KB Output is correct
27 Correct 4 ms 19036 KB Output is correct
28 Correct 4 ms 19040 KB Output is correct
29 Correct 56 ms 19032 KB Output is correct
30 Correct 94 ms 19044 KB Output is correct
31 Correct 87 ms 19032 KB Output is correct
32 Correct 67 ms 19380 KB Output is correct
33 Correct 48 ms 19036 KB Output is correct
34 Correct 95 ms 19240 KB Output is correct
35 Correct 97 ms 19240 KB Output is correct
36 Correct 28 ms 19288 KB Output is correct
37 Correct 59 ms 19036 KB Output is correct
38 Correct 54 ms 19036 KB Output is correct
39 Correct 66 ms 19032 KB Output is correct
40 Correct 57 ms 19032 KB Output is correct
41 Correct 58 ms 19036 KB Output is correct
42 Correct 59 ms 19252 KB Output is correct
43 Correct 48 ms 19036 KB Output is correct
44 Correct 69 ms 19240 KB Output is correct
45 Correct 86 ms 19032 KB Output is correct
46 Correct 94 ms 19032 KB Output is correct
47 Correct 54 ms 19036 KB Output is correct
48 Correct 61 ms 19032 KB Output is correct
49 Correct 75 ms 19036 KB Output is correct
50 Correct 88 ms 19232 KB Output is correct
51 Correct 53 ms 19032 KB Output is correct
52 Correct 66 ms 19240 KB Output is correct
53 Correct 80 ms 19032 KB Output is correct
54 Correct 96 ms 19232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 661 ms 32740 KB Output is correct
4 Correct 638 ms 35464 KB Output is correct
5 Correct 1354 ms 35580 KB Output is correct
6 Correct 783 ms 34072 KB Output is correct
7 Correct 760 ms 33980 KB Output is correct
8 Correct 1306 ms 32540 KB Output is correct
9 Correct 1716 ms 33468 KB Output is correct
10 Correct 1915 ms 35120 KB Output is correct
11 Correct 1324 ms 33652 KB Output is correct
12 Correct 1506 ms 35268 KB Output is correct
13 Correct 657 ms 32364 KB Output is correct
14 Correct 1316 ms 32976 KB Output is correct
15 Correct 1927 ms 34440 KB Output is correct
16 Correct 1908 ms 35412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 3 ms 19036 KB Output is correct
4 Correct 3 ms 19036 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 18868 KB Output is correct
9 Correct 4 ms 19044 KB Output is correct
10 Correct 5 ms 19032 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19032 KB Output is correct
13 Correct 4 ms 19032 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 4 ms 18868 KB Output is correct
16 Correct 4 ms 19084 KB Output is correct
17 Correct 4 ms 19032 KB Output is correct
18 Correct 6 ms 19036 KB Output is correct
19 Correct 4 ms 19112 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19036 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 4 ms 19036 KB Output is correct
24 Correct 4 ms 19040 KB Output is correct
25 Correct 4 ms 19032 KB Output is correct
26 Correct 4 ms 19036 KB Output is correct
27 Correct 4 ms 19036 KB Output is correct
28 Correct 4 ms 19040 KB Output is correct
29 Correct 661 ms 32740 KB Output is correct
30 Correct 638 ms 35464 KB Output is correct
31 Correct 1354 ms 35580 KB Output is correct
32 Correct 783 ms 34072 KB Output is correct
33 Correct 760 ms 33980 KB Output is correct
34 Correct 1306 ms 32540 KB Output is correct
35 Correct 1716 ms 33468 KB Output is correct
36 Correct 1915 ms 35120 KB Output is correct
37 Correct 1324 ms 33652 KB Output is correct
38 Correct 1506 ms 35268 KB Output is correct
39 Correct 657 ms 32364 KB Output is correct
40 Correct 1316 ms 32976 KB Output is correct
41 Correct 1927 ms 34440 KB Output is correct
42 Correct 1908 ms 35412 KB Output is correct
43 Correct 673 ms 33620 KB Output is correct
44 Correct 651 ms 36412 KB Output is correct
45 Correct 609 ms 36476 KB Output is correct
46 Correct 784 ms 34932 KB Output is correct
47 Correct 800 ms 34852 KB Output is correct
48 Correct 1754 ms 34408 KB Output is correct
49 Correct 1913 ms 36268 KB Output is correct
50 Correct 1184 ms 34528 KB Output is correct
51 Correct 1491 ms 35456 KB Output is correct
52 Correct 1467 ms 36524 KB Output is correct
53 Correct 667 ms 33128 KB Output is correct
54 Correct 1566 ms 34516 KB Output is correct
55 Correct 1950 ms 35580 KB Output is correct
56 Correct 1893 ms 36312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 3 ms 19036 KB Output is correct
4 Correct 3 ms 19036 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 18868 KB Output is correct
9 Correct 4 ms 19044 KB Output is correct
10 Correct 5 ms 19032 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19032 KB Output is correct
13 Correct 4 ms 19032 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 4 ms 18868 KB Output is correct
16 Correct 4 ms 19084 KB Output is correct
17 Correct 4 ms 19032 KB Output is correct
18 Correct 6 ms 19036 KB Output is correct
19 Correct 4 ms 19112 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19036 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 4 ms 19036 KB Output is correct
24 Correct 4 ms 19040 KB Output is correct
25 Correct 4 ms 19032 KB Output is correct
26 Correct 4 ms 19036 KB Output is correct
27 Correct 4 ms 19036 KB Output is correct
28 Correct 4 ms 19040 KB Output is correct
29 Correct 56 ms 19032 KB Output is correct
30 Correct 94 ms 19044 KB Output is correct
31 Correct 87 ms 19032 KB Output is correct
32 Correct 67 ms 19380 KB Output is correct
33 Correct 48 ms 19036 KB Output is correct
34 Correct 95 ms 19240 KB Output is correct
35 Correct 97 ms 19240 KB Output is correct
36 Correct 28 ms 19288 KB Output is correct
37 Correct 59 ms 19036 KB Output is correct
38 Correct 54 ms 19036 KB Output is correct
39 Correct 66 ms 19032 KB Output is correct
40 Correct 57 ms 19032 KB Output is correct
41 Correct 58 ms 19036 KB Output is correct
42 Correct 59 ms 19252 KB Output is correct
43 Correct 48 ms 19036 KB Output is correct
44 Correct 69 ms 19240 KB Output is correct
45 Correct 86 ms 19032 KB Output is correct
46 Correct 94 ms 19032 KB Output is correct
47 Correct 54 ms 19036 KB Output is correct
48 Correct 61 ms 19032 KB Output is correct
49 Correct 75 ms 19036 KB Output is correct
50 Correct 88 ms 19232 KB Output is correct
51 Correct 53 ms 19032 KB Output is correct
52 Correct 66 ms 19240 KB Output is correct
53 Correct 80 ms 19032 KB Output is correct
54 Correct 96 ms 19232 KB Output is correct
55 Incorrect 298 ms 35848 KB Output isn't correct
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 3 ms 19036 KB Output is correct
4 Correct 3 ms 19036 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 18868 KB Output is correct
9 Correct 4 ms 19044 KB Output is correct
10 Correct 5 ms 19032 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19032 KB Output is correct
13 Correct 4 ms 19032 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 4 ms 18868 KB Output is correct
16 Correct 4 ms 19084 KB Output is correct
17 Correct 4 ms 19032 KB Output is correct
18 Correct 6 ms 19036 KB Output is correct
19 Correct 4 ms 19112 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19036 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 4 ms 19036 KB Output is correct
24 Correct 4 ms 19040 KB Output is correct
25 Correct 4 ms 19032 KB Output is correct
26 Correct 4 ms 19036 KB Output is correct
27 Correct 4 ms 19036 KB Output is correct
28 Correct 4 ms 19040 KB Output is correct
29 Correct 56 ms 19032 KB Output is correct
30 Correct 94 ms 19044 KB Output is correct
31 Correct 87 ms 19032 KB Output is correct
32 Correct 67 ms 19380 KB Output is correct
33 Correct 48 ms 19036 KB Output is correct
34 Correct 95 ms 19240 KB Output is correct
35 Correct 97 ms 19240 KB Output is correct
36 Correct 28 ms 19288 KB Output is correct
37 Correct 59 ms 19036 KB Output is correct
38 Correct 54 ms 19036 KB Output is correct
39 Correct 66 ms 19032 KB Output is correct
40 Correct 57 ms 19032 KB Output is correct
41 Correct 58 ms 19036 KB Output is correct
42 Correct 59 ms 19252 KB Output is correct
43 Correct 48 ms 19036 KB Output is correct
44 Correct 69 ms 19240 KB Output is correct
45 Correct 86 ms 19032 KB Output is correct
46 Correct 94 ms 19032 KB Output is correct
47 Correct 54 ms 19036 KB Output is correct
48 Correct 61 ms 19032 KB Output is correct
49 Correct 75 ms 19036 KB Output is correct
50 Correct 88 ms 19232 KB Output is correct
51 Correct 53 ms 19032 KB Output is correct
52 Correct 66 ms 19240 KB Output is correct
53 Correct 80 ms 19032 KB Output is correct
54 Correct 96 ms 19232 KB Output is correct
55 Correct 661 ms 32740 KB Output is correct
56 Correct 638 ms 35464 KB Output is correct
57 Correct 1354 ms 35580 KB Output is correct
58 Correct 783 ms 34072 KB Output is correct
59 Correct 760 ms 33980 KB Output is correct
60 Correct 1306 ms 32540 KB Output is correct
61 Correct 1716 ms 33468 KB Output is correct
62 Correct 1915 ms 35120 KB Output is correct
63 Correct 1324 ms 33652 KB Output is correct
64 Correct 1506 ms 35268 KB Output is correct
65 Correct 657 ms 32364 KB Output is correct
66 Correct 1316 ms 32976 KB Output is correct
67 Correct 1927 ms 34440 KB Output is correct
68 Correct 1908 ms 35412 KB Output is correct
69 Correct 673 ms 33620 KB Output is correct
70 Correct 651 ms 36412 KB Output is correct
71 Correct 609 ms 36476 KB Output is correct
72 Correct 784 ms 34932 KB Output is correct
73 Correct 800 ms 34852 KB Output is correct
74 Correct 1754 ms 34408 KB Output is correct
75 Correct 1913 ms 36268 KB Output is correct
76 Correct 1184 ms 34528 KB Output is correct
77 Correct 1491 ms 35456 KB Output is correct
78 Correct 1467 ms 36524 KB Output is correct
79 Correct 667 ms 33128 KB Output is correct
80 Correct 1566 ms 34516 KB Output is correct
81 Correct 1950 ms 35580 KB Output is correct
82 Correct 1893 ms 36312 KB Output is correct
83 Incorrect 298 ms 35848 KB Output isn't correct
84 Halted 0 ms 0 KB -