結果

問題 No.1153 ねこちゃんゲーム
ユーザー yuto1115yuto1115
提出日時 2020-08-07 23:13:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 403 ms / 2,500 ms
コード長 3,999 bytes
コンパイル時間 2,455 ms
コンパイル使用メモリ 211,252 KB
実行使用メモリ 79,592 KB
最終ジャッジ日時 2024-09-24 23:57:30
合計ジャッジ時間 22,957 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 296 ms
48,692 KB
testcase_08 AC 313 ms
48,640 KB
testcase_09 AC 294 ms
48,608 KB
testcase_10 AC 292 ms
48,640 KB
testcase_11 AC 294 ms
48,640 KB
testcase_12 AC 296 ms
48,512 KB
testcase_13 AC 292 ms
48,640 KB
testcase_14 AC 298 ms
48,736 KB
testcase_15 AC 299 ms
48,640 KB
testcase_16 AC 297 ms
48,572 KB
testcase_17 AC 293 ms
48,768 KB
testcase_18 AC 293 ms
48,640 KB
testcase_19 AC 287 ms
48,636 KB
testcase_20 AC 297 ms
48,696 KB
testcase_21 AC 300 ms
48,768 KB
testcase_22 AC 302 ms
48,744 KB
testcase_23 AC 303 ms
48,768 KB
testcase_24 AC 318 ms
48,768 KB
testcase_25 AC 290 ms
48,740 KB
testcase_26 AC 302 ms
48,676 KB
testcase_27 AC 397 ms
78,836 KB
testcase_28 AC 393 ms
79,592 KB
testcase_29 AC 383 ms
76,504 KB
testcase_30 AC 403 ms
76,160 KB
testcase_31 AC 394 ms
65,024 KB
testcase_32 AC 272 ms
52,172 KB
testcase_33 AC 271 ms
52,264 KB
testcase_34 AC 271 ms
52,284 KB
testcase_35 AC 276 ms
52,172 KB
testcase_36 AC 275 ms
52,308 KB
testcase_37 AC 145 ms
52,360 KB
testcase_38 AC 127 ms
52,152 KB
testcase_39 AC 133 ms
52,152 KB
testcase_40 AC 145 ms
52,504 KB
testcase_41 AC 125 ms
52,424 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