Submission #879868

# Submission time Handle Problem Language Result Execution time Memory
879868 2023-11-28T08:54:04 Z vivkostov Jail (JOI22_jail) C++14
54 / 100
1012 ms 914516 KB
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int q,n,m,c,d,a[2000005],b[2000005],child[2000005],machild[2000005],used[2000005],up[2000005],bin[2000005][60],level[2000005],usedl[2000005],in,brr,brj;
vector<int>v[2000005],p[20000005];
void find_child(int beg,int par,int lev)
{
    level[beg]=lev;
    child[beg]++;
    bin[beg][0]=par;
    int w=0;
    for(int i=0;i<v[beg].size();i++)
    {
        w=v[beg][i];
        if(!child[w])
        {
            find_child(w,beg,lev+1);
            child[beg]+=child[w];
            machild[beg]=max(machild[beg],child[w]);
        }
    }
}
void heavylight(int beg,int last)
{
    in++;
    up[beg]=last;
    used[beg]=in;
    int w,ma=0;
    for(int i=0;i<v[beg].size();i++)
    {
        w=v[beg][i];
        if(!used[w]&&child[w]==machild[beg])
        {
            heavylight(w,last);
            break;
        }
    }
    for(int i=0;i<v[beg].size();i++)
    {
        w=v[beg][i];
        if(!used[w])heavylight(w,w);
    }
}
void make_start(int l,int r,int h)
{
    if(l==r)return;
    int mid=(l+r)/2;
    p[h+n].push_back(h*2+n);
    p[h+n].push_back(h*2+1+n);
    make_start(l,mid,h*2);
    make_start(mid+1,r,h*2+1);
}
void make_finish(int l,int r,int h)
{
    if(l==r)return;
    int mid=(l+r)/2;
    p[h*2+n*5].push_back(h+n*5);
    p[h*2+1+n*5].push_back(h+n*5);
    make_finish(l,mid,h*2);
    make_finish(mid+1,r,h*2+1);
}
void update(int l,int r,int ql,int qr,int h,int to,int type)
{
    if(l>qr||r<ql)return;
    if(l>=ql&&r<=qr)
    {
        if(type==1)p[h+n].push_back(to);
        if(type==2)p[to].push_back(h+n*5);
        if(type==3)p[to].push_back(h+n);
        if(type==4)p[h+n*5].push_back(to);
        return;
    }
    int mid=(l+r)/2;
    update(l,mid,ql,qr,h*2,to,type);
    update(mid+1,r,ql,qr,h*2+1,to,type);
}
void make_bin()
{
    for(int j=1;j<=30;j++)
    {
        for(int i=1;i<=n;i++)
        {
            bin[i][j]=bin[bin[i][j-1]][j-1];
        }
    }
}
void add_all()
{
    for(int i=1;i<=m;i++)
    {
        update(1,n,used[a[i]],used[a[i]],1,i,1);
        update(1,n,used[b[i]],used[b[i]],1,i,2);
    }
}
int lca(int x,int y)
{
    if(level[x]>level[y])swap(x,y);
    for(int i=30;i>=0;i--)
    {
        if(level[bin[y][i]]>=level[x])y=bin[y][i];
    }
    if(x==y)return x;
    for(int i=30;i>=0;i--)
    {
        if(bin[x][i]!=bin[y][i])
        {
            x=bin[x][i];
            y=bin[y][i];
        }
    }
    return bin[x][0];
}
void go_up_heavy(int beg,int lc,int prisoner)
{
    if(level[up[beg]]==level[lc])
    {
        update(1,n,used[up[beg]],used[beg],1,prisoner,3);
        update(1,n,used[up[beg]],used[beg],1,prisoner,4);
        return;
    }
    if(level[up[beg]]>level[lc])
    {
        update(1,n,used[up[beg]],used[beg],1,prisoner,3);
        update(1,n,used[up[beg]],used[beg],1,prisoner,4);
        go_up_heavy(bin[up[beg]][0],lc,prisoner);
        return;
    }
    update(1,n,used[lc],used[beg],1,prisoner,3);
    update(1,n,used[lc],used[beg],1,prisoner,4);
}
void add_path()
{
    for(int i=1;i<=m;i++)
    {
        int g=lca(a[i],b[i]);
        go_up_heavy(a[i],g,i);
        go_up_heavy(b[i],g,i);
    }
}
void fill_cycle(int beg)
{
    if(beg<=m)brj++;
    usedl[beg]=2;
    int w;
    for(int i=0; i<p[beg].size(); i++)
    {
        w=p[beg][i];
        if(usedl[w]==1)fill_cycle(w);
    }
}
void find_cycle(int beg)
{
    usedl[beg]=1;
    int w;
    for(int i=0; i<p[beg].size(); i++)
    {
        w=p[beg][i];
        if(!usedl[w])find_cycle(w);
    }
    if(usedl[beg]!=2&&beg<=m)
    {
        fill_cycle(beg);
        if(brj>=2)brr++;
        brj=0;
    }
}
void resh()
{
    find_child(1,0,1);
    heavylight(1,1);
    make_start(1,n,1);
    make_finish(1,n,1);
    add_all();
    make_bin();
    add_path();
    for(int i=1;i<=n*10;i++)
    {
        if(!usedl[i])find_cycle(i);
    }
    /*for(int i=1;i<=48;i++)
    {
        cout<<i<<" | ";
        for(int j=0;j<p[i].size();j++)
        {
            cout<<p[i][j]<<" ";
        }
        cout<<endl;
    }
    */
    if(brr)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
    for(int i=0;i<=n*10;i++)
    {
        a[i]=0;
        b[i]=0;
        child[i]=0;
        machild[i]=0;
        used[i]=0;
        up[i]=0;
        level[i]=0;
        usedl[i]=0;
        v[i].clear();
        for(int j=0;j<=30;j++)bin[i][j]=0;
    }
    for(int i=0;i<=10*n;i++)p[i].clear();
    in=0;
    brr=0;
    brj=0;
}
void read()
{
    cin>>q;
    for(int z=1;z<=q;z++)
    {
        cin>>n;
        for(int i=1;i<n;i++)
        {
            cin>>c>>d;
            v[c].push_back(d);
            v[d].push_back(c);
        }
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>a[i]>>b[i];
        }
        resh();
    }
}
int main()
{
    speed();
    read();
    return 0;
}

Compilation message

jail.cpp: In function 'void find_child(int, int, int)':
jail.cpp:18:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for(int i=0;i<v[beg].size();i++)
      |                 ~^~~~~~~~~~~~~~
jail.cpp: In function 'void heavylight(int, int)':
jail.cpp:35:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for(int i=0;i<v[beg].size();i++)
      |                 ~^~~~~~~~~~~~~~
jail.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for(int i=0;i<v[beg].size();i++)
      |                 ~^~~~~~~~~~~~~~
jail.cpp:34:11: warning: unused variable 'ma' [-Wunused-variable]
   34 |     int w,ma=0;
      |           ^~
jail.cpp: In function 'void fill_cycle(int)':
jail.cpp:151:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |     for(int i=0; i<p[beg].size(); i++)
      |                  ~^~~~~~~~~~~~~~
jail.cpp: In function 'void find_cycle(int)':
jail.cpp:161:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |     for(int i=0; i<p[beg].size(); i++)
      |                  ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 165 ms 532180 KB Output is correct
2 Correct 112 ms 531964 KB Output is correct
3 Correct 114 ms 532052 KB Output is correct
4 Correct 130 ms 532052 KB Output is correct
5 Correct 154 ms 532120 KB Output is correct
6 Correct 113 ms 532144 KB Output is correct
7 Correct 112 ms 532048 KB Output is correct
8 Correct 114 ms 532048 KB Output is correct
9 Correct 207 ms 548208 KB Output is correct
10 Correct 271 ms 872964 KB Output is correct
11 Correct 123 ms 532100 KB Output is correct
12 Correct 195 ms 532240 KB Output is correct
13 Correct 348 ms 879808 KB Output is correct
14 Correct 341 ms 880308 KB Output is correct
15 Correct 563 ms 886868 KB Output is correct
16 Correct 1012 ms 914516 KB Output is correct
17 Correct 405 ms 886836 KB Output is correct
18 Correct 434 ms 889168 KB Output is correct
19 Correct 391 ms 885064 KB Output is correct
20 Correct 433 ms 885072 KB Output is correct
21 Correct 409 ms 887044 KB Output is correct
22 Correct 327 ms 876368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 532076 KB Output is correct
2 Correct 112 ms 532088 KB Output is correct
3 Correct 115 ms 532148 KB Output is correct
4 Correct 117 ms 532052 KB Output is correct
5 Correct 115 ms 532112 KB Output is correct
6 Correct 123 ms 532052 KB Output is correct
7 Correct 113 ms 531924 KB Output is correct
8 Correct 113 ms 532048 KB Output is correct
9 Correct 114 ms 532020 KB Output is correct
10 Correct 113 ms 532048 KB Output is correct
11 Correct 114 ms 531916 KB Output is correct
12 Correct 112 ms 532052 KB Output is correct
13 Correct 113 ms 532336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 532076 KB Output is correct
2 Correct 112 ms 532088 KB Output is correct
3 Correct 115 ms 532148 KB Output is correct
4 Correct 117 ms 532052 KB Output is correct
5 Correct 115 ms 532112 KB Output is correct
6 Correct 123 ms 532052 KB Output is correct
7 Correct 113 ms 531924 KB Output is correct
8 Correct 113 ms 532048 KB Output is correct
9 Correct 114 ms 532020 KB Output is correct
10 Correct 113 ms 532048 KB Output is correct
11 Correct 114 ms 531916 KB Output is correct
12 Correct 112 ms 532052 KB Output is correct
13 Correct 113 ms 532336 KB Output is correct
14 Correct 112 ms 531892 KB Output is correct
15 Correct 111 ms 531932 KB Output is correct
16 Correct 113 ms 532096 KB Output is correct
17 Correct 114 ms 532224 KB Output is correct
18 Correct 112 ms 532068 KB Output is correct
19 Correct 114 ms 532236 KB Output is correct
20 Correct 114 ms 532052 KB Output is correct
21 Correct 115 ms 532048 KB Output is correct
22 Correct 114 ms 532048 KB Output is correct
23 Correct 113 ms 532052 KB Output is correct
24 Correct 112 ms 532112 KB Output is correct
25 Correct 112 ms 532108 KB Output is correct
26 Correct 116 ms 532024 KB Output is correct
27 Correct 115 ms 532048 KB Output is correct
28 Correct 121 ms 532264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 532076 KB Output is correct
2 Correct 112 ms 532088 KB Output is correct
3 Correct 115 ms 532148 KB Output is correct
4 Correct 117 ms 532052 KB Output is correct
5 Correct 115 ms 532112 KB Output is correct
6 Correct 123 ms 532052 KB Output is correct
7 Correct 113 ms 531924 KB Output is correct
8 Correct 113 ms 532048 KB Output is correct
9 Correct 114 ms 532020 KB Output is correct
10 Correct 113 ms 532048 KB Output is correct
11 Correct 114 ms 531916 KB Output is correct
12 Correct 112 ms 532052 KB Output is correct
13 Correct 113 ms 532336 KB Output is correct
14 Correct 112 ms 531892 KB Output is correct
15 Correct 111 ms 531932 KB Output is correct
16 Correct 113 ms 532096 KB Output is correct
17 Correct 114 ms 532224 KB Output is correct
18 Correct 112 ms 532068 KB Output is correct
19 Correct 114 ms 532236 KB Output is correct
20 Correct 114 ms 532052 KB Output is correct
21 Correct 115 ms 532048 KB Output is correct
22 Correct 114 ms 532048 KB Output is correct
23 Correct 113 ms 532052 KB Output is correct
24 Correct 112 ms 532112 KB Output is correct
25 Correct 112 ms 532108 KB Output is correct
26 Correct 116 ms 532024 KB Output is correct
27 Correct 115 ms 532048 KB Output is correct
28 Correct 121 ms 532264 KB Output is correct
29 Correct 116 ms 531988 KB Output is correct
30 Correct 117 ms 532128 KB Output is correct
31 Correct 116 ms 532128 KB Output is correct
32 Correct 120 ms 532348 KB Output is correct
33 Correct 114 ms 532052 KB Output is correct
34 Correct 116 ms 532052 KB Output is correct
35 Correct 119 ms 532048 KB Output is correct
36 Correct 115 ms 532236 KB Output is correct
37 Correct 115 ms 532048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 532076 KB Output is correct
2 Correct 112 ms 532088 KB Output is correct
3 Correct 115 ms 532148 KB Output is correct
4 Correct 117 ms 532052 KB Output is correct
5 Correct 115 ms 532112 KB Output is correct
6 Correct 123 ms 532052 KB Output is correct
7 Correct 113 ms 531924 KB Output is correct
8 Correct 113 ms 532048 KB Output is correct
9 Correct 114 ms 532020 KB Output is correct
10 Correct 113 ms 532048 KB Output is correct
11 Correct 114 ms 531916 KB Output is correct
12 Correct 112 ms 532052 KB Output is correct
13 Correct 113 ms 532336 KB Output is correct
14 Correct 112 ms 531892 KB Output is correct
15 Correct 111 ms 531932 KB Output is correct
16 Correct 113 ms 532096 KB Output is correct
17 Correct 114 ms 532224 KB Output is correct
18 Correct 112 ms 532068 KB Output is correct
19 Correct 114 ms 532236 KB Output is correct
20 Correct 114 ms 532052 KB Output is correct
21 Correct 115 ms 532048 KB Output is correct
22 Correct 114 ms 532048 KB Output is correct
23 Correct 113 ms 532052 KB Output is correct
24 Correct 112 ms 532112 KB Output is correct
25 Correct 112 ms 532108 KB Output is correct
26 Correct 116 ms 532024 KB Output is correct
27 Correct 115 ms 532048 KB Output is correct
28 Correct 121 ms 532264 KB Output is correct
29 Correct 116 ms 531988 KB Output is correct
30 Correct 117 ms 532128 KB Output is correct
31 Correct 116 ms 532128 KB Output is correct
32 Correct 120 ms 532348 KB Output is correct
33 Correct 114 ms 532052 KB Output is correct
34 Correct 116 ms 532052 KB Output is correct
35 Correct 119 ms 532048 KB Output is correct
36 Correct 115 ms 532236 KB Output is correct
37 Correct 115 ms 532048 KB Output is correct
38 Correct 191 ms 549396 KB Output is correct
39 Correct 247 ms 874580 KB Output is correct
40 Correct 209 ms 549200 KB Output is correct
41 Correct 214 ms 549052 KB Output is correct
42 Correct 209 ms 549340 KB Output is correct
43 Incorrect 168 ms 548688 KB Output isn't correct
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 110 ms 532052 KB Output is correct
2 Correct 110 ms 532084 KB Output is correct
3 Correct 110 ms 532084 KB Output is correct
4 Correct 110 ms 532052 KB Output is correct
5 Correct 135 ms 532048 KB Output is correct
6 Correct 111 ms 531968 KB Output is correct
7 Correct 113 ms 531932 KB Output is correct
8 Correct 110 ms 532096 KB Output is correct
9 Correct 111 ms 532100 KB Output is correct
10 Correct 111 ms 532120 KB Output is correct
11 Correct 111 ms 532192 KB Output is correct
12 Correct 116 ms 532036 KB Output is correct
13 Incorrect 169 ms 532188 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 165 ms 532180 KB Output is correct
2 Correct 112 ms 531964 KB Output is correct
3 Correct 114 ms 532052 KB Output is correct
4 Correct 130 ms 532052 KB Output is correct
5 Correct 154 ms 532120 KB Output is correct
6 Correct 113 ms 532144 KB Output is correct
7 Correct 112 ms 532048 KB Output is correct
8 Correct 114 ms 532048 KB Output is correct
9 Correct 207 ms 548208 KB Output is correct
10 Correct 271 ms 872964 KB Output is correct
11 Correct 123 ms 532100 KB Output is correct
12 Correct 195 ms 532240 KB Output is correct
13 Correct 348 ms 879808 KB Output is correct
14 Correct 341 ms 880308 KB Output is correct
15 Correct 563 ms 886868 KB Output is correct
16 Correct 1012 ms 914516 KB Output is correct
17 Correct 405 ms 886836 KB Output is correct
18 Correct 434 ms 889168 KB Output is correct
19 Correct 391 ms 885064 KB Output is correct
20 Correct 433 ms 885072 KB Output is correct
21 Correct 409 ms 887044 KB Output is correct
22 Correct 327 ms 876368 KB Output is correct
23 Correct 112 ms 532076 KB Output is correct
24 Correct 112 ms 532088 KB Output is correct
25 Correct 115 ms 532148 KB Output is correct
26 Correct 117 ms 532052 KB Output is correct
27 Correct 115 ms 532112 KB Output is correct
28 Correct 123 ms 532052 KB Output is correct
29 Correct 113 ms 531924 KB Output is correct
30 Correct 113 ms 532048 KB Output is correct
31 Correct 114 ms 532020 KB Output is correct
32 Correct 113 ms 532048 KB Output is correct
33 Correct 114 ms 531916 KB Output is correct
34 Correct 112 ms 532052 KB Output is correct
35 Correct 113 ms 532336 KB Output is correct
36 Correct 112 ms 531892 KB Output is correct
37 Correct 111 ms 531932 KB Output is correct
38 Correct 113 ms 532096 KB Output is correct
39 Correct 114 ms 532224 KB Output is correct
40 Correct 112 ms 532068 KB Output is correct
41 Correct 114 ms 532236 KB Output is correct
42 Correct 114 ms 532052 KB Output is correct
43 Correct 115 ms 532048 KB Output is correct
44 Correct 114 ms 532048 KB Output is correct
45 Correct 113 ms 532052 KB Output is correct
46 Correct 112 ms 532112 KB Output is correct
47 Correct 112 ms 532108 KB Output is correct
48 Correct 116 ms 532024 KB Output is correct
49 Correct 115 ms 532048 KB Output is correct
50 Correct 121 ms 532264 KB Output is correct
51 Correct 116 ms 531988 KB Output is correct
52 Correct 117 ms 532128 KB Output is correct
53 Correct 116 ms 532128 KB Output is correct
54 Correct 120 ms 532348 KB Output is correct
55 Correct 114 ms 532052 KB Output is correct
56 Correct 116 ms 532052 KB Output is correct
57 Correct 119 ms 532048 KB Output is correct
58 Correct 115 ms 532236 KB Output is correct
59 Correct 115 ms 532048 KB Output is correct
60 Correct 191 ms 549396 KB Output is correct
61 Correct 247 ms 874580 KB Output is correct
62 Correct 209 ms 549200 KB Output is correct
63 Correct 214 ms 549052 KB Output is correct
64 Correct 209 ms 549340 KB Output is correct
65 Incorrect 168 ms 548688 KB Output isn't correct
66 Halted 0 ms 0 KB -