This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
using namespace std;
const int mxn=2e5+5;
vector<int>g[mxn],f;
int sz[mxn]{0},lv[mxn]{0},pr[mxn];
bool vis[mxn]{0};
ll ans=0;
int getsz(int u,int p){
sz[u]=1;
for(auto v:g[u])if(v!=p&&!vis[v])sz[u]+=getsz(v,u);
return sz[u];
}
int getct(int u,int p,int r){
for(auto v:g[u])if(v!=p&&!vis[v]&&sz[v]*2>r)return getct(v,u,r);
return u;
}
vector<ll>dp[6][mxn];
ll td[6][mxn]{0};
struct few{
int fw[mxn]{0};
void add(int i,int amt){
for(;i<mxn;i+=i&-i)fw[i]+=amt;
}
ll qr(int i,ll res=0){
for(;i;i-=i&-i)res+=fw[i];
return res;
}
}fw[3][19],wf[3][19];
int tin[19][mxn],tout[19][mxn],cnt[19]{0};
ll getP(int col,int lvl,int u){
return fw[col][lvl].qr(tin[lvl][u]);
}
ll getC(int col,int lvl,int u){
return wf[col][lvl].qr(tout[lvl][u])-wf[col][lvl].qr(tin[lvl][u]-1);
}
void gen1(int u,int p,int x,int id,int x0,int x1,int x2){
dp[f[u]][x][id]++;tin[lv[x]][u]=++cnt[lv[x]];
fw[0][lv[x]].add(tin[lv[x]][u],x0);
fw[1][lv[x]].add(tin[lv[x]][u],x1);
fw[2][lv[x]].add(tin[lv[x]][u],x2);
if(f[u]==2)dp[3][x][id]+=x1;
for(auto v:g[u])if(v!=p&&!vis[v])gen1(v,u,x,id,x0+(f[v]==0),x1+(f[v]==1),x2+(f[v]==2));
tout[lv[x]][u]=++cnt[lv[x]];
}
void gen2(int u,int p,int x,int id){
for(auto v:g[u])if(v!=p&&!vis[v])gen2(v,u,x,id);
wf[f[u]][lv[x]].add(tin[lv[x]][u],1);
if(f[u]==1)dp[4][x][id]+=getC(0,lv[x],u);
}
void play(int u,int p){int id=-1;
u = getct(u,u,getsz(u,u));vis[u]=1;
pr[u]=p;if(lv[p]!=-1)lv[u]=lv[p]+1;
tin[lv[u]][u]=++cnt[lv[u]];
for(auto v:g[u]){
if(vis[v])continue;
for(int i=0;i<6;i++)dp[i][u].pb(0);id++;
gen1(v,u,u,id,(f[v]==0),(f[v]==1),(f[v]==2));gen2(v,u,u,id);
for(int i=0;i<6;i++)td[i][u]+=dp[i][u][id];
}tout[lv[u]][u]=cnt[lv[u]];
for(int j=0;j<=id;j++){
ans+=dp[0][u][j]*(td[3][u]-dp[3][u][j]);
ans+=dp[4][u][j]*(td[2][u]-dp[2][u][j]);
if(f[u]==1)ans+=dp[0][u][j]*(td[2][u]-dp[2][u][j]);
}if(f[u]==0)ans+=td[3][u];if(f[u]==2)ans+=td[4][u];
for(auto v:g[u])if(!vis[v])play(v,u);
}
void init(int N, std::vector<int> F, std::vector<int> U, std::vector<int> V,int Q){
f=F;for(int i=0;i<N-1;i++)g[U[i]].pb(V[i]),g[V[i]].pb(U[i]);
play(0,-1);
}
void change(int X, int Y) {}
long long num_tours() {
return ans;
}
Compilation message (stderr)
joitour.cpp: In function 'void play(int, int)':
joitour.cpp:69:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
69 | for(int i=0;i<6;i++)dp[i][u].pb(0);id++;
| ^~~
joitour.cpp:69:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
69 | for(int i=0;i<6;i++)dp[i][u].pb(0);id++;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |