Submission #877418

# Submission time Handle Problem Language Result Execution time Memory
877418 2023-11-23T08:24:24 Z simona1230 Jail (JOI22_jail) C++17
26 / 100
3398 ms 180104 KB
#include <bits/stdc++.h>

using namespace std;
const int maxn=1e6;
int q,n,m;
vector<int> f[200001];
bool test=0;
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);
    }
}
int num;
int ts[800001];// number of the vertex corresponding
int te[800001];// to the given tree and segment of the hld
int dw[200001];
int up[200001];

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

void make_tree(int i,int l,int r)
{
    if(l==r)
    {
        ts[i]=num++;
        dw[l]=ts[i];
        te[i]=num++;
        up[l]=te[i];
        //cout<<l<<" "<<r<<" "<<te[i]<<endl;
        return;
    }
    int mid=(l+r)/2;
    ts[i]=num++;
    te[i]=num++;
    //cout<<l<<" "<<r<<" "<<te[i]<<endl;
    make_tree(i*2,l,mid);
    make_tree(i*2+1,mid+1,r);
    int il=ts[i*2];
    int ir=ts[i*2+1];
    v[ts[i]].push_back(il);
    v[ts[i]].push_back(ir);
    o[il].push_back(ts[i]);
    o[ir].push_back(ts[i]);
    il=te[i*2];
    ir=te[i*2+1];
    v[il].push_back(te[i]);
    v[ir].push_back(te[i]);
    o[te[i]].push_back(il);
    o[te[i]].push_back(ir);
}

int used[200001];
int jump[200001][21];
int sz[200001];
int pos[200001];// new number of vertex i in original graph
int chain[200001];// leader of the chain in which i is
int maxx[200001];
int lvl[200001];
void necessities(int i,int h)
{
    sz[i]=1;
    used[i]=1;
    for(int j=0; j<f[i].size(); j++)
    {
        int nb=f[i][j];
        if(!used[nb])
        {
            necessities(nb,h+1);
            sz[i]+=sz[nb];
            if(sz[maxx[i]]<sz[nb])maxx[i]=nb;
        }
    }
}
int cnt=1;
void hld(int i,int lead,int h)
{
    pos[i]=cnt++;
    chain[pos[i]]=pos[lead];
    lvl[pos[i]]=h;
    if(maxx[i])
    {
        hld(maxx[i],lead,h+1);
        jump[pos[maxx[i]]][0]=pos[i];
    }
    for(int j=0; j<f[i].size(); j++)
    {
        int nb=f[i][j];
        if(pos[nb]==0)
        {
            hld(nb,nb,h+1);
            jump[pos[nb]][0]=pos[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];
            //cout<<i<<" "<<j<<" "<<jump[i][j]<<endl;
        }
    }
}
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);
    y=make_jump(y,abs(lvl[y]-lvl[x]));
    //cout<<x<<" "<<y<<endl;
    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)return;
    if(r<ql||l>qr)return;
    if(ql<=l&&r<=qr)
    {
        //cout<<l<<"-"<<r<<endl;
        int idx=ts[i];
        //cout<<idx<<endl;
        v[pr].push_back(idx);
        o[idx].push_back(pr);
        idx=te[i];
        //cout<<idx<<endl;
        v[idx].push_back(pr);
        o[pr].push_back(idx);
        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<=n;i++)
        cout<<chain[i]<<" ";
    cout<<endl;*/
    for(int i=1; i<=m; i++)
    {
        int x,y;
        cin>>x>>y;
        x=pos[x];
        y=pos[y];
        //cout<<i<<" "<<dw[pos[x]]<<" "<<up[pos[y]]<<endl;
        v[dw[x]].push_back(i);
        o[i].push_back(dw[x]);
        v[i].push_back(up[y]);
        o[up[y]].push_back(i);
        int ver=lca(x,y);
        //cout<<ver<<endl;
        int c1=x;

        int br = 0;

        while(1)
        {
            int c2=chain[c1];
            if(lvl[c2]<lvl[ver])c2=ver;
            update(1,1,n,c2,c1,i);
            //cout<<c2<<" "<<c1<<endl;
            if(c2==ver)break;
            c1=jump[c2][0];

            br++;
            //if(br >= n + 1) assert(false);

        }
        c1=y;

        br = 0;

        while(1)
        {
            int c2=chain[c1];
            if(lvl[c2]<lvl[ver])c2=ver;
            update(1,1,n,c2,c1,i);
            //cout<<c2<<" "<<c1<<endl;
            if(c2==ver)break;
            c1=jump[c2][0];

            br++;
            //if(br >= n + 1) assert(false);


        }
    }
}
int used1[maxn];
stack<int> st;
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);
        }
    }
    st.push(i);
}
int used2[maxn];
void dfs2(int i)
{
    used2[i]=1;
    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);
    }
    int cnt=0;
    while(st.size())
    {
        int vr=st.top();
        st.pop();
        if(vr<=m&&!used2[vr])
        {
            cnt++;
            dfs2(vr);
        }
    }
    if(cnt==m)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--)
    {
        test=0;
        for(int i=1; i<maxn; i++)
        {
            o[i].clear();
            v[i].clear();
        }
        memset(maxx,0,sizeof(maxx));
        memset(used,0,sizeof(used));
        memset(pos,0,sizeof(pos));
        memset(used1,0,sizeof(used1));
        memset(used2,0,sizeof(used2));
        memset(pos,0,sizeof(pos));
        read();
        cnt=1;
        necessities(1,1);
        hld(1,1,1);
        prec();
        cin>>m;
        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;
}

Compilation message

jail.cpp: In function 'void necessities(int, int)':
jail.cpp:71:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for(int j=0; j<f[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void hld(int, int, int)':
jail.cpp:93:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |     for(int j=0; j<f[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs1(int)':
jail.cpp:218:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  218 |     for(int j=0; j<v[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs2(int)':
jail.cpp:232:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  232 |     for(int j=0; j<o[i].size(); j++)
      |                  ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 72284 KB Output is correct
2 Correct 28 ms 72280 KB Output is correct
3 Correct 16 ms 72284 KB Output is correct
4 Correct 1560 ms 72520 KB Output is correct
5 Correct 3344 ms 72524 KB Output is correct
6 Correct 81 ms 72540 KB Output is correct
7 Correct 80 ms 72536 KB Output is correct
8 Correct 83 ms 72572 KB Output is correct
9 Correct 132 ms 74808 KB Output is correct
10 Correct 120 ms 121704 KB Output is correct
11 Correct 3380 ms 72484 KB Output is correct
12 Correct 3349 ms 72784 KB Output is correct
13 Correct 266 ms 133028 KB Output is correct
14 Correct 248 ms 133204 KB Output is correct
15 Correct 555 ms 142692 KB Output is correct
16 Correct 1106 ms 180104 KB Output is correct
17 Correct 303 ms 148576 KB Output is correct
18 Correct 252 ms 142012 KB Output is correct
19 Correct 282 ms 144580 KB Output is correct
20 Correct 287 ms 144684 KB Output is correct
21 Correct 355 ms 147024 KB Output is correct
22 Correct 202 ms 129876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB Output is correct
2 Correct 17 ms 72284 KB Output is correct
3 Correct 80 ms 72580 KB Output is correct
4 Correct 78 ms 72536 KB Output is correct
5 Correct 82 ms 72540 KB Output is correct
6 Correct 79 ms 72536 KB Output is correct
7 Correct 84 ms 72536 KB Output is correct
8 Correct 85 ms 72536 KB Output is correct
9 Correct 84 ms 72540 KB Output is correct
10 Correct 81 ms 72540 KB Output is correct
11 Correct 70 ms 72540 KB Output is correct
12 Correct 91 ms 72536 KB Output is correct
13 Correct 81 ms 72540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB Output is correct
2 Correct 17 ms 72284 KB Output is correct
3 Correct 80 ms 72580 KB Output is correct
4 Correct 78 ms 72536 KB Output is correct
5 Correct 82 ms 72540 KB Output is correct
6 Correct 79 ms 72536 KB Output is correct
7 Correct 84 ms 72536 KB Output is correct
8 Correct 85 ms 72536 KB Output is correct
9 Correct 84 ms 72540 KB Output is correct
10 Correct 81 ms 72540 KB Output is correct
11 Correct 70 ms 72540 KB Output is correct
12 Correct 91 ms 72536 KB Output is correct
13 Correct 81 ms 72540 KB Output is correct
14 Correct 23 ms 72280 KB Output is correct
15 Correct 28 ms 72292 KB Output is correct
16 Correct 82 ms 72792 KB Output is correct
17 Correct 81 ms 72552 KB Output is correct
18 Correct 84 ms 72536 KB Output is correct
19 Correct 86 ms 72284 KB Output is correct
20 Correct 81 ms 72536 KB Output is correct
21 Correct 80 ms 72540 KB Output is correct
22 Correct 81 ms 72540 KB Output is correct
23 Correct 81 ms 72284 KB Output is correct
24 Correct 80 ms 72280 KB Output is correct
25 Correct 82 ms 72544 KB Output is correct
26 Correct 80 ms 72504 KB Output is correct
27 Correct 83 ms 72536 KB Output is correct
28 Correct 81 ms 72280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB Output is correct
2 Correct 17 ms 72284 KB Output is correct
3 Correct 80 ms 72580 KB Output is correct
4 Correct 78 ms 72536 KB Output is correct
5 Correct 82 ms 72540 KB Output is correct
6 Correct 79 ms 72536 KB Output is correct
7 Correct 84 ms 72536 KB Output is correct
8 Correct 85 ms 72536 KB Output is correct
9 Correct 84 ms 72540 KB Output is correct
10 Correct 81 ms 72540 KB Output is correct
11 Correct 70 ms 72540 KB Output is correct
12 Correct 91 ms 72536 KB Output is correct
13 Correct 81 ms 72540 KB Output is correct
14 Correct 23 ms 72280 KB Output is correct
15 Correct 28 ms 72292 KB Output is correct
16 Correct 82 ms 72792 KB Output is correct
17 Correct 81 ms 72552 KB Output is correct
18 Correct 84 ms 72536 KB Output is correct
19 Correct 86 ms 72284 KB Output is correct
20 Correct 81 ms 72536 KB Output is correct
21 Correct 80 ms 72540 KB Output is correct
22 Correct 81 ms 72540 KB Output is correct
23 Correct 81 ms 72284 KB Output is correct
24 Correct 80 ms 72280 KB Output is correct
25 Correct 82 ms 72544 KB Output is correct
26 Correct 80 ms 72504 KB Output is correct
27 Correct 83 ms 72536 KB Output is correct
28 Correct 81 ms 72280 KB Output is correct
29 Correct 90 ms 72552 KB Output is correct
30 Correct 85 ms 72540 KB Output is correct
31 Correct 90 ms 72592 KB Output is correct
32 Correct 85 ms 72796 KB Output is correct
33 Correct 89 ms 72536 KB Output is correct
34 Correct 91 ms 72540 KB Output is correct
35 Correct 83 ms 72540 KB Output is correct
36 Correct 85 ms 72580 KB Output is correct
37 Incorrect 84 ms 72500 KB Output isn't correct
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB Output is correct
2 Correct 17 ms 72284 KB Output is correct
3 Correct 80 ms 72580 KB Output is correct
4 Correct 78 ms 72536 KB Output is correct
5 Correct 82 ms 72540 KB Output is correct
6 Correct 79 ms 72536 KB Output is correct
7 Correct 84 ms 72536 KB Output is correct
8 Correct 85 ms 72536 KB Output is correct
9 Correct 84 ms 72540 KB Output is correct
10 Correct 81 ms 72540 KB Output is correct
11 Correct 70 ms 72540 KB Output is correct
12 Correct 91 ms 72536 KB Output is correct
13 Correct 81 ms 72540 KB Output is correct
14 Correct 23 ms 72280 KB Output is correct
15 Correct 28 ms 72292 KB Output is correct
16 Correct 82 ms 72792 KB Output is correct
17 Correct 81 ms 72552 KB Output is correct
18 Correct 84 ms 72536 KB Output is correct
19 Correct 86 ms 72284 KB Output is correct
20 Correct 81 ms 72536 KB Output is correct
21 Correct 80 ms 72540 KB Output is correct
22 Correct 81 ms 72540 KB Output is correct
23 Correct 81 ms 72284 KB Output is correct
24 Correct 80 ms 72280 KB Output is correct
25 Correct 82 ms 72544 KB Output is correct
26 Correct 80 ms 72504 KB Output is correct
27 Correct 83 ms 72536 KB Output is correct
28 Correct 81 ms 72280 KB Output is correct
29 Correct 90 ms 72552 KB Output is correct
30 Correct 85 ms 72540 KB Output is correct
31 Correct 90 ms 72592 KB Output is correct
32 Correct 85 ms 72796 KB Output is correct
33 Correct 89 ms 72536 KB Output is correct
34 Correct 91 ms 72540 KB Output is correct
35 Correct 83 ms 72540 KB Output is correct
36 Correct 85 ms 72580 KB Output is correct
37 Incorrect 84 ms 72500 KB Output isn't correct
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 72280 KB Output is correct
2 Correct 20 ms 72292 KB Output is correct
3 Correct 24 ms 72492 KB Output is correct
4 Correct 16 ms 72280 KB Output is correct
5 Correct 3364 ms 72488 KB Output is correct
6 Correct 79 ms 72540 KB Output is correct
7 Correct 80 ms 72540 KB Output is correct
8 Correct 79 ms 72280 KB Output is correct
9 Correct 77 ms 72280 KB Output is correct
10 Correct 82 ms 72280 KB Output is correct
11 Correct 84 ms 72280 KB Output is correct
12 Correct 97 ms 72540 KB Output is correct
13 Incorrect 3398 ms 72836 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 72284 KB Output is correct
2 Correct 28 ms 72280 KB Output is correct
3 Correct 16 ms 72284 KB Output is correct
4 Correct 1560 ms 72520 KB Output is correct
5 Correct 3344 ms 72524 KB Output is correct
6 Correct 81 ms 72540 KB Output is correct
7 Correct 80 ms 72536 KB Output is correct
8 Correct 83 ms 72572 KB Output is correct
9 Correct 132 ms 74808 KB Output is correct
10 Correct 120 ms 121704 KB Output is correct
11 Correct 3380 ms 72484 KB Output is correct
12 Correct 3349 ms 72784 KB Output is correct
13 Correct 266 ms 133028 KB Output is correct
14 Correct 248 ms 133204 KB Output is correct
15 Correct 555 ms 142692 KB Output is correct
16 Correct 1106 ms 180104 KB Output is correct
17 Correct 303 ms 148576 KB Output is correct
18 Correct 252 ms 142012 KB Output is correct
19 Correct 282 ms 144580 KB Output is correct
20 Correct 287 ms 144684 KB Output is correct
21 Correct 355 ms 147024 KB Output is correct
22 Correct 202 ms 129876 KB Output is correct
23 Correct 19 ms 72280 KB Output is correct
24 Correct 17 ms 72284 KB Output is correct
25 Correct 80 ms 72580 KB Output is correct
26 Correct 78 ms 72536 KB Output is correct
27 Correct 82 ms 72540 KB Output is correct
28 Correct 79 ms 72536 KB Output is correct
29 Correct 84 ms 72536 KB Output is correct
30 Correct 85 ms 72536 KB Output is correct
31 Correct 84 ms 72540 KB Output is correct
32 Correct 81 ms 72540 KB Output is correct
33 Correct 70 ms 72540 KB Output is correct
34 Correct 91 ms 72536 KB Output is correct
35 Correct 81 ms 72540 KB Output is correct
36 Correct 23 ms 72280 KB Output is correct
37 Correct 28 ms 72292 KB Output is correct
38 Correct 82 ms 72792 KB Output is correct
39 Correct 81 ms 72552 KB Output is correct
40 Correct 84 ms 72536 KB Output is correct
41 Correct 86 ms 72284 KB Output is correct
42 Correct 81 ms 72536 KB Output is correct
43 Correct 80 ms 72540 KB Output is correct
44 Correct 81 ms 72540 KB Output is correct
45 Correct 81 ms 72284 KB Output is correct
46 Correct 80 ms 72280 KB Output is correct
47 Correct 82 ms 72544 KB Output is correct
48 Correct 80 ms 72504 KB Output is correct
49 Correct 83 ms 72536 KB Output is correct
50 Correct 81 ms 72280 KB Output is correct
51 Correct 90 ms 72552 KB Output is correct
52 Correct 85 ms 72540 KB Output is correct
53 Correct 90 ms 72592 KB Output is correct
54 Correct 85 ms 72796 KB Output is correct
55 Correct 89 ms 72536 KB Output is correct
56 Correct 91 ms 72540 KB Output is correct
57 Correct 83 ms 72540 KB Output is correct
58 Correct 85 ms 72580 KB Output is correct
59 Incorrect 84 ms 72500 KB Output isn't correct
60 Halted 0 ms 0 KB -