結果

問題 No.1098 LCAs
ユーザー i_am_nicolen_22i_am_nicolen_22
提出日時 2020-06-27 02:17:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,869 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 175,256 KB
実行使用メモリ 36,284 KB
最終ジャッジ日時 2023-09-18 15:27:19
合計ジャッジ時間 9,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 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,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 1 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 WA -
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

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]; // 子のサイズを加える
    }
   
}


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);
    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);
    vector<int>used(n,false);
    for (int i = 0; i < n; i++)
    {
        used[i]=true;
        int cnt=1;
        int c=0;
        int sum=0;
        for(auto &u : G[i]){
            if(used[u])continue;
            //cerr<<subtree_size[u]<<endl;
            cnt*=subtree_size[u];
            c+=subtree_size[u];
            sum++;
        }
        if(c==0){
            cout<<1<<endl;
        }
        else if (sum==1){
            cout<<1+2*c<<endl;
        }
        else cout<<2*cnt+2*c+1<<endl;
    }
    
    
    return 0;
}
0