結果

問題 No.1817 Reversed Edges
ユーザー iw522
提出日時 2022-01-21 22:32:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,252 bytes
コンパイル時間 2,426 ms
コンパイル使用メモリ 175,760 KB
実行使用メモリ 11,536 KB
最終ジャッジ日時 2024-11-26 01:28:08
合計ジャッジ時間 6,976 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0; i<(ll)n; i++)
#define reps(i,n) for(ll i=1; i<=(ll)n; i++)
#define repi(i,a,b) for(ll i=(ll)a; i<(ll)b; i++)
#define pb push_back
#define mp make_pair
#define ALL(i) i.begin(),i.end()
ll INF = 1000000000000000;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll n; cin >> n;
    vector<vector<ll>> G(n);
    ll a,b;
    rep(i,n-1){
        cin >> a >> b;
        a--; b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    vector<ll> dist(n,-1);
    queue<int> q;
    dist[0]=0;
    q.push(0);
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(auto nv : G[v]){
            if(dist[nv]!=-1) continue;
            dist[nv]=dist[v]+1;
            q.push(nv);
        }
    }
    q.push(0);
    vector<ll> dist2(n,-1);
    dist2[0]=0;
    ll ans=0;
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(auto nv : G[v]){
            if(dist2[nv]!=-1) continue;
            if(nv>v) dist2[nv]=dist2[v]+1;
            else dist2[nv]=dist2[v];
            if(v>nv) ans++;
            q.push(nv);
        }
    }
    rep(i,n){
        cout << ans+dist2[i]-(dist[i]-dist2[i])<<endl;
    }
}
0