Submission #477922

# Submission time Handle Problem Language Result Execution time Memory
477922 2021-10-04T15:41:44 Z stefantaga Colors (RMI18_colors) C++14
100 / 100
685 ms 69160 KB
#include <bits/stdc++.h>

using namespace std;
int viz[150005],b[150005],a[150005],ok[150005],t,n,m,i,x,y,j,ok1;
int sal[150005],fr[150005],tata[150005],rang[150005],stanga,dreapta;
struct wow
{
    int x,y;
} muchii[200005];
vector <int> cinea[150005],cineb[150005];
vector <int> arb[800005];
int parinte (int x)
{
    if (x!=tata[x])
    {
        return parinte(tata[x]);
    }
    return x;
}
int q;
struct cetin
{
    int poz,rangpoz,tatapoz;
};
cetin baubau[800005];
void update(int st,int dr,int nod,int ua,int ub,int poz)
{
    if (ua<=st&&dr<=ub)
    {
        arb[nod].push_back(poz);
        return;
    }
    int mij=(st+dr)/2;
    if (ua<=mij)
    {
        update(st,mij,2*nod,ua,ub,poz);
    }
    if (mij<ub)
    {
        update(mij+1,dr,2*nod+1,ua,ub,poz);
    }
}
void uneste(int x,int y)
{
    x=parinte(x);
    y=parinte(y);
    if (rang[x]<rang[y])
    {
        baubau[++q]=cetin{x,rang[x],tata[x]};
        tata[x]=tata[y];
    }
    else if (rang[x]>rang[y])
    {
        baubau[++q]=cetin{y,rang[y],tata[y]};
        tata[y]=tata[x];
    }
    else if (rang[x]==rang[y])
    {
        baubau[++q]=cetin{x,rang[x],tata[x]};
        tata[x]=tata[y];
        rang[y]++;
    }
}
int numar=0;
void dfs(int st,int dr,int nod)
{
    int salut2=0;
    for (int i=0; i<arb[nod].size(); i++)
    {
        int x=muchii[arb[nod][i]].x;
        int y=muchii[arb[nod][i]].y;
        if (parinte(x)!=parinte(y))
        {
            salut2++;
            uneste(x,y);
        }
    }
    if (st==dr)
    {
        int i;
        for (i=0; i<cinea[st].size(); i++)
        {
            fr[parinte(cinea[st][i])]++;
        }
        for (i=0; i<cineb[st].size(); i++)
        {
            if (fr[parinte(cineb[st][i])]!=0)
            {
                numar++;
            }
        }
        for (i=0; i<cinea[st].size(); i++)
        {
            fr[parinte(cinea[st][i])]--;
        }
        for (int i=salut2; i>=1; i--)
        {
            int pozitie=baubau[q].poz;
            tata[pozitie]=baubau[q].tatapoz;
            rang[pozitie]=baubau[q].rangpoz;
            q--;
        }
        return;
    }
    int mij=(st+dr)/2;
    dfs(st,mij,2*nod);
    dfs(mij+1,dr,2*nod+1);
    for (int i=salut2; i>=1; i--)
    {
        int pozitie=baubau[q].poz;
        tata[pozitie]=baubau[q].tatapoz;
        rang[pozitie]=baubau[q].rangpoz;
        q--;
    }
}
int max1(int a,int b)
{
    if (a>b)
    {
        return a;
    }
    return b;
}
int min1(int a,int b)
{
    if (a<b)
    {
        return a;
    }
    return b;
}
int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);
#ifdef HOME
    ifstream cin("date.in");
    ofstream cout("date.out");
#endif // HOME
    cin>>t;
    for (; t--;)
    {
        cin>>n>>m;
        for (i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        for (i=1; i<=n; i++)
        {
            cin>>b[i];
        }
        ok1=1;
        for (i=1; i<=n; i++)
        {
            tata[i]=i;
            rang[i]=1;
            fr[i]=0;
        }
        for (i=1; i<=n; i++)
        {
            cinea[i].clear();
            cineb[i].clear();
        }
        for (i=1; i<=n; i++)
        {
            cinea[a[i]].push_back(i);
            cineb[b[i]].push_back(i);
        }
        for (i=1; i<=4*n; i++)
        {
            arb[i].clear();
        }
        for (i=1; i<=m; i++)
        {
            cin>>muchii[i].x>>muchii[i].y;
        }
        for (i=1; i<=n; i++)
        {
            if (b[i]>a[i])
            {
                cout<<"0"<<'\n';
                ok1=0;
                break;
            }
        }
        if (ok1==0)
        {
            continue;
        }
        for (i=1; i<=m; i++)
        {
            x=muchii[i].x;
            y=muchii[i].y;
            stanga=max1(b[x],b[y]);
            dreapta=min1(a[x],a[y]);
            update(1,n,1,stanga,dreapta,i);
        }
        numar=0;
        q=0;
        dfs(1,n,1);
        if (numar!=n)
        {
            cout<<"0"<<'\n';
        }
        else
        {
            cout<<"1"<<'\n';
        }
    }
    return 0;
}

Compilation message

colors.cpp: In function 'void dfs(int, int, int)':
colors.cpp:68:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for (int i=0; i<arb[nod].size(); i++)
      |                   ~^~~~~~~~~~~~~~~~
colors.cpp:81:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for (i=0; i<cinea[st].size(); i++)
      |                   ~^~~~~~~~~~~~~~~~~
colors.cpp:85:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (i=0; i<cineb[st].size(); i++)
      |                   ~^~~~~~~~~~~~~~~~~
colors.cpp:92:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         for (i=0; i<cinea[st].size(); i++)
      |                   ~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 73 ms 26208 KB Output is correct
2 Correct 37 ms 26188 KB Output is correct
3 Correct 17 ms 26444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 26224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 26212 KB Output is correct
2 Correct 40 ms 26224 KB Output is correct
3 Correct 18 ms 26328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 26212 KB Output is correct
2 Correct 40 ms 26224 KB Output is correct
3 Correct 18 ms 26328 KB Output is correct
4 Correct 156 ms 26212 KB Output is correct
5 Correct 408 ms 37684 KB Output is correct
6 Correct 685 ms 52304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 26208 KB Output is correct
2 Correct 37 ms 26188 KB Output is correct
3 Correct 17 ms 26444 KB Output is correct
4 Correct 105 ms 26212 KB Output is correct
5 Correct 40 ms 26224 KB Output is correct
6 Correct 18 ms 26328 KB Output is correct
7 Correct 80 ms 26208 KB Output is correct
8 Correct 41 ms 26224 KB Output is correct
9 Correct 17 ms 26316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 26308 KB Output is correct
2 Correct 410 ms 38168 KB Output is correct
3 Correct 445 ms 52432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 26188 KB Output is correct
2 Correct 25 ms 26444 KB Output is correct
3 Correct 19 ms 26296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 26208 KB Output is correct
2 Correct 37 ms 26188 KB Output is correct
3 Correct 17 ms 26444 KB Output is correct
4 Correct 77 ms 26224 KB Output is correct
5 Correct 105 ms 26212 KB Output is correct
6 Correct 40 ms 26224 KB Output is correct
7 Correct 18 ms 26328 KB Output is correct
8 Correct 156 ms 26212 KB Output is correct
9 Correct 408 ms 37684 KB Output is correct
10 Correct 685 ms 52304 KB Output is correct
11 Correct 80 ms 26208 KB Output is correct
12 Correct 41 ms 26224 KB Output is correct
13 Correct 17 ms 26316 KB Output is correct
14 Correct 149 ms 26308 KB Output is correct
15 Correct 410 ms 38168 KB Output is correct
16 Correct 445 ms 52432 KB Output is correct
17 Correct 42 ms 26188 KB Output is correct
18 Correct 25 ms 26444 KB Output is correct
19 Correct 19 ms 26296 KB Output is correct
20 Correct 113 ms 28664 KB Output is correct
21 Correct 408 ms 46588 KB Output is correct
22 Correct 681 ms 69160 KB Output is correct