#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];
int S[120005], 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();
for(int i = 1; i <= n; ++i) deg[i] = S[i] = T[i] = 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]] = T[t[i]] = 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) {
if(S[tmp] && i != S[tmp]) DAG[S[tmp]].push_back(i), deg[i]++;
if(T[tmp] && i != T[tmp]) DAG[i].push_back(T[tmp]), deg[T[tmp]]++;
tmp = parent[tmp][0];
}
}
if(S[p] && i != S[p]) DAG[S[p]].push_back(i), deg[i]++;
if(T[p] && i != T[p]) DAG[i].push_back(T[p]), deg[T[p]]++;
}
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 |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5968 KB |
Output is correct |
3 |
Correct |
3 ms |
5968 KB |
Output is correct |
4 |
Correct |
11 ms |
6356 KB |
Output is correct |
5 |
Correct |
21 ms |
6628 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
6156 KB |
Output is correct |
9 |
Correct |
75 ms |
10172 KB |
Output is correct |
10 |
Correct |
429 ms |
29776 KB |
Output is correct |
11 |
Correct |
8 ms |
6100 KB |
Output is correct |
12 |
Correct |
31 ms |
7024 KB |
Output is correct |
13 |
Correct |
125 ms |
59192 KB |
Output is correct |
14 |
Correct |
130 ms |
73040 KB |
Output is correct |
15 |
Execution timed out |
5076 ms |
898908 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5908 KB |
Output is correct |
2 |
Correct |
3 ms |
5964 KB |
Output is correct |
3 |
Correct |
4 ms |
5976 KB |
Output is correct |
4 |
Correct |
4 ms |
5976 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
9 ms |
5976 KB |
Output is correct |
8 |
Correct |
6 ms |
6064 KB |
Output is correct |
9 |
Correct |
5 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
5 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5908 KB |
Output is correct |
2 |
Correct |
3 ms |
5964 KB |
Output is correct |
3 |
Correct |
4 ms |
5976 KB |
Output is correct |
4 |
Correct |
4 ms |
5976 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
9 ms |
5976 KB |
Output is correct |
8 |
Correct |
6 ms |
6064 KB |
Output is correct |
9 |
Correct |
5 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
5 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
4 ms |
6072 KB |
Output is correct |
17 |
Correct |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
35 ms |
6048 KB |
Output is correct |
19 |
Correct |
3 ms |
5964 KB |
Output is correct |
20 |
Correct |
4 ms |
5976 KB |
Output is correct |
21 |
Correct |
10 ms |
5976 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
3 ms |
5924 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
3 ms |
5968 KB |
Output is correct |
27 |
Correct |
34 ms |
5944 KB |
Output is correct |
28 |
Correct |
4 ms |
5964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5908 KB |
Output is correct |
2 |
Correct |
3 ms |
5964 KB |
Output is correct |
3 |
Correct |
4 ms |
5976 KB |
Output is correct |
4 |
Correct |
4 ms |
5976 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
9 ms |
5976 KB |
Output is correct |
8 |
Correct |
6 ms |
6064 KB |
Output is correct |
9 |
Correct |
5 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
5 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
4 ms |
6072 KB |
Output is correct |
17 |
Correct |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
35 ms |
6048 KB |
Output is correct |
19 |
Correct |
3 ms |
5964 KB |
Output is correct |
20 |
Correct |
4 ms |
5976 KB |
Output is correct |
21 |
Correct |
10 ms |
5976 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
3 ms |
5924 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
3 ms |
5968 KB |
Output is correct |
27 |
Correct |
34 ms |
5944 KB |
Output is correct |
28 |
Correct |
4 ms |
5964 KB |
Output is correct |
29 |
Correct |
4 ms |
6104 KB |
Output is correct |
30 |
Correct |
4 ms |
5976 KB |
Output is correct |
31 |
Correct |
41 ms |
6088 KB |
Output is correct |
32 |
Correct |
4 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
5976 KB |
Output is correct |
34 |
Correct |
4 ms |
5976 KB |
Output is correct |
35 |
Correct |
5 ms |
6032 KB |
Output is correct |
36 |
Correct |
9 ms |
5972 KB |
Output is correct |
37 |
Correct |
4 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5908 KB |
Output is correct |
2 |
Correct |
3 ms |
5964 KB |
Output is correct |
3 |
Correct |
4 ms |
5976 KB |
Output is correct |
4 |
Correct |
4 ms |
5976 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
9 ms |
5976 KB |
Output is correct |
8 |
Correct |
6 ms |
6064 KB |
Output is correct |
9 |
Correct |
5 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
5 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
4 ms |
6072 KB |
Output is correct |
17 |
Correct |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
35 ms |
6048 KB |
Output is correct |
19 |
Correct |
3 ms |
5964 KB |
Output is correct |
20 |
Correct |
4 ms |
5976 KB |
Output is correct |
21 |
Correct |
10 ms |
5976 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
3 ms |
5924 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
3 ms |
5968 KB |
Output is correct |
27 |
Correct |
34 ms |
5944 KB |
Output is correct |
28 |
Correct |
4 ms |
5964 KB |
Output is correct |
29 |
Correct |
4 ms |
6104 KB |
Output is correct |
30 |
Correct |
4 ms |
5976 KB |
Output is correct |
31 |
Correct |
41 ms |
6088 KB |
Output is correct |
32 |
Correct |
4 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
5976 KB |
Output is correct |
34 |
Correct |
4 ms |
5976 KB |
Output is correct |
35 |
Correct |
5 ms |
6032 KB |
Output is correct |
36 |
Correct |
9 ms |
5972 KB |
Output is correct |
37 |
Correct |
4 ms |
5972 KB |
Output is correct |
38 |
Correct |
92 ms |
10192 KB |
Output is correct |
39 |
Correct |
401 ms |
29776 KB |
Output is correct |
40 |
Correct |
59 ms |
9204 KB |
Output is correct |
41 |
Correct |
25 ms |
8120 KB |
Output is correct |
42 |
Correct |
37 ms |
9280 KB |
Output is correct |
43 |
Correct |
22 ms |
8076 KB |
Output is correct |
44 |
Correct |
8 ms |
6484 KB |
Output is correct |
45 |
Correct |
59 ms |
24796 KB |
Output is correct |
46 |
Correct |
52 ms |
24796 KB |
Output is correct |
47 |
Correct |
170 ms |
26540 KB |
Output is correct |
48 |
Correct |
168 ms |
26648 KB |
Output is correct |
49 |
Correct |
64 ms |
24908 KB |
Output is correct |
50 |
Correct |
60 ms |
24912 KB |
Output is correct |
51 |
Correct |
44 ms |
25408 KB |
Output is correct |
52 |
Correct |
43 ms |
25300 KB |
Output is correct |
53 |
Correct |
10 ms |
7516 KB |
Output is correct |
54 |
Correct |
55 ms |
24672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5968 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5964 KB |
Output is correct |
5 |
Correct |
8 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5972 KB |
Output is correct |
8 |
Correct |
3 ms |
5960 KB |
Output is correct |
9 |
Correct |
4 ms |
5968 KB |
Output is correct |
10 |
Correct |
3 ms |
5964 KB |
Output is correct |
11 |
Correct |
3 ms |
5964 KB |
Output is correct |
12 |
Correct |
3 ms |
6076 KB |
Output is correct |
13 |
Correct |
16 ms |
6580 KB |
Output is correct |
14 |
Correct |
23 ms |
6868 KB |
Output is correct |
15 |
Correct |
19 ms |
6648 KB |
Output is correct |
16 |
Correct |
58 ms |
25288 KB |
Output is correct |
17 |
Correct |
129 ms |
31308 KB |
Output is correct |
18 |
Correct |
235 ms |
51388 KB |
Output is correct |
19 |
Correct |
58 ms |
25540 KB |
Output is correct |
20 |
Correct |
61 ms |
25548 KB |
Output is correct |
21 |
Correct |
70 ms |
25492 KB |
Output is correct |
22 |
Correct |
103 ms |
31944 KB |
Output is correct |
23 |
Correct |
85 ms |
32228 KB |
Output is correct |
24 |
Correct |
85 ms |
31984 KB |
Output is correct |
25 |
Correct |
85 ms |
32080 KB |
Output is correct |
26 |
Correct |
92 ms |
32036 KB |
Output is correct |
27 |
Correct |
129 ms |
31664 KB |
Output is correct |
28 |
Correct |
118 ms |
31296 KB |
Output is correct |
29 |
Correct |
104 ms |
31252 KB |
Output is correct |
30 |
Correct |
75 ms |
27972 KB |
Output is correct |
31 |
Correct |
88 ms |
27972 KB |
Output is correct |
32 |
Correct |
98 ms |
28120 KB |
Output is correct |
33 |
Correct |
74 ms |
27968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5968 KB |
Output is correct |
3 |
Correct |
3 ms |
5968 KB |
Output is correct |
4 |
Correct |
11 ms |
6356 KB |
Output is correct |
5 |
Correct |
21 ms |
6628 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
6156 KB |
Output is correct |
9 |
Correct |
75 ms |
10172 KB |
Output is correct |
10 |
Correct |
429 ms |
29776 KB |
Output is correct |
11 |
Correct |
8 ms |
6100 KB |
Output is correct |
12 |
Correct |
31 ms |
7024 KB |
Output is correct |
13 |
Correct |
125 ms |
59192 KB |
Output is correct |
14 |
Correct |
130 ms |
73040 KB |
Output is correct |
15 |
Execution timed out |
5076 ms |
898908 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |