67222023-12-17 20:20:25111Tom és Jerry 3cpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1824 KiB
2Elfogadva0/03ms2028 KiB
3Elfogadva5/53ms2228 KiB
4Elfogadva1/13ms2452 KiB
5Elfogadva1/13ms2668 KiB
6Elfogadva1/13ms2644 KiB
7Elfogadva1/13ms2988 KiB
8Elfogadva1/13ms2848 KiB
9Elfogadva1/13ms2856 KiB
10Elfogadva1/13ms2980 KiB
11Elfogadva2/23ms3068 KiB
12Elfogadva2/23ms3196 KiB
13Elfogadva1/13ms3408 KiB
14Elfogadva2/2105ms7052 KiB
15Elfogadva2/297ms5452 KiB
16Elfogadva2/2107ms7216 KiB
17Elfogadva2/2100ms8468 KiB
18Elfogadva2/2108ms6860 KiB
19Elfogadva2/2107ms7124 KiB
20Elfogadva2/268ms8532 KiB
21Elfogadva2/297ms5648 KiB
22Elfogadva2/297ms5628 KiB
23Elfogadva3/398ms8876 KiB
24Elfogadva2/2108ms7584 KiB
25Elfogadva3/3104ms7724 KiB
26Elfogadva2/2100ms9532 KiB
27Elfogadva2/2101ms6496 KiB
28Elfogadva3/3101ms6560 KiB