#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=2e5+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
vector<int>adj[mxn+10],adj2[mxn+10];
int S[mxn+10],T[mxn+10],SID[mxn+10],TID[mxn+10],target,root;
stack<int>st;
void addedge(int x,int y){
if(x!=y)adj2[x].pb(y);
}
void dfs(int cur,int p){
st.push(cur);
if(cur==target){
while(!st.empty()&&st.top()!=S[root]){
if(SID[st.top()])addedge(SID[st.top()],root);
if(TID[st.top()])addedge(root,TID[st.top()]);
st.pop();
}
if(TID[st.top()])addedge(root,TID[st.top()]);
return;
}
for(auto i:adj[cur])if(i!=p)dfs(i,cur);
if(!st.empty())st.pop();
}
int vis[mxn+10],on[mxn+10];
bool findcycle(int cur){
if(vis[cur])return 0;
vis[cur]=on[cur]=1;
for(auto i:adj2[cur]){
if(on[i])return 1;
if(!vis[i]&&findcycle(i))return 1;
}
on[cur]=0;
return 0;
}
void solve(){
//brute force
cin>>n;
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
cin>>m;
for(int i=1;i<=m;i++)cin>>S[i]>>T[i],SID[S[i]]=i,TID[T[i]]=i;
for(int i=1;i<=m;i++){
root=i;
target=T[i];
dfs(S[i],-1);
while(!st.empty())st.pop();
}
bool yes=0;
for(int i=1;i<=m;i++)yes|=findcycle(i);
if(yes)cout<<"No\n";
else cout<<"Yes\n";
for(int i=1;i<=n;i++)adj[i].clear(),adj2[i].clear(),vis[i]=on[i]=S[i]=T[i]=SID[i]=TID[i]=0;
}
int32_t main(){
fastio
int t;cin>>t;
while(t--)solve();
}
/*
let say the end point of prisoneri is on the path of prisonerj
then j has to move before i
j->i
if the start point of prisoner i is on the path of prisoner j
then i has to move before j
i->j
we can build a dag then see if theres a cycle
when dfs in dag to check for cycle a node is visited only once
we dont need to build the whole graph
just dfs and slowly check for adjacent node
if the adjacent node is not on the current path then just continue dfs
else if its on we found a cycle
after each dfs we can delete all visited node from the graph
will only use O(n*getnxt())
getnxt :
find all prisoner that pass the start node
find all prisoner that has end point on path (start,end)->easy hld?
*/
Compilation message
jail.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
jail.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
38 | void setIO(string name){
| ^
jail.cpp:46:25: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
46 | void addedge(int x,int y){
| ^
jail.cpp:49:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
49 | void dfs(int cur,int p){
| ^
jail.cpp:64:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
64 | bool findcycle(int cur){
| ^
jail.cpp:74:12: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
74 | void solve(){
| ^
jail.cpp:96:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
96 | int32_t main(){
| ^
jail.cpp: In function 'void setIO(std::string)':
jail.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16976 KB |
Output is correct |
2 |
Correct |
3 ms |
16976 KB |
Output is correct |
3 |
Correct |
4 ms |
17016 KB |
Output is correct |
4 |
Correct |
9 ms |
17280 KB |
Output is correct |
5 |
Correct |
15 ms |
17676 KB |
Output is correct |
6 |
Correct |
4 ms |
16976 KB |
Output is correct |
7 |
Correct |
4 ms |
17232 KB |
Output is correct |
8 |
Correct |
7 ms |
17404 KB |
Output is correct |
9 |
Correct |
426 ms |
22868 KB |
Output is correct |
10 |
Correct |
1041 ms |
36680 KB |
Output is correct |
11 |
Correct |
7 ms |
16976 KB |
Output is correct |
12 |
Correct |
42 ms |
18184 KB |
Output is correct |
13 |
Execution timed out |
5073 ms |
45032 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
17016 KB |
Output is correct |
2 |
Correct |
3 ms |
16976 KB |
Output is correct |
3 |
Correct |
3 ms |
16976 KB |
Output is correct |
4 |
Correct |
3 ms |
17288 KB |
Output is correct |
5 |
Correct |
3 ms |
16976 KB |
Output is correct |
6 |
Correct |
3 ms |
16976 KB |
Output is correct |
7 |
Correct |
3 ms |
16976 KB |
Output is correct |
8 |
Correct |
3 ms |
16976 KB |
Output is correct |
9 |
Correct |
3 ms |
16976 KB |
Output is correct |
10 |
Correct |
3 ms |
16976 KB |
Output is correct |
11 |
Correct |
3 ms |
16976 KB |
Output is correct |
12 |
Correct |
3 ms |
16976 KB |
Output is correct |
13 |
Correct |
3 ms |
16976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
17016 KB |
Output is correct |
2 |
Correct |
3 ms |
16976 KB |
Output is correct |
3 |
Correct |
3 ms |
16976 KB |
Output is correct |
4 |
Correct |
3 ms |
17288 KB |
Output is correct |
5 |
Correct |
3 ms |
16976 KB |
Output is correct |
6 |
Correct |
3 ms |
16976 KB |
Output is correct |
7 |
Correct |
3 ms |
16976 KB |
Output is correct |
8 |
Correct |
3 ms |
16976 KB |
Output is correct |
9 |
Correct |
3 ms |
16976 KB |
Output is correct |
10 |
Correct |
3 ms |
16976 KB |
Output is correct |
11 |
Correct |
3 ms |
16976 KB |
Output is correct |
12 |
Correct |
3 ms |
16976 KB |
Output is correct |
13 |
Correct |
3 ms |
16976 KB |
Output is correct |
14 |
Correct |
3 ms |
16976 KB |
Output is correct |
15 |
Correct |
3 ms |
16976 KB |
Output is correct |
16 |
Correct |
4 ms |
16976 KB |
Output is correct |
17 |
Correct |
3 ms |
16976 KB |
Output is correct |
18 |
Correct |
4 ms |
17028 KB |
Output is correct |
19 |
Correct |
3 ms |
16976 KB |
Output is correct |
20 |
Correct |
4 ms |
16976 KB |
Output is correct |
21 |
Correct |
4 ms |
16976 KB |
Output is correct |
22 |
Correct |
4 ms |
16976 KB |
Output is correct |
23 |
Correct |
3 ms |
16976 KB |
Output is correct |
24 |
Correct |
3 ms |
16976 KB |
Output is correct |
25 |
Correct |
4 ms |
16976 KB |
Output is correct |
26 |
Correct |
3 ms |
16976 KB |
Output is correct |
27 |
Correct |
4 ms |
16976 KB |
Output is correct |
28 |
Correct |
4 ms |
16976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
17016 KB |
Output is correct |
2 |
Correct |
3 ms |
16976 KB |
Output is correct |
3 |
Correct |
3 ms |
16976 KB |
Output is correct |
4 |
Correct |
3 ms |
17288 KB |
Output is correct |
5 |
Correct |
3 ms |
16976 KB |
Output is correct |
6 |
Correct |
3 ms |
16976 KB |
Output is correct |
7 |
Correct |
3 ms |
16976 KB |
Output is correct |
8 |
Correct |
3 ms |
16976 KB |
Output is correct |
9 |
Correct |
3 ms |
16976 KB |
Output is correct |
10 |
Correct |
3 ms |
16976 KB |
Output is correct |
11 |
Correct |
3 ms |
16976 KB |
Output is correct |
12 |
Correct |
3 ms |
16976 KB |
Output is correct |
13 |
Correct |
3 ms |
16976 KB |
Output is correct |
14 |
Correct |
3 ms |
16976 KB |
Output is correct |
15 |
Correct |
3 ms |
16976 KB |
Output is correct |
16 |
Correct |
4 ms |
16976 KB |
Output is correct |
17 |
Correct |
3 ms |
16976 KB |
Output is correct |
18 |
Correct |
4 ms |
17028 KB |
Output is correct |
19 |
Correct |
3 ms |
16976 KB |
Output is correct |
20 |
Correct |
4 ms |
16976 KB |
Output is correct |
21 |
Correct |
4 ms |
16976 KB |
Output is correct |
22 |
Correct |
4 ms |
16976 KB |
Output is correct |
23 |
Correct |
3 ms |
16976 KB |
Output is correct |
24 |
Correct |
3 ms |
16976 KB |
Output is correct |
25 |
Correct |
4 ms |
16976 KB |
Output is correct |
26 |
Correct |
3 ms |
16976 KB |
Output is correct |
27 |
Correct |
4 ms |
16976 KB |
Output is correct |
28 |
Correct |
4 ms |
16976 KB |
Output is correct |
29 |
Correct |
9 ms |
17232 KB |
Output is correct |
30 |
Correct |
6 ms |
16976 KB |
Output is correct |
31 |
Correct |
6 ms |
16992 KB |
Output is correct |
32 |
Correct |
5 ms |
17028 KB |
Output is correct |
33 |
Correct |
4 ms |
17148 KB |
Output is correct |
34 |
Correct |
7 ms |
16976 KB |
Output is correct |
35 |
Correct |
7 ms |
16976 KB |
Output is correct |
36 |
Correct |
6 ms |
16976 KB |
Output is correct |
37 |
Correct |
5 ms |
16976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
17016 KB |
Output is correct |
2 |
Correct |
3 ms |
16976 KB |
Output is correct |
3 |
Correct |
3 ms |
16976 KB |
Output is correct |
4 |
Correct |
3 ms |
17288 KB |
Output is correct |
5 |
Correct |
3 ms |
16976 KB |
Output is correct |
6 |
Correct |
3 ms |
16976 KB |
Output is correct |
7 |
Correct |
3 ms |
16976 KB |
Output is correct |
8 |
Correct |
3 ms |
16976 KB |
Output is correct |
9 |
Correct |
3 ms |
16976 KB |
Output is correct |
10 |
Correct |
3 ms |
16976 KB |
Output is correct |
11 |
Correct |
3 ms |
16976 KB |
Output is correct |
12 |
Correct |
3 ms |
16976 KB |
Output is correct |
13 |
Correct |
3 ms |
16976 KB |
Output is correct |
14 |
Correct |
3 ms |
16976 KB |
Output is correct |
15 |
Correct |
3 ms |
16976 KB |
Output is correct |
16 |
Correct |
4 ms |
16976 KB |
Output is correct |
17 |
Correct |
3 ms |
16976 KB |
Output is correct |
18 |
Correct |
4 ms |
17028 KB |
Output is correct |
19 |
Correct |
3 ms |
16976 KB |
Output is correct |
20 |
Correct |
4 ms |
16976 KB |
Output is correct |
21 |
Correct |
4 ms |
16976 KB |
Output is correct |
22 |
Correct |
4 ms |
16976 KB |
Output is correct |
23 |
Correct |
3 ms |
16976 KB |
Output is correct |
24 |
Correct |
3 ms |
16976 KB |
Output is correct |
25 |
Correct |
4 ms |
16976 KB |
Output is correct |
26 |
Correct |
3 ms |
16976 KB |
Output is correct |
27 |
Correct |
4 ms |
16976 KB |
Output is correct |
28 |
Correct |
4 ms |
16976 KB |
Output is correct |
29 |
Correct |
9 ms |
17232 KB |
Output is correct |
30 |
Correct |
6 ms |
16976 KB |
Output is correct |
31 |
Correct |
6 ms |
16992 KB |
Output is correct |
32 |
Correct |
5 ms |
17028 KB |
Output is correct |
33 |
Correct |
4 ms |
17148 KB |
Output is correct |
34 |
Correct |
7 ms |
16976 KB |
Output is correct |
35 |
Correct |
7 ms |
16976 KB |
Output is correct |
36 |
Correct |
6 ms |
16976 KB |
Output is correct |
37 |
Correct |
5 ms |
16976 KB |
Output is correct |
38 |
Correct |
441 ms |
22820 KB |
Output is correct |
39 |
Correct |
1091 ms |
36680 KB |
Output is correct |
40 |
Correct |
808 ms |
20968 KB |
Output is correct |
41 |
Correct |
657 ms |
18504 KB |
Output is correct |
42 |
Correct |
368 ms |
20932 KB |
Output is correct |
43 |
Correct |
51 ms |
18760 KB |
Output is correct |
44 |
Correct |
71 ms |
17428 KB |
Output is correct |
45 |
Correct |
1984 ms |
24340 KB |
Output is correct |
46 |
Correct |
1970 ms |
24396 KB |
Output is correct |
47 |
Correct |
825 ms |
29256 KB |
Output is correct |
48 |
Correct |
784 ms |
29432 KB |
Output is correct |
49 |
Correct |
1379 ms |
24500 KB |
Output is correct |
50 |
Correct |
1520 ms |
24540 KB |
Output is correct |
51 |
Correct |
207 ms |
26012 KB |
Output is correct |
52 |
Correct |
226 ms |
25928 KB |
Output is correct |
53 |
Correct |
64 ms |
17760 KB |
Output is correct |
54 |
Correct |
1555 ms |
25000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16976 KB |
Output is correct |
2 |
Correct |
3 ms |
16976 KB |
Output is correct |
3 |
Correct |
3 ms |
16976 KB |
Output is correct |
4 |
Correct |
3 ms |
16976 KB |
Output is correct |
5 |
Correct |
9 ms |
17160 KB |
Output is correct |
6 |
Correct |
4 ms |
16976 KB |
Output is correct |
7 |
Correct |
4 ms |
17488 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16976 KB |
Output is correct |
10 |
Correct |
3 ms |
16976 KB |
Output is correct |
11 |
Correct |
3 ms |
17024 KB |
Output is correct |
12 |
Correct |
5 ms |
16976 KB |
Output is correct |
13 |
Correct |
31 ms |
17488 KB |
Output is correct |
14 |
Correct |
41 ms |
17736 KB |
Output is correct |
15 |
Correct |
37 ms |
17688 KB |
Output is correct |
16 |
Execution timed out |
5058 ms |
23384 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16976 KB |
Output is correct |
2 |
Correct |
3 ms |
16976 KB |
Output is correct |
3 |
Correct |
4 ms |
17016 KB |
Output is correct |
4 |
Correct |
9 ms |
17280 KB |
Output is correct |
5 |
Correct |
15 ms |
17676 KB |
Output is correct |
6 |
Correct |
4 ms |
16976 KB |
Output is correct |
7 |
Correct |
4 ms |
17232 KB |
Output is correct |
8 |
Correct |
7 ms |
17404 KB |
Output is correct |
9 |
Correct |
426 ms |
22868 KB |
Output is correct |
10 |
Correct |
1041 ms |
36680 KB |
Output is correct |
11 |
Correct |
7 ms |
16976 KB |
Output is correct |
12 |
Correct |
42 ms |
18184 KB |
Output is correct |
13 |
Execution timed out |
5073 ms |
45032 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |