Submission #876675

# Submission time Handle Problem Language Result Execution time Memory
876675 2023-11-22T08:04:10 Z serkanrashid Jail (JOI22_jail) C++14
5 / 100
4 ms 7372 KB
#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],v[maxn];
int s[maxn],t[maxn];
int used[maxn];
bool ans;
int kr,prechki;

void precom()
{
    for(int i=1;i<=n;i++)
    {
        g[i].clear();
        used[i] = 0;
        v[i].clear();
    }
}

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];
    }
}

void dfs(int beg)
{
    used[beg] = 1;
    for(int nb:v[beg])
    {
        if(used[nb]==1) ans = false;
        if(!used[nb]) dfs(nb);
    }
    used[beg] = 2;
}

void dfskr(int beg, int par, int sum)
{
    sum += used[beg];
    if(beg==kr) prechki = sum;
    for(int nb:g[beg])
    {
        if(nb!=par) dfskr(nb,beg,sum);
    }
}

void solve()
{
    for(int i=1;i<=m;i++)
    {
        for(int j=i+1;j<=m;j++)
        {
            int ch1,ch2;
            ch1 = ch2 = 0;
            used[s[j]] = 2;
            used[t[j]] = 1;
            kr = t[i];
            dfskr(s[i],s[i],0);
            ch1 = prechki;
            used[s[j]] = 0;
            used[t[j]] = 0;
            if(ch1==3)
            {
                cout << "No" << endl;
                return;
            }
            used[s[i]] = 2;
            used[t[i]] = 1;
            kr = t[j];
            dfskr(s[j],s[j],0);
            used[s[j]] = 0;
            used[t[j]] = 0;
            ch2 = prechki;
            if(ch2==3)
            {
                cout << "No" << endl;
                return;
            }
            if(ch1==1||ch2==2) v[i].push_back(j);
            if(ch1==2||ch2==1) v[j].push_back(i);
        }
    }
    ans = true;
    for(int i=1;i<=m;i++) used[i] = 0;
    for(int i=1;i<=m;i++)
    {
        if(!used[i]) dfs(i);
    }
    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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Incorrect 2 ms 7260 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 2 ms 7260 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 3 ms 7256 KB Output is correct
7 Correct 3 ms 7256 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7260 KB Output is correct
10 Correct 2 ms 7260 KB Output is correct
11 Correct 3 ms 7260 KB Output is correct
12 Correct 2 ms 7260 KB Output is correct
13 Correct 2 ms 7336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 2 ms 7260 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 3 ms 7256 KB Output is correct
7 Correct 3 ms 7256 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7260 KB Output is correct
10 Correct 2 ms 7260 KB Output is correct
11 Correct 3 ms 7260 KB Output is correct
12 Correct 2 ms 7260 KB Output is correct
13 Correct 2 ms 7336 KB Output is correct
14 Correct 2 ms 7256 KB Output is correct
15 Incorrect 2 ms 7256 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 2 ms 7260 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 3 ms 7256 KB Output is correct
7 Correct 3 ms 7256 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7260 KB Output is correct
10 Correct 2 ms 7260 KB Output is correct
11 Correct 3 ms 7260 KB Output is correct
12 Correct 2 ms 7260 KB Output is correct
13 Correct 2 ms 7336 KB Output is correct
14 Correct 2 ms 7256 KB Output is correct
15 Incorrect 2 ms 7256 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 2 ms 7260 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 3 ms 7256 KB Output is correct
7 Correct 3 ms 7256 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7260 KB Output is correct
10 Correct 2 ms 7260 KB Output is correct
11 Correct 3 ms 7260 KB Output is correct
12 Correct 2 ms 7260 KB Output is correct
13 Correct 2 ms 7336 KB Output is correct
14 Correct 2 ms 7256 KB Output is correct
15 Incorrect 2 ms 7256 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7260 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Incorrect 2 ms 7260 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Incorrect 2 ms 7260 KB Output isn't correct
3 Halted 0 ms 0 KB -