# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
876296 | serkanrashid | Jail (JOI22_jail) | C++14 | 7 ms | 4444 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 endl "\n"
using namespace std;
const int maxn = 12*1e4+5;
int q,n,m;
int inf = 1e9;
vector<int>g[maxn];
int s[maxn],t[maxn];
int used[maxn];
int want;
vector<pair<int,int> >v;
void precom()
{
for(int i=1;i<=n;i++)
{
g[i].clear();
used[i] = 0;
}
}
void read()
{
cin >> n;
precom();
int a,b;
for(int i=1;i<n;i++)
{
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> m;
for(int i=1;i<=m;i++)
{
cin >> s[i] >> t[i];
used[s[i]] = inf;
}
}
void dfs(int beg, int par)
{
if(beg==want) want = -1;
for(int nb:g[beg])
{
if(nb!=par&&used[nb]!=inf)
{
if(used[nb]&&!used[used[nb]])
{
v.push_back(make_pair(used[nb],used[used[nb]]));
used[used[nb]] = inf;
}
dfs(nb,beg);
}
}
}
void solve()
{
bool ans = true;
for(int z=1;z<=m;z++)
{
bool f = false;
for(int i=1;i<=m;i++)
{
if(used[s[i]]==inf&&!used[t[i]])
{
want = t[i];
dfs(s[i],s[i]);
for(int x=0;x<v.size();x++) used[v[x].first] = v[x].second;
v.clear();
if(want==-1)
{
used[s[i]] = 0;
used[t[i]] = s[i];
f = true;
break;
}
}
}
ans = (ans & f);
if(!ans) break;
}
if(ans) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> q;
for(int i=1;i<=q;i++)
{
read();
solve();
}
return 0;
}
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... |