Submission #877771

# Submission time Handle Problem Language Result Execution time Memory
877771 2023-11-23T14:17:20 Z simona1230 Jail (JOI22_jail) C++17
61 / 100
4963 ms 311788 KB
#include <bits/stdc++.h>

using namespace std;
const int maxver=1e6;
const int maxn=500000;
int q,n,m;
vector<int> f[maxn];
void read()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        f[i].clear();
    for(int i=1; i<n; i++)
    {
        int x,y;
        cin>>x>>y;
        f[x].push_back(y);
        f[y].push_back(x);
    }
    cin>>m;
}

int num;
int ts[4*maxn];
int te[4*maxn];
int dw[maxn];
int up[maxn];

vector<int> v[maxver],o[maxver];

void add_edge(int x,int y)
{
    v[x].push_back(y);
    o[y].push_back(x);
}

void make_tree(int i,int l,int r)
{
    if(l==r)
    {
        ts[i]=num++;
        te[i]=num++;
        //cout<<ts[i]<<" "<<te[i]<<" "<<l<<" "<<r<<endl;
        dw[l]=ts[i];
        up[l]=te[i];
        return;
    }

    ts[i]=num++;
    te[i]=num++;
    //cout<<ts[i]<<" "<<te[i]<<" "<<l<<" "<<r<<endl;
    int mid=(l+r)/2;

    make_tree(i*2,l,mid);
    make_tree(i*2+1,mid+1,r);

    add_edge(ts[i],ts[i*2]);
    add_edge(ts[i],ts[i*2+1]);
    add_edge(te[i*2],te[i]);
    add_edge(te[i*2+1],te[i]);
}

int used[maxn];
int sz[maxn];

void size_(int i)
{
    used[i]=1;
    sz[i]=1;
    for(int j=0; j<f[i].size(); j++)
    {
        int nb=f[i][j];
        if(!used[nb])
        {
            size_(nb);
            sz[i]+=sz[nb];
        }
    }
}

int jump[maxn][21];

int pos[maxn];
int chain[maxn];
int lvl[maxn];
int cnt=1;

void hld(int i,int lead,int level,int par)
{
    pos[i]=cnt++;
    chain[i]=lead;
    lvl[i]=level;

    int maxx=0,big=0;
    for(int j=0; j<f[i].size(); j++)
    {
        int nb=f[i][j];
        if(sz[nb]>maxx&&nb!=par)
        {
            maxx=sz[nb];
            big=nb;
        }
    }

    if(big)
    {
        //cout<<"here "<<i<<" "<<lead<<" "<<ver<<endl;
        hld(big,lead,level+1,i);
        jump[big][0]=i;
    }

    for(int j=0; j<f[i].size(); j++)
    {
        int nb=f[i][j];
        if(nb!=big&&nb!=par)
        {
            hld(nb,nb,level+1,i);
            jump[nb][0]=i;
        }
    }
}

void prec()
{
    for(int j=1; j<=20; j++)
        for(int i=1; i<=n; i++)
            jump[i][j]=jump[jump[i][j-1]][j-1];
}

int make_jump(int x,int k)
{
    for(int i=20; i>=0; i--)
        if(k&(1<<i))x=jump[x][i];
    return x;
}

int lca(int x,int y)
{
    if(lvl[x]<lvl[y])swap(x,y);
    x=make_jump(x,abs(lvl[x]-lvl[y]));
    if(x==y)return x;

    for(int i=20; i>=0; i--)
    {
        if(jump[x][i]!=jump[y][i])
        {
            x=jump[x][i];
            y=jump[y][i];
        }
    }

    return jump[x][0];
}

void update(int i,int l,int r,int ql,int qr,int pr)
{
    if(ql>qr||r<ql||l>qr)return;

    if(ql<=l&&r<=qr)
    {
        add_edge(pr,ts[i]);
        add_edge(te[i],pr);
        return;
    }

    int mid=(l+r)/2;
    update(i*2,l,mid,ql,qr,pr);
    update(i*2+1,mid+1,r,ql,qr,pr);
}
void make_graph()
{
    for(int i=1; i<=m; i++)
    {
        int x,y;
        cin>>x>>y;
        add_edge(dw[pos[x]],i);
        add_edge(i,up[pos[y]]);

        int st,lca_=lca(x,y);
        //cout<<lca_<<endl;

        st=x;
        while(true)
        {
            int ed=chain[st];
            if(lvl[ed]<lvl[lca_])ed=lca_;
            update(1,1,n,pos[ed],pos[st],i);
            //cout<<pos[ed]<<" "<<pos[st]<<endl;
            if(ed==lca_)break;
            st=jump[ed][0];
        }

        st=y;
        while(true)
        {
            int ed=chain[st];
            if(lvl[ed]<lvl[lca_])ed=lca_;
            update(1,1,n,pos[ed],pos[st],i);
            //cout<<pos[ed]<<" "<<pos[st]<<endl;
            if(ed==lca_)break;
            st=jump[ed][0];
        }
    }
}

int comp;
stack<int> topo;
int used1[maxn];
int used2[maxn];

void dfs1(int i)
{
    used1[i]=1;
    for(int j=0; j<v[i].size(); j++)
    {
        int nb=v[i][j];
        if(!used1[nb])
            dfs1(nb);
    }
    topo.push(i);
}

void dfs2(int i)
{
    used2[i]=comp;
    for(int j=0; j<o[i].size(); j++)
    {
        int nb=o[i][j];
        if(!used2[nb])
            dfs2(nb);
    }
}
void check()
{
    for(int i=1; i<=m; i++)
        if(!used1[i])dfs1(i);

    comp=0;
    while(topo.size())
    {
        int ver=topo.top();
        topo.pop();
        if(!used2[ver])
        {
            comp++;
            dfs2(ver);
        }
    }

    bool ans=1;
    for(int i=1;i<=m;i++)
    {
        for(int j=i+1;j<=m;j++)
        {
            if(used2[i]==used2[j])ans=0;
        }
    }
    if(ans)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>q;
    while(q--)
    {
        read();

        for(int i=1; i<maxver; i++)
        {
            v[i].clear();
            o[i].clear();
        }
        memset(used,0,sizeof(used));
        memset(pos,0,sizeof(pos));
        memset(used1,0,sizeof(used1));
        memset(used2,0,sizeof(used2));
        memset(chain,0,sizeof(chain));
        memset(ts,0,sizeof(ts));
        memset(te,0,sizeof(te));
        memset(sz,0,sizeof(sz));
        memset(dw,0,sizeof(dw));
        memset(up,0,sizeof(up));
        memset(lvl,0,sizeof(lvl));
        for(int i=1; i<=n; i++)
            for(int j=0; j<=20; j++)
                jump[i][j]=0;

        size_(1);
        cnt=1;
        hld(1,1,1,-1);
        prec();
        num=m+1;
        make_tree(1,1,n);
        make_graph();
        /*for(int i=1;i<num;i++)
        {
            cout<<i<<": ";
            for(int j=0;j<v[i].size();j++)
            {
                cout<<v[i][j]<<" ";
            }
            cout<<endl;
        }*/
        check();
    }
    return 0;
}
/*
1
6
2 4
4 1
1 5
5 3
3 6
2
2 3
6 1
*/

Compilation message

jail.cpp: In function 'void size_(int)':
jail.cpp:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int j=0; j<f[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void hld(int, int, int, int)':
jail.cpp:95:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for(int j=0; j<f[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp:112:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     for(int j=0; j<f[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs1(int)':
jail.cpp:214:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  214 |     for(int j=0; j<v[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs2(int)':
jail.cpp:226:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  226 |     for(int j=0; j<o[i].size(); j++)
      |                  ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 94288 KB Output is correct
2 Correct 30 ms 94300 KB Output is correct
3 Correct 21 ms 94300 KB Output is correct
4 Correct 2244 ms 95060 KB Output is correct
5 Correct 4963 ms 94800 KB Output is correct
6 Correct 116 ms 94552 KB Output is correct
7 Correct 115 ms 94628 KB Output is correct
8 Correct 117 ms 94680 KB Output is correct
9 Correct 162 ms 97840 KB Output is correct
10 Correct 123 ms 142932 KB Output is correct
11 Correct 4778 ms 94676 KB Output is correct
12 Correct 4862 ms 95648 KB Output is correct
13 Runtime error 296 ms 311476 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 94304 KB Output is correct
2 Correct 20 ms 94300 KB Output is correct
3 Correct 115 ms 94556 KB Output is correct
4 Correct 113 ms 94552 KB Output is correct
5 Correct 112 ms 94556 KB Output is correct
6 Correct 114 ms 94556 KB Output is correct
7 Correct 113 ms 94556 KB Output is correct
8 Correct 114 ms 94556 KB Output is correct
9 Correct 113 ms 94556 KB Output is correct
10 Correct 115 ms 94576 KB Output is correct
11 Correct 102 ms 94580 KB Output is correct
12 Correct 113 ms 94544 KB Output is correct
13 Correct 112 ms 94552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 94304 KB Output is correct
2 Correct 20 ms 94300 KB Output is correct
3 Correct 115 ms 94556 KB Output is correct
4 Correct 113 ms 94552 KB Output is correct
5 Correct 112 ms 94556 KB Output is correct
6 Correct 114 ms 94556 KB Output is correct
7 Correct 113 ms 94556 KB Output is correct
8 Correct 114 ms 94556 KB Output is correct
9 Correct 113 ms 94556 KB Output is correct
10 Correct 115 ms 94576 KB Output is correct
11 Correct 102 ms 94580 KB Output is correct
12 Correct 113 ms 94544 KB Output is correct
13 Correct 112 ms 94552 KB Output is correct
14 Correct 26 ms 94300 KB Output is correct
15 Correct 31 ms 94536 KB Output is correct
16 Correct 115 ms 94596 KB Output is correct
17 Correct 114 ms 94552 KB Output is correct
18 Correct 120 ms 94584 KB Output is correct
19 Correct 111 ms 94308 KB Output is correct
20 Correct 113 ms 94584 KB Output is correct
21 Correct 113 ms 94552 KB Output is correct
22 Correct 111 ms 94552 KB Output is correct
23 Correct 112 ms 94300 KB Output is correct
24 Correct 113 ms 94548 KB Output is correct
25 Correct 116 ms 94556 KB Output is correct
26 Correct 114 ms 94440 KB Output is correct
27 Correct 115 ms 94568 KB Output is correct
28 Correct 116 ms 94296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 94304 KB Output is correct
2 Correct 20 ms 94300 KB Output is correct
3 Correct 115 ms 94556 KB Output is correct
4 Correct 113 ms 94552 KB Output is correct
5 Correct 112 ms 94556 KB Output is correct
6 Correct 114 ms 94556 KB Output is correct
7 Correct 113 ms 94556 KB Output is correct
8 Correct 114 ms 94556 KB Output is correct
9 Correct 113 ms 94556 KB Output is correct
10 Correct 115 ms 94576 KB Output is correct
11 Correct 102 ms 94580 KB Output is correct
12 Correct 113 ms 94544 KB Output is correct
13 Correct 112 ms 94552 KB Output is correct
14 Correct 26 ms 94300 KB Output is correct
15 Correct 31 ms 94536 KB Output is correct
16 Correct 115 ms 94596 KB Output is correct
17 Correct 114 ms 94552 KB Output is correct
18 Correct 120 ms 94584 KB Output is correct
19 Correct 111 ms 94308 KB Output is correct
20 Correct 113 ms 94584 KB Output is correct
21 Correct 113 ms 94552 KB Output is correct
22 Correct 111 ms 94552 KB Output is correct
23 Correct 112 ms 94300 KB Output is correct
24 Correct 113 ms 94548 KB Output is correct
25 Correct 116 ms 94556 KB Output is correct
26 Correct 114 ms 94440 KB Output is correct
27 Correct 115 ms 94568 KB Output is correct
28 Correct 116 ms 94296 KB Output is correct
29 Correct 116 ms 94628 KB Output is correct
30 Correct 117 ms 94552 KB Output is correct
31 Correct 116 ms 94656 KB Output is correct
32 Correct 115 ms 94620 KB Output is correct
33 Correct 115 ms 94808 KB Output is correct
34 Correct 118 ms 94604 KB Output is correct
35 Correct 116 ms 94556 KB Output is correct
36 Correct 115 ms 94556 KB Output is correct
37 Correct 119 ms 94548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 94304 KB Output is correct
2 Correct 20 ms 94300 KB Output is correct
3 Correct 115 ms 94556 KB Output is correct
4 Correct 113 ms 94552 KB Output is correct
5 Correct 112 ms 94556 KB Output is correct
6 Correct 114 ms 94556 KB Output is correct
7 Correct 113 ms 94556 KB Output is correct
8 Correct 114 ms 94556 KB Output is correct
9 Correct 113 ms 94556 KB Output is correct
10 Correct 115 ms 94576 KB Output is correct
11 Correct 102 ms 94580 KB Output is correct
12 Correct 113 ms 94544 KB Output is correct
13 Correct 112 ms 94552 KB Output is correct
14 Correct 26 ms 94300 KB Output is correct
15 Correct 31 ms 94536 KB Output is correct
16 Correct 115 ms 94596 KB Output is correct
17 Correct 114 ms 94552 KB Output is correct
18 Correct 120 ms 94584 KB Output is correct
19 Correct 111 ms 94308 KB Output is correct
20 Correct 113 ms 94584 KB Output is correct
21 Correct 113 ms 94552 KB Output is correct
22 Correct 111 ms 94552 KB Output is correct
23 Correct 112 ms 94300 KB Output is correct
24 Correct 113 ms 94548 KB Output is correct
25 Correct 116 ms 94556 KB Output is correct
26 Correct 114 ms 94440 KB Output is correct
27 Correct 115 ms 94568 KB Output is correct
28 Correct 116 ms 94296 KB Output is correct
29 Correct 116 ms 94628 KB Output is correct
30 Correct 117 ms 94552 KB Output is correct
31 Correct 116 ms 94656 KB Output is correct
32 Correct 115 ms 94620 KB Output is correct
33 Correct 115 ms 94808 KB Output is correct
34 Correct 118 ms 94604 KB Output is correct
35 Correct 116 ms 94556 KB Output is correct
36 Correct 115 ms 94556 KB Output is correct
37 Correct 119 ms 94548 KB Output is correct
38 Correct 162 ms 97872 KB Output is correct
39 Correct 119 ms 142932 KB Output is correct
40 Correct 183 ms 98068 KB Output is correct
41 Correct 181 ms 97872 KB Output is correct
42 Correct 165 ms 97936 KB Output is correct
43 Correct 144 ms 97108 KB Output is correct
44 Correct 143 ms 95484 KB Output is correct
45 Correct 128 ms 132688 KB Output is correct
46 Correct 128 ms 132772 KB Output is correct
47 Correct 124 ms 137980 KB Output is correct
48 Correct 115 ms 137812 KB Output is correct
49 Correct 126 ms 132948 KB Output is correct
50 Correct 123 ms 132980 KB Output is correct
51 Correct 117 ms 133972 KB Output is correct
52 Correct 107 ms 134020 KB Output is correct
53 Correct 135 ms 99056 KB Output is correct
54 Correct 124 ms 132720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 94300 KB Output is correct
2 Correct 26 ms 94460 KB Output is correct
3 Correct 31 ms 94300 KB Output is correct
4 Correct 20 ms 94300 KB Output is correct
5 Correct 4804 ms 94528 KB Output is correct
6 Correct 111 ms 94556 KB Output is correct
7 Correct 112 ms 94556 KB Output is correct
8 Correct 113 ms 94296 KB Output is correct
9 Correct 118 ms 94300 KB Output is correct
10 Correct 115 ms 94524 KB Output is correct
11 Correct 113 ms 94300 KB Output is correct
12 Correct 119 ms 94592 KB Output is correct
13 Correct 4841 ms 94780 KB Output is correct
14 Correct 4849 ms 95236 KB Output is correct
15 Correct 4851 ms 95104 KB Output is correct
16 Correct 260 ms 136580 KB Output is correct
17 Runtime error 483 ms 311788 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 94288 KB Output is correct
2 Correct 30 ms 94300 KB Output is correct
3 Correct 21 ms 94300 KB Output is correct
4 Correct 2244 ms 95060 KB Output is correct
5 Correct 4963 ms 94800 KB Output is correct
6 Correct 116 ms 94552 KB Output is correct
7 Correct 115 ms 94628 KB Output is correct
8 Correct 117 ms 94680 KB Output is correct
9 Correct 162 ms 97840 KB Output is correct
10 Correct 123 ms 142932 KB Output is correct
11 Correct 4778 ms 94676 KB Output is correct
12 Correct 4862 ms 95648 KB Output is correct
13 Runtime error 296 ms 311476 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -