#include <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m, tt;
int parent[120005][25];
int s[120005], t[120005], dep[120005], deg[120005];
vector<int> S[120005];
vector<int> T[120005];
vector<int> graph[120005];
vector<int> DAG[120005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> tt;
while(tt--) {
cin >> n;
for(int i = 1; i <= n; ++i) graph[i].clear(), DAG[i].clear(), S[i].clear(), T[i].clear();
for(int i = 1; i <= n; ++i) deg[i] = 0;
for(int i = 1; i <= n; ++i) for(int j = 0; j <= 20; ++j) parent[i][j] = 0;
for(int i = 1; i <= n - 1; ++i) {
int a, b; cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
cin >> m;
for(int i = 1; i <= m; ++i) cin >> s[i] >> t[i];
auto dfs = [&](auto&& self, int node, int par) -> void {
parent[node][0] = par; dep[node] = dep[par] + 1;
for(auto v : graph[node]) if(v != par) self(self, v, node);
};
dfs(dfs, 1, 0);
for(int j = 1; j < 20; ++j) for(int i = 1; i <= n; ++i) parent[i][j] = parent[parent[i][j - 1]][j - 1];
auto LCA = [&](int u, int v) -> int {
if(dep[u] < dep[v]) swap(u, v);
int diff = dep[u] - dep[v], j = 0;
while(diff) {
if(diff & 1) u = parent[u][j];
diff >>= 1; ++j;
}
if(u == v) return u;
for(int i = 19; i >= 0; --i) {
if(parent[u][i] != parent[v][i]) {
u = parent[u][i];
v = parent[v][i];
}
}
return parent[u][0];
};
for(int i = 1; i <= m; ++i) S[s[i]].push_back(i), T[t[i]].push_back(i);
for(int i = 1; i <= m; ++i) {
int p = LCA(s[i], t[i]);
for(int tmp : {s[i], t[i]}) {
while(tmp != p) {
for(auto j : S[tmp]) if(j != i) DAG[j].push_back(i), deg[i]++;
for(auto j : T[tmp]) if(j != i) DAG[i].push_back(j), deg[j]++;
tmp = parent[tmp][0];
}
}
for(auto j : S[p]) if(j != i) DAG[j].push_back(i), deg[i]++;
for(auto j : T[p]) if(j != i) DAG[i].push_back(j), deg[j]++;
}
queue<int> q; int cnt = 0;
for(int i = 1; i <= m; ++i) if(deg[i] == 0) q.push(i);
while(!q.empty()) {
int node = q.front(); q.pop(); ++cnt;
for(auto v : DAG[node]) if(--deg[v] == 0) q.push(v);
}
if(cnt == m) cout << "Yes\n";
else cout << "No\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11620 KB |
Output is correct |
2 |
Correct |
5 ms |
11604 KB |
Output is correct |
3 |
Correct |
6 ms |
11604 KB |
Output is correct |
4 |
Correct |
13 ms |
11728 KB |
Output is correct |
5 |
Correct |
23 ms |
11756 KB |
Output is correct |
6 |
Correct |
7 ms |
11604 KB |
Output is correct |
7 |
Correct |
6 ms |
11608 KB |
Output is correct |
8 |
Correct |
7 ms |
11824 KB |
Output is correct |
9 |
Correct |
103 ms |
16056 KB |
Output is correct |
10 |
Correct |
621 ms |
34540 KB |
Output is correct |
11 |
Correct |
10 ms |
11772 KB |
Output is correct |
12 |
Correct |
43 ms |
12696 KB |
Output is correct |
13 |
Correct |
127 ms |
66936 KB |
Output is correct |
14 |
Correct |
139 ms |
80852 KB |
Output is correct |
15 |
Execution timed out |
5095 ms |
502236 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11600 KB |
Output is correct |
2 |
Correct |
8 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11680 KB |
Output is correct |
4 |
Correct |
6 ms |
11604 KB |
Output is correct |
5 |
Correct |
6 ms |
11608 KB |
Output is correct |
6 |
Correct |
7 ms |
11688 KB |
Output is correct |
7 |
Correct |
6 ms |
11608 KB |
Output is correct |
8 |
Correct |
8 ms |
11620 KB |
Output is correct |
9 |
Correct |
6 ms |
11612 KB |
Output is correct |
10 |
Correct |
7 ms |
11604 KB |
Output is correct |
11 |
Correct |
12 ms |
11624 KB |
Output is correct |
12 |
Correct |
6 ms |
11584 KB |
Output is correct |
13 |
Correct |
6 ms |
11672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11600 KB |
Output is correct |
2 |
Correct |
8 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11680 KB |
Output is correct |
4 |
Correct |
6 ms |
11604 KB |
Output is correct |
5 |
Correct |
6 ms |
11608 KB |
Output is correct |
6 |
Correct |
7 ms |
11688 KB |
Output is correct |
7 |
Correct |
6 ms |
11608 KB |
Output is correct |
8 |
Correct |
8 ms |
11620 KB |
Output is correct |
9 |
Correct |
6 ms |
11612 KB |
Output is correct |
10 |
Correct |
7 ms |
11604 KB |
Output is correct |
11 |
Correct |
12 ms |
11624 KB |
Output is correct |
12 |
Correct |
6 ms |
11584 KB |
Output is correct |
13 |
Correct |
6 ms |
11672 KB |
Output is correct |
14 |
Correct |
13 ms |
11520 KB |
Output is correct |
15 |
Correct |
11 ms |
11604 KB |
Output is correct |
16 |
Correct |
7 ms |
11604 KB |
Output is correct |
17 |
Correct |
7 ms |
11608 KB |
Output is correct |
18 |
Correct |
6 ms |
11628 KB |
Output is correct |
19 |
Correct |
8 ms |
11604 KB |
Output is correct |
20 |
Correct |
7 ms |
11620 KB |
Output is correct |
21 |
Correct |
9 ms |
11616 KB |
Output is correct |
22 |
Correct |
6 ms |
11604 KB |
Output is correct |
23 |
Correct |
6 ms |
11604 KB |
Output is correct |
24 |
Correct |
6 ms |
11600 KB |
Output is correct |
25 |
Correct |
8 ms |
11824 KB |
Output is correct |
26 |
Correct |
6 ms |
11604 KB |
Output is correct |
27 |
Correct |
7 ms |
11696 KB |
Output is correct |
28 |
Correct |
6 ms |
11604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11600 KB |
Output is correct |
2 |
Correct |
8 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11680 KB |
Output is correct |
4 |
Correct |
6 ms |
11604 KB |
Output is correct |
5 |
Correct |
6 ms |
11608 KB |
Output is correct |
6 |
Correct |
7 ms |
11688 KB |
Output is correct |
7 |
Correct |
6 ms |
11608 KB |
Output is correct |
8 |
Correct |
8 ms |
11620 KB |
Output is correct |
9 |
Correct |
6 ms |
11612 KB |
Output is correct |
10 |
Correct |
7 ms |
11604 KB |
Output is correct |
11 |
Correct |
12 ms |
11624 KB |
Output is correct |
12 |
Correct |
6 ms |
11584 KB |
Output is correct |
13 |
Correct |
6 ms |
11672 KB |
Output is correct |
14 |
Correct |
13 ms |
11520 KB |
Output is correct |
15 |
Correct |
11 ms |
11604 KB |
Output is correct |
16 |
Correct |
7 ms |
11604 KB |
Output is correct |
17 |
Correct |
7 ms |
11608 KB |
Output is correct |
18 |
Correct |
6 ms |
11628 KB |
Output is correct |
19 |
Correct |
8 ms |
11604 KB |
Output is correct |
20 |
Correct |
7 ms |
11620 KB |
Output is correct |
21 |
Correct |
9 ms |
11616 KB |
Output is correct |
22 |
Correct |
6 ms |
11604 KB |
Output is correct |
23 |
Correct |
6 ms |
11604 KB |
Output is correct |
24 |
Correct |
6 ms |
11600 KB |
Output is correct |
25 |
Correct |
8 ms |
11824 KB |
Output is correct |
26 |
Correct |
6 ms |
11604 KB |
Output is correct |
27 |
Correct |
7 ms |
11696 KB |
Output is correct |
28 |
Correct |
6 ms |
11604 KB |
Output is correct |
29 |
Correct |
9 ms |
11736 KB |
Output is correct |
30 |
Correct |
7 ms |
11732 KB |
Output is correct |
31 |
Correct |
7 ms |
11732 KB |
Output is correct |
32 |
Correct |
8 ms |
11596 KB |
Output is correct |
33 |
Correct |
7 ms |
11592 KB |
Output is correct |
34 |
Correct |
7 ms |
11608 KB |
Output is correct |
35 |
Correct |
8 ms |
11732 KB |
Output is correct |
36 |
Correct |
7 ms |
11612 KB |
Output is correct |
37 |
Correct |
6 ms |
11612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11600 KB |
Output is correct |
2 |
Correct |
8 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11680 KB |
Output is correct |
4 |
Correct |
6 ms |
11604 KB |
Output is correct |
5 |
Correct |
6 ms |
11608 KB |
Output is correct |
6 |
Correct |
7 ms |
11688 KB |
Output is correct |
7 |
Correct |
6 ms |
11608 KB |
Output is correct |
8 |
Correct |
8 ms |
11620 KB |
Output is correct |
9 |
Correct |
6 ms |
11612 KB |
Output is correct |
10 |
Correct |
7 ms |
11604 KB |
Output is correct |
11 |
Correct |
12 ms |
11624 KB |
Output is correct |
12 |
Correct |
6 ms |
11584 KB |
Output is correct |
13 |
Correct |
6 ms |
11672 KB |
Output is correct |
14 |
Correct |
13 ms |
11520 KB |
Output is correct |
15 |
Correct |
11 ms |
11604 KB |
Output is correct |
16 |
Correct |
7 ms |
11604 KB |
Output is correct |
17 |
Correct |
7 ms |
11608 KB |
Output is correct |
18 |
Correct |
6 ms |
11628 KB |
Output is correct |
19 |
Correct |
8 ms |
11604 KB |
Output is correct |
20 |
Correct |
7 ms |
11620 KB |
Output is correct |
21 |
Correct |
9 ms |
11616 KB |
Output is correct |
22 |
Correct |
6 ms |
11604 KB |
Output is correct |
23 |
Correct |
6 ms |
11604 KB |
Output is correct |
24 |
Correct |
6 ms |
11600 KB |
Output is correct |
25 |
Correct |
8 ms |
11824 KB |
Output is correct |
26 |
Correct |
6 ms |
11604 KB |
Output is correct |
27 |
Correct |
7 ms |
11696 KB |
Output is correct |
28 |
Correct |
6 ms |
11604 KB |
Output is correct |
29 |
Correct |
9 ms |
11736 KB |
Output is correct |
30 |
Correct |
7 ms |
11732 KB |
Output is correct |
31 |
Correct |
7 ms |
11732 KB |
Output is correct |
32 |
Correct |
8 ms |
11596 KB |
Output is correct |
33 |
Correct |
7 ms |
11592 KB |
Output is correct |
34 |
Correct |
7 ms |
11608 KB |
Output is correct |
35 |
Correct |
8 ms |
11732 KB |
Output is correct |
36 |
Correct |
7 ms |
11612 KB |
Output is correct |
37 |
Correct |
6 ms |
11612 KB |
Output is correct |
38 |
Correct |
95 ms |
16104 KB |
Output is correct |
39 |
Correct |
662 ms |
34544 KB |
Output is correct |
40 |
Correct |
76 ms |
15060 KB |
Output is correct |
41 |
Correct |
34 ms |
13900 KB |
Output is correct |
42 |
Correct |
49 ms |
15060 KB |
Output is correct |
43 |
Correct |
28 ms |
13664 KB |
Output is correct |
44 |
Correct |
12 ms |
12120 KB |
Output is correct |
45 |
Correct |
61 ms |
29500 KB |
Output is correct |
46 |
Correct |
61 ms |
29500 KB |
Output is correct |
47 |
Correct |
271 ms |
31360 KB |
Output is correct |
48 |
Correct |
287 ms |
31368 KB |
Output is correct |
49 |
Correct |
65 ms |
29492 KB |
Output is correct |
50 |
Correct |
66 ms |
29556 KB |
Output is correct |
51 |
Correct |
52 ms |
30044 KB |
Output is correct |
52 |
Correct |
51 ms |
30120 KB |
Output is correct |
53 |
Correct |
15 ms |
13080 KB |
Output is correct |
54 |
Correct |
67 ms |
29424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11604 KB |
Output is correct |
2 |
Correct |
5 ms |
11604 KB |
Output is correct |
3 |
Correct |
8 ms |
11532 KB |
Output is correct |
4 |
Correct |
6 ms |
11604 KB |
Output is correct |
5 |
Correct |
12 ms |
11784 KB |
Output is correct |
6 |
Correct |
6 ms |
11604 KB |
Output is correct |
7 |
Correct |
7 ms |
11552 KB |
Output is correct |
8 |
Correct |
7 ms |
11600 KB |
Output is correct |
9 |
Correct |
6 ms |
11532 KB |
Output is correct |
10 |
Correct |
6 ms |
11604 KB |
Output is correct |
11 |
Correct |
6 ms |
11592 KB |
Output is correct |
12 |
Correct |
8 ms |
11728 KB |
Output is correct |
13 |
Correct |
21 ms |
12176 KB |
Output is correct |
14 |
Correct |
30 ms |
12500 KB |
Output is correct |
15 |
Correct |
25 ms |
12244 KB |
Output is correct |
16 |
Correct |
75 ms |
30668 KB |
Output is correct |
17 |
Correct |
147 ms |
39816 KB |
Output is correct |
18 |
Correct |
400 ms |
63552 KB |
Output is correct |
19 |
Correct |
72 ms |
31488 KB |
Output is correct |
20 |
Correct |
75 ms |
31516 KB |
Output is correct |
21 |
Correct |
73 ms |
31520 KB |
Output is correct |
22 |
Correct |
126 ms |
40252 KB |
Output is correct |
23 |
Correct |
113 ms |
40780 KB |
Output is correct |
24 |
Correct |
113 ms |
40408 KB |
Output is correct |
25 |
Correct |
117 ms |
40516 KB |
Output is correct |
26 |
Correct |
114 ms |
40408 KB |
Output is correct |
27 |
Correct |
192 ms |
43488 KB |
Output is correct |
28 |
Correct |
172 ms |
43544 KB |
Output is correct |
29 |
Correct |
171 ms |
43536 KB |
Output is correct |
30 |
Correct |
103 ms |
36420 KB |
Output is correct |
31 |
Correct |
104 ms |
36452 KB |
Output is correct |
32 |
Correct |
114 ms |
36436 KB |
Output is correct |
33 |
Correct |
108 ms |
36416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11620 KB |
Output is correct |
2 |
Correct |
5 ms |
11604 KB |
Output is correct |
3 |
Correct |
6 ms |
11604 KB |
Output is correct |
4 |
Correct |
13 ms |
11728 KB |
Output is correct |
5 |
Correct |
23 ms |
11756 KB |
Output is correct |
6 |
Correct |
7 ms |
11604 KB |
Output is correct |
7 |
Correct |
6 ms |
11608 KB |
Output is correct |
8 |
Correct |
7 ms |
11824 KB |
Output is correct |
9 |
Correct |
103 ms |
16056 KB |
Output is correct |
10 |
Correct |
621 ms |
34540 KB |
Output is correct |
11 |
Correct |
10 ms |
11772 KB |
Output is correct |
12 |
Correct |
43 ms |
12696 KB |
Output is correct |
13 |
Correct |
127 ms |
66936 KB |
Output is correct |
14 |
Correct |
139 ms |
80852 KB |
Output is correct |
15 |
Execution timed out |
5095 ms |
502236 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |