結果

問題 No.872 All Tree Path
ユーザー yukudo
提出日時 2019-08-31 21:08:51
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 117 ms / 3,000 ms
コード長 1,310 bytes
コンパイル時間 1,259 ms
コンパイル使用メモリ 162,980 KB
実行使用メモリ 31,940 KB
最終ジャッジ日時 2024-11-25 07:30:06
合計ジャッジ時間 3,160 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int nextInt()’:
main.cpp:13:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 | int nextInt() { int x; scanf("%d", &x); return x;}
      |                        ~~~~~^~~~~~~~~~

ソースコード

diff #

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}

struct Edge {
  int to;
  int w;
};
const int MAX_N = 212345;
vector<Edge> g[MAX_N];

int N;
ll size[MAX_N];
ll ans;
void dfs(int cur, int prev, int d) {
  size[cur] = 1;
  for (Edge e : g[cur]) if (e.to != prev) {
    dfs(e.to, cur, e.w);
    size[cur] += size[e.to];
  }
  if (prev != -1) {
    ans += (ll)d * size[cur] * (N - size[cur]) * 2;
  }
}
int main2() {
  REP(i, MAX_N) g[i].clear();
  CLR(size, 0);

  N = nextInt();
  REP(i, N-1) {
    int a = nextInt() - 1;
    int b = nextInt() - 1;
    int w = nextInt();
    g[a].push_back({b, w});
    g[b].push_back({a, w});
  }

  ans = 0;
  dfs(0, -1, 0);
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0