24492023-01-13 10:22:48rennTom és Jerry 3cpp11Hibás válasz 45/50170ms8484 KiB
#include    <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> gr;
typedef pair<int, int> pii;

inline int bfs(gr &graf, int kezdo, vector<int> &tavok)
{
    queue<int> next;
    next.push(kezdo);

    int i;
    while(!next.empty())
    {
        i = next.front();
        next.pop();

        for(auto &x : graf[i])
        {
            if(tavok[x] > -1) continue;

            next.push(x);
            tavok[x] = tavok[i]+1;
        }
    }

    return i;
}

inline int bfsm(gr &graf, int kezdo)
{
    queue<int> next;
    next.push(kezdo);
    vector<int> tavok(graf.size(), -1);

    int i, j;
    while(!next.empty())
    {
        i = next.front();
        next.pop();

        for(auto &x : graf[i])
        {
            if(tavok[x] > -1) continue;

            next.push(x);
            tavok[x] = tavok[i]+1;
        }
    }

    j = tavok[i]/2;
 
    //j = (j & 1)^1 ? j+1 : j;

    return j;
}

int jerry(gr &graf, int j, int &t, vector<int> &tomtavok)
{
    vector<int> jertavok(graf.size(), -1);
    jertavok[j] = 0;

    queue<int> next;
    next.push(j);
    int m = -1;

    int i;
    while(!next.empty())
    {
        i = next.front();
        next.pop();

        for(auto &x : graf[i])
        {
            if(jertavok[x] > -1 || tomtavok[x] <= jertavok[i]+2) continue;

            next.push(x);
            jertavok[x] = jertavok[i]+1;
            m = m > -1 ? (tomtavok[m] > tomtavok[x] ? m : x) : x;
        }
    }
    return m > -1 ? tomtavok[m] : tomtavok[i];
}

int main()
{
    int T;
    cin >> T;

    int n, t, j, k, nn, a, b;
    int kozep;

    while(T--)
    {
        cin >> n >> t >> j >> k;
        j--;
        t--;
        nn = n-1;

        gr graf(n);
        
        vector<int> tomtavok(n, -1);
        tomtavok[t] = 0;

        while(nn--)
        {
            cin >> a >> b;

            a--; b--;
            graf[a].push_back(b);
            graf[b].push_back(a);

        }

        nn = t;
        
        nn = bfs(graf, nn, tomtavok);

        if(tomtavok[j] == 1)
        {
            cout << "IGEN\n";
            continue;
        }

        if(jerry(graf, j, t, tomtavok) <= k)
        {
            cout << "IGEN\n";
            continue;
        }

        kozep = bfsm(graf, nn);

        cout << (kozep < k ? "IGEN" : "NEM") << "\n";
    }

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base45/50
1Elfogadva0/03ms1812 KiB
2Elfogadva0/02ms2064 KiB
3Elfogadva5/52ms2260 KiB
4Elfogadva1/13ms2420 KiB
5Hibás válasz0/13ms2656 KiB
6Elfogadva1/13ms2828 KiB
7Elfogadva1/13ms2936 KiB
8Elfogadva1/13ms3060 KiB
9Hibás válasz0/13ms3236 KiB
10Elfogadva1/13ms3316 KiB
11Elfogadva2/23ms3440 KiB
12Elfogadva2/23ms3512 KiB
13Elfogadva1/13ms3516 KiB
14Elfogadva2/2170ms6392 KiB
15Elfogadva2/2151ms5116 KiB
16Elfogadva2/2165ms6504 KiB
17Elfogadva2/2170ms8124 KiB
18Elfogadva2/2165ms6660 KiB
19Elfogadva2/2165ms6936 KiB
20Elfogadva2/2104ms8284 KiB
21Elfogadva2/2150ms5704 KiB
22Elfogadva2/2150ms5900 KiB
23Elfogadva3/3153ms8484 KiB
24Elfogadva2/2165ms7280 KiB
25Elfogadva3/3162ms7320 KiB
26Elfogadva2/2156ms8480 KiB
27Elfogadva2/2150ms6032 KiB
28Hibás válasz0/3150ms5956 KiB