結果

問題 No.386 貪欲な領主
ユーザー shimomireshimomire
提出日時 2016-07-01 23:56:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 6,410 bytes
コンパイル時間 1,302 ms
コンパイル使用メモリ 123,000 KB
実行使用メモリ 25,684 KB
最終ジャッジ日時 2024-10-12 19:13:50
合計ジャッジ時間 2,602 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 183 ms
25,684 KB
testcase_05 AC 135 ms
23,208 KB
testcase_06 AC 140 ms
22,984 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 17 ms
5,248 KB
testcase_09 AC 4 ms
5,248 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 4 ms
5,248 KB
testcase_14 AC 142 ms
23,004 KB
testcase_15 AC 151 ms
25,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
using namespace std;

typedef long long ll;typedef unsigned long long ull;typedef long double ld;

#define ALL(c) c.begin(),c.end()
#define IN(l,v,r) (l<=v && v < r)
template<class T> void UNIQUE(T& v){v.erase(unique(ALL(v)),v.end());}
//debug
#define DUMP(x) cerr << #x <<" = " << (x)
#define LINE() cerr<< " (L" << __LINE__ << ")"

struct range{
	struct Iter{
		int v,step;
		Iter& operator++(){v+=step;return *this;}
		bool operator!=(Iter& itr){return v<itr.v;}
		int& operator*(){return v;}
	};
	Iter i, n;
	range(int i, int n,int step):i({i,step}), n({n,step}){}
	range(int i, int n):range(i,n,1){}
	range(int n):range(0,n){}
	Iter& begin(){return i;}
	Iter& end(){return n;}
};
struct rrange{
	struct Iter{
		int v,step;
		Iter& operator++(){v-=step;return *this;}
		bool operator!=(Iter& itr){return v>itr.v;}
		int& operator*(){return v;}
	};
	Iter i, n;
	rrange(int i, int n,int step):i({i-1,step}), n({n-1,step}){}
	rrange(int i, int n):rrange(i,n,1){}
	rrange(int n) :rrange(0,n){}
	Iter& begin(){return n;}
	Iter& end(){return i;}
};

//input
template<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}
template<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}
template<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}
template<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}
template<typename T> istream& operator >> (istream& is,vector<T>& as){for(int i:range(as.size()))is >>as[i];return is;}
//output
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<" "<<p.second;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}
template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<" "<<get<1>(t);}
template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);}
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){for(int i:range(as.size())){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){for(int i:range(as.size())){if(i!=0)os<<endl; os<<as[i];}return os;}

// values
template<typename T> inline T INF(){assert(false);};
template<> inline int INF<int>(){return 1<<28;};
template<> inline ll INF<ll>(){return 1LL<<58;};
template<> inline double INF<double>(){return 1e16;};
template<> inline long double INF<long double>(){return 1e16;};

template<class T> inline T EPS(){assert(false);};
template<> inline int EPS<int>(){return 1;};
template<> inline ll EPS<ll>(){return 1LL;};
template<> inline double EPS<double>(){return 1e-8;};
template<> inline long double EPS<long double>(){return 1e-8;};

// min{2^r | n < 2^r}
template<typename T> T upper_pow2(T n){ T res=1;while(res<n)res<<=1;return res;}
// max{d | 2^d  <= n}
template<typename T> T msb(T n){ int d=62;while((1LL<<d)>n)d--;return d;}

template<typename T,typename U> T pmod(T v,U M){return (v%M+M)%M;}

class Main{
	public:

	typedef vector<vector<int>> Graph;
	//O(Q * logN)
	class DoublingLCA{
		Graph& g;int r;
		vector<vector<int> > parent;
		vector<vector<int> > ds;
		vector<int> depth;
	public:
		DoublingLCA(Graph& g,int r,vector<int> us):g(g),r(r),parent(msb(g.size())+1,vector<int>(g.size())),ds(msb(g.size())+1,vector<int>(g.size())),depth(vector<int>(g.size())){
			dfs(-1,r,0);
			ds[0] = us;
			for(int l:range(1,parent.size()))for(int s:range(parent[l].size())){
				parent[l][s] = (parent[l-1][s]==-1)?-1:parent[l-1][parent[l-1][s]];
				ds[l][s] = (parent[l-1][s]==-1)?ds[l-1][s]:ds[l-1][parent[l-1][s]] + ds[l-1][s];
			}
		}
		void dfs(int p,int s,int d){
			parent[0][s]=p;depth[s]=d;
			for(int t:g[s])if(t!=p)dfs(s,t,d+1);
		}
		int lca(int u,int v){
			if(depth[u]>depth[v])swap(u,v);
			for(int l:range(parent.size()))if((depth[v]-depth[u])&(1<<l)) v = parent[l][v];
			assert(depth[u] == depth[v]);
			if(u==v)return u;
			for(int l=parent.size()-1;l>=0;l--)if(parent[l][u]!=parent[l][v]){
				u=parent[l][u];
				v=parent[l][v];
			}
			return parent[0][u];
		}

		ll getD(int u,int v){
			ll res = 0;
			if(depth[u]>depth[v])swap(u,v);
			for(int l:range(parent.size()))if((depth[v]-depth[u])&(1<<l)){
				res += ds[l][v];
				v = parent[l][v];
			}

			assert(depth[u] == depth[v]);
			if(u==v){
				res += ds[0][u];
				return res;
			}
			for(int l=parent.size()-1;l>=0;l--)if(parent[l][u]!=parent[l][v]){
				res += ds[l][u];
				u=parent[l][u];
				res += ds[l][v];
				v=parent[l][v];
			}
			res += ds[0][u];
			res += ds[0][v];
			res += ds[0][parent[0][u]];
			return res;
		}
	};

	void run(){
		int N;cin >> N;
		vector<vector<int>> es(N);
		for(int i:range(N-1)){
			int u,v;cin >> u >> v;
			es[u].push_back(v);
			es[v].push_back(u);
		}
		vector<int> us(N);cin >> us;
		DoublingLCA lca(es, 0, us);
		int M;cin >> M;
		ll res = 0;
		for(int q:range(M)){
			int a,b; ll c;cin >> a >> b >> c;
			res += lca.getD(a,b)*c;
		}
		cout << res << endl;
    }
};

int main(){
	cout <<fixed<<setprecision(20);
	cin.tie(0);
	ios::sync_with_stdio(false);
	Main().run();
	return 0;
}
0