#include<bits/stdc++.h>
#define int long long
using namespace std;
struct wi{
vector<int> Q;
int skoki[20];
int poziom=0;
vector<int> po;
int przed=0;
}*w;
int pocz[130000];
int kon[130000];
int mam[130000][2];
void pusc(int x)
{
int gdzie=0;
while(w[w[x].skoki[gdzie]].skoki[gdzie]!=0)
{
w[x].skoki[gdzie+1]=w[w[x].skoki[gdzie]].skoki[gdzie];
gdzie++;
}
}
void dfs(int x,int ojc)
{
w[x].poziom=w[ojc].poziom+1;
for(int i=0;i<w[x].Q.size();i++)
{
int pom=w[x].Q[i];
if(pom==ojc)
continue;
w[pom].skoki[0]=x;
pusc(pom);
dfs(pom,x);
}
}
int lca(int x,int y)
{
if(w[x].poziom>w[y].poziom)
swap(x,y);
for(int i=19;i>=0;i--)
if(w[w[y].skoki[i]].poziom>=w[x].poziom)
y=w[y].skoki[i];
if(x==y)
return x;
for(int i=19;i>=0;i--)
if(w[x].skoki[i]!=w[y].skoki[i])
x=w[x].skoki[i],y=w[y].skoki[i];
return w[x].skoki[0];
}
void lec(int x,int y,int kto)
{
while(x!=y)
{
if(pocz[x]!=0 && pocz[x]!=kto)
{
w[kto].przed++;
w[pocz[x]].po.push_back(kto);
}
if(kon[x]!=0 && kon[x]!=kto)
{
w[kto].po.push_back(kon[x]);
w[kon[x]].przed++;
}
x=w[x].skoki[0];
}
}
int32_t main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int zap;
cin>>zap;
while(zap--)
{
int n,x,y;
cin>>n;
w=new wi[n+3];
for(int i=1;i<n;i++)
{
cin>>x>>y;
w[x].Q.push_back(y);
w[y].Q.push_back(x);
}
dfs(1,0);
int m;
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
pocz[x]=i;
kon[y]=i;
mam[i][0]=x;
mam[i][1]=y;
}
for(int i=1;i<=m;i++)
{
x=mam[i][0];
y=mam[i][1];
int lcaa=lca(x,y);
lec(x,lcaa,i);
lec(y,lcaa,i);
}
int ilu=0;
vector<int> mam;
for(int i=1;i<=m;i++)
if(w[i].przed==0)
mam.push_back(i);
while(mam.size())
{
int x=mam.back();
mam.pop_back();
ilu++;
for(int i=0;i<w[x].po.size();i++)
{
w[w[x].po[i]].przed--;
if(w[w[x].po[i]].przed==0)
mam.push_back(w[x].po[i]);
}
}
if(ilu==m)
cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
Compilation message
jail.cpp: In function 'void dfs(long long int, long long int)':
jail.cpp:26:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i=0;i<w[x].Q.size();i++)
| ~^~~~~~~~~~~~~~
jail.cpp: In function 'int32_t main()':
jail.cpp:114:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for(int i=0;i<w[x].po.size();i++)
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |