結果

問題 No.1153 ねこちゃんゲーム
ユーザー nikkukunnikkukun
提出日時 2020-08-14 17:05:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,500 ms
コード長 1,805 bytes
コンパイル時間 4,431 ms
コンパイル使用メモリ 167,108 KB
実行使用メモリ 61,968 KB
最終ジャッジ日時 2023-07-31 18:43:06
合計ジャッジ時間 20,042 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,856 KB
testcase_01 AC 4 ms
8,912 KB
testcase_02 AC 5 ms
9,052 KB
testcase_03 AC 5 ms
9,080 KB
testcase_04 AC 5 ms
9,060 KB
testcase_05 AC 5 ms
9,056 KB
testcase_06 AC 4 ms
9,064 KB
testcase_07 AC 209 ms
47,972 KB
testcase_08 AC 212 ms
48,048 KB
testcase_09 AC 207 ms
48,100 KB
testcase_10 AC 220 ms
47,976 KB
testcase_11 AC 220 ms
48,028 KB
testcase_12 AC 207 ms
47,980 KB
testcase_13 AC 210 ms
48,056 KB
testcase_14 AC 226 ms
47,964 KB
testcase_15 AC 213 ms
47,964 KB
testcase_16 AC 230 ms
47,968 KB
testcase_17 AC 236 ms
47,980 KB
testcase_18 AC 231 ms
48,068 KB
testcase_19 AC 217 ms
48,032 KB
testcase_20 AC 220 ms
48,060 KB
testcase_21 AC 211 ms
48,172 KB
testcase_22 AC 207 ms
48,264 KB
testcase_23 AC 208 ms
47,996 KB
testcase_24 AC 211 ms
48,000 KB
testcase_25 AC 207 ms
48,024 KB
testcase_26 AC 223 ms
48,100 KB
testcase_27 AC 262 ms
61,608 KB
testcase_28 AC 243 ms
61,968 KB
testcase_29 AC 242 ms
60,628 KB
testcase_30 AC 235 ms
60,556 KB
testcase_31 AC 229 ms
55,552 KB
testcase_32 AC 157 ms
48,308 KB
testcase_33 AC 156 ms
48,284 KB
testcase_34 AC 156 ms
48,452 KB
testcase_35 AC 154 ms
48,376 KB
testcase_36 AC 154 ms
48,436 KB
testcase_37 AC 110 ms
48,432 KB
testcase_38 AC 96 ms
48,376 KB
testcase_39 AC 102 ms
48,448 KB
testcase_40 AC 106 ms
48,440 KB
testcase_41 AC 92 ms
48,556 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