結果

問題 No.1817 Reversed Edges
ユーザー iw522iw522
提出日時 2022-01-21 22:32:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,252 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 173,600 KB
実行使用メモリ 11,276 KB
最終ジャッジ日時 2023-08-17 06:49:16
合計ジャッジ時間 6,031 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 157 ms
10,064 KB
testcase_08 AC 73 ms
6,116 KB
testcase_09 AC 142 ms
9,316 KB
testcase_10 AC 86 ms
6,704 KB
testcase_11 AC 107 ms
7,760 KB
testcase_12 AC 174 ms
10,704 KB
testcase_13 AC 174 ms
10,640 KB
testcase_14 AC 176 ms
10,704 KB
testcase_15 AC 174 ms
10,672 KB
testcase_16 AC 176 ms
10,596 KB
testcase_17 AC 173 ms
10,636 KB
testcase_18 AC 177 ms
10,692 KB
testcase_19 AC 173 ms
10,532 KB
testcase_20 AC 174 ms
10,644 KB
testcase_21 AC 174 ms
10,636 KB
testcase_22 AC 153 ms
11,188 KB
testcase_23 AC 155 ms
11,276 KB
testcase_24 AC 156 ms
10,072 KB
権限があれば一括ダウンロードができます

ソースコード

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