結果

問題 No.1333 Squared Sum
ユーザー 沙耶花沙耶花
提出日時 2021-03-04 21:55:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,372 ms / 2,000 ms
コード長 4,651 bytes
コンパイル時間 3,598 ms
コンパイル使用メモリ 239,976 KB
実行使用メモリ 104,520 KB
最終ジャッジ日時 2024-10-05 10:53:35
合計ジャッジ時間 30,971 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 1,029 ms
82,456 KB
testcase_04 AC 1,035 ms
82,780 KB
testcase_05 AC 1,040 ms
82,620 KB
testcase_06 AC 1,047 ms
82,336 KB
testcase_07 AC 1,016 ms
82,224 KB
testcase_08 AC 1,015 ms
82,504 KB
testcase_09 AC 1,018 ms
82,136 KB
testcase_10 AC 1,034 ms
82,276 KB
testcase_11 AC 990 ms
82,512 KB
testcase_12 AC 1,003 ms
81,988 KB
testcase_13 AC 641 ms
104,520 KB
testcase_14 AC 1,341 ms
94,640 KB
testcase_15 AC 1,372 ms
102,108 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 1,339 ms
104,020 KB
testcase_27 AC 1,311 ms
94,892 KB
testcase_28 AC 1,367 ms
103,924 KB
testcase_29 AC 647 ms
104,520 KB
testcase_30 AC 322 ms
35,896 KB
testcase_31 AC 152 ms
21,376 KB
testcase_32 AC 535 ms
50,820 KB
testcase_33 AC 385 ms
40,704 KB
testcase_34 AC 776 ms
66,704 KB
testcase_35 AC 536 ms
50,948 KB
testcase_36 AC 263 ms
31,452 KB
testcase_37 AC 274 ms
32,520 KB
testcase_38 AC 341 ms
37,412 KB
testcase_39 AC 628 ms
57,428 KB
testcase_40 AC 496 ms
68,976 KB
testcase_41 AC 483 ms
68,976 KB
testcase_42 AC 493 ms
69,072 KB
testcase_43 AC 502 ms
68,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000000

struct centroid_tree{
	
	vector<vector<int>> E;
	vector<int> cnt;
	int r;

	vector<vector<int>> pos;
	vector<vector<int>> bind;
	vector<int> ind;
	
	centroid_tree(vector<vector<int>> argE){
		int n = argE.size();
		E.resize(n,vector<int>());

		cnt.resize(n);
		r = go(0,argE);
		
		pos.resize(n);
		bind.resize(n);
		dfs(r,-1,E);
		build(r,0);
		
	}
	
	int go(int a,vector<vector<int>> &argE){
		
		dfs(a,-1,argE);
		
		int sz = cnt[a];
		
		int ret = dfs2(a,-1,argE,sz);

		rep(i,argE[ret].size()){
			int to = argE[ret][i];
			argE[to].erase(find(argE[to].begin(),argE[to].end(),ret));
			
			int g = go(to,argE);
			E[ret].push_back(g);
		}
		argE[ret].clear();

		return ret;
		
	}
	
	void dfs(int cur,int p,vector<vector<int>> &argE){
		cnt[cur] = 1;
		rep(i,argE[cur].size()){
			int to = argE[cur][i];
			if(to==p)continue;
			dfs(to,cur,argE);
			cnt[cur] += cnt[to];
		}
	}
	
	int dfs2(int cur,int p,vector<vector<int>> &argE,int sz){
		int ret = -1;
		int m = 0;
		rep(i,argE[cur].size()){
			int to = argE[cur][i];
			if(to==p)continue;
			ret = dfs2(to,cur,argE,sz);
			if(ret!=-1)return ret;
			m = max(m,cnt[to]);
		}
		
		if(sz-cnt[cur]<=sz/2 && m<=sz/2){
			ret = cur;
		}
		return ret;
		
	}
	
	void build(int cur,int d){
		int x = cur;
		if(pos[x].size()<=d)pos[x].resize(d+1,-1);
		pos[x][d] = ind.size();
		bind[cur].push_back(ind.size());
		ind.push_back(cur);
		
		rep(i,E[cur].size()){
			int to = E[cur][i];
			int x = to;
			if(pos[x].size()<=d)pos[x].resize(d+1,-1);
			pos[x][d] = ind.size();
			bind[cur].push_back(ind.size());
			build(to,d+1);
		}
		bind[cur].push_back(ind.size());
	}
	
};

struct HLD{
	vector<int> sz,parent,depth,root,pos;
	vector<int> arr;
	HLD(vector<vector<int>> &E){
		sz.resize(E.size(),1);
		parent.resize(E.size(),0);
		depth.resize(E.size(),0);
		root.resize(E.size(),0);
		pos.resize(E.size(),0);
		
		dfs(0,-1,E);
		dfs2(0,-1,E,0);
	}
	
	void dfs(int now,int p,vector<vector<int>> &E){
		parent[now] = p;
		if(p==-1){
			depth[now] = 0;
		}
		else{
			depth[now] = depth[p]+1;
		}
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			dfs(to,now,E);
			sz[now] += sz[to];
		}
	}
	
	void dfs2(int now,int p,vector<vector<int>> &E,int r){
		pos[now] = arr.size();
		arr.push_back(now);
		root[now] = r;
		int maxi = 0;
		int ind = -1;
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			if(maxi<sz[to]){
				maxi = sz[to];
				ind = to;
			}
		}
		if(ind==-1)return;
		dfs2(ind,now,E,r);
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p||to==ind)continue;
			dfs2(to,now,E,to);
		}
	}
	
	vector<pair<int,int>> query(int u,int v){
		vector<pair<int,int>> ret;
		int t = 0;
		while(root[u]!=root[v]){
			if(depth[root[u]] <= depth[root[v]]){
				ret.insert(ret.begin()+t,{pos[root[v]], pos[v]});
				v = parent[root[v]];
			}
			else{
				ret.insert(ret.begin()+t,{pos[u],pos[root[u]]});
				u = parent[root[u]];
				t++;
			}
		}
		ret.insert(ret.begin()+t,{pos[u],pos[v]});
		return ret;
	}
	
	int lca(int u,int v){
		for(;;v=parent[root[v]]){
			if(pos[u]>pos[v])swap(u,v);
			if(root[u]==root[v])return u;
		}
	}
	
	int get_distance(int u,int v){
		return depth[u] + depth[v] - 2 * depth[lca(u,v)];
	}
	
};

mint get_dis(HLD &H,vector<mint> &S,int u,int v){
	int l = H.lca(u,v);
	mint ret = S[u] + S[v] - S[l]*2;
	return ret;
}

int main(){
	
	int n;
	cin>>n;
	
	vector<vector<int>> E(n);
	vector<vector<pair<int,mint>>> E2(n);
	rep(i,n-1){
		int u,v,w;
		scanf("%d %d %d",&u,&v,&w);
		u--;v--;
		E[u].push_back(v);
		E[v].push_back(u);
		E2[u].emplace_back(v,w);
		E2[v].emplace_back(u,w);
	}
	
	centroid_tree C(E);

	HLD H(E);

	vector<mint> dis(n,0);
	vector<bool> visited(n,false);
	queue<int> Q;
	Q.push(0);
	visited[0] = true;
	while(Q.size()>0){
		int u = Q.front();
		Q.pop();
		rep(i,E2[u].size()){
			int v = E2[u][i].first;
			mint w = E2[u][i].second;
			if(visited[v])continue;
			visited[v] = true;
			dis[v] = dis[u] + w;
			Q.push(v);
		}
	}
	
	mint ans = 0;
	
	rep(i,n){
		mint sum = 0;
		mint temp = 0;
		int cur = 0;
		for(int j=C.bind[i][0];j<C.bind[i].back();j++){
			mint d = get_dis(H,dis,i,C.ind[j]);
			ans += d*d * ((C.bind[i].back() - C.bind[i][0]) - (C.bind[i][cur+1] - C.bind[i][cur]));
			ans += d*2*sum;
			temp += d;
			if(j+1 == C.bind[i][cur+1]){
				cur++;
				sum += temp;
				temp = 0;
			}
		}
	}
	
	cout<<ans.val()<<endl;
	
    return 0;
}
0