24582023-01-13 10:36:22rennTom és Jerry 3cpp11Accepted 50/5090ms17300 KiB
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair
#define ins insert

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

using ll = long long;
using ull = unsigned long long ;
using ld = long double ;
using str = string;
using ordered_set=tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)

const int MAXN=200001;

int n,t,j,k;
vector<int> adj[MAXN];
vector<int> distt, distj;

vector<int> bfs(int x) {
	vector<int> dist(n+1, -1);
	
	queue<pair<int,int>> q;
	q.push({x, 0});
	dist[x]=0;
	
	while(!q.empty()) {
		auto akt=q.front();q.pop();
		for(auto i:adj[akt.first]) {
			if(dist[i]==-1) {
				dist[i]=akt.yy+1;
				q.push({i, dist[i]});
			}
		}
	}
	
	return dist;
}

int vis[MAXN];
int explore(int jerry) {
	if(distj[jerry]>=distt[jerry]-1 || vis[jerry]) return -1;
	vis[jerry]=1;
	
	int mx=distt[jerry];
	for(auto i:adj[jerry]) {
		mx=max(mx, explore(i));
	}
	
	return mx;
}

int main() {
	IO;
	int T;
	cin>>T;
	while(T--) {
		cin>>n>>t>>j>>k;
		for(int i=1;i<=n;++i) {
			adj[i].clear();
			vis[i]=0;
		}
		
		for(int i=1;i<n;++i) {
			int a,b;
			cin>>a>>b;
			adj[a].pb(b);
			adj[b].pb(a);
		}
		
		
		vector<int> dist1=bfs(1);
		int furthest=max_element(all(dist1))-dist1.begin();
		vector<int> distf=bfs(furthest);
		int diameter=*max_element(all(distf));
				
		bool can_catch=false;
		can_catch|=diameter<=2*k;
		
		distt=bfs(t);
		distj=bfs(j);
		
		int mx=explore(j);
		can_catch|=mx<=k;
		
		cout<<(can_catch?"IGEN":"NEM")<<"\n";
	}
	
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/07ms11264 KiB
2Accepted0/06ms11528 KiB
3Accepted5/54ms11860 KiB
4Accepted1/17ms12020 KiB
5Accepted1/16ms12156 KiB
6Accepted1/16ms12364 KiB
7Accepted1/16ms12564 KiB
8Accepted1/17ms12764 KiB
9Accepted1/17ms12848 KiB
10Accepted1/16ms12972 KiB
11Accepted2/26ms13048 KiB
12Accepted2/26ms13040 KiB
13Accepted1/16ms13048 KiB
14Accepted2/286ms15348 KiB
15Accepted2/276ms14328 KiB
16Accepted2/289ms15620 KiB
17Accepted2/286ms16564 KiB
18Accepted2/286ms15676 KiB
19Accepted2/290ms16188 KiB
20Accepted2/267ms16924 KiB
21Accepted2/276ms15004 KiB
22Accepted2/275ms15232 KiB
23Accepted3/389ms17300 KiB
24Accepted2/286ms16260 KiB
25Accepted3/386ms16832 KiB
26Accepted2/286ms17188 KiB
27Accepted2/274ms15160 KiB
28Accepted3/375ms15476 KiB