24722023-01-13 11:01:44rennTom és Jerry 3cpp11Accepted 50/5086ms7724 KiB
#include    <bits/stdc++.h>
using namespace std;

#define InTheNameOfGod cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);

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);
    tavok[kezdo] = 0;

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

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 = j;

    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 = tomtavok[m] > tomtavok[x] ? m : x;
        }
    }
    return m > -1 ? tomtavok[m] : tomtavok[i];
}

int main()
{

    InTheNameOfGod

    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> tavok(n, -1);

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

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

        }

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

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

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

        std::fill(tavok.begin(), tavok.end(), -1);
        kozep = bfs(graf, nn, tavok);
        kozep = (tavok[kozep]-1)/2;

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

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1824 KiB
2Accepted0/02ms2024 KiB
3Accepted5/52ms2228 KiB
4Accepted1/13ms2464 KiB
5Accepted1/13ms2808 KiB
6Accepted1/13ms2880 KiB
7Accepted1/13ms3012 KiB
8Accepted1/13ms3084 KiB
9Accepted1/13ms3280 KiB
10Accepted1/13ms3200 KiB
11Accepted2/23ms3284 KiB
12Accepted2/23ms3324 KiB
13Accepted1/13ms3300 KiB
14Accepted2/282ms5996 KiB
15Accepted2/276ms4776 KiB
16Accepted2/286ms6040 KiB
17Accepted2/279ms7300 KiB
18Accepted2/283ms6192 KiB
19Accepted2/285ms6296 KiB
20Accepted2/254ms7420 KiB
21Accepted2/275ms4976 KiB
22Accepted2/275ms5144 KiB
23Accepted3/379ms7724 KiB
24Accepted2/282ms6372 KiB
25Accepted3/382ms6428 KiB
26Accepted2/281ms7500 KiB
27Accepted2/276ms5108 KiB
28Accepted3/375ms5356 KiB