結果

問題 No.1153 ねこちゃんゲーム
ユーザー nikkukunnikkukun
提出日時 2020-08-14 17:05:35
言語 C++14
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 144 ms / 2,500 ms
コード長 1,805 bytes
コンパイル時間 1,234 ms
使用メモリ 62,032 KB
最終ジャッジ日時 2022-12-03 10:37:26
合計ジャッジ時間 14,624 ms
ジャッジサーバーID
(参考情報)
judge16 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 3 ms
10,960 KB
testcase_01 AC 3 ms
11,088 KB
testcase_02 AC 3 ms
11,140 KB
testcase_03 AC 3 ms
11,188 KB
testcase_04 AC 4 ms
11,236 KB
testcase_05 AC 4 ms
11,212 KB
testcase_06 AC 4 ms
11,140 KB
testcase_07 AC 116 ms
48,092 KB
testcase_08 AC 113 ms
48,168 KB
testcase_09 AC 117 ms
48,088 KB
testcase_10 AC 113 ms
48,052 KB
testcase_11 AC 117 ms
48,096 KB
testcase_12 AC 115 ms
48,036 KB
testcase_13 AC 116 ms
48,056 KB
testcase_14 AC 113 ms
48,172 KB
testcase_15 AC 120 ms
48,108 KB
testcase_16 AC 117 ms
48,044 KB
testcase_17 AC 117 ms
48,244 KB
testcase_18 AC 117 ms
48,160 KB
testcase_19 AC 115 ms
48,144 KB
testcase_20 AC 118 ms
48,272 KB
testcase_21 AC 113 ms
48,260 KB
testcase_22 AC 124 ms
48,208 KB
testcase_23 AC 111 ms
48,132 KB
testcase_24 AC 109 ms
48,132 KB
testcase_25 AC 114 ms
48,104 KB
testcase_26 AC 115 ms
48,168 KB
testcase_27 AC 140 ms
61,684 KB
testcase_28 AC 135 ms
62,032 KB
testcase_29 AC 139 ms
60,764 KB
testcase_30 AC 144 ms
60,596 KB
testcase_31 AC 133 ms
55,564 KB
testcase_32 AC 93 ms
48,280 KB
testcase_33 AC 90 ms
48,224 KB
testcase_34 AC 96 ms
48,420 KB
testcase_35 AC 92 ms
48,412 KB
testcase_36 AC 89 ms
48,240 KB
testcase_37 AC 78 ms
48,448 KB
testcase_38 AC 65 ms
48,388 KB
testcase_39 AC 68 ms
48,240 KB
testcase_40 AC 72 ms
48,324 KB
testcase_41 AC 60 ms
48,432 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