結果

問題 No.1098 LCAs
ユーザー 増井舜増井舜
提出日時 2020-06-27 14:46:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 1,919 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 170,420 KB
実行使用メモリ 30,056 KB
最終ジャッジ日時 2023-09-18 19:29:11
合計ジャッジ時間 8,674 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,384 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 369 ms
15,600 KB
testcase_19 AC 367 ms
15,592 KB
testcase_20 AC 367 ms
15,820 KB
testcase_21 AC 354 ms
15,888 KB
testcase_22 AC 378 ms
15,832 KB
testcase_23 AC 336 ms
16,300 KB
testcase_24 AC 332 ms
16,084 KB
testcase_25 AC 327 ms
16,440 KB
testcase_26 AC 332 ms
16,368 KB
testcase_27 AC 334 ms
16,476 KB
testcase_28 AC 403 ms
28,824 KB
testcase_29 AC 405 ms
30,056 KB
testcase_30 AC 405 ms
28,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#include<memory>

#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
using Graph=vector<vector<int>>;
using ll=long long;
#define WHATS(var)cout<<__LINE__<<' '<<#var<<"="<<var<<endl;
	
template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
	os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
	int i=0;
	for(auto it:v){
		if(i > 0){os << ' ';}
		os << it;
		i++;
	} 
	return os;
}


ll dfs(int now,int from,const Graph&g,vector<ll>&anss){
    ll sum=0;
    for(auto e:g[now]){
        if(e!=from){
            ll kk=dfs(e,now,g,anss);
            anss[now]-=kk*kk;
            sum+=kk;
        }
    }
    sum++;
    anss[now]+=ll(sum)*sum;
    return sum;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie();
    int N;cin>>N;
    Graph g(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);
    }
    vector<ll>anss(N);
    dfs(0,-1,g,anss);
    for(auto a:anss)cout<<a<<endl;
	return 0;
}
0