結果

問題 No.1153 ねこちゃんゲーム
ユーザー tarattata1tarattata1
提出日時 2020-07-11 16:40:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 388 ms / 2,500 ms
コード長 3,220 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 86,784 KB
実行使用メモリ 65,196 KB
最終ジャッジ日時 2023-08-08 12:18:04
合計ジャッジ時間 22,353 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 308 ms
42,496 KB
testcase_08 AC 312 ms
42,436 KB
testcase_09 AC 353 ms
42,356 KB
testcase_10 AC 303 ms
42,496 KB
testcase_11 AC 299 ms
42,432 KB
testcase_12 AC 319 ms
42,232 KB
testcase_13 AC 318 ms
42,432 KB
testcase_14 AC 309 ms
42,444 KB
testcase_15 AC 312 ms
42,308 KB
testcase_16 AC 304 ms
42,444 KB
testcase_17 AC 298 ms
43,032 KB
testcase_18 AC 309 ms
43,816 KB
testcase_19 AC 329 ms
43,824 KB
testcase_20 AC 322 ms
45,152 KB
testcase_21 AC 339 ms
45,324 KB
testcase_22 AC 298 ms
42,976 KB
testcase_23 AC 318 ms
43,500 KB
testcase_24 AC 321 ms
43,544 KB
testcase_25 AC 333 ms
44,608 KB
testcase_26 AC 315 ms
44,604 KB
testcase_27 AC 319 ms
56,688 KB
testcase_28 AC 388 ms
65,196 KB
testcase_29 AC 387 ms
63,348 KB
testcase_30 AC 385 ms
63,076 KB
testcase_31 AC 329 ms
47,780 KB
testcase_32 AC 218 ms
38,012 KB
testcase_33 AC 195 ms
37,988 KB
testcase_34 AC 206 ms
38,068 KB
testcase_35 AC 204 ms
38,060 KB
testcase_36 AC 219 ms
38,092 KB
testcase_37 AC 117 ms
37,784 KB
testcase_38 AC 98 ms
37,716 KB
testcase_39 AC 105 ms
37,712 KB
testcase_40 AC 112 ms
37,756 KB
testcase_41 AC 96 ms
37,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <iterator>
#pragma warning(disable:4996) 
#define INF 2140000000
using namespace std;

vector<vector<pair<int, int> > > g;   // to, edgeid
vector<int> parent;              // parent[i]: parent of vertex i
vector<int> val;                 // val[2*i]:val for downward dir, val[2*i+1]: val for upward dir
vector<map<int, int> > z;        // z[i]: vals conter for directed edges from vertex i
vector<int> ans;                 // ans[i]: Grundy number for vertex i

int dfs(int par, int curr)
{
    parent[curr] = par;
    int i;
    for (i = 0; i < (int)g[curr].size(); i++) {
        int next = g[curr][i].first;
        int edge = g[curr][i].second;
        if (next == par) continue;

        int tmp = dfs(curr, next);
        val[edge * 2] = tmp;
        z[curr][tmp]++;
    }
    for (i = 0; i < INF; i++) {
        if (z[curr].empty() || z[curr].find(i) == z[curr].end()) {
            break;
        }
    }
    return i;
}

void dfs2(int par, int curr)
{
    int i;
    for (i = 0; i < (int)g[curr].size(); i++) {
        int next = g[curr][i].first;
        int edge = g[curr][i].second;
        if (next == par) continue;

        int k;
        for (k = 0; k < INF; k++) {
            if (z[curr].empty()) {
                break;
            }
            auto it = z[curr].find(k);
            if (it == z[curr].end()) {
                break;
            }
            int num=it->second;
            if (val[edge*2]==k) num--;
            if (num < 1) {
                break;
            }
        }
        val[edge * 2 + 1] = k;
        z[next][k]++;
        dfs2(curr, next);
    }
    for (i = 0; i < INF; i++) {
        if (z[curr].empty() || z[curr].find(i) == z[curr].end()) {
            break;
        }
    }
    ans[curr] = i;
    return;
}

void solve()
{
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> a(m);
    int i;
    for (i = 0; i < m; i++) {
        scanf("%d", &a[i]); a[i]--;
    }

    g.resize(n);
    parent.resize(n);
    z.resize(n);
    ans.resize(n);
    val.resize((n-1) * 2);
    for (i = 0; i < n - 1; i++) {
        int p, q;
        scanf("%d%d", &p, &q); p--; q--;
        g[p].push_back(make_pair(q, i));
        g[q].push_back(make_pair(p, i));
    }
    dfs(-1, 0);  
    dfs2(-1, 0);

    int sum = 0;
    for (i = 0; i < m; i++) {
        sum ^= ans[a[i]];
    }
    if (sum == 0) {
        printf("-1 -1\n");
    }
    else {
        int max = -1;
        int maxi = -1;
        for (i = 0; i < m; i++) {
            int tmp = ans[a[i]] & sum;
            if (max < tmp) {
                max = tmp;
                maxi = i;
            }
        }

        int target = ans[a[maxi]]^sum;
        int nextvtx = -1;
        for (i = 0; i < (int)g[a[maxi]].size(); i++) {
            int next = g[a[maxi]][i].first;
            int edge = g[a[maxi]][i].second;
            int tmpval = (next == parent[a[maxi]] ? val[2 * edge + 1] : val[2 * edge]);
            if (target == tmpval) {
                nextvtx = next;
                break;
            }
        }
        printf("%d %d\n", maxi + 1, nextvtx + 1);
    }
    return;
}

int main()
{
    solve();
    return 0;
}
0