# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
890879 | vjudge1 | Jail (JOI22_jail) | C++17 | 4 ms | 15964 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define rnd(l, r) uniform_int_distribution<int>(l, r)(rng)
using namespace std;
void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;}
int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5;
vector <int> g[N];
int vis[N], parn[N];
int tin[N], tout[N];
int timer;
int up[N][30];
int s[N], t[N];
void dfs(int v, int p){
parn[v] = p;
tin[v] = timer++;
up[v][0] = p;
for (int i=1; i<=25; ++i)
up[v][i] = up[up[v][i-1]][i-1];
for(auto to : g[v]){
if(to == p)continue;
dfs(to, v);
}
tout[v] = timer++;
}
bool upper (int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca (int a, int b) {
if (upper (a, b)) return a;
if (upper (b, a)) return b;
for (int i=25; i>=0; --i)
if (! upper (up[a][i], b))
a = up[a][i];
return up[a][0];
}
void solve(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
g[i].clear();
vis[i] = 0;
tin[i] = 0;
}
timer = 0;
for(int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
int m;
cin >> m;
for(int i = 0; i < m; i++){
cin >> s[i] >> t[i];
}
dfs(1, 1);
for(int i = 0; i < m; i++){
int x1 = s[i], y1 = t[i];
//cout << x1 <<"-"<< x2 << endl;
//first
int lc = lca(x1, y1);
vis[lc]++;
while(x1 != lc){
vis[x1]++;
x1 = parn[x1];
}
while(y1 != lc){
vis[y1]++;
y1 = parn[y1];
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
if(i == j)continue;
int x1 = s[i], y1 = t[i], x2 = s[j], y2 = t[j];
x1 = s[i], x2 = s[j];
int lc = lca(x1, x2);
int cnt = 1, cntvis = (vis[lc] >= 2);
while(x1 != lc){
cntvis += (vis[x1] >= 2);
cnt++;
x1 = parn[x1];
}
while(x2 != lc){
cntvis += (vis[x2] >= 2);
cnt++;
x2 = parn[x2];
}
if(cnt == cntvis){
cout << "No\n";
return;
}
}
}
cout << "Yes\n";
}
main(){
iostream::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tests;
cin >> tests;
while(tests--)
solve();
}
/*
* Before implementing the problem:
Do I understand the problem correctly?
Which places are tricky? What do I need to remember to implement them correctly?
Which place is the heaviest by implementation? Can I do it simpler?
Which place is the slowest? Where do I need to be careful about constant factors and where I can choose slower but simpler implementation?
----------------------------------
If not AC:
Did you remember to do everything to handle the tricky places you thought about before?
Is the solution correct?
Do I understand the problem correctly?
----------------------------------
If you have a test on which the solution gives wrong answer:
Is the solution doing what it was supposed to do?
Is the problem in the code or in the idea?
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |