結果

問題 No.1153 ねこちゃんゲーム
ユーザー nikkukunnikkukun
提出日時 2020-08-14 17:05:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 248 ms / 2,500 ms
コード長 1,805 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 166,668 KB
実行使用メモリ 64,896 KB
最終ジャッジ日時 2024-04-18 18:45:22
合計ジャッジ時間 19,448 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,320 KB
testcase_01 AC 5 ms
8,320 KB
testcase_02 AC 5 ms
8,448 KB
testcase_03 AC 5 ms
8,448 KB
testcase_04 AC 5 ms
8,448 KB
testcase_05 AC 5 ms
8,320 KB
testcase_06 AC 5 ms
8,576 KB
testcase_07 AC 208 ms
48,128 KB
testcase_08 AC 200 ms
48,000 KB
testcase_09 AC 206 ms
48,128 KB
testcase_10 AC 211 ms
48,032 KB
testcase_11 AC 210 ms
48,228 KB
testcase_12 AC 212 ms
48,256 KB
testcase_13 AC 229 ms
48,164 KB
testcase_14 AC 190 ms
48,256 KB
testcase_15 AC 206 ms
48,128 KB
testcase_16 AC 204 ms
48,224 KB
testcase_17 AC 201 ms
48,156 KB
testcase_18 AC 194 ms
48,128 KB
testcase_19 AC 196 ms
48,000 KB
testcase_20 AC 198 ms
48,304 KB
testcase_21 AC 215 ms
48,128 KB
testcase_22 AC 213 ms
48,128 KB
testcase_23 AC 195 ms
48,128 KB
testcase_24 AC 191 ms
48,256 KB
testcase_25 AC 210 ms
48,252 KB
testcase_26 AC 211 ms
48,256 KB
testcase_27 AC 248 ms
64,512 KB
testcase_28 AC 243 ms
64,896 KB
testcase_29 AC 227 ms
63,180 KB
testcase_30 AC 245 ms
63,104 KB
testcase_31 AC 238 ms
57,216 KB
testcase_32 AC 150 ms
48,468 KB
testcase_33 AC 146 ms
48,548 KB
testcase_34 AC 154 ms
48,464 KB
testcase_35 AC 154 ms
48,464 KB
testcase_36 AC 157 ms
48,332 KB
testcase_37 AC 112 ms
48,536 KB
testcase_38 AC 97 ms
48,584 KB
testcase_39 AC 99 ms
48,568 KB
testcase_40 AC 106 ms
48,708 KB
testcase_41 AC 90 ms
48,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 15:36 - 15:56
// debug: 15:56 -
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 2e5 + 5;
const int K = 18 + 2;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

vector<int> a[N];
int sg[N], cnt[N][K];
int rt[N], nxt[N][K];
int pos[N];

int Mex(int cnt[]) {
	for (int i = 0; i < K; i++)
		if (cnt[i] == 0)
			return i;
	return -1; // impossible
}

void DFS(int u, int pa) {
	for (auto v: a[u]) {
		if (v == pa) continue;
		DFS(v, u);
		cnt[u][sg[v]]++;
	}
	sg[u] = Mex(cnt[u]);
}

void Cal(int u, int pa, int sgPa) {
	// update sg(u)
	if (sgPa != -1) {
		nxt[u][sgPa] = pa;
		cnt[u][sgPa]++;
		sg[u] = Mex(cnt[u]);
	}
	rt[u] = sg[u];

	// top-down
	for (auto v: a[u]) {
		if (v == pa) continue;
		nxt[u][sg[v]] = v;
		cnt[u][sg[v]]--;
		int sgNew = sg[u];
		if (sg[v] < sg[u] && cnt[u][sg[v]] == 0) sgNew = sg[v];
		Cal(v, u, sgNew);
		cnt[u][sg[v]]++;
	}

	// restore sg(u)
	if (sgPa != -1) {
		cnt[u][sgPa]--;
		sg[u] = Mex(cnt[u]);
	}
}

int main() {
	ios::sync_with_stdio(0);

	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
		cin >> pos[i];
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		a[u].push_back(v);
		a[v].push_back(u);
	}
	DFS(1, 0);
	Cal(1, 0, -1);

	int sgSum = 0;
	for (int i = 1; i <= m; i++)
		sgSum ^= rt[pos[i]];
	if (sgSum == 0) {
		cout << "-1 -1" << endl;
		return 0;
	}

	for (int i = 1; i <= m; i++) {
		int u = pos[i];
		int need = rt[u] ^ sgSum;
		if (need >= K) continue;
		if (nxt[u][need]) {
			cout << i << ' ' << nxt[u][need] << endl;
			return 0;
		}
	}

	return 0;
}
0