# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
551838 |
2022-04-21T17:12:43 Z |
kshitij_sodani |
Jail (JOI22_jail) |
C++14 |
|
122 ms |
207308 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'
const llo mod=1e9+7;
int q;
int ee;
vector<int> adj[200001];
int cc[200001];
int dd[200001];
int vis[200001];
vector<int> ss;
vector<int> tt;
vector<int> pre[200001];
map<int,int> ind[200001];
int ind2[200001];
int ind3[200001];
vector<int> adj2[200001*40];
int vis2[200001*40];
int vis3[200001*40];
void dfs(int no,int par2=-1){
ss.pb(no);
if(ee==no){
tt=ss;
}
for(auto j:adj[no]){
if(j!=par2){
dfs(j,no);
}
}
ss.pop_back();
}
int stt=1;
void dfs2(int no){
vis2[no]=1;
vis3[no]=1;
for(auto j:adj2[no]){
if(j==-1){
continue;
}
if(vis3[j]==1){
stt=0;
}
if(vis2[j]==0){
dfs2(j);
}
}
vis3[no]=0;
}
int par[120001][20];
int pp[120001][20];
int pp2[120001][20];
int up[120001];
int lev[120001];
void dfs3(int no,int par2=-1,int xx=-1,int levv=0){
par[no][0]=par2;
up[no]=xx;
lev[no]=levv;
if(ind2[no]>=0 or ind3[no]>=0){
xx=no;
}
for(auto j:adj[no]){
if(j!=par2){
dfs3(j,no,xx,levv+1);
}
}
}
int lca(int aa,int bb){
if(lev[bb]<lev[aa]){
swap(aa,bb);
}
//cout<<lev[bb]<<",,"<<lev[aa]<<endl;
int dif=lev[bb]-lev[aa];
//cout<<dif<<",,"<<endl;
for(int j=19;j>=0;j--){
if((1<<j)&dif){
bb=par[bb][j];
}
}
/* cout<<bb<<",,"<<par[bb][0]<<endl;
cout<<dif<<",,"<<endl;
cout<<aa<<",,"<<bb<<endl;*/
if(aa==bb){
return aa;
}
for(int j=19;j>=0;j--){
if(par[aa][j]!=par[bb][j]){
aa=par[aa][j];
bb=par[bb][j];
}
}
return par[aa][0];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>q;
while(q--){
int n;
cin>>n;
for(int i=0;i<n;i++){
adj[i].clear();
}
for(int i=0;i<n*50;i++){
adj2[i].clear();
}
for(int i=0;i<n-1;i++){
int aa,bb;
cin>>aa>>bb;
aa--;
bb--;
adj[aa].pb(bb);
adj[bb].pb(aa);
}
int m;
cin>>m;
stt=1;
for(int i=0;i<n;i++){
ind2[i]=-1;
ind3[i]=-1;
}
for(int i=0;i<m;i++){
cin>>cc[i]>>dd[i];
ind[i].clear();
cc[i]--;
dd[i]--;
ind2[dd[i]]=i;
ind3[cc[i]]=i;
ee=dd[i];
/* dfs(cc[i]);
pre[i]=tt;
for(auto j:tt){
ind[i][j]++;
}*/
}
dfs3(0);
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1]==-1){
par[i][j]=-1;
}
else{
par[i][j]=par[par[i][j-1]][j-1];
}
}
}
int ind6=m;
for(int j=0;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j]==-1){
pp[i][j]=-1;
continue;
}
ind6++;
pp[i][j]=ind6;
if(j>0){
adj2[pp[i][j]].pb(pp[i][j-1]);
adj2[pp[i][j]].pb(pp[par[i][j-1]][j-1]);
}
}
}
for(int j=0;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j]==-1){
pp2[i][j]=-1;
continue;
}
ind6++;
pp2[i][j]=ind6;
if(j>0){
adj2[pp2[i][j-1]].pb(pp2[i][j]);
adj2[pp2[par[i][j-1]][j-1]].pb(pp2[i][j]);
}
}
}
for(int i=0;i<n;i++){
if(ind2[i]>=0){
adj2[pp[i][0]].pb(ind2[i]);
}
if(ind3[i]>=0){
adj2[ind3[i]].pb(pp2[i][0]);
}
}
/* for(int i=0;i<ind6;i++){
adj2[i].clear();
}*/
for(int i=0;i<m;i++){
int x=lca(cc[i],dd[i]);
/// cout<<cc[i]<<":"<<dd[i]<<":"<<x<<endl;
int j=par[cc[i]][0];
if(x!=cc[i] and lev[j]>lev[x]){
int dif=lev[j]-lev[x];
for(int jj=19;jj>=0;jj--){
if((1<<jj)&dif){
adj2[i].pb(pp[j][jj]);
adj2[pp2[j][jj]].pb(i);
j=par[j][jj];
}
}
}
j=par[dd[i]][0];
if(x!=dd[i] and lev[j]>lev[x]){
int dif=lev[j]-lev[x];
for(int jj=19;jj>=0;jj--){
if((1<<jj)&dif){
adj2[i].pb(pp[j][jj]);
adj2[pp2[j][jj]].pb(i);
j=par[j][jj];
}
}
}
vector<int> tte;
tte.pb(cc[i]);
tte.pb(dd[i]);
tte.pb(x);
for(auto j:tte){
if(ind3[j]>=0 and ind3[j]!=i){
adj2[i].pb(ind3[j]);
//cout<<i<<":"<<ind3[j]<<endl;
}
if(ind2[j]>=0 and ind2[j]!=i){
adj2[ind2[j]].pb(i);
//cout<<ind2[j]<<":"<<i<<endl;
}
}
/*while(j!=-1){
if(lev[j]<lev[x]){
break;
}
if(ind3[j]>=0 and ind3[j]!=i){
adj2[i].pb(ind3[j]);
//cout<<i<<":"<<ind3[j]<<endl;
}
if(ind2[j]>=0 and ind2[j]!=i){
adj2[ind2[j]].pb(i);
//cout<<ind2[j]<<":"<<i<<endl;
}
j=up[j];
}
j=dd[i];
while(j!=-1){
if(lev[j]<lev[x]){
break;
}
if(ind3[j]>=0 and ind3[j]!=i){
adj2[i].pb(ind3[j]);
//cout<<i<<":"<<ind3[j]<<endl;
}
if(ind2[j]>=0 and ind2[j]!=i){
adj2[ind2[j]].pb(i);
//cout<<ind2[j]<<":"<<i<<endl;
}
j=up[j];
}*/
//cout<<endl;
/* for(auto j:pre[i]){
if(ind3[j]>=0 and ind3[j]!=i){
adj2[i].pb(ind3[j]);
//cout<<i<<":"<<ind3[j]<<endl;
}
if(ind2[j]>=0 and ind2[j]!=i){
adj2[ind2[j]].pb(i);
// cout<<ind2[j]<<":"<<i<<endl;
}
}*/
}
for(int i=0;i<ind6;i++){
vis2[i]=0;
}
for(int i=0;i<ind6;i++){
/*for(auto j:adj2[i]){
cout<<i<<","<<j<<endl;
}*/
if(vis2[i]==0){
dfs2(i);
}
}
if(stt==0){
cout<<"No"<<endl;
}
else{
cout<<"Yes"<<endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
206924 KB |
Output is correct |
2 |
Correct |
112 ms |
207076 KB |
Output is correct |
3 |
Correct |
93 ms |
206996 KB |
Output is correct |
4 |
Incorrect |
122 ms |
207116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
207024 KB |
Output is correct |
2 |
Correct |
104 ms |
206928 KB |
Output is correct |
3 |
Correct |
96 ms |
207308 KB |
Output is correct |
4 |
Incorrect |
97 ms |
207128 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
207024 KB |
Output is correct |
2 |
Correct |
104 ms |
206928 KB |
Output is correct |
3 |
Correct |
96 ms |
207308 KB |
Output is correct |
4 |
Incorrect |
97 ms |
207128 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
207024 KB |
Output is correct |
2 |
Correct |
104 ms |
206928 KB |
Output is correct |
3 |
Correct |
96 ms |
207308 KB |
Output is correct |
4 |
Incorrect |
97 ms |
207128 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
207024 KB |
Output is correct |
2 |
Correct |
104 ms |
206928 KB |
Output is correct |
3 |
Correct |
96 ms |
207308 KB |
Output is correct |
4 |
Incorrect |
97 ms |
207128 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
206924 KB |
Output is correct |
2 |
Correct |
105 ms |
206968 KB |
Output is correct |
3 |
Correct |
116 ms |
207140 KB |
Output is correct |
4 |
Correct |
108 ms |
207076 KB |
Output is correct |
5 |
Incorrect |
109 ms |
206996 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
206924 KB |
Output is correct |
2 |
Correct |
112 ms |
207076 KB |
Output is correct |
3 |
Correct |
93 ms |
206996 KB |
Output is correct |
4 |
Incorrect |
122 ms |
207116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |