답안 #631509

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631509 2022-08-18T08:03:20 Z mdn2002 Jail (JOI22_jail) C++14
77 / 100
5000 ms 355116 KB
#include<bits/stdc++.h>
using namespace std;
int n , m , st [200005][21] , dp [200005] , s [200005] , t [200005] , vis [200005] , ff;
vector < int > gr [200005] , grr [200005] , ss [200005] , tt [200005];
int tree [800005];
void up ( int nod , int l , int r , int x , int val )
{
    if ( x < l || r < x ) return;
    if ( l == r )
    {
        tree [nod] += val;
        return;
    }
    int mid = ( l + r ) / 2;
    up ( nod * 2 , l , mid , x , val );
    up ( nod * 2 + 1 , mid + 1 , r , x , val );
    tree [nod] = tree [ nod * 2 ] + tree [ nod * 2 + 1 ];
}
long long qr ( int nod , int l , int r , int x , int y )
{
    if ( y < l || r < x ) return 0;
    if ( x <= l && r <= y ) return tree [nod];
    int mid = ( l + r ) / 2;
    return ( qr ( nod * 2 , l , mid , x , y ) + qr ( nod * 2 + 1 , mid + 1 , r , x , y ) );
}
void dfs ( int x , int p )
{
    dp [x] = dp [p] + 1;
    st [x][0] = p;
    for ( auto u : gr [x] )
    {
        if ( u == p ) continue;
        dfs ( u , x );
    }
}
void dfss ( int x )
{
    vis [x] = 1;
    for ( auto u : grr [x] )
    {
        if ( vis [u] == 1 )
        {
            ff = 1;
            continue;
        }
        else if ( vis [u] == 2 ) continue;
        else dfss ( u );
    }
    vis [x] = 2;
}
int lca ( int x , int y )
{
    if ( dp [x] > dp [y] ) swap ( x , y );
    int dif = dp [y] - dp [x];
    for ( int i = 0 ; i <= 20 ; i ++ )
    {
        if ( ( dif & ( 1 << i ) ) ) y = st [y][i];
    }
    if ( x == y ) return x;
    for ( int i = 20 ; i >= 0 ; i -- )
    {
        if ( st [x][i] != st [y][i] )
        {
            x = st [x][i];
            y = st [y][i];
        }
    }
    return st [x][0];
}
int inbet ( int x , int y , int z )
{
    if ( dp [z] > dp [x] && dp [z] > dp [y] ) return 0;
    int aa = lca ( x , y );
    if ( dp [aa] > dp [z] ) return 0;
    int a = lca ( x , z );
    int b = lca ( y , z );
    if ( a == z || b == z ) return 1;
    return 0;
}
int main()
{
    int q;
    cin >> q;
    while ( q -- )
    {
        ff = 0;
        cin >> n;
        int ffff = 0;
        for ( int i = 1 ; i <= n ; i ++ )
        {
            gr [i] . clear ();
            grr [i] . clear ();
            ss [i] . clear ();
            tt [i] . clear ();
            dp [i] = 0;
            vis [i] = 0;
            for ( int j = 0 ; j <= 20 ; j ++ ) st [i][j] = 0;
        }
        for ( int i = 0 ; i < n - 1 ; i ++ )
        {
            int x , y;
            cin >> x >> y;
            if ( x != i + 1 || y != i + 2 ) ffff = 1;
            gr [x] . push_back ( y );
            gr [y] . push_back ( x );
        }
        dfs ( 1 , 0 );
        for ( int i = 1 ; i <= 20 ; i ++ )
        {
            for ( int j = 1 ; j <= n ; j ++ ) st [j][i] = st [ st [j][ i - 1 ] ][ i - 1 ];
        }
        cin >> m;
        for ( int i = 1 ; i <= m ; i ++ )
        {
            cin >> s [i] >> t [i];
            ss [ s [i] ] . push_back ( i );
            tt [ t [i] ] . push_back ( i );
        }
        if ( ffff == 0 )
        {
            int ks = 1;
            vector < pair < int , int > > vv;
            for ( int i = 1 ; i <= m ; i ++ )
            {
                if ( s [i] < t [i] ) vv . push_back ( { s [i] , t [i] } );
                up ( 1 , 0 , n , s [i] , 1 );
            }
            sort ( vv . begin () , vv . end () );
            for ( int i = vv . size () - 1 ; i >= 0 ; i -- )
            {
                int x = vv [i] . first , y = vv [i] . second;
                if ( qr ( 1 , 0 , n , x + 1 , y ) >= 1 ) ks = 0;
                up ( 1 , 0 , n , x , -1 );
                up ( 1 , 0 , n , y , 1 );
            }
            vv . clear ();
            for ( int i = 1 ; i <= m ; i ++ )
            {
                if ( s [i] > t [i] ) vv . push_back ( { s [i] , t [i] } );
            }
            sort ( vv . begin () , vv . end () );
            for ( int i = 0 ; i < vv . size () ; i ++ )
            {
                int x = vv [i] . first , y = vv [i] . second;
                if ( qr ( 1 , 0 , n , y , x - 1 ) >= 1 ) ks = 0;
                up ( 1 , 0 , n , x , -1 );
                up ( 1 , 0 , n , y , 1 );
            }
            if ( ks == 0 ) cout << "No" << endl;
            else cout << "Yes" << endl;
            for ( int i = 1 ; i <= m ; i ++ ) up ( 1 , 0 , n , t [i] , -1 );
        }
        else
        {
            if ( m <= 500 )
            {
                for ( int i = 1 ; i <= m ; i ++ )
                {
                    for ( int j = 1 ; j <= m ; j ++ )
                    {
                        if ( i == j ) continue;
                        if ( inbet ( s [i] , t [i] , s [j] ) ) grr [i] . push_back ( j );
                        if ( inbet ( s [i] , t [i] , t [j] ) ) grr [j] . push_back ( i );
                    }
                }
            }
            else
            {
                for ( int i = 1 ; i <= m ; i ++ )
                {
                    int a = lca ( s [i] , t [i] ) , x = s [i];
                    while ( true )
                    {
                        for ( auto u : ss [x] )
                        {
                            if ( u != i ) grr [i] . push_back ( u );
                        }
                        for ( auto u : tt [x] )
                        {
                            if ( u != i ) grr [u] . push_back ( i );
                        }
                        if ( x == a ) break;
                        x = st [x][0];
                    }
                    x = t [i];
                    while ( true )
                    {
                        for ( auto u : ss [x] )
                        {
                            if ( u != i ) grr [i] . push_back ( u );
                        }
                        for ( auto u : tt [x] )
                        {
                            if ( u != i ) grr [u] . push_back ( i );
                        }
                        if ( a == x ) break;
                        x = st [x][0];
                    }
                }
            }
            for ( int i = 1 ; i <= m ; i ++ )
            {
                if ( vis [i] == 0 ) dfss ( i );
            }
            if ( ff ) cout << "No" << endl;
            else cout << "Yes" << endl;
        }
    }
}

/*
1
4
1 2
2 3
3 4
2
1 3
4 2
*/

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:142:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |             for ( int i = 0 ; i < vv . size () ; i ++ )
      |                               ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 33 ms 19028 KB Output is correct
5 Correct 59 ms 19156 KB Output is correct
6 Correct 12 ms 19156 KB Output is correct
7 Correct 12 ms 19156 KB Output is correct
8 Correct 14 ms 19156 KB Output is correct
9 Correct 83 ms 20368 KB Output is correct
10 Correct 101 ms 39364 KB Output is correct
11 Correct 22 ms 19028 KB Output is correct
12 Correct 84 ms 19028 KB Output is correct
13 Correct 169 ms 44392 KB Output is correct
14 Correct 167 ms 44420 KB Output is correct
15 Correct 193 ms 44200 KB Output is correct
16 Correct 364 ms 52300 KB Output is correct
17 Correct 186 ms 47200 KB Output is correct
18 Correct 252 ms 52624 KB Output is correct
19 Correct 186 ms 47300 KB Output is correct
20 Correct 201 ms 47276 KB Output is correct
21 Correct 190 ms 47056 KB Output is correct
22 Correct 180 ms 47192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 13 ms 19156 KB Output is correct
5 Correct 13 ms 19156 KB Output is correct
6 Correct 12 ms 19108 KB Output is correct
7 Correct 13 ms 19156 KB Output is correct
8 Correct 13 ms 19156 KB Output is correct
9 Correct 12 ms 19156 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 11 ms 19088 KB Output is correct
12 Correct 11 ms 19156 KB Output is correct
13 Correct 11 ms 19028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 13 ms 19156 KB Output is correct
5 Correct 13 ms 19156 KB Output is correct
6 Correct 12 ms 19108 KB Output is correct
7 Correct 13 ms 19156 KB Output is correct
8 Correct 13 ms 19156 KB Output is correct
9 Correct 12 ms 19156 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 11 ms 19088 KB Output is correct
12 Correct 11 ms 19156 KB Output is correct
13 Correct 11 ms 19028 KB Output is correct
14 Correct 12 ms 19028 KB Output is correct
15 Correct 10 ms 19028 KB Output is correct
16 Correct 14 ms 19156 KB Output is correct
17 Correct 12 ms 19156 KB Output is correct
18 Correct 12 ms 19096 KB Output is correct
19 Correct 10 ms 19028 KB Output is correct
20 Correct 13 ms 19156 KB Output is correct
21 Correct 15 ms 19156 KB Output is correct
22 Correct 12 ms 19156 KB Output is correct
23 Correct 11 ms 19032 KB Output is correct
24 Correct 13 ms 19112 KB Output is correct
25 Correct 12 ms 19156 KB Output is correct
26 Correct 11 ms 19028 KB Output is correct
27 Correct 13 ms 19156 KB Output is correct
28 Correct 9 ms 19028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 13 ms 19156 KB Output is correct
5 Correct 13 ms 19156 KB Output is correct
6 Correct 12 ms 19108 KB Output is correct
7 Correct 13 ms 19156 KB Output is correct
8 Correct 13 ms 19156 KB Output is correct
9 Correct 12 ms 19156 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 11 ms 19088 KB Output is correct
12 Correct 11 ms 19156 KB Output is correct
13 Correct 11 ms 19028 KB Output is correct
14 Correct 12 ms 19028 KB Output is correct
15 Correct 10 ms 19028 KB Output is correct
16 Correct 14 ms 19156 KB Output is correct
17 Correct 12 ms 19156 KB Output is correct
18 Correct 12 ms 19096 KB Output is correct
19 Correct 10 ms 19028 KB Output is correct
20 Correct 13 ms 19156 KB Output is correct
21 Correct 15 ms 19156 KB Output is correct
22 Correct 12 ms 19156 KB Output is correct
23 Correct 11 ms 19032 KB Output is correct
24 Correct 13 ms 19112 KB Output is correct
25 Correct 12 ms 19156 KB Output is correct
26 Correct 11 ms 19028 KB Output is correct
27 Correct 13 ms 19156 KB Output is correct
28 Correct 9 ms 19028 KB Output is correct
29 Correct 13 ms 19072 KB Output is correct
30 Correct 44 ms 19176 KB Output is correct
31 Correct 27 ms 19156 KB Output is correct
32 Correct 22 ms 19156 KB Output is correct
33 Correct 14 ms 19156 KB Output is correct
34 Correct 82 ms 19172 KB Output is correct
35 Correct 77 ms 19220 KB Output is correct
36 Correct 72 ms 19264 KB Output is correct
37 Correct 35 ms 19156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19028 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 13 ms 19156 KB Output is correct
5 Correct 13 ms 19156 KB Output is correct
6 Correct 12 ms 19108 KB Output is correct
7 Correct 13 ms 19156 KB Output is correct
8 Correct 13 ms 19156 KB Output is correct
9 Correct 12 ms 19156 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 11 ms 19088 KB Output is correct
12 Correct 11 ms 19156 KB Output is correct
13 Correct 11 ms 19028 KB Output is correct
14 Correct 12 ms 19028 KB Output is correct
15 Correct 10 ms 19028 KB Output is correct
16 Correct 14 ms 19156 KB Output is correct
17 Correct 12 ms 19156 KB Output is correct
18 Correct 12 ms 19096 KB Output is correct
19 Correct 10 ms 19028 KB Output is correct
20 Correct 13 ms 19156 KB Output is correct
21 Correct 15 ms 19156 KB Output is correct
22 Correct 12 ms 19156 KB Output is correct
23 Correct 11 ms 19032 KB Output is correct
24 Correct 13 ms 19112 KB Output is correct
25 Correct 12 ms 19156 KB Output is correct
26 Correct 11 ms 19028 KB Output is correct
27 Correct 13 ms 19156 KB Output is correct
28 Correct 9 ms 19028 KB Output is correct
29 Correct 13 ms 19072 KB Output is correct
30 Correct 44 ms 19176 KB Output is correct
31 Correct 27 ms 19156 KB Output is correct
32 Correct 22 ms 19156 KB Output is correct
33 Correct 14 ms 19156 KB Output is correct
34 Correct 82 ms 19172 KB Output is correct
35 Correct 77 ms 19220 KB Output is correct
36 Correct 72 ms 19264 KB Output is correct
37 Correct 35 ms 19156 KB Output is correct
38 Correct 77 ms 20356 KB Output is correct
39 Correct 112 ms 39308 KB Output is correct
40 Correct 1484 ms 21308 KB Output is correct
41 Correct 1700 ms 20220 KB Output is correct
42 Correct 1713 ms 21500 KB Output is correct
43 Correct 73 ms 20044 KB Output is correct
44 Correct 1646 ms 19660 KB Output is correct
45 Correct 238 ms 33756 KB Output is correct
46 Correct 289 ms 33736 KB Output is correct
47 Correct 188 ms 36396 KB Output is correct
48 Correct 198 ms 36452 KB Output is correct
49 Correct 192 ms 33924 KB Output is correct
50 Correct 198 ms 33740 KB Output is correct
51 Correct 105 ms 34636 KB Output is correct
52 Correct 112 ms 34616 KB Output is correct
53 Correct 1363 ms 20280 KB Output is correct
54 Correct 220 ms 33616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19092 KB Output is correct
2 Correct 12 ms 19100 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19048 KB Output is correct
5 Correct 22 ms 19028 KB Output is correct
6 Correct 15 ms 19144 KB Output is correct
7 Correct 13 ms 19028 KB Output is correct
8 Correct 11 ms 19012 KB Output is correct
9 Correct 11 ms 19028 KB Output is correct
10 Correct 11 ms 19028 KB Output is correct
11 Correct 12 ms 19028 KB Output is correct
12 Correct 79 ms 19148 KB Output is correct
13 Correct 648 ms 19304 KB Output is correct
14 Correct 334 ms 19132 KB Output is correct
15 Correct 543 ms 19160 KB Output is correct
16 Correct 126 ms 34764 KB Output is correct
17 Correct 282 ms 44300 KB Output is correct
18 Correct 682 ms 71432 KB Output is correct
19 Correct 139 ms 35404 KB Output is correct
20 Correct 145 ms 35532 KB Output is correct
21 Correct 146 ms 35428 KB Output is correct
22 Correct 286 ms 44796 KB Output is correct
23 Correct 213 ms 44700 KB Output is correct
24 Correct 237 ms 44564 KB Output is correct
25 Correct 232 ms 44776 KB Output is correct
26 Correct 223 ms 44696 KB Output is correct
27 Correct 329 ms 50568 KB Output is correct
28 Correct 324 ms 51876 KB Output is correct
29 Correct 348 ms 48968 KB Output is correct
30 Correct 217 ms 42392 KB Output is correct
31 Correct 208 ms 42916 KB Output is correct
32 Correct 222 ms 42052 KB Output is correct
33 Correct 202 ms 42808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 33 ms 19028 KB Output is correct
5 Correct 59 ms 19156 KB Output is correct
6 Correct 12 ms 19156 KB Output is correct
7 Correct 12 ms 19156 KB Output is correct
8 Correct 14 ms 19156 KB Output is correct
9 Correct 83 ms 20368 KB Output is correct
10 Correct 101 ms 39364 KB Output is correct
11 Correct 22 ms 19028 KB Output is correct
12 Correct 84 ms 19028 KB Output is correct
13 Correct 169 ms 44392 KB Output is correct
14 Correct 167 ms 44420 KB Output is correct
15 Correct 193 ms 44200 KB Output is correct
16 Correct 364 ms 52300 KB Output is correct
17 Correct 186 ms 47200 KB Output is correct
18 Correct 252 ms 52624 KB Output is correct
19 Correct 186 ms 47300 KB Output is correct
20 Correct 201 ms 47276 KB Output is correct
21 Correct 190 ms 47056 KB Output is correct
22 Correct 180 ms 47192 KB Output is correct
23 Correct 10 ms 19028 KB Output is correct
24 Correct 10 ms 19028 KB Output is correct
25 Correct 11 ms 19156 KB Output is correct
26 Correct 13 ms 19156 KB Output is correct
27 Correct 13 ms 19156 KB Output is correct
28 Correct 12 ms 19108 KB Output is correct
29 Correct 13 ms 19156 KB Output is correct
30 Correct 13 ms 19156 KB Output is correct
31 Correct 12 ms 19156 KB Output is correct
32 Correct 12 ms 19156 KB Output is correct
33 Correct 11 ms 19088 KB Output is correct
34 Correct 11 ms 19156 KB Output is correct
35 Correct 11 ms 19028 KB Output is correct
36 Correct 12 ms 19028 KB Output is correct
37 Correct 10 ms 19028 KB Output is correct
38 Correct 14 ms 19156 KB Output is correct
39 Correct 12 ms 19156 KB Output is correct
40 Correct 12 ms 19096 KB Output is correct
41 Correct 10 ms 19028 KB Output is correct
42 Correct 13 ms 19156 KB Output is correct
43 Correct 15 ms 19156 KB Output is correct
44 Correct 12 ms 19156 KB Output is correct
45 Correct 11 ms 19032 KB Output is correct
46 Correct 13 ms 19112 KB Output is correct
47 Correct 12 ms 19156 KB Output is correct
48 Correct 11 ms 19028 KB Output is correct
49 Correct 13 ms 19156 KB Output is correct
50 Correct 9 ms 19028 KB Output is correct
51 Correct 13 ms 19072 KB Output is correct
52 Correct 44 ms 19176 KB Output is correct
53 Correct 27 ms 19156 KB Output is correct
54 Correct 22 ms 19156 KB Output is correct
55 Correct 14 ms 19156 KB Output is correct
56 Correct 82 ms 19172 KB Output is correct
57 Correct 77 ms 19220 KB Output is correct
58 Correct 72 ms 19264 KB Output is correct
59 Correct 35 ms 19156 KB Output is correct
60 Correct 77 ms 20356 KB Output is correct
61 Correct 112 ms 39308 KB Output is correct
62 Correct 1484 ms 21308 KB Output is correct
63 Correct 1700 ms 20220 KB Output is correct
64 Correct 1713 ms 21500 KB Output is correct
65 Correct 73 ms 20044 KB Output is correct
66 Correct 1646 ms 19660 KB Output is correct
67 Correct 238 ms 33756 KB Output is correct
68 Correct 289 ms 33736 KB Output is correct
69 Correct 188 ms 36396 KB Output is correct
70 Correct 198 ms 36452 KB Output is correct
71 Correct 192 ms 33924 KB Output is correct
72 Correct 198 ms 33740 KB Output is correct
73 Correct 105 ms 34636 KB Output is correct
74 Correct 112 ms 34616 KB Output is correct
75 Correct 1363 ms 20280 KB Output is correct
76 Correct 220 ms 33616 KB Output is correct
77 Correct 11 ms 19092 KB Output is correct
78 Correct 12 ms 19100 KB Output is correct
79 Correct 10 ms 19028 KB Output is correct
80 Correct 10 ms 19048 KB Output is correct
81 Correct 22 ms 19028 KB Output is correct
82 Correct 15 ms 19144 KB Output is correct
83 Correct 13 ms 19028 KB Output is correct
84 Correct 11 ms 19012 KB Output is correct
85 Correct 11 ms 19028 KB Output is correct
86 Correct 11 ms 19028 KB Output is correct
87 Correct 12 ms 19028 KB Output is correct
88 Correct 79 ms 19148 KB Output is correct
89 Correct 648 ms 19304 KB Output is correct
90 Correct 334 ms 19132 KB Output is correct
91 Correct 543 ms 19160 KB Output is correct
92 Correct 126 ms 34764 KB Output is correct
93 Correct 282 ms 44300 KB Output is correct
94 Correct 682 ms 71432 KB Output is correct
95 Correct 139 ms 35404 KB Output is correct
96 Correct 145 ms 35532 KB Output is correct
97 Correct 146 ms 35428 KB Output is correct
98 Correct 286 ms 44796 KB Output is correct
99 Correct 213 ms 44700 KB Output is correct
100 Correct 237 ms 44564 KB Output is correct
101 Correct 232 ms 44776 KB Output is correct
102 Correct 223 ms 44696 KB Output is correct
103 Correct 329 ms 50568 KB Output is correct
104 Correct 324 ms 51876 KB Output is correct
105 Correct 348 ms 48968 KB Output is correct
106 Correct 217 ms 42392 KB Output is correct
107 Correct 208 ms 42916 KB Output is correct
108 Correct 222 ms 42052 KB Output is correct
109 Correct 202 ms 42808 KB Output is correct
110 Correct 691 ms 20168 KB Output is correct
111 Correct 80 ms 19768 KB Output is correct
112 Execution timed out 5103 ms 355116 KB Time limit exceeded
113 Halted 0 ms 0 KB -