Submission #640869

# Submission time Handle Problem Language Result Execution time Memory
640869 2022-09-15T12:09:58 Z Tenis0206 Colors (RMI18_colors) C++11
0 / 100
3000 ms 20692 KB
#include <bits/stdc++.h>

using namespace std;

int n,m;

int a[200005],b[200005];
bool sel[200005],ok[200005];

vector<int> G[200005],l[200005],tr[200005],pr[200005];

bool e[200005];

struct dsu
{
    int t[200005],rang[200005];
    stack<pair<int,int>> st;
    dsu()
    {
        for(int i=1;i<=200000;i++)
        {
            rang[i] = 1, t[i] = 0;
        }
    }
    int reprezentant(int x)
    {
        if(!t[x])
        {
            return x;
        }
        return reprezentant(t[x]);
    }
    void uneste(int x, int y)
    {
        x = reprezentant(x);
        y = reprezentant(y);
        st.push({x,y});
        if(x==y)
        {
            return;
        }
        if(rang[x] > rang[y])
        {
            rang[x] += rang[y];
            t[y] = x;
        }
        else
        {
            rang[y] += rang[x];
            t[x] = y;
        }
    }
    void remove_last()
    {
        int x = st.top().first;
        int y = st.top().second;
        st.pop();
        if(x==y)
        {
            return;
        }
        if(t[x]==y)
        {
            rang[y] -= rang[x];
            t[x] = x;
        }
        else
        {
            rang[x] -= rang[y];
            t[y] = y;
        }
    }
};

dsu d;

void add_node(int nod)
{
    sel[nod] = true;
    for(auto it : G[nod])
    {
        if(!sel[it])
        {
            continue;
        }
        d.uneste(nod,it);
    }
}

void remove_node(int nod)
{
    sel[nod] = false;
    for(int i=G[nod].size()-1;i>=0;i--)
    {
        int it = G[nod][i];
        if(!sel[it])
        {
            continue;
        }
        d.remove_last();
    }
}

void solve(int cul)
{
    for(auto it : tr[cul])
    {
        e[d.reprezentant(it)] = true;
    }
    for(auto it : pr[cul])
    {
        ok[it] = e[d.reprezentant(it)];
    }
    for(auto it : tr[cul])
    {
        e[d.reprezentant(it)] = false;
    }
}

void update(int ua, int ub, int val, int nod, int a, int b)
{
    if(ua<=a && ub>=b)
    {
        l[nod].push_back(val);
        return;
    }
    int mij = (a + b) >> 1;
    if(ua<=mij)
    {
        update(ua,ub,val,nod*2,a,mij);
    }
    if(ub>mij)
    {
        update(ua,ub,val,nod*2+1,mij+1,b);
    }
}

void solve_queries(int nod, int a, int b)
{
    for(auto it : l[nod])
    {
        add_node(it);
    }
    if(a==b)
    {
        solve(a);
        for(int i=l[nod].size()-1;i>=0;i--)
        {
            int it = l[nod][i];
            remove_node(it);
        }
        return;
    }
    int mij = (a + b) >> 1;
    solve_queries(nod*2,a,mij);
    solve_queries(nod*2+1,mij+1,b);
    for(int i=l[nod].size()-1;i>=0;i--)
    {
        int it = l[nod][i];
        remove_node(it);
    }
}

void solve_test()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        tr[a[i]].push_back(i);
    }
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        pr[b[i]].push_back(i);
    }
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
    {
        update(b[i],a[i],i,1,1,n);
    }
    solve_queries(1,1,n);
    bool rez = true;
    for(int i=1;i<=n;i++)
    {
        if(!ok[i])
        {
            rez = false;
        }
        ok[i] = false;
        G[i].clear();
        tr[i].clear();
        pr[i].clear();
        l[i].clear();
    }
    cout<<rez<<'\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    for(int test=1;test<=t;test++)
    {
        solve_test();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 3068 ms 20692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3057 ms 20620 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3027 ms 20692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3027 ms 20692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3068 ms 20692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3053 ms 20692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3089 ms 20692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3068 ms 20692 KB Time limit exceeded
2 Halted 0 ms 0 KB -