結果

問題 No.1153 ねこちゃんゲーム
ユーザー Guo yinhe
提出日時 2025-07-30 19:03:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 214 ms / 2,500 ms
コード長 2,309 bytes
コンパイル時間 1,904 ms
コンパイル使用メモリ 201,124 KB
実行使用メモリ 39,276 KB
最終ジャッジ日時 2025-07-30 19:03:26
合計ジャッジ時間 16,937 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

//Date: 2025-07-29 13:18:22
#include <bits/stdc++.h>
using namespace std;
#define P emplace_back
#define CLEAR(a, v) memset(a, (v), sizeof(a))
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
char buf[1 << 20], *p1, *p2;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
inline int rd() {
    int s = 0, m = 0; char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') m = 1; ch = getchar();}
    while(isdigit(ch)) s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
    return m ? -s : s;
}
bool MBE;
namespace SOLVER {
const int N = 4e5 + 5;
int n, m, tot, a[N], f[N], h[N], vis[N], sum, Mn[N]; vector<pii> g[N]; 
void dfs1(int u, int p, int las) {
    for(auto [v, id] : g[u]) if(v != p) dfs1(v, u, id);
    for(auto [v, id] : g[u]) if(v != p) vis[f[id]]++;
    if(p) while(vis[f[las]]) f[las]++; 
    for(auto [v, id] : g[u]) if(v != p) vis[f[id]] = 0;
}
void dfs2(int u, int p, int las) {
    for(auto [v, id] : g[u]) if(v != p) vis[f[id]]++; if(p) vis[f[las ^ 1]]++;
    int cur = 0; while(vis[cur]) cur++;
    for(auto [v, id] : g[u]) if(v != p) f[id ^ 1] = vis[f[id]] == 1 && f[id] < cur ? f[id] : cur;
    for(auto [v, id] : g[u]) if(v != p) vis[f[id]] = 0; vis[f[las ^ 1]] = 0;
    for(auto [v, id] : g[u]) if(v != p) dfs2(v, u, id);
}
void MAIN() {
    n = rd(), m = rd(); rep(i, 1, m) a[i] = rd();
    rep(i, 1, n - 1) {int u = rd(), v = rd(); g[u].P(v, tot++), g[v].P(u, tot++);}
    dfs1(1, 0, 0), dfs2(1, 0, 0);
    rep(u, 1, n) {
        for(auto [v, id] : g[u]) vis[f[id]]++;
        while(vis[h[u]]) h[u]++; for(auto [v, id] : g[u]) vis[f[id]] = 0;
    }
    rep(i, 1, m) sum ^= h[a[i]]; CLEAR(Mn, 0x3f);
    rep(u, 1, n) for(auto [v, id] : g[u]) if(!(sum ^ h[u] ^ f[id])) Mn[u] = min(Mn[u], v);
    rep(i, 1, m) if(Mn[a[i]] <= 1e9) return cout << i << ' ' << Mn[a[i]] << endl, void(); puts("-1 -1");
}
}
bool MED;
signed main() {
    // freopen("test.in", "r", stdin); // freopen(".out", "w", stdout);
    // cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
    for(int tt = 1; tt; tt--) SOLVER::MAIN();
    cerr << (&MBE - &MED) / 1024 << " KB, " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
    return 0;
}
0