답안 #441688

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441688 2021-07-05T19:45:52 Z JovanB Colors (RMI18_colors) C++17
0 / 100
3000 ms 35560 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;
    };
    stack <operacija> st;
    void init(int g){
        n = g;
        for(int i=1; i<=n; i++) par[i] = i;
        for(int i=1; i<=n; i++) rnk[i] = 0;
    }
    int root(int x){ return (x == par[x] ? x : root(par[x])); }
    void povezi(int a, int b){
        int a1 = root(a);
        int b1 = root(b);
        st.push({a1, b1, rnk[a1], rnk[b1]});
        if(a1 == b1) return;
        if(rnk[a1] < rnk[b1]) swap(a1, b1);
        if(rnk[a1] == rnk[b1]) rnk[a1]++;
        par[b1] = a1;
    }
    void rollback(){
        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();
    }
};
 
vector <int> koji[MAXN+5];
vector <int> treba[MAXN+5];
 
vector <pair <int, int>> 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, bool> ima;
 
vector <pair <int, int>> zivi[4*MAXN+5];
 
void upd(int node, int l, int r, int tl, int tr, pair <int, int> edge){
    if(tl > r || l > tr) return;
    if(tl <= l && r <= tr){
        zivi[node].push_back(edge);
        return;
    }
    int mid = (l+r)/2;
    upd(node*2, l, mid, tl, tr, edge);
    upd(node*2+1, mid+1, r, tl, tr, edge);
}
 
DSU dsu;
 
bool dfs(int node, int l, int r){
    for(auto c : zivi[node]) dsu.povezi(c.first, c.second);
    if(l == r){
        bool svi = 1;
        ima.clear();
        for(auto c : koji[l]) ima[dsu.root(c)] = 1;
        for(auto c : treba[l]) svi &= ima[dsu.root(c)];
        for(auto c : zivi[node]) dsu.rollback();
        return svi;
    }
    int mid = (l+r)/2;
    bool moze = (dfs(node*2, l, mid) & dfs(node*2+1, mid+1, r));
    for(auto c : zivi[node]) dsu.rollback();
    return moze;
}
 
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});
    }
    for(int i=1; i<=n; i++){
        if(A[i] < B[i]){
            cout << "0\n";
            klear(n);
            return;
        }
    }
    for(auto edge : edges){
        int a = edge.first;
        int b = edge.second;
        int l = max(B[a], B[b]);
        int r = min(A[a], A[b]);
        if(l <= r) upd(1, 1, n, l, r, {a, b});
    }
    dsu.init(n);
    if(dfs(1, 1, n)) cout << "1\n";
    else cout << "0\n";
    klear(n);
    return;
}
 
int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;
 
    //freopen("input.in", "r", stdin);
    //freopen("output.out", "w", stdout);
 
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message

colors.cpp: In function 'bool dfs(int, int, int)':
colors.cpp:81:18: warning: variable 'c' set but not used [-Wunused-but-set-variable]
   81 |         for(auto c : zivi[node]) dsu.rollback();
      |                  ^
colors.cpp:86:14: warning: variable 'c' set but not used [-Wunused-but-set-variable]
   86 |     for(auto c : zivi[node]) dsu.rollback();
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3088 ms 30948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3087 ms 35560 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3098 ms 30388 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3098 ms 30388 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3088 ms 30948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3093 ms 30856 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1232 ms 32272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3088 ms 30948 KB Time limit exceeded
2 Halted 0 ms 0 KB -