QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#180383 | #7086. Inner Product | brz# | WA | 54ms | 14012kb | C++20 | 6.5kb | 2023-09-15 19:11:12 | 2023-09-15 19:11:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++)
#define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--)
#define debug(x) cerr<<#x<<"="<<x<<endl;
#define all(x) (x).begin(),(x).end()
#define cle(x) memset(x,0,sizeof(x))
#define ll long long
#define ull unsigned ll
#define db double
#define lb long db
#define pb push_back
#define mp make_pair
#define fi first
#define se second
inline ll read()
{
ll x=0; char ch=getchar(); bool f=0;
for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
return f?-x:x;
}
const ll mod = 1e9 + 7;
inline ll Pow(ll x,ll y) {
ll ans = 1;
for(; y; y >>= 1, x = x * x % mod)
if(y & 1)
ans = ans * x % mod;
return ans;
}
inline ll Add(ll x,ll y) {
x += y;
return (x >= mod) ? x - mod : x;
}
inline ll Dec(ll x,ll y) {
x -= y;
return (x < 0) ? x + mod : x;
}
inline ll Mul(ll x,ll y) {
return 1ll * x * y % mod;
}
const int N=200010;
const int M=18;
const int inf=2e9;
const ll Inf=4e18;
int lg[N];
int top[2],s[2][N];
ll w[2][N];
ll ans;
namespace T2{
ll ans;
int ne[N],head[N],ver[N],tot=1,dep[N];
int dfn[N],tim,low[N];
ll val[N],dis[N];
int cnt,fir[N],f[M][N];
inline void add(ll z,int y,int x)
{
ver[++tot]=y; val[tot]=z; ne[tot]=head[x]; head[x]=tot;
ver[++tot]=x; val[tot]=z; ne[tot]=head[y]; head[y]=tot;
}
inline void _add(int x,int y,ll z)
{
ver[++tot]=y; val[tot]=z; ne[tot]=head[x]; head[x]=tot;
}
void dfs(int u,int pre)
{
dep[u]=dep[pre]+1; f[0][++cnt]=u; fir[u]=cnt;
dfn[u]=++tim;
for(int i=head[u],v;i;i=ne[i])
if((v=ver[i])!=pre)
{
dis[v]=dis[u]+val[i];
dfs(v,u);
f[0][++cnt]=u;
}
low[u]=tim;
}
inline int cmp(int x,int y){return dep[x]<dep[y]?x:y;}
void pre(int n)
{
dfs(1,0);
fo(j,1,M-1)
fo(i,1,cnt+1-(1<<j))
f[j][i]=cmp(f[j-1][i],f[j-1][i+(1<<(j-1))]);
fo(i,1,n) head[i] = 0;
tot=1;
}
inline int lca(int x,int y)
{
x=fir[x]; y=fir[y];
if(x>y) swap(x,y);
int k=lg[y-x+1];
return cmp(f[k][x],f[k][y-(1<<k)+1]);
}
int b[N],m,opt[N],st[N];
ll va[N];
bool vis[N];
int siz[N][2];
ll d1[N][2], d2[N][2], d3[N][2];
void dfs(int u)
{
fo(j,0,1)
d1[u][j] = d2[u][j] = d3[u][j] = siz[u][j] = 0;
if (vis[u]) {
d1[u][opt[u]] = va[u] % mod;
d2[u][opt[u]] = dis[u] % mod;
d3[u][opt[u]] = 1ll * va[u] * dis[u] % mod;
siz[u][opt[u]] = 1;
}
for(int i=head[u],v;i;i=ne[i])
{
v=ver[i];
dfs(v);
fo(j,0,1)
ans = (ans +
d3[u][j] * siz[v][1-j] + d2[u][j] * d1[v][1-j] +
d3[v][j] * siz[u][1-j] + d2[v][j] * d1[u][1-j] +
-2ll * (dis[u]) * siz[u][j] % mod * siz[v][1-j]
) % mod;
fo(j,0,1)
siz[u][j] += siz[v][j],
d1[u][j] = Add(d1[u][j], d1[v][j]),
d2[u][j] = Add(d2[u][j], d2[v][j]),
d3[u][j] = Add(d3[u][j], d3[v][j]);
}
}
inline void build_tree()
{
m=0;
fo(j,0,1) {
fo(i,1,top[j]) b[++m]=s[j][i];
}
sort(b+1,b+m+1,[&](const int &x,const int &y){return dfn[x]<dfn[y];});
fo(i,1,m) vis[b[i]]=1;
fo(j,0,1) fo(i,1,top[j]) opt[s[j][i]]=j;
fo(j,0,1) fo(i,1,top[j]) va[s[j][i]]=w[j][i];
fo(i,1,m-1) b[++m]=lca(b[i],b[i+1]);
sort(b+1,b+m+1,[&](const int &x,const int &y){return dfn[x]<dfn[y];});
m=unique(b+1,b+m+1)-b-1;
fo(i,1,m) head[b[i]]=0; tot=1;
int top=0;
fo(i,1,m)
{
for(;top&&low[st[top]]<dfn[b[i]];--top);
if(top) _add(st[top],b[i],dis[b[i]]-dis[st[top]]);
st[++top]=b[i];
}
}
void init()
{
fo(i,1,m) va[b[i]]=opt[b[i]]=vis[b[i]]=head[b[i]]=0;
m=0; tot=1;
}
inline ll solve() {
ans = 0;
build_tree();
dfs(b[1]);
init();
return ans;
}
inline void clear(int n) {
cnt = tim = 0;
fo(i,1,n) head[i] = dfn[i] = low[i] = 0;
tot = 1;
}
}
int n;
namespace T1{
int pre_n;
int ne[N],head[N],ver[N],tot=1;
ll val[N];
bool vis[N];
inline void add(int x,int y,ll z)
{
ver[++tot]=y; val[tot]=z; ne[tot]=head[x]; head[x]=tot;
ver[++tot]=x; val[tot]=z; ne[tot]=head[y]; head[y]=tot;
}
vector<int> son[N]; ll value[N];
void dfs(int u,int pre)
{
for(int i=head[u],v;i;i=ne[i])
if((v=ver[i])!=pre)
{
dfs(v,u);
son[u].pb(v); value[v]=val[i];
}
}
void rebuild()
{
dfs(1,0);
fo(i,1,n) head[i]=0;
tot=1;
int N=n,w[2],d=0;
for(int i=1;i<=N;i++)
if(son[i].size()<=2) for(auto v:son[i]) add(i,v,value[v]);
else
{
w[0]=++N; w[1]=++N;
add(i,w[0],0); add(i,w[1],0);
for(auto v:son[i])
{
d=1-d; son[w[d]].pb(v);
}
}
n=N;
}
int siz[N],minn,edge;
ll dis[N];
void getroot(int u,int pre,int s)
{
siz[u]=1;
for(int i=head[u],v,tmp;i;i=ne[i])
if((v=ver[i])!=pre&&!vis[i>>1])
{
getroot(v,u,s);
siz[u]+=siz[v];
tmp=max(siz[v],s-siz[v]);
if(tmp<minn) minn=tmp,edge=i;
}
}
int now;
void getdis(int u,int pre)
{
if(u<=pre_n)
{
++top[now];
s[now][top[now]]=u;
w[now][top[now]]=dis[u];
}
for(int i=head[u],v;i;i=ne[i])
if(!vis[i>>1]&&(v=ver[i])!=pre)
{
dis[v]=dis[u]+val[i];
getdis(v,u);
}
}
void divide(int u,int s)
{
minn=inf; getroot(u,0,s);
if(minn>=inf) return;
int j=edge,s1=siz[ver[j]],s2=s-s1;
vis[j>>1]=1; top[0]=top[1]=0;
dis[ver[j]]=dis[ver[j^1]]=0;
dis[ver[j]]=val[j];
now=0; getdis(ver[j],0);
now=1; getdis(ver[j^1],0);
ans = (ans + T2::solve() + mod) % mod;
fo(j,0,1) fo(i,1,top[j]) w[j][i]=0;
divide(ver[j],s1); divide(ver[j^1],s2);
}
inline void work()
{
pre_n=n;
rebuild();
divide(1,n);
}
void clear() {
fo(i,1,n)
son[i].clear(), head[i] = 0;
fo(i,1,tot) vis[i>>1] = 0;
tot = 1;
}
}
void Solve() {
n=read();
fo(i,2,n<<1) lg[i]=lg[i>>1]+1;
int x,y; ll z;
fo(i,2,n) x=read(),y=read(),z=read(),T1::add(x,y,z);
fo(i,2,n) T2::add(read(),read(),read());
T2::pre(n); T1::work();
ans = (ans * 2 % mod + mod) % mod;
printf("%lld\n", ans);
T1::clear(); T2::clear(n);
}
int main() {
int T = read();
for (;T--;) {
ans = 0;
Solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 14012kb
input:
1 2 1 2 3 1 2 4
output:
24
result:
ok "24"
Test #2:
score: -100
Wrong Answer
time: 54ms
memory: 11952kb
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...
output:
184776352 0 985425997 579643234 208803491 0 175966374 103217087 724335383 183341035 733132614 0 606508676 227875361 816161103 28912458 160893179 909368452 862272661 794852470 943851366 375432426 193725560 349257376 563806287 382139695 783380770 857285339 439093366 201365480 9517840 718878173 9374276...
result:
wrong answer 1st words differ - expected: '503735836', found: '184776352'