結果

問題 No.1817 Reversed Edges
ユーザー srjywrdnprkt
提出日時 2023-05-24 01:27:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 110,996 KB
最終ジャッジ日時 2025-02-13 04:19:25
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>
using namespace std;
using ll = long long;
int x=0;
vector<int> ans;
void dfs(vector<vector<pair<int, int>>> &E, int from, int p){
for(auto [to, c] : E[from]){
if (p == to) continue;
if (c == -1) x++;
dfs(E, to, from);
}
}
void dfs2(vector<vector<pair<int, int>>> &E, int from, int p){
for(auto [to, c] : E[from]){
if (p == to) continue;
if (c == 1) ans[to] = ans[from]+1;
else ans[to] = ans[from]-1;
dfs2(E, to, from);
}
}
int main(){
int N, a, b;
cin >> N;
ans.resize(N);
vector<vector<pair<int, int>>> E(N);
for (int i=0; i<N-1; i++){
cin >> a >> b; a--; b--;
if (a > b) swap(a, b);
E[a].push_back({b, 1});
E[b].push_back({a, -1});
}
dfs(E, 0, -1);
ans[0] = x;
dfs2(E, 0, -1);
for (int i=0; i<N; i++) cout << ans[i] << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0