結果

問題 No.1153 ねこちゃんゲーム
ユーザー yuto1115yuto1115
提出日時 2020-08-07 23:13:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 399 ms / 2,500 ms
コード長 3,999 bytes
コンパイル時間 2,385 ms
コンパイル使用メモリ 212,776 KB
実行使用メモリ 79,428 KB
最終ジャッジ日時 2023-10-25 04:33:12
合計ジャッジ時間 23,069 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 321 ms
48,804 KB
testcase_08 AC 328 ms
48,804 KB
testcase_09 AC 323 ms
48,804 KB
testcase_10 AC 330 ms
48,804 KB
testcase_11 AC 327 ms
48,804 KB
testcase_12 AC 333 ms
48,804 KB
testcase_13 AC 318 ms
48,804 KB
testcase_14 AC 324 ms
48,804 KB
testcase_15 AC 331 ms
48,804 KB
testcase_16 AC 330 ms
48,804 KB
testcase_17 AC 329 ms
48,804 KB
testcase_18 AC 331 ms
48,804 KB
testcase_19 AC 334 ms
48,804 KB
testcase_20 AC 328 ms
48,804 KB
testcase_21 AC 329 ms
48,804 KB
testcase_22 AC 333 ms
48,804 KB
testcase_23 AC 332 ms
48,804 KB
testcase_24 AC 329 ms
48,804 KB
testcase_25 AC 329 ms
48,804 KB
testcase_26 AC 330 ms
48,804 KB
testcase_27 AC 399 ms
78,900 KB
testcase_28 AC 397 ms
79,428 KB
testcase_29 AC 393 ms
76,524 KB
testcase_30 AC 399 ms
76,260 KB
testcase_31 AC 383 ms
65,172 KB
testcase_32 AC 276 ms
52,436 KB
testcase_33 AC 276 ms
52,432 KB
testcase_34 AC 279 ms
52,436 KB
testcase_35 AC 276 ms
52,432 KB
testcase_36 AC 267 ms
52,436 KB
testcase_37 AC 143 ms
52,508 KB
testcase_38 AC 126 ms
52,088 KB
testcase_39 AC 133 ms
52,088 KB
testcase_40 AC 141 ms
52,436 KB
testcase_41 AC 124 ms
52,436 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function 'int rerooting::dfs(int, int)':
main.cpp:56:5: warning: control reaches end of non-void function [-Wreturn-type]
   56 |     }
      |     ^
main.cpp: In member function 'int rerooting::find(int, int)':
main.cpp:102:5: warning: control reaches end of non-void function [-Wreturn-type]
  102 |     }
      |     ^
main.cpp: In member function 'void rerooting::dfs2(int, int, int)':
main.cpp:68:16: warning: 'mex' may be used uninitialized [-Wmaybe-uninitialized]
   68 |         ans[v] = mex;
main.cpp:63:13: note: 'mex' was declared here
   63 |         int mex;
      |             ^~~

ソースコード

diff #

//@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

class rerooting {
    int n;
    vvi tree;
    vector<vector<int>> dp;
    vector<int> ans;
    
    int dfs(int v = 0, int p = -1) {
        int sz = tree[v].size();
        vb ex(sz + 1);
        dp[v].resize(sz);
        rep(i, sz) {
            int u = tree[v][i];
            if (u == p) continue;
            dp[v][i] = dfs(u, v);
            if (dp[v][i] <= sz) ex[dp[v][i]] = true;
        }
        rep(i, tree[v].size() + 1) if (!ex[i]) return i;
    }
    
    void dfs2(int dpP, int v = 0, int p = -1) {
        int sz = tree[v].size();
        rep(i, sz) if (tree[v][i] == p) dp[v][i] = dpP;
        vi ex(sz + 1);
        rep(i, sz) if (dp[v][i] <= sz) ex[dp[v][i]]++;
        int mex;
        rep(i, sz + 1) if (!ex[i]) {
                mex = i;
                break;
            }
        ans[v] = mex;
        rep(i, sz) {
            int u = tree[v][i];
            if (u == p) continue;
            int t;
            if (dp[v][i] > mex) t = mex;
            else {
                assert(dp[v][i] < mex and ex[dp[v][i]]);
                if (ex[dp[v][i]] > 1) t = mex;
                else t = dp[v][i];
            }
            dfs2(t, u, v);
        }
    }
    
    void init() {
        dfs();
        dfs2(0);
    }

public:
    rerooting(int n, vvi tree)
        : n(n), tree(tree), ans(n), dp(n) {
        init();
    };
    
    int get_ans(int i) {
        return ans[i];
    }
    
    int find(int pos, int gr) {
        rep(i, tree[pos].size()) {
            if (dp[pos][i] == gr) return tree[pos][i];
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vi a(m);
    cin >> a;
    rep(i, m) a[i]--;
    vvi G(n);
    rep(_, n - 1) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].pb(v);
        G[v].pb(u);
    }
    rerooting rt(n, G);
    int x = 0;
    vi b(m);
    rep(i, m) {
        b[i] = rt.get_ans(a[i]);
        x ^= b[i];
    }
    if (x == 0) {
        cout << "-1 -1" << endl;
        return 0;
    }
    int mx = 0, nx = x;
    while (nx > 1) {
        mx++;
        nx >>= 1;
    }
    rep(i, m) if (b[i] >> mx & 1) {
            cout << i + 1 << ' ';
            int gr = x ^ b[i];
            assert(gr < b[i]);
            cout << rt.find(a[i], gr) + 1 << endl;
            return 0;
        }
}
0