結果

問題 No.1098 LCAs
ユーザー i_am_nicolen_22i_am_nicolen_22
提出日時 2020-06-27 02:24:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,785 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 174,272 KB
実行使用メモリ 38,976 KB
最終ジャッジ日時 2023-09-18 15:41:44
合計ジャッジ時間 8,578 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 437 ms
37,300 KB
testcase_29 AC 429 ms
38,976 KB
testcase_30 AC 436 ms
36,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

using Graph=vector<vector<int>>;
Graph G;


vector<int> depth;
vector<int> subtree_size;
vector<int>ans;
void dfs(const Graph &G, int v, int p, int d)
{
    depth[v] = d;
    for (auto nv : G[v])
    {
        if (nv == p)
            continue; // nv が親 p だったらダメ
        dfs(G, nv, v, d + 1);
    }

    // 帰りがけ時に、部分木サイズを求める
    subtree_size[v] = 1; // 自分自身
    for (auto c : G[v])
    {
        if (c == p)
            continue;
        subtree_size[v] += subtree_size[c]; // 子のサイズを加える
    }
    int cnt=1;
    int sum=0;
    int r=0;
    for(auto & u : G[v]){
        if(u == p){
            continue;
        }
        cnt*=subtree_size[u];
        r+=subtree_size[u];
        sum++;
    }
    if(sum==0){
        ans[v]=1;
    }
    else if(sum==1){
        ans[v]=cnt*2+1;
    }
    else {
        ans[v]=cnt*2+r*2+1;
    }
   
}


signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin>>n;
    G.assign(n,vector<int>());
    depth.resize(n,0);
    subtree_size.resize(n,0);
    ans.resize(n);
    for (int i = 0; i < n-1; i++)
    {
        int u,v;
        cin>>u>>v;
        u--,v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(G,0,-1,0);
    for (int i = 0; i < n; i++)
    {
        cout<<ans[i]<<endl;
    }
    
    
    return 0;
}
0