Submission #441674

# Submission time Handle Problem Language Result Execution time Memory
441674 2021-07-05T18:35:27 Z JovanB Colors (RMI18_colors) C++17
7 / 100
183 ms 14360 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int MAXN = 200000;

int A[MAXN+5];
int B[MAXN+5];

struct DSU{
    int n;
    int par[MAXN+5];
    int rnk[MAXN+5];
    struct operacija{
        int a, b, rnka, rnkb, c;
    };
    stack <operacija> st;
    DSU(int g){
        n = g;
        for(int i=1; i<=n; i++) par[i] = i;
    }
    int root(int x){ return (x == par[x] ? x : root(par[x])); }
    void povezi(int a, int b, int c){
        int a1 = root(a);
        int b1 = root(b);
        if(a1 == b1) return;
        if(rnk[a1] < rnk[b1]) swap(a1, b1);
        st.push({a1, b1, rnk[a1], rnk[b1], c});
        if(rnk[a1] == rnk[b1]) rnk[a1]++;
        par[b1] = a1;
    }
    void rollback(){
        if(st.empty()) return;
        int a = st.top().a;
        int b = st.top().b;
        rnk[a] = st.top().rnka;
        rnk[b] = st.top().rnkb;
        par[a] = a;
        par[b] = b;
        st.pop();
    }
    void brisi(int c){
        while(!st.empty() && st.top().c == c) rollback();
    }
};

vector <int> koji[MAXN+5];
vector <int> treba[MAXN+5];

struct Edge{
    int a, b, c;
    bool operator <(const Edge &x){
        return c < x.c;
    }
};

vector <Edge> edges;

void klear(int n){
    for(int i=1; i<=n; i++) koji[A[i]].clear();
    for(int i=1; i<=n; i++) treba[B[i]].clear();
    edges.clear();
}

map <int, int> ima;

void solve(){
    int n, m;
    cin >> n >> m;
    for(int i=1; i<=n; i++) cin >> A[i];
    for(int i=1; i<=n; i++) cin >> B[i];
    for(int i=1; i<=n; i++) koji[A[i]].push_back(i);
    for(int i=1; i<=n; i++) treba[B[i]].push_back(i);
    for(int i=1; i<=m; i++){
        int a, b;
        cin >> a >> b;
        edges.push_back({a, b, max(B[a], B[b])});
    }
    for(int i=1; i<=n; i++){
        if(A[i] < B[i]){
            cout << "0\n";
            klear(n);
            return;
        }
    }
    sort(edges.begin(), edges.end());
    DSU dsu(n);
    for(auto edge : edges) dsu.povezi(edge.a, edge.b, edge.c);
    for(int i=n; i>=1; i--){
        ima.clear();
        for(auto c : koji[i]) ima[dsu.root(c)] = 1;
        for(auto c : treba[i]){
            if(!ima[dsu.root(c)]){
                cout << "0\n";
                klear(n);
                return;
            }
        }
        dsu.brisi(i);
    }
    cout << "1\n";
    klear(n);
    return;
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 12676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 73 ms 12972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 105 ms 12680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 105 ms 12680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 12676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 183 ms 14360 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 12076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 12676 KB Output isn't correct
2 Halted 0 ms 0 KB -