#include <bits/stdc++.h>
using namespace std;
const int MX=4e5+5;
int Q,N,M;
int S[MX], T[MX];
vector<int> path[MX]; // store the path between Si and Ti
vector<int> adj[MX];
int par[MX];
void dfs(int v, int p) {
par[v]=p;
for(auto u:adj[v]) {
if(u==p) continue;
dfs(u,v);
}
}
vector<int> ADJ[MX];
int deg[MX];
bool f(int i, int x) {
for(auto y:path[i])
if(y==x) return true;
return false;
}
int cntT[MX], cntS[MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>Q;
while(Q--) {
cin>>N;
for(int i=1;i<=N;i++) adj[i].clear(), cntT[i]=0, cntS[i]=0;
for(int i=0;i<N-1;i++) {
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin>>M;
for(int i=0;i<2*N+M;i++) path[i].clear(), ADJ[i].clear(), deg[i]=0;
for(int i=0;i<M;i++) {
cin>>S[i]>>T[i];
dfs(S[i],S[i]);
int p=T[i];
while(p!=S[i]) {
path[i].push_back(p);
p=par[p];
}
path[i].push_back(S[i]);
}
for(int i=0;i<M;i++) {
cntS[S[i]]+=1;
cntT[T[i]]+=1;
}
set<pair<int,int>> st;
for(int i=0;i<M;i++) {
cntS[S[i]]-=1;
cntT[T[i]]-=1;
for(auto x:path[i]) {
if(cntS[x]) {
ADJ[M-1+x].push_back(i);
deg[i]+=1;
}
if(cntT[x]) {
ADJ[i].push_back(M-1+x+N);
deg[M-1+x+N]+=1;
}
}
ADJ[i].push_back(M-1+S[i]);
deg[M-1+S[i]]+=1;
ADJ[M-1+T[i]+N].push_back(i);
deg[i]+=1;
cntS[S[i]]+=1;
cntT[T[i]]+=1;
}
queue<int> q;
for(int i=0;i<2*N+M;i++) {
if(!deg[i]) {
q.push(i);
}
}
int ans=0;
while(!q.empty()) {
auto v=q.front(); q.pop();
ans+=1;
for(auto u:ADJ[v]) {
deg[u]--;
if(!deg[u]) q.push(u);
}
}
cout<<(ans==2*N+M?"Yes":"No")<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
37468 KB |
Output is correct |
2 |
Correct |
8 ms |
37212 KB |
Output is correct |
3 |
Correct |
8 ms |
37316 KB |
Output is correct |
4 |
Correct |
15 ms |
37724 KB |
Output is correct |
5 |
Correct |
24 ms |
38076 KB |
Output is correct |
6 |
Correct |
8 ms |
37212 KB |
Output is correct |
7 |
Correct |
9 ms |
37344 KB |
Output is correct |
8 |
Correct |
13 ms |
37464 KB |
Output is correct |
9 |
Correct |
450 ms |
57440 KB |
Output is correct |
10 |
Correct |
971 ms |
282436 KB |
Output is correct |
11 |
Correct |
12 ms |
37468 KB |
Output is correct |
12 |
Correct |
62 ms |
38496 KB |
Output is correct |
13 |
Execution timed out |
5093 ms |
48624 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37212 KB |
Output is correct |
2 |
Correct |
7 ms |
37208 KB |
Output is correct |
3 |
Correct |
9 ms |
37208 KB |
Output is correct |
4 |
Correct |
9 ms |
37460 KB |
Output is correct |
5 |
Correct |
10 ms |
37468 KB |
Output is correct |
6 |
Correct |
8 ms |
37212 KB |
Output is correct |
7 |
Correct |
9 ms |
37212 KB |
Output is correct |
8 |
Correct |
9 ms |
37212 KB |
Output is correct |
9 |
Correct |
9 ms |
37212 KB |
Output is correct |
10 |
Correct |
9 ms |
37352 KB |
Output is correct |
11 |
Correct |
8 ms |
37464 KB |
Output is correct |
12 |
Correct |
8 ms |
37208 KB |
Output is correct |
13 |
Correct |
9 ms |
37212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37212 KB |
Output is correct |
2 |
Correct |
7 ms |
37208 KB |
Output is correct |
3 |
Correct |
9 ms |
37208 KB |
Output is correct |
4 |
Correct |
9 ms |
37460 KB |
Output is correct |
5 |
Correct |
10 ms |
37468 KB |
Output is correct |
6 |
Correct |
8 ms |
37212 KB |
Output is correct |
7 |
Correct |
9 ms |
37212 KB |
Output is correct |
8 |
Correct |
9 ms |
37212 KB |
Output is correct |
9 |
Correct |
9 ms |
37212 KB |
Output is correct |
10 |
Correct |
9 ms |
37352 KB |
Output is correct |
11 |
Correct |
8 ms |
37464 KB |
Output is correct |
12 |
Correct |
8 ms |
37208 KB |
Output is correct |
13 |
Correct |
9 ms |
37212 KB |
Output is correct |
14 |
Correct |
9 ms |
37208 KB |
Output is correct |
15 |
Correct |
8 ms |
37212 KB |
Output is correct |
16 |
Correct |
10 ms |
37324 KB |
Output is correct |
17 |
Correct |
9 ms |
37208 KB |
Output is correct |
18 |
Correct |
9 ms |
37464 KB |
Output is correct |
19 |
Correct |
9 ms |
37408 KB |
Output is correct |
20 |
Correct |
10 ms |
37212 KB |
Output is correct |
21 |
Correct |
9 ms |
37212 KB |
Output is correct |
22 |
Correct |
8 ms |
37324 KB |
Output is correct |
23 |
Correct |
8 ms |
37212 KB |
Output is correct |
24 |
Correct |
9 ms |
37208 KB |
Output is correct |
25 |
Correct |
9 ms |
37212 KB |
Output is correct |
26 |
Correct |
9 ms |
37344 KB |
Output is correct |
27 |
Correct |
9 ms |
37464 KB |
Output is correct |
28 |
Correct |
8 ms |
37388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37212 KB |
Output is correct |
2 |
Correct |
7 ms |
37208 KB |
Output is correct |
3 |
Correct |
9 ms |
37208 KB |
Output is correct |
4 |
Correct |
9 ms |
37460 KB |
Output is correct |
5 |
Correct |
10 ms |
37468 KB |
Output is correct |
6 |
Correct |
8 ms |
37212 KB |
Output is correct |
7 |
Correct |
9 ms |
37212 KB |
Output is correct |
8 |
Correct |
9 ms |
37212 KB |
Output is correct |
9 |
Correct |
9 ms |
37212 KB |
Output is correct |
10 |
Correct |
9 ms |
37352 KB |
Output is correct |
11 |
Correct |
8 ms |
37464 KB |
Output is correct |
12 |
Correct |
8 ms |
37208 KB |
Output is correct |
13 |
Correct |
9 ms |
37212 KB |
Output is correct |
14 |
Correct |
9 ms |
37208 KB |
Output is correct |
15 |
Correct |
8 ms |
37212 KB |
Output is correct |
16 |
Correct |
10 ms |
37324 KB |
Output is correct |
17 |
Correct |
9 ms |
37208 KB |
Output is correct |
18 |
Correct |
9 ms |
37464 KB |
Output is correct |
19 |
Correct |
9 ms |
37408 KB |
Output is correct |
20 |
Correct |
10 ms |
37212 KB |
Output is correct |
21 |
Correct |
9 ms |
37212 KB |
Output is correct |
22 |
Correct |
8 ms |
37324 KB |
Output is correct |
23 |
Correct |
8 ms |
37212 KB |
Output is correct |
24 |
Correct |
9 ms |
37208 KB |
Output is correct |
25 |
Correct |
9 ms |
37212 KB |
Output is correct |
26 |
Correct |
9 ms |
37344 KB |
Output is correct |
27 |
Correct |
9 ms |
37464 KB |
Output is correct |
28 |
Correct |
8 ms |
37388 KB |
Output is correct |
29 |
Correct |
12 ms |
37464 KB |
Output is correct |
30 |
Correct |
12 ms |
37468 KB |
Output is correct |
31 |
Correct |
11 ms |
37464 KB |
Output is correct |
32 |
Correct |
11 ms |
37468 KB |
Output is correct |
33 |
Correct |
9 ms |
37212 KB |
Output is correct |
34 |
Correct |
11 ms |
37464 KB |
Output is correct |
35 |
Correct |
13 ms |
37560 KB |
Output is correct |
36 |
Correct |
10 ms |
37472 KB |
Output is correct |
37 |
Correct |
10 ms |
37296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37212 KB |
Output is correct |
2 |
Correct |
7 ms |
37208 KB |
Output is correct |
3 |
Correct |
9 ms |
37208 KB |
Output is correct |
4 |
Correct |
9 ms |
37460 KB |
Output is correct |
5 |
Correct |
10 ms |
37468 KB |
Output is correct |
6 |
Correct |
8 ms |
37212 KB |
Output is correct |
7 |
Correct |
9 ms |
37212 KB |
Output is correct |
8 |
Correct |
9 ms |
37212 KB |
Output is correct |
9 |
Correct |
9 ms |
37212 KB |
Output is correct |
10 |
Correct |
9 ms |
37352 KB |
Output is correct |
11 |
Correct |
8 ms |
37464 KB |
Output is correct |
12 |
Correct |
8 ms |
37208 KB |
Output is correct |
13 |
Correct |
9 ms |
37212 KB |
Output is correct |
14 |
Correct |
9 ms |
37208 KB |
Output is correct |
15 |
Correct |
8 ms |
37212 KB |
Output is correct |
16 |
Correct |
10 ms |
37324 KB |
Output is correct |
17 |
Correct |
9 ms |
37208 KB |
Output is correct |
18 |
Correct |
9 ms |
37464 KB |
Output is correct |
19 |
Correct |
9 ms |
37408 KB |
Output is correct |
20 |
Correct |
10 ms |
37212 KB |
Output is correct |
21 |
Correct |
9 ms |
37212 KB |
Output is correct |
22 |
Correct |
8 ms |
37324 KB |
Output is correct |
23 |
Correct |
8 ms |
37212 KB |
Output is correct |
24 |
Correct |
9 ms |
37208 KB |
Output is correct |
25 |
Correct |
9 ms |
37212 KB |
Output is correct |
26 |
Correct |
9 ms |
37344 KB |
Output is correct |
27 |
Correct |
9 ms |
37464 KB |
Output is correct |
28 |
Correct |
8 ms |
37388 KB |
Output is correct |
29 |
Correct |
12 ms |
37464 KB |
Output is correct |
30 |
Correct |
12 ms |
37468 KB |
Output is correct |
31 |
Correct |
11 ms |
37464 KB |
Output is correct |
32 |
Correct |
11 ms |
37468 KB |
Output is correct |
33 |
Correct |
9 ms |
37212 KB |
Output is correct |
34 |
Correct |
11 ms |
37464 KB |
Output is correct |
35 |
Correct |
13 ms |
37560 KB |
Output is correct |
36 |
Correct |
10 ms |
37472 KB |
Output is correct |
37 |
Correct |
10 ms |
37296 KB |
Output is correct |
38 |
Correct |
455 ms |
57280 KB |
Output is correct |
39 |
Correct |
849 ms |
282468 KB |
Output is correct |
40 |
Correct |
802 ms |
45880 KB |
Output is correct |
41 |
Correct |
764 ms |
39164 KB |
Output is correct |
42 |
Correct |
307 ms |
46928 KB |
Output is correct |
43 |
Correct |
89 ms |
38860 KB |
Output is correct |
44 |
Correct |
62 ms |
37724 KB |
Output is correct |
45 |
Correct |
1592 ms |
44312 KB |
Output is correct |
46 |
Correct |
1675 ms |
44256 KB |
Output is correct |
47 |
Correct |
692 ms |
85768 KB |
Output is correct |
48 |
Correct |
708 ms |
87568 KB |
Output is correct |
49 |
Correct |
1081 ms |
46504 KB |
Output is correct |
50 |
Correct |
1204 ms |
46472 KB |
Output is correct |
51 |
Correct |
391 ms |
44012 KB |
Output is correct |
52 |
Correct |
401 ms |
44024 KB |
Output is correct |
53 |
Correct |
63 ms |
37972 KB |
Output is correct |
54 |
Correct |
1401 ms |
43788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
37208 KB |
Output is correct |
2 |
Correct |
7 ms |
37212 KB |
Output is correct |
3 |
Correct |
7 ms |
37212 KB |
Output is correct |
4 |
Correct |
9 ms |
37208 KB |
Output is correct |
5 |
Correct |
14 ms |
37468 KB |
Output is correct |
6 |
Correct |
8 ms |
37212 KB |
Output is correct |
7 |
Correct |
8 ms |
37212 KB |
Output is correct |
8 |
Correct |
8 ms |
37212 KB |
Output is correct |
9 |
Correct |
8 ms |
37396 KB |
Output is correct |
10 |
Correct |
8 ms |
37212 KB |
Output is correct |
11 |
Correct |
8 ms |
37388 KB |
Output is correct |
12 |
Correct |
10 ms |
37400 KB |
Output is correct |
13 |
Correct |
39 ms |
37972 KB |
Output is correct |
14 |
Correct |
50 ms |
38224 KB |
Output is correct |
15 |
Correct |
45 ms |
37972 KB |
Output is correct |
16 |
Execution timed out |
5047 ms |
43604 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
37468 KB |
Output is correct |
2 |
Correct |
8 ms |
37212 KB |
Output is correct |
3 |
Correct |
8 ms |
37316 KB |
Output is correct |
4 |
Correct |
15 ms |
37724 KB |
Output is correct |
5 |
Correct |
24 ms |
38076 KB |
Output is correct |
6 |
Correct |
8 ms |
37212 KB |
Output is correct |
7 |
Correct |
9 ms |
37344 KB |
Output is correct |
8 |
Correct |
13 ms |
37464 KB |
Output is correct |
9 |
Correct |
450 ms |
57440 KB |
Output is correct |
10 |
Correct |
971 ms |
282436 KB |
Output is correct |
11 |
Correct |
12 ms |
37468 KB |
Output is correct |
12 |
Correct |
62 ms |
38496 KB |
Output is correct |
13 |
Execution timed out |
5093 ms |
48624 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |