답안 #948100

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
948100 2024-03-17T15:16:32 Z Blagoj Colors (RMI18_colors) C++17
17 / 100
3000 ms 27480 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()

const int mxn = 2e5;

vector<int> a(mxn), b(mxn), cntA[mxn], cntB[mxn], ldr(mxn), rnk(mxn);
vector<pair<int, int>> edges, sgt[3 * mxn];

void clear(int n) {
    edges.clear();
    for (int i = 1; i <= n; i++) {
        ldr[i] = i;
        rnk[i] = 1;
        cntA[i].clear();
        cntB[i].clear();
    }
    for (int i = 1; i <= 3 * mxn; i++) sgt[i].clear();        
}

void update(int k, int l, int r, int i, int j, pair<int, int> x) {
    if (l > j || r < i) return;
    if (i <= l && r <= j) {
        sgt[k].push_back(x);
        return;
    }
    int mid = (l + r) / 2;
    update(k * 2, l, mid, i, j, x);
    update(k * 2 + 1, mid + 1, r, i, j, x);
}

bool flag;

int Find(int x) {
    if (x != ldr[x]) return ldr[x] = Find(ldr[x]);
    return ldr[x];
}

struct State {
    int x, y, xSize, ySize;
};

stack<State> s;

void Union(int x, int y) {
    x = Find(x), y = Find(y);
    if (rnk[x] < rnk[y]) swap(x, y);
    s.push({x, y, rnk[x], rnk[y]});
    if (x == y) return;
    ldr[y] = x;
    rnk[x] += rnk[y];
}

void rollback() {
    State top = s.top();
    s.pop();
    ldr[top.x] = top.x;
    ldr[top.y] = top.y;
    rnk[top.x] = top.xSize;
    rnk[top.y] = top.ySize;
}

void solve(int k, int l, int r) {
    for (auto [f, t] : sgt[k]) Union(f, t);
    if (l == r) {
        unordered_set<int> cnt;
        for (auto x : cntA[l]) cnt.insert(Find(x));
        for (auto x : cntB[l]) {
            if (!cnt.count(Find(x))) {
                flag = 0;
                break;
            }
        }
    }
    else {
        int mid = (l + r) / 2;
        solve(k * 2, l, mid);
        solve(k * 2 + 1, mid + 1, r);
    }
    for (auto [f, t] : sgt[k]) rollback();
}

void testCase() {
    int n, m;
    cin >> n >> m;
    clear(n);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        cntA[a[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
        cntB[b[i]].push_back(i);
    }
    for (int i = 0; i < m; i++) {
        int f, t;
        cin >> f >> t;
        edges.push_back({f, t});
    }
    for (int i = 1; i <= n; i++) {
        if (a[i] < b[i]) {
            cout << 0 << endl;
            return;
        }
    }
    for (auto [f, t] : edges) {
        int mx = min(a[f], a[t]);
        int mn = max(b[f], b[t]);
        if (mn <= mx) update(1, 1, n, mn, mx, {f, t});
    }
    flag = 1;
    solve(1, 1, n);
    cout << flag << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        testCase();
    }
}

Compilation message

colors.cpp: In function 'void solve(int, int, int)':
colors.cpp:84:15: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   84 |     for (auto [f, t] : sgt[k]) rollback();
      |               ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3001 ms 27348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 472 ms 27192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3059 ms 27100 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3059 ms 27100 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3001 ms 27348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3062 ms 27228 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 359 ms 27220 KB Output is correct
2 Correct 38 ms 27480 KB Output is correct
3 Correct 16 ms 27224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3001 ms 27348 KB Time limit exceeded
2 Halted 0 ms 0 KB -