Submission #876223

# Submission time Handle Problem Language Result Execution time Memory
876223 2023-11-21T12:53:59 Z simona1230 Jail (JOI22_jail) C++17
61 / 100
5000 ms 35472 KB
#include <bits/stdc++.h>
using namespace std;
int q,n,m;
vector<int> v[120001];
vector<int> g[120001],o[120001];
int s[120001],f[120001];
void read()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        v[i].clear();
    for(int i=1; i<n; i++)
    {
        int a,b;
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    cin>>m;
    for(int i=1; i<=m; i++)
    {
        cin>>s[i]>>f[i];
    }
}
vector<int> p;
int jump[120001][21];
int used[120001];
int lvl[120001];
void dfs(int i)
{
    used[i]=1;
    for(int j=0; j<v[i].size(); j++)
    {
        int nb=v[i][j];
        if(!used[nb])
        {
            lvl[nb]=lvl[i]+1;
            jump[nb][0]=i;
            dfs(nb);
        }
    }
}
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);
    y=make_jump(y,abs(lvl[y]-lvl[x]));
    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];
}
int used1[120001];
stack<int> st;
void dfs1(int i)
{
    used1[i]=1;
    for(int j=0;j<g[i].size();j++)
    {
        int nb=g[i][j];
        if(!used1[nb])
        {
            dfs1(nb);
        }
    }
    st.push(i);
}
int used2[120001];
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 solve()
{
    bool pos=1;
    for(int i=1;i<=m;i++)
    {
        o[i].clear();
        g[i].clear();
    }
    memset(used1,0,sizeof(used1));
    memset(used2,0,sizeof(used2));
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int l=lca(s[i],f[i]);
            int l1=lca(s[i],s[j]);
            int l2=lca(f[i],s[j]);
            //cout<<l<<" "<<l1<<" "<<l2<<endl;
            if(l1==s[j]&&l2==l||l1==l&&l2==s[j])
            {
                g[j].push_back(i);
                //cout<<i<<" "<<j<<endl;
                o[i].push_back(j);
            }
            l1=lca(s[i],f[j]);
            l2=lca(f[i],f[j]);
            if(l1==f[j]&&l2==l||l1==l&&l2==f[j])
            {
                g[i].push_back(j);
                //cout<<j<<" "<<i<<endl;
                o[j].push_back(i);
            }
        }
    }
    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(!used2[vr])
        {
            cnt++;
            dfs2(vr);
        }
    }
    if(cnt==m)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}
int main()
{
    cin>>q;
    while(q--)
    {
        memset(used,0,sizeof(used));
        read();
        dfs(1);
        prec();
        solve();
    }
    return 0;
}

Compilation message

jail.cpp: In function 'void dfs(int)':
jail.cpp:32:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for(int j=0; j<v[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs1(int)':
jail.cpp:76:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for(int j=0;j<g[i].size();j++)
      |                 ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs2(int)':
jail.cpp:90:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |     for(int j=0;j<o[i].size();j++)
      |                 ~^~~~~~~~~~~~
jail.cpp: In function 'void solve()':
jail.cpp:117:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  117 |             if(l1==s[j]&&l2==l||l1==l&&l2==s[j])
      |                ~~~~~~~~^~~~~~~
jail.cpp:125:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  125 |             if(l1==f[j]&&l2==l||l1==l&&l2==f[j])
      |                ~~~~~~~~^~~~~~~
jail.cpp:101:10: warning: unused variable 'pos' [-Wunused-variable]
  101 |     bool pos=1;
      |          ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13176 KB Output is correct
4 Correct 32 ms 13144 KB Output is correct
5 Correct 64 ms 13148 KB Output is correct
6 Correct 5 ms 13148 KB Output is correct
7 Correct 5 ms 13220 KB Output is correct
8 Correct 45 ms 13404 KB Output is correct
9 Correct 1468 ms 19924 KB Output is correct
10 Correct 133 ms 35412 KB Output is correct
11 Correct 47 ms 13144 KB Output is correct
12 Correct 567 ms 13364 KB Output is correct
13 Execution timed out 5030 ms 33096 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 5 ms 13148 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 5 ms 13144 KB Output is correct
6 Correct 5 ms 13148 KB Output is correct
7 Correct 5 ms 13380 KB Output is correct
8 Correct 5 ms 13148 KB Output is correct
9 Correct 5 ms 13228 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 4 ms 13168 KB Output is correct
12 Correct 4 ms 13160 KB Output is correct
13 Correct 5 ms 13240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 5 ms 13148 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 5 ms 13144 KB Output is correct
6 Correct 5 ms 13148 KB Output is correct
7 Correct 5 ms 13380 KB Output is correct
8 Correct 5 ms 13148 KB Output is correct
9 Correct 5 ms 13228 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 4 ms 13168 KB Output is correct
12 Correct 4 ms 13160 KB Output is correct
13 Correct 5 ms 13240 KB Output is correct
14 Correct 3 ms 13148 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 5 ms 13232 KB Output is correct
17 Correct 5 ms 13144 KB Output is correct
18 Correct 5 ms 13144 KB Output is correct
19 Correct 3 ms 13144 KB Output is correct
20 Correct 5 ms 13144 KB Output is correct
21 Correct 5 ms 13148 KB Output is correct
22 Correct 5 ms 13148 KB Output is correct
23 Correct 3 ms 13400 KB Output is correct
24 Correct 3 ms 13148 KB Output is correct
25 Correct 5 ms 13144 KB Output is correct
26 Correct 3 ms 13144 KB Output is correct
27 Correct 5 ms 13148 KB Output is correct
28 Correct 3 ms 13168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 5 ms 13148 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 5 ms 13144 KB Output is correct
6 Correct 5 ms 13148 KB Output is correct
7 Correct 5 ms 13380 KB Output is correct
8 Correct 5 ms 13148 KB Output is correct
9 Correct 5 ms 13228 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 4 ms 13168 KB Output is correct
12 Correct 4 ms 13160 KB Output is correct
13 Correct 5 ms 13240 KB Output is correct
14 Correct 3 ms 13148 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 5 ms 13232 KB Output is correct
17 Correct 5 ms 13144 KB Output is correct
18 Correct 5 ms 13144 KB Output is correct
19 Correct 3 ms 13144 KB Output is correct
20 Correct 5 ms 13144 KB Output is correct
21 Correct 5 ms 13148 KB Output is correct
22 Correct 5 ms 13148 KB Output is correct
23 Correct 3 ms 13400 KB Output is correct
24 Correct 3 ms 13148 KB Output is correct
25 Correct 5 ms 13144 KB Output is correct
26 Correct 3 ms 13144 KB Output is correct
27 Correct 5 ms 13148 KB Output is correct
28 Correct 3 ms 13168 KB Output is correct
29 Correct 47 ms 13472 KB Output is correct
30 Correct 30 ms 13404 KB Output is correct
31 Correct 30 ms 13404 KB Output is correct
32 Correct 19 ms 13144 KB Output is correct
33 Correct 7 ms 13144 KB Output is correct
34 Correct 51 ms 13400 KB Output is correct
35 Correct 65 ms 13404 KB Output is correct
36 Correct 45 ms 13368 KB Output is correct
37 Correct 33 ms 13404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 5 ms 13148 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 5 ms 13144 KB Output is correct
6 Correct 5 ms 13148 KB Output is correct
7 Correct 5 ms 13380 KB Output is correct
8 Correct 5 ms 13148 KB Output is correct
9 Correct 5 ms 13228 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 4 ms 13168 KB Output is correct
12 Correct 4 ms 13160 KB Output is correct
13 Correct 5 ms 13240 KB Output is correct
14 Correct 3 ms 13148 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 5 ms 13232 KB Output is correct
17 Correct 5 ms 13144 KB Output is correct
18 Correct 5 ms 13144 KB Output is correct
19 Correct 3 ms 13144 KB Output is correct
20 Correct 5 ms 13144 KB Output is correct
21 Correct 5 ms 13148 KB Output is correct
22 Correct 5 ms 13148 KB Output is correct
23 Correct 3 ms 13400 KB Output is correct
24 Correct 3 ms 13148 KB Output is correct
25 Correct 5 ms 13144 KB Output is correct
26 Correct 3 ms 13144 KB Output is correct
27 Correct 5 ms 13148 KB Output is correct
28 Correct 3 ms 13168 KB Output is correct
29 Correct 47 ms 13472 KB Output is correct
30 Correct 30 ms 13404 KB Output is correct
31 Correct 30 ms 13404 KB Output is correct
32 Correct 19 ms 13144 KB Output is correct
33 Correct 7 ms 13144 KB Output is correct
34 Correct 51 ms 13400 KB Output is correct
35 Correct 65 ms 13404 KB Output is correct
36 Correct 45 ms 13368 KB Output is correct
37 Correct 33 ms 13404 KB Output is correct
38 Correct 1462 ms 19784 KB Output is correct
39 Correct 137 ms 35472 KB Output is correct
40 Correct 1730 ms 17744 KB Output is correct
41 Correct 1431 ms 16192 KB Output is correct
42 Correct 1314 ms 18188 KB Output is correct
43 Correct 72 ms 15752 KB Output is correct
44 Correct 1151 ms 13704 KB Output is correct
45 Correct 191 ms 25168 KB Output is correct
46 Correct 195 ms 25172 KB Output is correct
47 Correct 222 ms 29088 KB Output is correct
48 Correct 214 ms 29012 KB Output is correct
49 Correct 222 ms 25424 KB Output is correct
50 Correct 220 ms 25448 KB Output is correct
51 Correct 96 ms 26452 KB Output is correct
52 Correct 95 ms 26452 KB Output is correct
53 Correct 974 ms 15736 KB Output is correct
54 Correct 186 ms 25572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 4 ms 13368 KB Output is correct
4 Correct 4 ms 13232 KB Output is correct
5 Correct 47 ms 13148 KB Output is correct
6 Correct 4 ms 13148 KB Output is correct
7 Correct 4 ms 13148 KB Output is correct
8 Correct 3 ms 13148 KB Output is correct
9 Correct 4 ms 13148 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 3 ms 13148 KB Output is correct
12 Correct 44 ms 13388 KB Output is correct
13 Correct 565 ms 13504 KB Output is correct
14 Correct 313 ms 13396 KB Output is correct
15 Correct 439 ms 13148 KB Output is correct
16 Execution timed out 5045 ms 26060 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13176 KB Output is correct
4 Correct 32 ms 13144 KB Output is correct
5 Correct 64 ms 13148 KB Output is correct
6 Correct 5 ms 13148 KB Output is correct
7 Correct 5 ms 13220 KB Output is correct
8 Correct 45 ms 13404 KB Output is correct
9 Correct 1468 ms 19924 KB Output is correct
10 Correct 133 ms 35412 KB Output is correct
11 Correct 47 ms 13144 KB Output is correct
12 Correct 567 ms 13364 KB Output is correct
13 Execution timed out 5030 ms 33096 KB Time limit exceeded
14 Halted 0 ms 0 KB -