Submission #876220

# Submission time Handle Problem Language Result Execution time Memory
876220 2023-11-21T12:50:51 Z simona1230 Jail (JOI22_jail) C++17
61 / 100
5000 ms 40140 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];
int par[120001];
int nums[120001];
int numf[120001];
void read()
{
    memset(nums,0,sizeof(nums));
    memset(numf,0,sizeof(numf));
    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])
        {
            par[nb]=i;
            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 ver;
vector<int> p1,p2;
void backtr(int i,int idx)
{
    if(i==ver)return;
    if(idx==1)p1.push_back(i);
    else p2.push_back(i);
    backtr(par[i],idx);
}
void gen(int a,int b)
{
    p.clear();
    ver=lca(a,b);
    p1.clear();
    p2.clear();
    backtr(a,1);
    backtr(b,2);
    p=p1;
    p.push_back(ver);
    for(int i=p2.size()-1; i>=0; i--)
        p.push_back(p2[i]);
}
int in[1001][1001];
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[i].push_back(j);
                //cout<<i<<" "<<j<<endl;
                o[j].push_back(i);
            }
            l1=lca(s[i],f[j]);
            l2=lca(f[i],f[j]);
            if(l1==f[j]&&l2==l||l1==l&&l2==f[j])
            {
                g[j].push_back(i);
                //cout<<j<<" "<<i<<endl;
                o[i].push_back(j);
            }
        }
    }
    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();
        //read();
        //subt();
    }
    return 0;
}

Compilation message

jail.cpp: In function 'void dfs(int)':
jail.cpp:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for(int j=0; j<v[i].size(); j++)
      |                  ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs1(int)':
jail.cpp:105:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for(int j=0;j<g[i].size();j++)
      |                 ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs2(int)':
jail.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |     for(int j=0;j<o[i].size();j++)
      |                 ~^~~~~~~~~~~~
jail.cpp: In function 'void solve()':
jail.cpp:146:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  146 |             if(l1==s[j]&&l2==l||l1==l&&l2==s[j])
      |                ~~~~~~~~^~~~~~~
jail.cpp:154:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  154 |             if(l1==f[j]&&l2==l||l1==l&&l2==f[j])
      |                ~~~~~~~~^~~~~~~
jail.cpp:130:10: warning: unused variable 'pos' [-Wunused-variable]
  130 |     bool pos=1;
      |          ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 4 ms 16476 KB Output is correct
3 Correct 3 ms 16476 KB Output is correct
4 Correct 43 ms 16760 KB Output is correct
5 Correct 78 ms 17252 KB Output is correct
6 Correct 6 ms 16476 KB Output is correct
7 Correct 6 ms 16476 KB Output is correct
8 Correct 48 ms 16708 KB Output is correct
9 Correct 1503 ms 22044 KB Output is correct
10 Correct 136 ms 40140 KB Output is correct
11 Correct 62 ms 16732 KB Output is correct
12 Correct 565 ms 17780 KB Output is correct
13 Execution timed out 5045 ms 38692 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 4 ms 16476 KB Output is correct
3 Correct 6 ms 16476 KB Output is correct
4 Correct 6 ms 16476 KB Output is correct
5 Correct 6 ms 16476 KB Output is correct
6 Correct 6 ms 16692 KB Output is correct
7 Correct 6 ms 16472 KB Output is correct
8 Correct 6 ms 16692 KB Output is correct
9 Correct 6 ms 16472 KB Output is correct
10 Correct 6 ms 16476 KB Output is correct
11 Correct 6 ms 16484 KB Output is correct
12 Correct 5 ms 16476 KB Output is correct
13 Correct 7 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 4 ms 16476 KB Output is correct
3 Correct 6 ms 16476 KB Output is correct
4 Correct 6 ms 16476 KB Output is correct
5 Correct 6 ms 16476 KB Output is correct
6 Correct 6 ms 16692 KB Output is correct
7 Correct 6 ms 16472 KB Output is correct
8 Correct 6 ms 16692 KB Output is correct
9 Correct 6 ms 16472 KB Output is correct
10 Correct 6 ms 16476 KB Output is correct
11 Correct 6 ms 16484 KB Output is correct
12 Correct 5 ms 16476 KB Output is correct
13 Correct 7 ms 16732 KB Output is correct
14 Correct 4 ms 16476 KB Output is correct
15 Correct 3 ms 16476 KB Output is correct
16 Correct 6 ms 16476 KB Output is correct
17 Correct 7 ms 16472 KB Output is correct
18 Correct 6 ms 16476 KB Output is correct
19 Correct 5 ms 16476 KB Output is correct
20 Correct 6 ms 16476 KB Output is correct
21 Correct 6 ms 16476 KB Output is correct
22 Correct 6 ms 16476 KB Output is correct
23 Correct 5 ms 16476 KB Output is correct
24 Correct 5 ms 16476 KB Output is correct
25 Correct 6 ms 16476 KB Output is correct
26 Correct 5 ms 16476 KB Output is correct
27 Correct 6 ms 16476 KB Output is correct
28 Correct 6 ms 16476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 4 ms 16476 KB Output is correct
3 Correct 6 ms 16476 KB Output is correct
4 Correct 6 ms 16476 KB Output is correct
5 Correct 6 ms 16476 KB Output is correct
6 Correct 6 ms 16692 KB Output is correct
7 Correct 6 ms 16472 KB Output is correct
8 Correct 6 ms 16692 KB Output is correct
9 Correct 6 ms 16472 KB Output is correct
10 Correct 6 ms 16476 KB Output is correct
11 Correct 6 ms 16484 KB Output is correct
12 Correct 5 ms 16476 KB Output is correct
13 Correct 7 ms 16732 KB Output is correct
14 Correct 4 ms 16476 KB Output is correct
15 Correct 3 ms 16476 KB Output is correct
16 Correct 6 ms 16476 KB Output is correct
17 Correct 7 ms 16472 KB Output is correct
18 Correct 6 ms 16476 KB Output is correct
19 Correct 5 ms 16476 KB Output is correct
20 Correct 6 ms 16476 KB Output is correct
21 Correct 6 ms 16476 KB Output is correct
22 Correct 6 ms 16476 KB Output is correct
23 Correct 5 ms 16476 KB Output is correct
24 Correct 5 ms 16476 KB Output is correct
25 Correct 6 ms 16476 KB Output is correct
26 Correct 5 ms 16476 KB Output is correct
27 Correct 6 ms 16476 KB Output is correct
28 Correct 6 ms 16476 KB Output is correct
29 Correct 46 ms 16724 KB Output is correct
30 Correct 34 ms 16472 KB Output is correct
31 Correct 38 ms 16752 KB Output is correct
32 Correct 21 ms 16472 KB Output is correct
33 Correct 8 ms 16484 KB Output is correct
34 Correct 55 ms 16732 KB Output is correct
35 Correct 72 ms 17180 KB Output is correct
36 Correct 48 ms 16704 KB Output is correct
37 Correct 35 ms 16480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 4 ms 16476 KB Output is correct
3 Correct 6 ms 16476 KB Output is correct
4 Correct 6 ms 16476 KB Output is correct
5 Correct 6 ms 16476 KB Output is correct
6 Correct 6 ms 16692 KB Output is correct
7 Correct 6 ms 16472 KB Output is correct
8 Correct 6 ms 16692 KB Output is correct
9 Correct 6 ms 16472 KB Output is correct
10 Correct 6 ms 16476 KB Output is correct
11 Correct 6 ms 16484 KB Output is correct
12 Correct 5 ms 16476 KB Output is correct
13 Correct 7 ms 16732 KB Output is correct
14 Correct 4 ms 16476 KB Output is correct
15 Correct 3 ms 16476 KB Output is correct
16 Correct 6 ms 16476 KB Output is correct
17 Correct 7 ms 16472 KB Output is correct
18 Correct 6 ms 16476 KB Output is correct
19 Correct 5 ms 16476 KB Output is correct
20 Correct 6 ms 16476 KB Output is correct
21 Correct 6 ms 16476 KB Output is correct
22 Correct 6 ms 16476 KB Output is correct
23 Correct 5 ms 16476 KB Output is correct
24 Correct 5 ms 16476 KB Output is correct
25 Correct 6 ms 16476 KB Output is correct
26 Correct 5 ms 16476 KB Output is correct
27 Correct 6 ms 16476 KB Output is correct
28 Correct 6 ms 16476 KB Output is correct
29 Correct 46 ms 16724 KB Output is correct
30 Correct 34 ms 16472 KB Output is correct
31 Correct 38 ms 16752 KB Output is correct
32 Correct 21 ms 16472 KB Output is correct
33 Correct 8 ms 16484 KB Output is correct
34 Correct 55 ms 16732 KB Output is correct
35 Correct 72 ms 17180 KB Output is correct
36 Correct 48 ms 16704 KB Output is correct
37 Correct 35 ms 16480 KB Output is correct
38 Correct 1493 ms 22268 KB Output is correct
39 Correct 129 ms 40028 KB Output is correct
40 Correct 1759 ms 20516 KB Output is correct
41 Correct 1528 ms 18136 KB Output is correct
42 Correct 1399 ms 20616 KB Output is correct
43 Correct 69 ms 17976 KB Output is correct
44 Correct 1194 ms 17472 KB Output is correct
45 Correct 190 ms 29884 KB Output is correct
46 Correct 188 ms 30032 KB Output is correct
47 Correct 220 ms 34132 KB Output is correct
48 Correct 219 ms 33684 KB Output is correct
49 Correct 225 ms 30036 KB Output is correct
50 Correct 210 ms 30288 KB Output is correct
51 Correct 92 ms 31060 KB Output is correct
52 Correct 95 ms 31312 KB Output is correct
53 Correct 1032 ms 19116 KB Output is correct
54 Correct 186 ms 30032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16472 KB Output is correct
2 Correct 4 ms 16476 KB Output is correct
3 Correct 4 ms 16468 KB Output is correct
4 Correct 4 ms 16472 KB Output is correct
5 Correct 69 ms 16732 KB Output is correct
6 Correct 5 ms 16476 KB Output is correct
7 Correct 6 ms 16476 KB Output is correct
8 Correct 4 ms 16476 KB Output is correct
9 Correct 4 ms 16476 KB Output is correct
10 Correct 7 ms 16476 KB Output is correct
11 Correct 5 ms 16476 KB Output is correct
12 Correct 47 ms 16472 KB Output is correct
13 Correct 705 ms 17288 KB Output is correct
14 Correct 379 ms 17260 KB Output is correct
15 Correct 480 ms 17156 KB Output is correct
16 Execution timed out 5014 ms 30644 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 4 ms 16476 KB Output is correct
3 Correct 3 ms 16476 KB Output is correct
4 Correct 43 ms 16760 KB Output is correct
5 Correct 78 ms 17252 KB Output is correct
6 Correct 6 ms 16476 KB Output is correct
7 Correct 6 ms 16476 KB Output is correct
8 Correct 48 ms 16708 KB Output is correct
9 Correct 1503 ms 22044 KB Output is correct
10 Correct 136 ms 40140 KB Output is correct
11 Correct 62 ms 16732 KB Output is correct
12 Correct 565 ms 17780 KB Output is correct
13 Execution timed out 5045 ms 38692 KB Time limit exceeded
14 Halted 0 ms 0 KB -