結果

問題 No.1817 Reversed Edges
ユーザー otoshigootoshigo
提出日時 2023-02-24 15:30:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,063 bytes
コンパイル時間 664 ms
コンパイル使用メモリ 76,988 KB
実行使用メモリ 16,688 KB
最終ジャッジ日時 2023-10-10 00:26:45
合計ジャッジ時間 3,587 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,356 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 56 ms
8,368 KB
testcase_08 AC 25 ms
5,684 KB
testcase_09 AC 57 ms
8,024 KB
testcase_10 AC 29 ms
5,860 KB
testcase_11 AC 39 ms
6,736 KB
testcase_12 AC 67 ms
9,036 KB
testcase_13 AC 60 ms
9,028 KB
testcase_14 AC 64 ms
9,140 KB
testcase_15 AC 66 ms
9,004 KB
testcase_16 AC 71 ms
9,048 KB
testcase_17 AC 68 ms
9,060 KB
testcase_18 AC 72 ms
9,168 KB
testcase_19 AC 70 ms
9,048 KB
testcase_20 AC 70 ms
9,052 KB
testcase_21 AC 71 ms
9,264 KB
testcase_22 AC 31 ms
9,464 KB
testcase_23 AC 33 ms
9,452 KB
testcase_24 AC 44 ms
16,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

void dfs(int x,int p,vector<vector<int>>&g,vector<int>&ans,int &k){
    for(auto i:g[x]){
        if(i==p)continue;
        if(x<i){
            ans[i]=ans[x]+1;
        }else{
            k++;
            ans[i]=ans[x]-1;
        }
        dfs(i,x,g,ans,k);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin>>N;
    vector<vector<int>>g(N);
    rep(i,N-1){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int>ans(N);
    int k=0;
    dfs(0,-1,g,ans,k);
    rep(i,N){
        ans[i]+=k;
        cout<<ans[i]<<"\n";
    }
}
0