답안 #874674

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
874674 2023-11-17T14:43:06 Z winter0101 Jail (JOI22_jail) C++14
100 / 100
776 ms 115096 KB
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define lastbit(i) __builtin_ctz(i)
const int maxn=1e5+2e4+9;
/*
nhan xet: neu co the chuyen duoc thi ta co the chuyen tung thang 1
chung minh: dat day a voi a[i] la thang thu i di chuyen tu dau den dau
xet 1 doan l r voi a[l]=a[r] va a[l]->a[r-1] doi mot khac nhau
ta se chung minh giam duoc do dai doan l r di 1
goi thang x la thang trong thao tac a[l] va a[r]
gia su trong thao tac a[l] thang x di tu dinh v1 den v2, a[r] tu v2 den v3
nhan xet la cac thao tac tu l+1 den r-1 khong di qua dinh v2
nen ta co the goi 3 tap nhu sau la p la tap cac thao tac di chuyen trong tplt cua v1,q trong tplt cua v3, r la con lai
sau khi bo dinh v2
p,q,r la lay tu trong l+1 den r-1
nhan xet ta co the viet lai cac thao tac la q a[l] a[r] p r va co the gop a[l] voi a[r] thanh mot va ta co the giam dan ve dung m lan
=>dpcm
*/
vector<int>a[maxn];
pii b[maxn];
int sub[maxn];
int d[maxn],p[maxn];
void dfs(int u,int par){
sub[u]=1;
for (auto &v:a[u]){
if (v==par)continue;
d[v]=d[u]+1;
p[v]=u;
dfs(v,u);
sub[u]+=sub[v];
if (a[u][0]==par||sub[a[u][0]]<sub[v])swap(a[u][0],v);
}
}
int pos[maxn],h[maxn],tle=0,rev[maxn];
void hld(int u,int par,int head){
pos[u]=++tle;
h[u]=head;
if (a[u][0]!=par){
hld(a[u][0],u,head);
}
for (auto v:a[u]){
    if (v==par||v==a[u][0])continue;
    hld(v,u,v);
}
}
vector<int>g[maxn*9];//two segtree and m
bool fl=1;
int low[maxn*9],num[maxn*9],tme=0;
int n,m;
void build(int id,int l,int r){
if (l==r){
rev[l]=id;
return;
}
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
g[id].pb(id*2);
g[id].pb(id*2+1);
g[id*2+4*n].pb(id+4*n);
g[id*2+1+4*n].pb(id+4*n);
}
void addedg(int id,int l,int r,int u,int v,int ver){
if (l>v||r<u||u>v)return;
if (u<=l&&r<=v){
g[ver+8*n].pb(id);
g[id+4*n].pb(ver+8*n);
return;
}
int mid=(l+r)/2;
addedg(id*2,l,mid,u,v,ver);
addedg(id*2+1,mid+1,r,u,v,ver);
}
void takepath(int id,int u,int v){
bool f1=1,f2=1;
while (h[u]!=h[v]){
if (d[h[u]]<d[h[v]]){
swap(f1,f2);
swap(u,v);
}
addedg(1,1,n,pos[h[u]],pos[u]-f1,id);
f1=0;
u=p[h[u]];
}
if (pos[u]>pos[v]){
swap(u,v);
swap(f1,f2);
}
addedg(1,1,n,pos[u]+f1,pos[v]-f2,id);
}
stack<int>t;
void redfs(int u){
t.push(u);
low[u]=num[u]=++tme;
for (auto v:g[u]){
if (num[v]==-1)continue;
if (num[v])low[u]=min(low[u],low[v]);
else {
    redfs(v);
    low[u]=min(low[u],low[v]);
}
}
if (low[u]==num[u]){
    int dem=0;
    while (!t.empty()&&t.top()!=u){
    num[t.top()]=-1;
    dem+=(t.top()>8*n);
    t.pop();
    }
    dem+=(u>8*n);
    if (dem>1)fl=0;
    num[u]=-1;
    t.pop();
}
}
int t1[maxn];
void solve(){
    cin>>n;
    for1(i,1,n){
    a[i].clear();
    t1[i]=0;
    }
    for1(i,1,9*n){
    g[i].clear();
    num[i]=low[i]=0;
    }
    for1(i,1,n-1){
    int u,v;
    cin>>u>>v;
    a[u].pb(v);
    a[v].pb(u);
    }
    dfs(1,0);
    tle=0;
    hld(1,0,1);
    build(1,1,n);
    cin>>m;
    for1(i,1,m){
    cin>>b[i].fi>>b[i].se;
    g[i+8*n].pb(rev[pos[b[i].fi]]+4*n);
    g[rev[pos[b[i].se]]].pb(i+8*n);
    t1[b[i].se]=i;
    takepath(i,b[i].fi,b[i].se);
    }
    for1(i,1,m){
    if (t1[b[i].fi]){
    g[i+8*n].pb(t1[b[i].fi]+8*n);
    }
    }
    tme=0;
    fl=1;
    for1(i,1,8*n+m)if (!num[i])redfs(i);
    if (fl)cout<<"Yes"<<'\n';
    else cout<<"No"<<'\n';
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("temp.INP","r",stdin);
    //freopen("temp.OUT","w",stdout);
    int test=1;
    cin>>test;
    while (test--)solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 36956 KB Output is correct
2 Correct 9 ms 36956 KB Output is correct
3 Correct 9 ms 36956 KB Output is correct
4 Correct 18 ms 37228 KB Output is correct
5 Correct 31 ms 37568 KB Output is correct
6 Correct 10 ms 36956 KB Output is correct
7 Correct 9 ms 37000 KB Output is correct
8 Correct 10 ms 36952 KB Output is correct
9 Correct 49 ms 39508 KB Output is correct
10 Correct 94 ms 67288 KB Output is correct
11 Correct 14 ms 37152 KB Output is correct
12 Correct 42 ms 37960 KB Output is correct
13 Correct 132 ms 72228 KB Output is correct
14 Correct 135 ms 74064 KB Output is correct
15 Correct 342 ms 83948 KB Output is correct
16 Correct 561 ms 115096 KB Output is correct
17 Correct 182 ms 80972 KB Output is correct
18 Correct 110 ms 75604 KB Output is correct
19 Correct 147 ms 78360 KB Output is correct
20 Correct 173 ms 78624 KB Output is correct
21 Correct 182 ms 81176 KB Output is correct
22 Correct 97 ms 73040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37032 KB Output is correct
2 Correct 8 ms 36972 KB Output is correct
3 Correct 9 ms 37044 KB Output is correct
4 Correct 9 ms 36956 KB Output is correct
5 Correct 9 ms 37116 KB Output is correct
6 Correct 9 ms 36956 KB Output is correct
7 Correct 9 ms 37112 KB Output is correct
8 Correct 9 ms 36956 KB Output is correct
9 Correct 10 ms 36952 KB Output is correct
10 Correct 10 ms 37104 KB Output is correct
11 Correct 10 ms 36956 KB Output is correct
12 Correct 9 ms 36964 KB Output is correct
13 Correct 8 ms 36952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37032 KB Output is correct
2 Correct 8 ms 36972 KB Output is correct
3 Correct 9 ms 37044 KB Output is correct
4 Correct 9 ms 36956 KB Output is correct
5 Correct 9 ms 37116 KB Output is correct
6 Correct 9 ms 36956 KB Output is correct
7 Correct 9 ms 37112 KB Output is correct
8 Correct 9 ms 36956 KB Output is correct
9 Correct 10 ms 36952 KB Output is correct
10 Correct 10 ms 37104 KB Output is correct
11 Correct 10 ms 36956 KB Output is correct
12 Correct 9 ms 36964 KB Output is correct
13 Correct 8 ms 36952 KB Output is correct
14 Correct 8 ms 36956 KB Output is correct
15 Correct 8 ms 36956 KB Output is correct
16 Correct 9 ms 37040 KB Output is correct
17 Correct 12 ms 36956 KB Output is correct
18 Correct 9 ms 36956 KB Output is correct
19 Correct 8 ms 36956 KB Output is correct
20 Correct 9 ms 36964 KB Output is correct
21 Correct 10 ms 37120 KB Output is correct
22 Correct 9 ms 36952 KB Output is correct
23 Correct 9 ms 36956 KB Output is correct
24 Correct 8 ms 36956 KB Output is correct
25 Correct 9 ms 36956 KB Output is correct
26 Correct 8 ms 36956 KB Output is correct
27 Correct 9 ms 36956 KB Output is correct
28 Correct 9 ms 36956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37032 KB Output is correct
2 Correct 8 ms 36972 KB Output is correct
3 Correct 9 ms 37044 KB Output is correct
4 Correct 9 ms 36956 KB Output is correct
5 Correct 9 ms 37116 KB Output is correct
6 Correct 9 ms 36956 KB Output is correct
7 Correct 9 ms 37112 KB Output is correct
8 Correct 9 ms 36956 KB Output is correct
9 Correct 10 ms 36952 KB Output is correct
10 Correct 10 ms 37104 KB Output is correct
11 Correct 10 ms 36956 KB Output is correct
12 Correct 9 ms 36964 KB Output is correct
13 Correct 8 ms 36952 KB Output is correct
14 Correct 8 ms 36956 KB Output is correct
15 Correct 8 ms 36956 KB Output is correct
16 Correct 9 ms 37040 KB Output is correct
17 Correct 12 ms 36956 KB Output is correct
18 Correct 9 ms 36956 KB Output is correct
19 Correct 8 ms 36956 KB Output is correct
20 Correct 9 ms 36964 KB Output is correct
21 Correct 10 ms 37120 KB Output is correct
22 Correct 9 ms 36952 KB Output is correct
23 Correct 9 ms 36956 KB Output is correct
24 Correct 8 ms 36956 KB Output is correct
25 Correct 9 ms 36956 KB Output is correct
26 Correct 8 ms 36956 KB Output is correct
27 Correct 9 ms 36956 KB Output is correct
28 Correct 9 ms 36956 KB Output is correct
29 Correct 11 ms 37212 KB Output is correct
30 Correct 10 ms 36956 KB Output is correct
31 Correct 10 ms 36956 KB Output is correct
32 Correct 11 ms 37128 KB Output is correct
33 Correct 9 ms 37120 KB Output is correct
34 Correct 10 ms 36960 KB Output is correct
35 Correct 9 ms 36956 KB Output is correct
36 Correct 9 ms 36956 KB Output is correct
37 Correct 9 ms 36956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37032 KB Output is correct
2 Correct 8 ms 36972 KB Output is correct
3 Correct 9 ms 37044 KB Output is correct
4 Correct 9 ms 36956 KB Output is correct
5 Correct 9 ms 37116 KB Output is correct
6 Correct 9 ms 36956 KB Output is correct
7 Correct 9 ms 37112 KB Output is correct
8 Correct 9 ms 36956 KB Output is correct
9 Correct 10 ms 36952 KB Output is correct
10 Correct 10 ms 37104 KB Output is correct
11 Correct 10 ms 36956 KB Output is correct
12 Correct 9 ms 36964 KB Output is correct
13 Correct 8 ms 36952 KB Output is correct
14 Correct 8 ms 36956 KB Output is correct
15 Correct 8 ms 36956 KB Output is correct
16 Correct 9 ms 37040 KB Output is correct
17 Correct 12 ms 36956 KB Output is correct
18 Correct 9 ms 36956 KB Output is correct
19 Correct 8 ms 36956 KB Output is correct
20 Correct 9 ms 36964 KB Output is correct
21 Correct 10 ms 37120 KB Output is correct
22 Correct 9 ms 36952 KB Output is correct
23 Correct 9 ms 36956 KB Output is correct
24 Correct 8 ms 36956 KB Output is correct
25 Correct 9 ms 36956 KB Output is correct
26 Correct 8 ms 36956 KB Output is correct
27 Correct 9 ms 36956 KB Output is correct
28 Correct 9 ms 36956 KB Output is correct
29 Correct 11 ms 37212 KB Output is correct
30 Correct 10 ms 36956 KB Output is correct
31 Correct 10 ms 36956 KB Output is correct
32 Correct 11 ms 37128 KB Output is correct
33 Correct 9 ms 37120 KB Output is correct
34 Correct 10 ms 36960 KB Output is correct
35 Correct 9 ms 36956 KB Output is correct
36 Correct 9 ms 36956 KB Output is correct
37 Correct 9 ms 36956 KB Output is correct
38 Correct 42 ms 39716 KB Output is correct
39 Correct 84 ms 67084 KB Output is correct
40 Correct 56 ms 39552 KB Output is correct
41 Correct 53 ms 39356 KB Output is correct
42 Correct 42 ms 39608 KB Output is correct
43 Correct 37 ms 39016 KB Output is correct
44 Correct 22 ms 37464 KB Output is correct
45 Correct 118 ms 57912 KB Output is correct
46 Correct 90 ms 57680 KB Output is correct
47 Correct 78 ms 62528 KB Output is correct
48 Correct 86 ms 62540 KB Output is correct
49 Correct 89 ms 57984 KB Output is correct
50 Correct 91 ms 58112 KB Output is correct
51 Correct 78 ms 59180 KB Output is correct
52 Correct 100 ms 59216 KB Output is correct
53 Correct 21 ms 38748 KB Output is correct
54 Correct 92 ms 57684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 36952 KB Output is correct
2 Correct 8 ms 36956 KB Output is correct
3 Correct 8 ms 36956 KB Output is correct
4 Correct 10 ms 36956 KB Output is correct
5 Correct 15 ms 36960 KB Output is correct
6 Correct 9 ms 37044 KB Output is correct
7 Correct 8 ms 36980 KB Output is correct
8 Correct 8 ms 36956 KB Output is correct
9 Correct 8 ms 36956 KB Output is correct
10 Correct 8 ms 36956 KB Output is correct
11 Correct 8 ms 36952 KB Output is correct
12 Correct 10 ms 36956 KB Output is correct
13 Correct 32 ms 37424 KB Output is correct
14 Correct 47 ms 37720 KB Output is correct
15 Correct 38 ms 37724 KB Output is correct
16 Correct 124 ms 59552 KB Output is correct
17 Correct 351 ms 72056 KB Output is correct
18 Correct 557 ms 91772 KB Output is correct
19 Correct 169 ms 61284 KB Output is correct
20 Correct 204 ms 61200 KB Output is correct
21 Correct 170 ms 61320 KB Output is correct
22 Correct 288 ms 69392 KB Output is correct
23 Correct 222 ms 68256 KB Output is correct
24 Correct 236 ms 68144 KB Output is correct
25 Correct 270 ms 68440 KB Output is correct
26 Correct 267 ms 68112 KB Output is correct
27 Correct 208 ms 71556 KB Output is correct
28 Correct 235 ms 77180 KB Output is correct
29 Correct 213 ms 72032 KB Output is correct
30 Correct 198 ms 66768 KB Output is correct
31 Correct 213 ms 72492 KB Output is correct
32 Correct 258 ms 68044 KB Output is correct
33 Correct 216 ms 72272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 36956 KB Output is correct
2 Correct 9 ms 36956 KB Output is correct
3 Correct 9 ms 36956 KB Output is correct
4 Correct 18 ms 37228 KB Output is correct
5 Correct 31 ms 37568 KB Output is correct
6 Correct 10 ms 36956 KB Output is correct
7 Correct 9 ms 37000 KB Output is correct
8 Correct 10 ms 36952 KB Output is correct
9 Correct 49 ms 39508 KB Output is correct
10 Correct 94 ms 67288 KB Output is correct
11 Correct 14 ms 37152 KB Output is correct
12 Correct 42 ms 37960 KB Output is correct
13 Correct 132 ms 72228 KB Output is correct
14 Correct 135 ms 74064 KB Output is correct
15 Correct 342 ms 83948 KB Output is correct
16 Correct 561 ms 115096 KB Output is correct
17 Correct 182 ms 80972 KB Output is correct
18 Correct 110 ms 75604 KB Output is correct
19 Correct 147 ms 78360 KB Output is correct
20 Correct 173 ms 78624 KB Output is correct
21 Correct 182 ms 81176 KB Output is correct
22 Correct 97 ms 73040 KB Output is correct
23 Correct 9 ms 37032 KB Output is correct
24 Correct 8 ms 36972 KB Output is correct
25 Correct 9 ms 37044 KB Output is correct
26 Correct 9 ms 36956 KB Output is correct
27 Correct 9 ms 37116 KB Output is correct
28 Correct 9 ms 36956 KB Output is correct
29 Correct 9 ms 37112 KB Output is correct
30 Correct 9 ms 36956 KB Output is correct
31 Correct 10 ms 36952 KB Output is correct
32 Correct 10 ms 37104 KB Output is correct
33 Correct 10 ms 36956 KB Output is correct
34 Correct 9 ms 36964 KB Output is correct
35 Correct 8 ms 36952 KB Output is correct
36 Correct 8 ms 36956 KB Output is correct
37 Correct 8 ms 36956 KB Output is correct
38 Correct 9 ms 37040 KB Output is correct
39 Correct 12 ms 36956 KB Output is correct
40 Correct 9 ms 36956 KB Output is correct
41 Correct 8 ms 36956 KB Output is correct
42 Correct 9 ms 36964 KB Output is correct
43 Correct 10 ms 37120 KB Output is correct
44 Correct 9 ms 36952 KB Output is correct
45 Correct 9 ms 36956 KB Output is correct
46 Correct 8 ms 36956 KB Output is correct
47 Correct 9 ms 36956 KB Output is correct
48 Correct 8 ms 36956 KB Output is correct
49 Correct 9 ms 36956 KB Output is correct
50 Correct 9 ms 36956 KB Output is correct
51 Correct 11 ms 37212 KB Output is correct
52 Correct 10 ms 36956 KB Output is correct
53 Correct 10 ms 36956 KB Output is correct
54 Correct 11 ms 37128 KB Output is correct
55 Correct 9 ms 37120 KB Output is correct
56 Correct 10 ms 36960 KB Output is correct
57 Correct 9 ms 36956 KB Output is correct
58 Correct 9 ms 36956 KB Output is correct
59 Correct 9 ms 36956 KB Output is correct
60 Correct 42 ms 39716 KB Output is correct
61 Correct 84 ms 67084 KB Output is correct
62 Correct 56 ms 39552 KB Output is correct
63 Correct 53 ms 39356 KB Output is correct
64 Correct 42 ms 39608 KB Output is correct
65 Correct 37 ms 39016 KB Output is correct
66 Correct 22 ms 37464 KB Output is correct
67 Correct 118 ms 57912 KB Output is correct
68 Correct 90 ms 57680 KB Output is correct
69 Correct 78 ms 62528 KB Output is correct
70 Correct 86 ms 62540 KB Output is correct
71 Correct 89 ms 57984 KB Output is correct
72 Correct 91 ms 58112 KB Output is correct
73 Correct 78 ms 59180 KB Output is correct
74 Correct 100 ms 59216 KB Output is correct
75 Correct 21 ms 38748 KB Output is correct
76 Correct 92 ms 57684 KB Output is correct
77 Correct 8 ms 36952 KB Output is correct
78 Correct 8 ms 36956 KB Output is correct
79 Correct 8 ms 36956 KB Output is correct
80 Correct 10 ms 36956 KB Output is correct
81 Correct 15 ms 36960 KB Output is correct
82 Correct 9 ms 37044 KB Output is correct
83 Correct 8 ms 36980 KB Output is correct
84 Correct 8 ms 36956 KB Output is correct
85 Correct 8 ms 36956 KB Output is correct
86 Correct 8 ms 36956 KB Output is correct
87 Correct 8 ms 36952 KB Output is correct
88 Correct 10 ms 36956 KB Output is correct
89 Correct 32 ms 37424 KB Output is correct
90 Correct 47 ms 37720 KB Output is correct
91 Correct 38 ms 37724 KB Output is correct
92 Correct 124 ms 59552 KB Output is correct
93 Correct 351 ms 72056 KB Output is correct
94 Correct 557 ms 91772 KB Output is correct
95 Correct 169 ms 61284 KB Output is correct
96 Correct 204 ms 61200 KB Output is correct
97 Correct 170 ms 61320 KB Output is correct
98 Correct 288 ms 69392 KB Output is correct
99 Correct 222 ms 68256 KB Output is correct
100 Correct 236 ms 68144 KB Output is correct
101 Correct 270 ms 68440 KB Output is correct
102 Correct 267 ms 68112 KB Output is correct
103 Correct 208 ms 71556 KB Output is correct
104 Correct 235 ms 77180 KB Output is correct
105 Correct 213 ms 72032 KB Output is correct
106 Correct 198 ms 66768 KB Output is correct
107 Correct 213 ms 72492 KB Output is correct
108 Correct 258 ms 68044 KB Output is correct
109 Correct 216 ms 72272 KB Output is correct
110 Correct 46 ms 37972 KB Output is correct
111 Correct 34 ms 37712 KB Output is correct
112 Correct 346 ms 83304 KB Output is correct
113 Correct 178 ms 66620 KB Output is correct
114 Correct 224 ms 73680 KB Output is correct
115 Correct 76 ms 58068 KB Output is correct
116 Correct 171 ms 62536 KB Output is correct
117 Correct 776 ms 105780 KB Output is correct
118 Correct 110 ms 57936 KB Output is correct
119 Correct 93 ms 57940 KB Output is correct
120 Correct 18 ms 38748 KB Output is correct
121 Correct 191 ms 63680 KB Output is correct
122 Correct 190 ms 63684 KB Output is correct
123 Correct 165 ms 68936 KB Output is correct
124 Correct 163 ms 68948 KB Output is correct
125 Correct 145 ms 69600 KB Output is correct
126 Correct 533 ms 108900 KB Output is correct
127 Correct 239 ms 80968 KB Output is correct
128 Correct 196 ms 79804 KB Output is correct
129 Correct 185 ms 77868 KB Output is correct
130 Correct 225 ms 79400 KB Output is correct