# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1053430 |
2024-08-11T11:40:46 Z |
j_vdd16 |
Jail (JOI22_jail) |
C++17 |
|
233 ms |
39356 KB |
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
//#define int long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef uint64_t u64;
typedef int64_t i64;
int n, m;
vvi adj;
vi depth;
void dfs(int node, int parent, int d) {
depth[node] = d;
for (int child : adj[node]) {
if (child == parent) continue;
dfs(child, node, d + 1);
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
constexpr int POW = 20;
while (q--) {
cin >> n;
adj = vvi(n);
depth = vi(n);
loop(i, n - 1) {
int a, b;
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0, -1, 0);
vi lift[POW] = {};
vvi endPointLift[POW] = {};
loop(i, POW) {
lift[i] = vi(n);
endPointLift[i] = vvi(n);
}
loop(i, n) lift[0][i] = i;
vii routes;
cin >> m;
loop(i, m) {
int s, t;
cin >> s >> t;
s--; t--;
routes.push_back({s, t});
endPointLift[0][t].push_back(i);
}
for (int i = 1; i < POW; i++) {
loop(j, n) {
endPointLift[i][j] = endPointLift[i - 1][j];
int next = lift[i - 1][j];
lift[i][j] = lift[i - 1][next];
for (int x : endPointLift[i - 1][next]) {
endPointLift[i][j].push_back(x);
}
}
}
vi inDegree(n);
vvi out(n);
loop(i, m) {
auto [s, t] = routes[i];
vi endPointsOnRoute;
if (depth[s] < depth[t]) swap(s, t);
int todo = depth[s] - depth[t];
loop(j, POW) {
if (todo & (1 << j)) {
for (int x : endPointLift[j][s]) endPointsOnRoute.push_back(x);
s = lift[j][s];
}
}
for (int j = POW - 1; j >= 0; j--) {
if (lift[j][s] == lift[j][t]) continue;
for (int x : endPointLift[j][s]) endPointsOnRoute.push_back(x);
for (int x : endPointLift[j][t]) endPointsOnRoute.push_back(x);
s = lift[j][s];
t = lift[j][t];
}
for (int x : endPointLift[0][s]) endPointsOnRoute.push_back(x);
s = lift[0][s];
for (int x : endPointsOnRoute) {
if (x == i) continue;
inDegree[x]++;
out[i].push_back(x);
}
}
stack<int> startingPoints;
loop(i, m) {
if (inDegree[i] == 0) startingPoints.push(i);
}
int count = 0;
while (startingPoints.size()) {
int node = startingPoints.top();
startingPoints.pop();
count++;
for (int child : out[node]) {
inDegree[child]--;
if (inDegree[child] == 0) startingPoints.push(child);
}
}
if (count == m) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
34036 KB |
Output is correct |
2 |
Incorrect |
63 ms |
39356 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35228 KB |
Output is correct |
2 |
Correct |
23 ms |
34924 KB |
Output is correct |
3 |
Incorrect |
233 ms |
22652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35228 KB |
Output is correct |
2 |
Correct |
23 ms |
34924 KB |
Output is correct |
3 |
Incorrect |
233 ms |
22652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35228 KB |
Output is correct |
2 |
Correct |
23 ms |
34924 KB |
Output is correct |
3 |
Incorrect |
233 ms |
22652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35228 KB |
Output is correct |
2 |
Correct |
23 ms |
34924 KB |
Output is correct |
3 |
Incorrect |
233 ms |
22652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
34740 KB |
Output is correct |
2 |
Correct |
39 ms |
37184 KB |
Output is correct |
3 |
Incorrect |
63 ms |
37688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
34036 KB |
Output is correct |
2 |
Incorrect |
63 ms |
39356 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |