結果
問題 | No.1153 ねこちゃんゲーム |
ユーザー |
|
提出日時 | 2020-08-14 16:56:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,753 bytes |
コンパイル時間 | 2,015 ms |
コンパイル使用メモリ | 169,736 KB |
実行使用メモリ | 64,768 KB |
最終ジャッジ日時 | 2024-10-10 12:03:47 |
合計ジャッジ時間 | 23,763 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 10 WA * 30 |
ソースコード
// 15:36 - 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-downfor (auto v: a[u]) {if (v == pa) continue;nxt[u][sg[v]] = v;cnt[u][sg[v]]--;int sgNew = cnt[u][sg[v]] ? sg[u] : 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;}