結果

問題 No.1817 Reversed Edges
ユーザー houren
提出日時 2022-01-22 01:50:05
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 1,283 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 181,188 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-11-26 10:20:44
合計ジャッジ時間 6,483 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool chmax(T&a, const T&b){if(a<b){a=b;return 1;}return 0;}

int main(){
    int n;
    cin >> n;
    vector<vector<int>> to(n);
    rep(i,n-1){
        int u,v;
        cin >> u >> v;
        u--; v--;
        to[u].push_back(v);
        to[v].push_back(u);
    }
    vector<bool> seen(n,false);
    queue<int> que;
    int cnt = 0;
    seen[0] = true;
    que.push(0);
    while(que.size()){
        int now = que.front();
        que.pop();
        for(int x:to[now]){
            if(seen[x]) continue;
            seen[x] = true;
            if(now>x) cnt++;
            que.push(x);
        }
    }
    vector<int> ans(n);
    ans[0] = cnt;
    seen.assign(n,false);
    seen[0] = true;
    que.push(0);
    while(que.size()){
        int now = que.front();
        que.pop();
        for(int x:to[now]){
            if(seen[x]) continue;
            seen[x] = true;
            ans[x] = ans[now] + 1 - (now>x) * 2;
            que.push(x);
        }
    }
    for(int x:ans) cout << x << endl;
    return 0;
}
0