67222023-12-17 20:20:25111Tom és Jerry 3cpp17Accepted 50/50108ms9532 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int T;
	cin >> T;
	while (T--) {
		int N, TS, JS, K;
		cin >> N >> TS >> JS >> K;
		vector<vector<int>> g(N + 1);
		for (int i = 0; i < N - 1; i++) {
			int a, b;
			cin >> a >> b;
			g[a].push_back(b);
			g[b].push_back(a);
		}
		deque<int> q;
		vector<int> td(N + 1, -1);
		td[TS] = 0;
		q.push_back(TS);
		while (!q.empty()) {
			int i = q.front();
			q.pop_front();
			for (int j : g[i]) {
				if (td[j] == -1) {
					td[j] = td[i] + 1;
					q.push_back(j);
				}
			}
		}
		vector<int> jd(N + 1, -1);
		jd[JS] = 0;
		q.push_back(JS);
		while (!q.empty()) {
			int i = q.front();
			q.pop_front();
			for (int j : g[i]) {
				if (jd[j] == -1) {
					jd[j] = jd[i] + 1;
					q.push_back(j);
				}
			}
		}
		int s = 0;
		for (int i = 1; i <= N; i++) {
			if (jd[i] < td[i] - 1) {
				s = max(s, td[i]);
			}
		}
		if (s <= K) {
			cout << "IGEN" << '\n';
			continue;
		}
		auto solve = [&](int i) {
			vector<int> v(N + 1, -1);
			deque<int> q;
			q.push_back(i);
			v[i] = 0;
			int f;
			while (!q.empty()) {
				int j = f = q.front();
				q.pop_front();
				for (int k : g[j]) {
					if (v[k] == -1) {
						v[k] = v[j] + 1;
						q.push_back(k);
					}
				}
			}
			return pii{f, v[f]};
		};
		auto [a, b] = solve(1);
		auto [c, d] = solve(a);
		if ((d + 1) / 2 <= K) {
			cout << "IGEN" << '\n';
			continue;
		}
		cout << "NEM" << '\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1824 KiB
2Accepted0/03ms2028 KiB
3Accepted5/53ms2228 KiB
4Accepted1/13ms2452 KiB
5Accepted1/13ms2668 KiB
6Accepted1/13ms2644 KiB
7Accepted1/13ms2988 KiB
8Accepted1/13ms2848 KiB
9Accepted1/13ms2856 KiB
10Accepted1/13ms2980 KiB
11Accepted2/23ms3068 KiB
12Accepted2/23ms3196 KiB
13Accepted1/13ms3408 KiB
14Accepted2/2105ms7052 KiB
15Accepted2/297ms5452 KiB
16Accepted2/2107ms7216 KiB
17Accepted2/2100ms8468 KiB
18Accepted2/2108ms6860 KiB
19Accepted2/2107ms7124 KiB
20Accepted2/268ms8532 KiB
21Accepted2/297ms5648 KiB
22Accepted2/297ms5628 KiB
23Accepted3/398ms8876 KiB
24Accepted2/2108ms7584 KiB
25Accepted3/3104ms7724 KiB
26Accepted2/2100ms9532 KiB
27Accepted2/2101ms6496 KiB
28Accepted3/3101ms6560 KiB