結果

問題 No.2892 Lime and Karin
ユーザー 沙耶花沙耶花
提出日時 2024-09-13 22:42:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,296 bytes
コンパイル時間 4,535 ms
コンパイル使用メモリ 266,180 KB
実行使用メモリ 20,316 KB
最終ジャッジ日時 2024-09-13 22:42:37
合計ジャッジ時間 14,956 ms
ジャッジサーバーID
(参考情報)
judge6 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 2 ms
5,376 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 2 ms
6,944 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 AC 68 ms
16,128 KB
testcase_35 AC 68 ms
16,132 KB
testcase_36 AC 71 ms
16,124 KB
testcase_37 AC 70 ms
16,128 KB
testcase_38 AC 70 ms
16,380 KB
testcase_39 AC 347 ms
16,080 KB
testcase_40 RE -
testcase_41 AC 341 ms
16,664 KB
testcase_42 AC 336 ms
20,316 KB
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001LL
long long ans = 0;
int sz[200000];string S;
void dfs(vector<vector<int>> &E,int cv,int pv){
	sz[cv] = 1;
	rep(i,E[cv].size()){
		int to = E[cv][i];
		if(to==pv)continue;
		dfs(E,to,cv);
		sz[cv] += sz[to];
	}
}
int find_centroid(vector<vector<int>> &E,int cv,int pv,int N){
	int ret = -1;
	int ma = 0;
	int sum = 0;
	rep(i,E[cv].size()){
		int to = E[cv][i];
		if(to==pv)continue;
		ret = find_centroid(E,to,cv,N);
		ma = max(ma,sz[to]);
		sum += sz[to];
	}
	ma = max(ma,N-sum-1);
	if(ma <= N/2)return cv;
	return ret;
	
}
int get(char c){
	if(c=='0')return -1;
	return 1;
}
void get(vector<vector<int>> &E,int cv,int pv,vector<int> &ret,int sum){
	sum += get(S[cv]);
	ret.push_back(sum);
	rep(i,E[cv].size()){
		int to = E[cv][i];
		if(to==pv)continue;
		get(E,to,cv,ret,sum);
	}
}
long long Get(vector<int> a,int x){
	if(a.size()==0)return 0;
	sort(a.begin(),a.end());
	int cp = a.size()-1;
	long long ret = 0;
	rep(i,a.size()){
		while(cp!=-1 && a[i]+x+a[cp]>0)cp--;
		ret += a.size()-1 - cp;
		if(a[i]+x+a[i]>0)ret++;
		//cout<<ret<<endl;
	}
	
	return ret;
}

void solve(vector<vector<int>> &E,int cv){
	dfs(E,cv,-1);
	int c = find_centroid(E,cv,-1,sz[cv]);
	//cout<<cv<<' '<<c<<endl;
	assert(c!=-1);
	rep(i,E[c].size()){
		int to = E[c][i];
		rep(j,E[to].size()){
			if(E[to][j]==c){
				E[to].erase(E[to].begin()+j);
				break;
			}
		}
	}
	vector<vector<int>> lists(E[c].size()+1);
	rep(i,E[c].size()){
		get(E,E[c][i],-1,lists[i],0);
	}
	lists.back() = {0};
	
	long long ts = 0;
	rep(i,lists.size()){
		ts -= Get(lists[i],get(S[c]));
	}
	vector<int> t;
	rep(i,lists.size()){
		rep(j,lists[i].size()){
			t.push_back(lists[i][j]);
		}
	}
	ts += Get(t,get(S[c]));
	//cout<<"t"<<ts<<endl;
	ts /= 2;
	ans += ts;
	if(get(S[c])>0)ans++;
	//cout<<'a'<<ans<<endl;
	rep(i,E[c].size()){
		int to = E[c][i];
		solve(E,to);
	}
}

int main(){
	
	int n;
	cin>>n;
	vector<vector<int>> E(n);
	rep(i,n-1){
		int u,v;
		scanf("%d %d",&u,&v);
		u--;v--;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	cin>>S;
	solve(E,0);
	cout<<ans<<endl;
	
	return 0;
	
}
0