結果

問題 No.872 All Tree Path
ユーザー tarattata1
提出日時 2019-08-31 01:47:23
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 214 ms / 3,000 ms
コード長 1,560 bytes
コンパイル時間 811 ms
コンパイル使用メモリ 78,140 KB
実行使用メモリ 51,284 KB
最終ジャッジ日時 2024-11-22 10:02:28
合計ジャッジ時間 3,354 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:60:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   60 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
main.cpp:66:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   66 |         scanf("%d%d%d", &u, &v, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #
プレゼンテーションモードにする

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996)
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;
vector<vector<pair<int,ll> > > g;
ll ans;
void dfs(int par, int curr, ll val[2]) // val[0]:num val[1]:sum of dist
{
int siz=(int)g[curr].size();
vector<ll> val0(2*siz);
int i;
for(i=0; i<siz; i++) {
int next=g[curr][i].first;
ll dist=g[curr][i].second;
if(next==par) continue;
ll val0tmp[2]={0};
dfs(curr, next, val0tmp);
val0[2*i]=val0tmp[0];
val0[2*i+1]=(val0tmp[1]+val0tmp[0]*dist);
val[0]+=val0[2*i];
val[1]+=val0[2*i+1];
}
for(i=0; i<siz; i++) {
int next=g[curr][i].first;
if(next==par) continue;
ans += (val0[2*i+1])*(val[0]-val0[2*i]+1);
}
val[0]++;
return;
}
int main(int argc, char* argv[])
{
int n;
scanf("%d", &n);
g.resize(n);
int i;
for(i=0; i<n-1; i++) {
int u,v,w;
scanf("%d%d%d", &u, &v, &w);
g[u-1].push_back(make_pair(v-1,w));
g[v-1].push_back(make_pair(u-1,w));
}
ans=0;
ll val[2]={0};
dfs(-1, 0, val);
printf("%lld\n", ans*2);
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0