結果
| 問題 |
No.386 貪欲な領主
|
| コンテスト | |
| ユーザー |
kotamanegi
|
| 提出日時 | 2016-07-01 23:58:21 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,895 bytes |
| コンパイル時間 | 984 ms |
| コンパイル使用メモリ | 91,720 KB |
| 実行使用メモリ | 25,472 KB |
| 最終ジャッジ日時 | 2024-10-12 19:15:12 |
| 合計ジャッジ時間 | 4,715 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 TLE * 1 -- * 11 |
ソースコード
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include<map>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <iomanip>
using namespace std;
#define MAX_MOD 1000000007
#define REP(i,n) for(int i = 0;i < n;++i)
vector<int> sectors_first[200001];
vector<int> sectors_cal[200001];
int kazu[200001] = {};
long long score[200001] = {};
int main() {
int n;
cin >> n;
for (int i = 0;i < n-1;++i) {
int a, b;
cin >> a >> b;
sectors_first[a].push_back(b);
sectors_first[b].push_back(a);
}
for (int i = 0;i < n;++i) {
cin >> kazu[i];
}
score[150000] = 1;
score[0] = kazu[0]+1;//1+
queue<int> next_go;
next_go.push(0);
while (next_go.empty() == false) {
int hoge = next_go.front();
next_go.pop();
for (int q = 0;q < sectors_first[hoge].size();++q) {
if (score[sectors_first[hoge][q]] == 0) {
sectors_cal[sectors_first[hoge][q]].push_back(hoge);
score[sectors_first[hoge][q]] = kazu[sectors_first[hoge][q]] + score[hoge];
next_go.push(sectors_first[hoge][q]);
}
}
}
sectors_cal[0].push_back(150000);
long long ans = 0;
int m;
cin >> m;
for (int i = 0;i < m;++i) {
int a, b, c;
cin >> a >> b >> c;
long long pre_ans = score[a] + score[b] - 2;
bool ok[200000] = {};
queue<int> hogee;
hogee.push(a);
hogee.push(b);
while (hogee.empty() == false) {
int wow = hogee.front();hogee.pop();
if (ok[wow] == false) {
ok[wow] = true;
if (wow != 0) {
hogee.push(sectors_cal[wow][0]);
}
}else{
pre_ans -= score[wow] - 1+score[sectors_cal[wow][0]]-1;
goto ok;
}
}
ok:;
ans += pre_ans * c;
}
cout << ans << endl;
}
kotamanegi