Submission #1013142

# Submission time Handle Problem Language Result Execution time Memory
1013142 2024-07-03T08:24:49 Z ReLice Jail (JOI22_jail) C++14
77 / 100
5000 ms 231452 KB
#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>*/
template<class S,class T>
bool chmin(S &a,const T &b) {
	return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
	return a<b?(a=b)==b:false;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const ll inf=1e18;
const ll mod=998244353;
const ll N=2e5+7;
const ld eps=1e-9;
vector<vll> g(N);
vector<set<ll>> g2(N);
ll vis[N];
ll up[N][20],d[N];
ll mx;
bool flag;
vll s(N),t(N);
void dfs(ll v){
    chmax(mx,d[v]);
    for(ll i=1;i<20;i++) up[v][i]=up[up[v][i-1]][i-1];
    for(auto i : g[v]){
        if(i==up[v][0])continue;
        up[i][0]=v;
        d[i]=d[v]+1;
        dfs(i);
    }
}
ll lca(ll a,ll b){
    if(d[a]<d[b])swap(a,b);
    for(ll i=19;i>=0;i--){
        if(d[a]-d[b]>=(1ll<<i)){
            a=up[a][i];
        }
    }
    if(a==b)return a;
    for(ll i=19;i>=0;i--){
        if(d[a]>(1ll<<i) && up[a][i]!=up[b][i]){
            a=up[a][i];
            b=up[b][i];
        }
    }
    return up[a][0];
}
bool on_path(ll a,ll b,ll x){
    ll k=lca(a,x),l=lca(b,x);
    ll lc=lca(a,b);
    if((k==x && l==lc) || (k==lc && l==x))return true;
    return false;
}
bool f[N];
void check(ll v){
    f[v]=1;
    vis[v]=1;
    if(flag)return;
    for(auto i : g2[v]){
        if(f[i])flag=true;
        else if(vis[i])continue;
        if(flag){f[v]=0;return;}
        check(i);

    }
    f[v]=0;
}
void build(ll a,ll b,ll x){
    ll lc=lca(a,b);
    while(a!=lc){
        if(s[a] && s[a]!=x) g2[s[a]].ins(x);
        if(t[a] && t[a]!=x) g2[x].ins(t[a]);
        a=up[a][0];
    }
    if(s[a] && s[a]!=x) g2[s[a]].ins(x);
    if(t[a] && t[a]!=x) g2[x].ins(t[a]);
    while(b!=lc){
        if(s[b] && s[b]!=x) g2[s[b]].ins(x);
        if(t[b] && t[b]!=x) g2[x].ins(t[b]);
        b=up[b][0];
    }

}
str solve(){
    ll i,j;
    ll n;
    cin>>n;
    flag=0;
    for(i=0;i<=n;i++){
        g[i].clear();
        vis[i]=0;
        t[i]=s[i]=0;
        for(j=0;j<20;j++)up[i][j]=0;
    }
    ll a,b;
    bool bamboo=1;
    for(i=1;i<n;i++){
        cin>>a>>b;
        g[a].pb(b);
        g[b].pb(a);
        if(a+1!=b)bamboo=false;
    }
    d[i]=1;
    dfs(1);
    ll m;
    cin>>m;
    for(i=1;i<=m;i++)g2[i].clear();
    vector<pair<ll,ll> > v(1);
    vector<vll> v2(n+5);
    for(i=0;i<m;i++){
        cin>>a>>b;
        v.pb({a,b});
        v2[a].pb(i+1);
        v2[b].pb(i+1);
    }
    if(bamboo){
        bool f=0;
        deque<ll> dq;
        map<ll,ll> mp;
        for(i=1;i<=n;i++){
            if(v2[i].sz>1){
                if(mp[v2[i][0]]==mp[v2[i][1]]) return "No";
                if(mp[v2[i][1]]) swap(v2[i][0],v2[i][1]);
                if(dq[0]!=v2[i][0])return "No";
                dq.pof;
                dq.pb(v2[i][1]);
                mp[dq.bc]++;
            }
            else if(v2[i].sz){
                ll a=v2[i][0];
                if(mp[a]){
                    if(dq[0]!=a) return "No";
                    dq.pof;
                }else {
                    if(!dq.sz)f=(v[a].fr<v[a].sc);
                    else if(f!=(v[a].fr<v[a].sc))return "No";
                    dq.pb(a);
                    mp[a]++;
                }
            }
        }
        return "Yes";
    }
    if(mx<=20){
        for(i=1;i<=m;i++){
            s[v[i].fr]=i;
            t[v[i].sc]=i;
        }
        for(i=1;i<=m;i++){
            build(v[i].fr,v[i].sc,i);
        }
    }
    else{
        for(i=1;i<=m;i++){
            for(j=1;j<=m;j++){
                if(i==j) continue;
                bool s=on_path(v[i].fr,v[i].sc,v[j].fr),t=on_path(v[i].fr,v[i].sc,v[j].sc);
                if(s)g2[j].ins(i);
                if(t)g2[i].ins(j);
            }
        }
    }
    for(i=1;i<=m;i++){
        if(!vis[i]){
            check(i);
            if(flag)return "No";
        }
    }
    return "Yes";
}
signed main(){
    start();
    ll t=1;
    cin>>t;
    while(t--) cout<<solve()<<endl;
    return 0;
}
/*
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4
1 5
2 6
3 7
4 8




*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19548 KB Output is correct
2 Correct 5 ms 19756 KB Output is correct
3 Correct 5 ms 19660 KB Output is correct
4 Correct 12 ms 20092 KB Output is correct
5 Correct 21 ms 20060 KB Output is correct
6 Correct 6 ms 19800 KB Output is correct
7 Correct 5 ms 19824 KB Output is correct
8 Correct 6 ms 19704 KB Output is correct
9 Correct 31 ms 23208 KB Output is correct
10 Correct 39 ms 54100 KB Output is correct
11 Correct 8 ms 19804 KB Output is correct
12 Correct 28 ms 20516 KB Output is correct
13 Correct 64 ms 61384 KB Output is correct
14 Correct 56 ms 58308 KB Output is correct
15 Correct 54 ms 57540 KB Output is correct
16 Correct 73 ms 60608 KB Output is correct
17 Correct 61 ms 63428 KB Output is correct
18 Correct 137 ms 68032 KB Output is correct
19 Correct 62 ms 62916 KB Output is correct
20 Correct 52 ms 60616 KB Output is correct
21 Correct 56 ms 59204 KB Output is correct
22 Correct 51 ms 59044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19544 KB Output is correct
2 Correct 5 ms 19752 KB Output is correct
3 Correct 6 ms 19816 KB Output is correct
4 Correct 5 ms 19804 KB Output is correct
5 Correct 5 ms 19804 KB Output is correct
6 Correct 7 ms 19736 KB Output is correct
7 Correct 8 ms 19816 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 5 ms 19804 KB Output is correct
10 Correct 6 ms 19816 KB Output is correct
11 Correct 6 ms 19800 KB Output is correct
12 Correct 7 ms 19548 KB Output is correct
13 Correct 6 ms 19672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19544 KB Output is correct
2 Correct 5 ms 19752 KB Output is correct
3 Correct 6 ms 19816 KB Output is correct
4 Correct 5 ms 19804 KB Output is correct
5 Correct 5 ms 19804 KB Output is correct
6 Correct 7 ms 19736 KB Output is correct
7 Correct 8 ms 19816 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 5 ms 19804 KB Output is correct
10 Correct 6 ms 19816 KB Output is correct
11 Correct 6 ms 19800 KB Output is correct
12 Correct 7 ms 19548 KB Output is correct
13 Correct 6 ms 19672 KB Output is correct
14 Correct 5 ms 19544 KB Output is correct
15 Correct 5 ms 20000 KB Output is correct
16 Correct 6 ms 19632 KB Output is correct
17 Correct 5 ms 19800 KB Output is correct
18 Correct 6 ms 19800 KB Output is correct
19 Correct 4 ms 19548 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 19804 KB Output is correct
23 Correct 6 ms 19548 KB Output is correct
24 Correct 5 ms 19548 KB Output is correct
25 Correct 6 ms 19572 KB Output is correct
26 Correct 5 ms 19548 KB Output is correct
27 Correct 6 ms 19804 KB Output is correct
28 Correct 5 ms 19800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19544 KB Output is correct
2 Correct 5 ms 19752 KB Output is correct
3 Correct 6 ms 19816 KB Output is correct
4 Correct 5 ms 19804 KB Output is correct
5 Correct 5 ms 19804 KB Output is correct
6 Correct 7 ms 19736 KB Output is correct
7 Correct 8 ms 19816 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 5 ms 19804 KB Output is correct
10 Correct 6 ms 19816 KB Output is correct
11 Correct 6 ms 19800 KB Output is correct
12 Correct 7 ms 19548 KB Output is correct
13 Correct 6 ms 19672 KB Output is correct
14 Correct 5 ms 19544 KB Output is correct
15 Correct 5 ms 20000 KB Output is correct
16 Correct 6 ms 19632 KB Output is correct
17 Correct 5 ms 19800 KB Output is correct
18 Correct 6 ms 19800 KB Output is correct
19 Correct 4 ms 19548 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 19804 KB Output is correct
23 Correct 6 ms 19548 KB Output is correct
24 Correct 5 ms 19548 KB Output is correct
25 Correct 6 ms 19572 KB Output is correct
26 Correct 5 ms 19548 KB Output is correct
27 Correct 6 ms 19804 KB Output is correct
28 Correct 5 ms 19800 KB Output is correct
29 Correct 6 ms 19804 KB Output is correct
30 Correct 7 ms 19856 KB Output is correct
31 Correct 31 ms 19800 KB Output is correct
32 Correct 18 ms 19804 KB Output is correct
33 Correct 8 ms 19804 KB Output is correct
34 Correct 7 ms 19804 KB Output is correct
35 Correct 71 ms 20060 KB Output is correct
36 Correct 6 ms 19804 KB Output is correct
37 Correct 30 ms 19852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19544 KB Output is correct
2 Correct 5 ms 19752 KB Output is correct
3 Correct 6 ms 19816 KB Output is correct
4 Correct 5 ms 19804 KB Output is correct
5 Correct 5 ms 19804 KB Output is correct
6 Correct 7 ms 19736 KB Output is correct
7 Correct 8 ms 19816 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 5 ms 19804 KB Output is correct
10 Correct 6 ms 19816 KB Output is correct
11 Correct 6 ms 19800 KB Output is correct
12 Correct 7 ms 19548 KB Output is correct
13 Correct 6 ms 19672 KB Output is correct
14 Correct 5 ms 19544 KB Output is correct
15 Correct 5 ms 20000 KB Output is correct
16 Correct 6 ms 19632 KB Output is correct
17 Correct 5 ms 19800 KB Output is correct
18 Correct 6 ms 19800 KB Output is correct
19 Correct 4 ms 19548 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 19804 KB Output is correct
23 Correct 6 ms 19548 KB Output is correct
24 Correct 5 ms 19548 KB Output is correct
25 Correct 6 ms 19572 KB Output is correct
26 Correct 5 ms 19548 KB Output is correct
27 Correct 6 ms 19804 KB Output is correct
28 Correct 5 ms 19800 KB Output is correct
29 Correct 6 ms 19804 KB Output is correct
30 Correct 7 ms 19856 KB Output is correct
31 Correct 31 ms 19800 KB Output is correct
32 Correct 18 ms 19804 KB Output is correct
33 Correct 8 ms 19804 KB Output is correct
34 Correct 7 ms 19804 KB Output is correct
35 Correct 71 ms 20060 KB Output is correct
36 Correct 6 ms 19804 KB Output is correct
37 Correct 30 ms 19852 KB Output is correct
38 Correct 27 ms 23200 KB Output is correct
39 Correct 39 ms 54288 KB Output is correct
40 Correct 1763 ms 26628 KB Output is correct
41 Correct 838 ms 23064 KB Output is correct
42 Correct 1238 ms 26560 KB Output is correct
43 Correct 43 ms 23336 KB Output is correct
44 Correct 19 ms 20316 KB Output is correct
45 Correct 138 ms 47188 KB Output is correct
46 Correct 146 ms 47188 KB Output is correct
47 Correct 222 ms 50840 KB Output is correct
48 Correct 230 ms 51280 KB Output is correct
49 Correct 165 ms 47440 KB Output is correct
50 Correct 160 ms 47440 KB Output is correct
51 Correct 68 ms 48464 KB Output is correct
52 Correct 63 ms 48468 KB Output is correct
53 Correct 14 ms 22620 KB Output is correct
54 Correct 109 ms 48020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 5 ms 19544 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19548 KB Output is correct
5 Correct 8 ms 19800 KB Output is correct
6 Correct 5 ms 19768 KB Output is correct
7 Correct 6 ms 19548 KB Output is correct
8 Correct 5 ms 19544 KB Output is correct
9 Correct 4 ms 19548 KB Output is correct
10 Correct 4 ms 19572 KB Output is correct
11 Correct 4 ms 19548 KB Output is correct
12 Correct 6 ms 19804 KB Output is correct
13 Correct 36 ms 20316 KB Output is correct
14 Correct 30 ms 20396 KB Output is correct
15 Correct 37 ms 20280 KB Output is correct
16 Correct 53 ms 49260 KB Output is correct
17 Correct 271 ms 87572 KB Output is correct
18 Correct 1302 ms 231452 KB Output is correct
19 Correct 69 ms 49868 KB Output is correct
20 Correct 57 ms 50228 KB Output is correct
21 Correct 55 ms 50024 KB Output is correct
22 Correct 250 ms 96452 KB Output is correct
23 Correct 181 ms 96244 KB Output is correct
24 Correct 196 ms 96616 KB Output is correct
25 Correct 189 ms 98244 KB Output is correct
26 Correct 200 ms 97476 KB Output is correct
27 Correct 105 ms 62148 KB Output is correct
28 Correct 111 ms 66404 KB Output is correct
29 Correct 101 ms 63428 KB Output is correct
30 Correct 83 ms 58052 KB Output is correct
31 Correct 78 ms 58564 KB Output is correct
32 Correct 88 ms 57236 KB Output is correct
33 Correct 83 ms 58568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19548 KB Output is correct
2 Correct 5 ms 19756 KB Output is correct
3 Correct 5 ms 19660 KB Output is correct
4 Correct 12 ms 20092 KB Output is correct
5 Correct 21 ms 20060 KB Output is correct
6 Correct 6 ms 19800 KB Output is correct
7 Correct 5 ms 19824 KB Output is correct
8 Correct 6 ms 19704 KB Output is correct
9 Correct 31 ms 23208 KB Output is correct
10 Correct 39 ms 54100 KB Output is correct
11 Correct 8 ms 19804 KB Output is correct
12 Correct 28 ms 20516 KB Output is correct
13 Correct 64 ms 61384 KB Output is correct
14 Correct 56 ms 58308 KB Output is correct
15 Correct 54 ms 57540 KB Output is correct
16 Correct 73 ms 60608 KB Output is correct
17 Correct 61 ms 63428 KB Output is correct
18 Correct 137 ms 68032 KB Output is correct
19 Correct 62 ms 62916 KB Output is correct
20 Correct 52 ms 60616 KB Output is correct
21 Correct 56 ms 59204 KB Output is correct
22 Correct 51 ms 59044 KB Output is correct
23 Correct 5 ms 19544 KB Output is correct
24 Correct 5 ms 19752 KB Output is correct
25 Correct 6 ms 19816 KB Output is correct
26 Correct 5 ms 19804 KB Output is correct
27 Correct 5 ms 19804 KB Output is correct
28 Correct 7 ms 19736 KB Output is correct
29 Correct 8 ms 19816 KB Output is correct
30 Correct 5 ms 19804 KB Output is correct
31 Correct 5 ms 19804 KB Output is correct
32 Correct 6 ms 19816 KB Output is correct
33 Correct 6 ms 19800 KB Output is correct
34 Correct 7 ms 19548 KB Output is correct
35 Correct 6 ms 19672 KB Output is correct
36 Correct 5 ms 19544 KB Output is correct
37 Correct 5 ms 20000 KB Output is correct
38 Correct 6 ms 19632 KB Output is correct
39 Correct 5 ms 19800 KB Output is correct
40 Correct 6 ms 19800 KB Output is correct
41 Correct 4 ms 19548 KB Output is correct
42 Correct 5 ms 19804 KB Output is correct
43 Correct 5 ms 19804 KB Output is correct
44 Correct 5 ms 19804 KB Output is correct
45 Correct 6 ms 19548 KB Output is correct
46 Correct 5 ms 19548 KB Output is correct
47 Correct 6 ms 19572 KB Output is correct
48 Correct 5 ms 19548 KB Output is correct
49 Correct 6 ms 19804 KB Output is correct
50 Correct 5 ms 19800 KB Output is correct
51 Correct 6 ms 19804 KB Output is correct
52 Correct 7 ms 19856 KB Output is correct
53 Correct 31 ms 19800 KB Output is correct
54 Correct 18 ms 19804 KB Output is correct
55 Correct 8 ms 19804 KB Output is correct
56 Correct 7 ms 19804 KB Output is correct
57 Correct 71 ms 20060 KB Output is correct
58 Correct 6 ms 19804 KB Output is correct
59 Correct 30 ms 19852 KB Output is correct
60 Correct 27 ms 23200 KB Output is correct
61 Correct 39 ms 54288 KB Output is correct
62 Correct 1763 ms 26628 KB Output is correct
63 Correct 838 ms 23064 KB Output is correct
64 Correct 1238 ms 26560 KB Output is correct
65 Correct 43 ms 23336 KB Output is correct
66 Correct 19 ms 20316 KB Output is correct
67 Correct 138 ms 47188 KB Output is correct
68 Correct 146 ms 47188 KB Output is correct
69 Correct 222 ms 50840 KB Output is correct
70 Correct 230 ms 51280 KB Output is correct
71 Correct 165 ms 47440 KB Output is correct
72 Correct 160 ms 47440 KB Output is correct
73 Correct 68 ms 48464 KB Output is correct
74 Correct 63 ms 48468 KB Output is correct
75 Correct 14 ms 22620 KB Output is correct
76 Correct 109 ms 48020 KB Output is correct
77 Correct 4 ms 19548 KB Output is correct
78 Correct 5 ms 19544 KB Output is correct
79 Correct 4 ms 19548 KB Output is correct
80 Correct 4 ms 19548 KB Output is correct
81 Correct 8 ms 19800 KB Output is correct
82 Correct 5 ms 19768 KB Output is correct
83 Correct 6 ms 19548 KB Output is correct
84 Correct 5 ms 19544 KB Output is correct
85 Correct 4 ms 19548 KB Output is correct
86 Correct 4 ms 19572 KB Output is correct
87 Correct 4 ms 19548 KB Output is correct
88 Correct 6 ms 19804 KB Output is correct
89 Correct 36 ms 20316 KB Output is correct
90 Correct 30 ms 20396 KB Output is correct
91 Correct 37 ms 20280 KB Output is correct
92 Correct 53 ms 49260 KB Output is correct
93 Correct 271 ms 87572 KB Output is correct
94 Correct 1302 ms 231452 KB Output is correct
95 Correct 69 ms 49868 KB Output is correct
96 Correct 57 ms 50228 KB Output is correct
97 Correct 55 ms 50024 KB Output is correct
98 Correct 250 ms 96452 KB Output is correct
99 Correct 181 ms 96244 KB Output is correct
100 Correct 196 ms 96616 KB Output is correct
101 Correct 189 ms 98244 KB Output is correct
102 Correct 200 ms 97476 KB Output is correct
103 Correct 105 ms 62148 KB Output is correct
104 Correct 111 ms 66404 KB Output is correct
105 Correct 101 ms 63428 KB Output is correct
106 Correct 83 ms 58052 KB Output is correct
107 Correct 78 ms 58564 KB Output is correct
108 Correct 88 ms 57236 KB Output is correct
109 Correct 83 ms 58568 KB Output is correct
110 Correct 42 ms 20560 KB Output is correct
111 Correct 103 ms 20260 KB Output is correct
112 Execution timed out 5011 ms 148292 KB Time limit exceeded
113 Halted 0 ms 0 KB -