Submission #851824

# Submission time Handle Problem Language Result Execution time Memory
851824 2023-09-20T16:35:19 Z prvocislo Colors (RMI18_colors) C++17
100 / 100
496 ms 52960 KB
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
 
struct dsu_rollback
{
    vector<int> p, s, ch;
    dsu_rollback(int n)
    {
        p.resize(n), s.resize(n, 1);
        for (int i = 0; i < n; i++) p[i] = i;
    }
    int root(int i) { return p[i] == i ? i : root(p[i]); }
    void merge(int i, int j)
    {
        i = root(i), j = root(j);
        if (i == j) return;
        if (s[i] < s[j]) swap(i, j);
        p[j] = i, s[i] += s[j], ch.push_back(j);
    }
    void undo(int siz)
    {
        while (ch.size() > siz)
        {
            int v = ch.back(); ch.pop_back();
            s[p[v]] -= s[v], p[v] = v;
        }
    }
};
vector<int> ok, on;
vector<vector<int> > oa, ob, g; // vyskyty a-cok a vyskty b-cok
struct udaj
{
    int l, r, u;
};
bool dandc(int l, int r, vector<udaj> &v, dsu_rollback &d)
{
    int siz = d.ch.size();
    int m = (l + r) / 2;
    vector<udaj> v2;
    for (udaj u : v)
    {
        if (u.r < l || u.l > r) continue;
        if (u.l <= l && r <= u.r)
        {
            on[u.u] = 1;
            for (int i : g[u.u]) if (on[i]) d.merge(u.u, i);
            continue;
        }
        v2.push_back(u);
    }
    bool ans = true;
    if (l == r)
    {
        for (int x : oa[l]) ok[d.root(x)] = 1;
        for (int x : ob[l]) if (!ok[d.root(x)]) ans = false;
        for (int x : oa[l]) ok[d.root(x)] = 0;
    }
    else ans = dandc(l, m, v2, d) && dandc(m + 1, r, v2, d);
    for (udaj u : v) if (u.l <= l && r <= u.r) on[u.u] = 0;
    d.undo(siz);
    return ans;
}
int solve()
{
    int n, m;
    scanf("%d%d", &n, &m);
    oa.assign(n, {}), ob.assign(n, {}), g.assign(n, {}), ok.assign(n, 0), on.assign(n, 0);
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]), oa[--a[i]].push_back(i);
    for (int i = 0; i < n; i++) scanf("%d", &b[i]), ob[--b[i]].push_back(i);
    for (int i = 0, u, v; i < m; i++) scanf("%d%d", &u, &v), g[--u].push_back(--v), g[v].push_back(u);
    vector<udaj> v;
    for (int i = 0; i < n; i++)
    {
        if (a[i] < b[i]) return 0;
        v.push_back({ b[i], a[i], i });
    }
    dsu_rollback d(n);
    return dandc(0, n - 1, v, d);
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--) printf("%d\n", solve());
    return 0;
}

Compilation message

colors.cpp: In member function 'void dsu_rollback::undo(int)':
colors.cpp:29:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |         while (ch.size() > siz)
      |                ~~~~~~~~~~^~~~~
colors.cpp: In function 'int solve()':
colors.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
colors.cpp:76:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     for (int i = 0; i < n; i++) scanf("%d", &a[i]), oa[--a[i]].push_back(i);
      |                                 ~~~~~^~~~~~~~~~~~~
colors.cpp:77:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |     for (int i = 0; i < n; i++) scanf("%d", &b[i]), ob[--b[i]].push_back(i);
      |                                 ~~~~~^~~~~~~~~~~~~
colors.cpp:78:44: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |     for (int i = 0, u, v; i < m; i++) scanf("%d%d", &u, &v), g[--u].push_back(--v), g[v].push_back(u);
      |                                       ~~~~~^~~~~~~~~~~~~~~~
colors.cpp: In function 'int main()':
colors.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     scanf("%d", &t);
      |     ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 80 ms 348 KB Output is correct
2 Correct 33 ms 348 KB Output is correct
3 Correct 3 ms 772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 448 KB Output is correct
2 Correct 27 ms 344 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 448 KB Output is correct
2 Correct 27 ms 344 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 190 ms 736 KB Output is correct
5 Correct 372 ms 14348 KB Output is correct
6 Correct 496 ms 41536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 348 KB Output is correct
2 Correct 33 ms 348 KB Output is correct
3 Correct 3 ms 772 KB Output is correct
4 Correct 90 ms 448 KB Output is correct
5 Correct 27 ms 344 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 98 ms 548 KB Output is correct
8 Correct 28 ms 516 KB Output is correct
9 Correct 4 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 476 KB Output is correct
2 Correct 340 ms 14728 KB Output is correct
3 Correct 375 ms 35652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 344 KB Output is correct
2 Correct 11 ms 344 KB Output is correct
3 Correct 5 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 348 KB Output is correct
2 Correct 33 ms 348 KB Output is correct
3 Correct 3 ms 772 KB Output is correct
4 Correct 57 ms 480 KB Output is correct
5 Correct 90 ms 448 KB Output is correct
6 Correct 27 ms 344 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 190 ms 736 KB Output is correct
9 Correct 372 ms 14348 KB Output is correct
10 Correct 496 ms 41536 KB Output is correct
11 Correct 98 ms 548 KB Output is correct
12 Correct 28 ms 516 KB Output is correct
13 Correct 4 ms 604 KB Output is correct
14 Correct 169 ms 476 KB Output is correct
15 Correct 340 ms 14728 KB Output is correct
16 Correct 375 ms 35652 KB Output is correct
17 Correct 28 ms 344 KB Output is correct
18 Correct 11 ms 344 KB Output is correct
19 Correct 5 ms 348 KB Output is correct
20 Correct 76 ms 524 KB Output is correct
21 Correct 319 ms 17404 KB Output is correct
22 Correct 468 ms 52960 KB Output is correct