結果

問題 No.386 貪欲な領主
ユーザー h_nosonh_noson
提出日時 2016-07-02 01:11:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,721 bytes
コンパイル時間 732 ms
コンパイル使用メモリ 68,188 KB
実行使用メモリ 31,744 KB
最終ジャッジ日時 2024-04-20 21:29:07
合計ジャッジ時間 2,748 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,272 KB
testcase_01 AC 3 ms
6,272 KB
testcase_02 AC 4 ms
6,272 KB
testcase_03 AC 3 ms
6,144 KB
testcase_04 AC 197 ms
31,744 KB
testcase_05 AC 144 ms
25,856 KB
testcase_06 AC 150 ms
25,600 KB
testcase_07 AC 5 ms
6,144 KB
testcase_08 AC 22 ms
8,192 KB
testcase_09 AC 6 ms
6,400 KB
testcase_10 AC 3 ms
6,144 KB
testcase_11 AC 3 ms
6,144 KB
testcase_12 AC 4 ms
6,144 KB
testcase_13 AC 7 ms
6,656 KB
testcase_14 AC 158 ms
25,600 KB
testcase_15 AC 180 ms
31,744 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:55:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   55 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
main.cpp:58:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |         scanf("%d%d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~
main.cpp:62:20: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   62 |     rep (i,n) scanf("%d",&u[i]);
      |               ~~~~~^~~~~~~~~~~~
main.cpp:70:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |     scanf("%d",&m);
      |     ~~~~~^~~~~~~~~
main.cpp:74:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   74 |         scanf("%d%d%d",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

#define REP(i,s,e) for (i = s; i <= e; i++)
#define rep(i,n) REP (i,0,(int)(n)-1)
#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP (i,(int)(n)-1,0)
#define INF (int)1e8
#define MOD (int)(1e9+7)
#define MAX 100000

typedef long long ll;

vector<int> e[MAX];
int u[MAX];

int parent[20][MAX];
int dist[20][MAX];
int depth[MAX];

void dfs(int v, int p, int d) {
    parent[0][v] = p;
    depth[v] = d;
    dist[0][v] = u[v];
    for (auto& x : e[v]) if (parent[0][v] != x) {
        dfs(x,v,d+1);
    }
}

int query(int a, int b) {
    int i, ret = 0;
    if (depth[a] > depth[b]) swap(a,b);
    rep (i,20) {
        if ((depth[b] - depth[a]) >> i & 1) {
            ret += dist[i][b];
            b = parent[i][b];
        }
    }
    if (a == b) return ret + u[a];
    rrep (i,20) {
        if (parent[i][a] != parent[i][b]) {
            ret += dist[i][a] + dist[i][b];
            a = parent[i][a];
            b = parent[i][b];
        }
    }
    return ret + u[a] + u[b] + u[parent[0][a]];
}

int main(void) {
    int i, j, n, m;
    scanf("%d",&n);
    rep (i,n-1) {
        int a, b;
        scanf("%d%d",&a,&b);
        e[a].push_back(b);
        e[b].push_back(a);
    }
    rep (i,n) scanf("%d",&u[i]);

    dfs(0,-1,0);
    for (i = 1; i < 20; i++) rep (j,n) {
        parent[i][j] = parent[i][j] >= 0 ? parent[i-1][parent[i-1][j]] : -1;
        dist[i][j] = dist[i-1][parent[i-1][j]] + dist[i-1][j];
    }

    scanf("%d",&m);
    ll ans = 0;
    while (m--) {
        int a, b, c;
        scanf("%d%d%d",&a,&b,&c);
        ans += query(a,b) * c;
    }
    printf("%lld\n",ans);
    return 0;
}
0