#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 120005;
int q;
vector<int> adj[MAXN];
int S[MAXN], T[MAXN];
int dep[MAXN];
int twok[MAXN][20];
int pre[MAXN], post[MAXN];
int cnt;
int tc;
int lift(int x, int k) {
FOR(i,0,19){
if((1<<i)&k)x=twok[x][i];
if(x==-1)return -1;
}
return x;
}
int lca(int x, int y) {
db2(x,y);
db2(dep[x],dep[y]);
if(dep[x]>dep[y]) {
int dd=dep[x]-dep[y];
x=lift(x,dd);
} else {
int dd=dep[y]-dep[x];
y=lift(y,dd);
}
reach
if(x==y)return x;
db2(dep[x],dep[y]);
reach
db(x);
DEC(i,19,0){
db(i);
db2(x,y);
if(twok[x][i]!=twok[y][i]){
x=twok[x][i];
y=twok[y][i];
}
}
reach
return twok[x][0];
}
void dfs(int x, int p, int lvl) {
db(x);
dbv(adj[x]);
pre[x]=cnt++;
dep[x]=lvl;
twok[x][0]=p;
FOR(i,1,19){
if(twok[x][i-1] == -1)continue;
twok[x][i]=twok[twok[x][i-1]][i-1];
}
for(auto v:adj[x])if(v!=p) {
db2(v,x);
dfs(v,x,lvl+1);
}
post[x]=cnt-1;
}
set<int> V[MAXN];
bool ispar(int par, int child) {
if(pre[par]<=pre[child] && post[par]>=post[child]) return 1;
else return 0;
}
bool onpath(int x, int y, int node) {
reach
int par=lca(x,y);
reach
if(ispar(par,node) && (ispar(node, x) || ispar(node, y))) return 1;
else return 0;
}
int hcycle = 0;
int vis[MAXN];
void dfs(int x) {
bool res=0;
if(vis[x] == 2) {
hcycle=1;
return;
}
if(vis[x]==1){
return;
}
vis[x]=2; // active
for(auto v:V[x]){
dfs(v);
}
vis[x]=1;
}
int in[MAXN];
void solve(){
reach
int n; cin >> n;
FOR(i,1,n)FOR(j,0,19)twok[i][j]=-1;
FOR(i,1,n-1) {
int a,b; cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1,-1,0);
int m; cin >> m;
FOR(i,1,m) cin >> S[i] >> T[i];
bool bad=0;
FOR(i,1,m){
FOR(j,1,m){
if(i!=j) {
db2(i,j);
reach
// check if there's an edge between i and j
bool y1=onpath(S[i],T[i],S[j]);
bool y2=onpath(S[i],T[i],T[j]);
reach
if(y1&&y2){
bad=1;
goto done;
}
if(y1) { // you must do j before i
V[j].insert(i);
}
if(y2) { // you must do j after i
V[i].insert(j);
}
}
}
}
reach
hcycle=0;
FOR(i,1,m) {
if(tc==9) {
dbv(V[i]);
}
dfs(i);
}
bad|=hcycle;
done:;
if(bad)cout<<"No\n";
else cout<<"Yes\n";
cnt=0;
FOR(i,1,n)FOR(j,0,19)twok[i][j]=-1;
FOR(i,1,n)pre[i]=post[i]=0;
FOR(i,1,n)dep[i]=0;
FOR(i,1,n)vis[i]=0;
FOR(i,1,n)V[i].clear();
FOR(i,1,n)S[i]=T[i]=0;
FOR(i,1,n)adj[i].clear();
}
int32_t main()
{
IAMSPEED
cin >> q;
while(q--) {
tc++;
solve();
}
}
Compilation message
jail.cpp: In function 'void dfs(long long int)':
jail.cpp:116:7: warning: unused variable 'res' [-Wunused-variable]
116 | bool res=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Correct |
5 ms |
8788 KB |
Output is correct |
4 |
Correct |
14 ms |
8860 KB |
Output is correct |
5 |
Correct |
25 ms |
8860 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
6 ms |
8788 KB |
Output is correct |
8 |
Correct |
14 ms |
9036 KB |
Output is correct |
9 |
Correct |
221 ms |
16316 KB |
Output is correct |
10 |
Correct |
97 ms |
48340 KB |
Output is correct |
11 |
Correct |
10 ms |
8788 KB |
Output is correct |
12 |
Correct |
62 ms |
8856 KB |
Output is correct |
13 |
Execution timed out |
5044 ms |
41944 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
6 ms |
8788 KB |
Output is correct |
3 |
Correct |
7 ms |
8788 KB |
Output is correct |
4 |
Correct |
8 ms |
8788 KB |
Output is correct |
5 |
Correct |
6 ms |
8788 KB |
Output is correct |
6 |
Correct |
6 ms |
8788 KB |
Output is correct |
7 |
Correct |
7 ms |
8796 KB |
Output is correct |
8 |
Correct |
6 ms |
8792 KB |
Output is correct |
9 |
Correct |
6 ms |
8808 KB |
Output is correct |
10 |
Correct |
6 ms |
8788 KB |
Output is correct |
11 |
Correct |
5 ms |
8788 KB |
Output is correct |
12 |
Correct |
6 ms |
8820 KB |
Output is correct |
13 |
Correct |
7 ms |
8788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
6 ms |
8788 KB |
Output is correct |
3 |
Correct |
7 ms |
8788 KB |
Output is correct |
4 |
Correct |
8 ms |
8788 KB |
Output is correct |
5 |
Correct |
6 ms |
8788 KB |
Output is correct |
6 |
Correct |
6 ms |
8788 KB |
Output is correct |
7 |
Correct |
7 ms |
8796 KB |
Output is correct |
8 |
Correct |
6 ms |
8792 KB |
Output is correct |
9 |
Correct |
6 ms |
8808 KB |
Output is correct |
10 |
Correct |
6 ms |
8788 KB |
Output is correct |
11 |
Correct |
5 ms |
8788 KB |
Output is correct |
12 |
Correct |
6 ms |
8820 KB |
Output is correct |
13 |
Correct |
7 ms |
8788 KB |
Output is correct |
14 |
Correct |
4 ms |
8788 KB |
Output is correct |
15 |
Correct |
5 ms |
8788 KB |
Output is correct |
16 |
Correct |
5 ms |
8788 KB |
Output is correct |
17 |
Correct |
6 ms |
8788 KB |
Output is correct |
18 |
Correct |
6 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
8788 KB |
Output is correct |
20 |
Correct |
6 ms |
8788 KB |
Output is correct |
21 |
Correct |
6 ms |
8788 KB |
Output is correct |
22 |
Correct |
5 ms |
8788 KB |
Output is correct |
23 |
Correct |
4 ms |
8792 KB |
Output is correct |
24 |
Correct |
5 ms |
8788 KB |
Output is correct |
25 |
Correct |
6 ms |
8900 KB |
Output is correct |
26 |
Correct |
5 ms |
8788 KB |
Output is correct |
27 |
Correct |
6 ms |
8916 KB |
Output is correct |
28 |
Correct |
6 ms |
8788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
6 ms |
8788 KB |
Output is correct |
3 |
Correct |
7 ms |
8788 KB |
Output is correct |
4 |
Correct |
8 ms |
8788 KB |
Output is correct |
5 |
Correct |
6 ms |
8788 KB |
Output is correct |
6 |
Correct |
6 ms |
8788 KB |
Output is correct |
7 |
Correct |
7 ms |
8796 KB |
Output is correct |
8 |
Correct |
6 ms |
8792 KB |
Output is correct |
9 |
Correct |
6 ms |
8808 KB |
Output is correct |
10 |
Correct |
6 ms |
8788 KB |
Output is correct |
11 |
Correct |
5 ms |
8788 KB |
Output is correct |
12 |
Correct |
6 ms |
8820 KB |
Output is correct |
13 |
Correct |
7 ms |
8788 KB |
Output is correct |
14 |
Correct |
4 ms |
8788 KB |
Output is correct |
15 |
Correct |
5 ms |
8788 KB |
Output is correct |
16 |
Correct |
5 ms |
8788 KB |
Output is correct |
17 |
Correct |
6 ms |
8788 KB |
Output is correct |
18 |
Correct |
6 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
8788 KB |
Output is correct |
20 |
Correct |
6 ms |
8788 KB |
Output is correct |
21 |
Correct |
6 ms |
8788 KB |
Output is correct |
22 |
Correct |
5 ms |
8788 KB |
Output is correct |
23 |
Correct |
4 ms |
8792 KB |
Output is correct |
24 |
Correct |
5 ms |
8788 KB |
Output is correct |
25 |
Correct |
6 ms |
8900 KB |
Output is correct |
26 |
Correct |
5 ms |
8788 KB |
Output is correct |
27 |
Correct |
6 ms |
8916 KB |
Output is correct |
28 |
Correct |
6 ms |
8788 KB |
Output is correct |
29 |
Correct |
13 ms |
9172 KB |
Output is correct |
30 |
Correct |
10 ms |
8948 KB |
Output is correct |
31 |
Correct |
8 ms |
8916 KB |
Output is correct |
32 |
Correct |
14 ms |
8884 KB |
Output is correct |
33 |
Correct |
8 ms |
8916 KB |
Output is correct |
34 |
Correct |
28 ms |
8956 KB |
Output is correct |
35 |
Correct |
20 ms |
9044 KB |
Output is correct |
36 |
Correct |
42 ms |
8924 KB |
Output is correct |
37 |
Correct |
28 ms |
9072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
6 ms |
8788 KB |
Output is correct |
3 |
Correct |
7 ms |
8788 KB |
Output is correct |
4 |
Correct |
8 ms |
8788 KB |
Output is correct |
5 |
Correct |
6 ms |
8788 KB |
Output is correct |
6 |
Correct |
6 ms |
8788 KB |
Output is correct |
7 |
Correct |
7 ms |
8796 KB |
Output is correct |
8 |
Correct |
6 ms |
8792 KB |
Output is correct |
9 |
Correct |
6 ms |
8808 KB |
Output is correct |
10 |
Correct |
6 ms |
8788 KB |
Output is correct |
11 |
Correct |
5 ms |
8788 KB |
Output is correct |
12 |
Correct |
6 ms |
8820 KB |
Output is correct |
13 |
Correct |
7 ms |
8788 KB |
Output is correct |
14 |
Correct |
4 ms |
8788 KB |
Output is correct |
15 |
Correct |
5 ms |
8788 KB |
Output is correct |
16 |
Correct |
5 ms |
8788 KB |
Output is correct |
17 |
Correct |
6 ms |
8788 KB |
Output is correct |
18 |
Correct |
6 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
8788 KB |
Output is correct |
20 |
Correct |
6 ms |
8788 KB |
Output is correct |
21 |
Correct |
6 ms |
8788 KB |
Output is correct |
22 |
Correct |
5 ms |
8788 KB |
Output is correct |
23 |
Correct |
4 ms |
8792 KB |
Output is correct |
24 |
Correct |
5 ms |
8788 KB |
Output is correct |
25 |
Correct |
6 ms |
8900 KB |
Output is correct |
26 |
Correct |
5 ms |
8788 KB |
Output is correct |
27 |
Correct |
6 ms |
8916 KB |
Output is correct |
28 |
Correct |
6 ms |
8788 KB |
Output is correct |
29 |
Correct |
13 ms |
9172 KB |
Output is correct |
30 |
Correct |
10 ms |
8948 KB |
Output is correct |
31 |
Correct |
8 ms |
8916 KB |
Output is correct |
32 |
Correct |
14 ms |
8884 KB |
Output is correct |
33 |
Correct |
8 ms |
8916 KB |
Output is correct |
34 |
Correct |
28 ms |
8956 KB |
Output is correct |
35 |
Correct |
20 ms |
9044 KB |
Output is correct |
36 |
Correct |
42 ms |
8924 KB |
Output is correct |
37 |
Correct |
28 ms |
9072 KB |
Output is correct |
38 |
Correct |
215 ms |
17540 KB |
Output is correct |
39 |
Correct |
110 ms |
49868 KB |
Output is correct |
40 |
Correct |
482 ms |
11916 KB |
Output is correct |
41 |
Correct |
925 ms |
11568 KB |
Output is correct |
42 |
Correct |
583 ms |
11980 KB |
Output is correct |
43 |
Correct |
36 ms |
11596 KB |
Output is correct |
44 |
Correct |
548 ms |
9756 KB |
Output is correct |
45 |
Correct |
134 ms |
38972 KB |
Output is correct |
46 |
Correct |
129 ms |
38980 KB |
Output is correct |
47 |
Correct |
127 ms |
41644 KB |
Output is correct |
48 |
Correct |
127 ms |
41676 KB |
Output is correct |
49 |
Correct |
74 ms |
38968 KB |
Output is correct |
50 |
Correct |
124 ms |
38960 KB |
Output is correct |
51 |
Correct |
72 ms |
39756 KB |
Output is correct |
52 |
Correct |
66 ms |
39780 KB |
Output is correct |
53 |
Correct |
909 ms |
11264 KB |
Output is correct |
54 |
Correct |
167 ms |
39532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8764 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Correct |
4 ms |
8788 KB |
Output is correct |
4 |
Correct |
5 ms |
8788 KB |
Output is correct |
5 |
Correct |
11 ms |
8748 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
5 ms |
8788 KB |
Output is correct |
8 |
Correct |
5 ms |
8788 KB |
Output is correct |
9 |
Correct |
5 ms |
8788 KB |
Output is correct |
10 |
Correct |
5 ms |
8788 KB |
Output is correct |
11 |
Correct |
6 ms |
8788 KB |
Output is correct |
12 |
Correct |
46 ms |
8916 KB |
Output is correct |
13 |
Correct |
52 ms |
9380 KB |
Output is correct |
14 |
Correct |
162 ms |
9600 KB |
Output is correct |
15 |
Correct |
175 ms |
9620 KB |
Output is correct |
16 |
Execution timed out |
5041 ms |
36976 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Correct |
5 ms |
8788 KB |
Output is correct |
4 |
Correct |
14 ms |
8860 KB |
Output is correct |
5 |
Correct |
25 ms |
8860 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
6 ms |
8788 KB |
Output is correct |
8 |
Correct |
14 ms |
9036 KB |
Output is correct |
9 |
Correct |
221 ms |
16316 KB |
Output is correct |
10 |
Correct |
97 ms |
48340 KB |
Output is correct |
11 |
Correct |
10 ms |
8788 KB |
Output is correct |
12 |
Correct |
62 ms |
8856 KB |
Output is correct |
13 |
Execution timed out |
5044 ms |
41944 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |