結果

問題 No.872 All Tree Path
ユーザー 👑 tute7627tute7627
提出日時 2019-08-30 22:19:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 190 ms / 3,000 ms
コード長 2,616 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 173,428 KB
実行使用メモリ 27,980 KB
最終ジャッジ日時 2023-08-14 05:45:08
合計ジャッジ時間 4,446 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 171 ms
19,492 KB
testcase_01 AC 161 ms
19,256 KB
testcase_02 AC 166 ms
19,640 KB
testcase_03 AC 87 ms
27,980 KB
testcase_04 AC 5 ms
8,960 KB
testcase_05 AC 190 ms
19,524 KB
testcase_06 AC 173 ms
19,164 KB
testcase_07 AC 172 ms
19,600 KB
testcase_08 AC 14 ms
10,172 KB
testcase_09 AC 15 ms
10,272 KB
testcase_10 AC 15 ms
10,292 KB
testcase_11 AC 15 ms
10,248 KB
testcase_12 AC 15 ms
10,424 KB
testcase_13 AC 5 ms
9,168 KB
testcase_14 AC 6 ms
8,964 KB
testcase_15 AC 5 ms
9,136 KB
testcase_16 AC 6 ms
9,120 KB
testcase_17 AC 4 ms
9,172 KB
testcase_18 AC 5 ms
9,060 KB
testcase_19 AC 5 ms
9,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
template<typename T>
void emp(map<T,ll>&m, T x){m.emplace(x,0).first->second++;}

ll MAX_V = 2e5 + 5; //頂点数
struct edge {
    ll to;
    ll cost;
};

//<最短距離, 頂点の番号>using P = pair<ll, ll>;
vector<vector<struct edge>> G(MAX_V); //隣接リスト
vector<ll>t(MAX_V);//訪問したかどうか
ll ret=0;
ll n;
ll dfs(ll k){
  t[k]=1;
  ll num=1;
  for(ll i=0;i<G[k].size();i++){
    if(t[G[k][i].to]==0){
      ll tmp=dfs(G[k][i].to);
      num+=tmp;
      ret+=2*(n-tmp)*tmp*G[k][i].cost;
      //cout<<ret spa tmp spa G[k][i].cost<<endl;
    }
  }
  return num;
}

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  cin>>n;
  for(ll i=0;i<n-1;i++){
    ll u,v,c;
    cin>>u>>v>>c;
    struct edge e ={v,c};
    G[u].push_back(e);
    e ={u,c};
    G[v].push_back(e);
  }
  dfs(1);
  cout<<ret<<endl;
  return 0;
}
0