QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592412 | #7086. Inner Product | Afterlife | ML | 0ms | 13744kb | C++14 | 4.9kb | 2024-09-26 22:21:32 | 2024-09-26 22:21:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=100010;
ll W[N];
int n;
ll ans;
struct Tree2{
vector<pair<int,int> > G[N];
ll sw[N];
int dfn[N],top[N],siz[N],son[N],dep[N],f[N],num;
void dfs1(int u,int fa){
siz[u]=1;
son[u]=0;
dep[u]=dep[fa]+1;
for(auto [v,w]:G[u]){
if(v==fa)continue;
sw[v]=(sw[u]+w)%mod;
f[v]=u;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]){
son[u]=v;
}
}
}
void dfs2(int u,int topf){
dfn[u]=++num;
top[u]=topf;
if(!son[u])return;
dfs2(son[u],topf);
for(auto [v,w]:G[u]){
if(v==f[u]||v==son[u])continue;
dfs2(v,v);
}
}
int LCA(int u,int v){
while(top[u]^top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
u=f[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
return u;
}
ll totw;
int totn;
bool is[N];
vector<pair<int,ll> > H[N];
ll sum;
int gn[N];
ll gw[N];
void dfs(int u){
gn[u]=is[u];
if(is[u])gw[u]=W[u];
for(auto [v,w]:H[u]){
dfs(v);
gn[u]+=gn[v];
gw[u]+=gw[v];
sum=(sum+(gw[v]*(totn-gn[v])+gn[v]*(totw-gw[v]))%mod*w)%mod;
}
}
ll calc(vector<int> a){
totw=totn=0;
for(auto x:a){
++totn;
totw=(totw+W[x])%mod;
is[x]=1;
}
sort(a.begin(),a.end(),[&](int x,int y){return dfn[x]<dfn[y];});
vector<int> bin;
static int st[N],top;
st[top=1]=1;
bin.push_back(1);
for(auto u:a){
if(u==1)continue;
bin.push_back(u);
int lca=LCA(st[top],u);
while(dfn[st[top-1]]>=dfn[lca]){
H[st[top-1]].emplace_back(st[top],sw[st[top]]-sw[st[top-1]]);
--top;
}
if(st[top]^lca){
bin.push_back(lca);
H[lca].emplace_back(st[top],sw[st[top]]-sw[lca]);
st[top]=lca;
}
st[++top]=u;
}
while(top>1){
H[st[top-1]].emplace_back(st[top],sw[st[top]]-sw[st[top-1]]);
--top;
}
sum=0;
dfs(1);
for(auto x:bin){
is[x]=0;
H[x].clear();
gn[x]=gw[x]=0;
}
return sum;
}
void clear(int n){
num=0;
for(int i=1;i<=n;++i){
G[i].clear();
}
}
void adde(int u,int v,int w){
G[u].emplace_back(v,w);
G[v].emplace_back(u,w);
}
void init(){
for(int i=1;i<n;++i){
int u,v,w;
cin>>u>>v>>w;
adde(u,v,w);
}
dfs1(1,0);
dfs2(1,1);
}
}T2;
struct Tree1{
vector<pair<int,int> > G[N];
bool vis[N];
void clear(int n){
for(int i=1;i<=n;++i){
G[i].clear();
vis[i]=0;
}
}
void adde(int u,int v,int w){
G[u].emplace_back(v,w);
G[v].emplace_back(u,w);
}
void init(){
for(int i=1;i<n;++i){
int u,v,w;
cin>>u>>v>>w;
adde(u,v,w);
}
}
int siz[N],maxp[N];
int rt;
void get_root(int u,int fa,int total){
siz[u]=1;
maxp[u]=0;
for(auto [v,w]:G[u]){
if(v==fa)continue;
get_root(v,u,total);
siz[u]+=siz[v];
maxp[u]=max(maxp[u],siz[v]);
}
maxp[u]=max(maxp[u],total-siz[u]);
if(!rt||maxp[u]<maxp[rt]){
rt=u;
}
}
vector<int> P,Q;
void dfs1(int u,int fa,ll totw){
W[u]=totw;
P.push_back(u),Q.push_back(u);
for(auto [v,w]:G[u]){
if(v==fa||vis[v])continue;
dfs1(v,u,(totw+w)%mod);
}
}
void Div(int u){
vis[u]=1;
W[u]=0;
Q.clear();
Q.push_back(u);
for(auto [v,w]:G[u]){
if(vis[v])continue;
P.clear();
dfs1(v,u,w);
ans=(ans-T2.calc(P))%mod;
}
ans=(ans+T2.calc(Q))%mod;
for(auto [v,w]:G[u]){
if(vis[v])continue;
rt=0;
get_root(v,u,siz[v]);
Div(rt);
}
}
void Solve(){
rt=0;
get_root(1,0,n);
Div(rt);
}
}T1;
void Solve(){
cin>>n;
ans=0;
T1.clear(n);
T2.clear(n);
T1.init();
T2.init();
T1.Solve();
ans=ans*2%mod;
cout<<(ans+mod)%mod<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13744kb
input:
1 2 1 2 3 1 2 4
output:
24
result:
ok "24"
Test #2:
score: -100
Memory Limit Exceeded
input:
18265 4 1 4 770451592 2 4 277067438 3 2 307076074 3 4 164467604 1 4 588797903 2 1 537346425 1 7 7 4 382835486 2 7 473042763 5 2 39929242 6 2 585229407 3 5 807830350 1 2 898554961 3 7 541921359 2 7 796516696 6 3 239857604 4 3 145052803 5 2 418549973 1 2 781381628 9 1 7 158088424 3 7 61764661 5 1 6866...