# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
875634 |
2023-11-20T09:14:38 Z |
dimashhh |
Jail (JOI22_jail) |
C++17 |
|
1 ms |
604 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10, MOD = 998244353;
vector<int> g[N];
int timer = 0,tin[N],tout[N],n,m,s[N],t[N],blocked[N],c[N],pp[N];
void dfs(int v,int pr = 0){
tin[v] = ++timer;
pp[v] = pr;
for(int to:g[v]){
if(to == pr) continue;
dfs(to,v);
}
tout[v] = ++timer;
}
bool is(int v,int u){
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
bool can(int i){
int v =s[i],u = t[i];
while(!is(v,u)){
if(c[v] && c[v] != i) return false;
v = pp[v];
}
if(!is(u,v)){
if(c[u] && c[u] != i) return false;
u = pp[u];
}
assert(v == u);
if((c[v] && c[v] != i) || (c[u] && c[u] != i)) return false;
return 1;
}
void test(){
timer =0 ;
cin >> n;
for(int i = 1;i <= n;i++)
g[i].clear();
for(int i = 1;i <= n - 1;i++){
int x,y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
c[x] = c[y] = 0;
}
dfs(1);
cin >> m;
for(int i = 1;i <= m;i++){
blocked[i] = 0;
}
for(int i = 1;i <= m;++i){
cin >> s[i] >> t[i];
c[s[i]] = i;
}
// cout << can(2);
for(int i = 1;i <= m;i++){
bool ok = false;
for(int j = 1;j <= m;j++){
// cout << .
// cout << j << ' ' << can(j) << '\n';
if(blocked[j] || !can(j)) continue;
blocked[j] = 1;
c[s[j]] = 0;
c[t[j]] =j;
ok = 1;
break;
}
// cout << can(1) << '\n';
if(!ok){
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
for (int i = 1; i <= T; i++)
{
test();
}
}
Compilation message
jail.cpp:76:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
76 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |