結果
| 問題 |
No.2892 Lime and Karin
|
| コンテスト | |
| ユーザー |
shkiiii_
|
| 提出日時 | 2024-09-15 16:20:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 389 ms / 8,000 ms |
| コード長 | 4,642 bytes |
| コンパイル時間 | 2,299 ms |
| コンパイル使用メモリ | 213,920 KB |
| 最終ジャッジ日時 | 2025-02-24 08:44:04 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 52 |
ソースコード
#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
// verifiy
// 重心分解 https://atcoder.jp/contests/abc291/submissions/54751516
// 分割統治 https://atcoder.jp/contests/ddcc2016-qual/submissions/54751335
struct edge{
int to;
// 情報を追加
// ll we;
// edge(int a,ll b):to(a),we(b){}
edge(int k):to(k){}
};
using vve = vector<vector<edge>>;
struct CD{
int n;
vector<vector<edge>> v;
vector<bool> used_centroid;
vector<int> sub_sz;//内部用。重心分解の各段階で計算する部分木の大きさ
vector<int> par;//重心分解後の親の頂点
CD(vector<vector<edge>> &a) :
n((int)a.size()),v(a),used_centroid(a.size(),false),sub_sz(a.size(),1),par(a.size(),-1){}
int subtree_sz(int ov,int pre){
int res = 1;
for(int i = 0;i < (int)v[ov].size();i++){
int to = v[ov][i].to;
if(to == pre || used_centroid[to])continue;
res += subtree_sz(to,ov);
}
sub_sz[ov] = res;
return res;
}
int get_centroid(int ov,int now_sz){
while(1){
int mx = -1,can_v = -1;
for(auto nv : v[ov]){
// 重心でない / 逆走しない 条件
if(used_centroid[nv.to] || sub_sz[nv.to] > sub_sz[ov])continue;
mx = max(mx,sub_sz[nv.to]);
if(mx == sub_sz[nv.to])can_v = nv.to;
}
if(mx <= now_sz/2)break;
ov = can_v;
}
return ov;
}
// ovが所属する部分木に対する重心分解
// 使うときはpre = -1を入れる
// O(nlogn)
void cd(int ov,int pre,string &s,ll &res){//分割統治に必要なら引数を増やす
subtree_sz(ov,-1);
ov = get_centroid(ov,sub_sz[ov]);
par[ov] = pre;
used_centroid[ov] = true;
vi ds;ds.emplace_back(0);
// /*
// 重心ovに対して➀u->ov->v ➁ov->u となる頂点対に対する部分問題を書く。必要ならば下のdfsを用いる
auto count = [](vi &d,string &s,int ov){
sort(ALL(d));
ll res = 0;
rep(i,sz(d)){
int k = LB(d,-((s[ov] == '1' ? 1 : -1) + d[i])+1);
res += sz(d)-k;
}
return res;
};
rep(i,sz(v[ov])){
int to = v[ov][i].to;
if(used_centroid[to])continue;
vi dt;
auto dfs = [&](auto &dfs,int OV,int PRE,ll cnt)->void{
dt.emplace_back(cnt + (s[OV] == '1' ? 1 : -1));
for(auto nv : v[OV]){
if(nv.to == PRE || used_centroid[nv.to])continue;
dfs(dfs,nv.to,OV,cnt + (s[OV] == '1' ? 1 : -1));
}
};
dfs(dfs,v[ov][i].to,ov,0);
ll we = count(dt,s,ov);
res -= we;
ds.insert(ds.end(),ALL(dt));
}
ll we = count(ds,s,ov);
res += we;
if(s[ov] == '1')res++;
for(int i = 0;i < (int)v[ov].size();i++){
if(used_centroid[v[ov][i].to])continue;
cd(v[ov][i].to,ov,s,res);
}
}
};
int main(){
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vve v(n);
rep(i,n-1){
int a,b;cin >> a >> b;
a--;b--;
v[a].emplace_back((edge)(b));
v[b].emplace_back((edge)(a));
}
CD cd(v);
string s;cin >> s;
ll res = 0;
cd.cd(0,-1,s,res);
res /= 2;
cout << res << "\n";
return 0;
}
shkiiii_