链接:https://nanti.jisuanke.com/t/39277

思路:

一开始看着很像树分治,就用树分治写了下,发现因为异或操作的特殊性,我们是可以优化树分治中的容斥操作的,不合理的情况只有当两点在一条链上才存在,那么直接一遍dfs从根节点向下跑途中维护一下前缀和,把所有情况中不合理情况造成的值修正。

这样的话时间复杂度就可以降得非常低了,感觉还可以优化,但是懒得写了

代码耗时:142ms.

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll M = 2e5+;
const ll inf = 1e18+;
struct node{
ll to,next,w;
}e[M];
const ll mod = ;
struct node1{
ll num,id;
}Xor[M];
bool cmp(node1 x,node1 y){
return x.num < y.num;
}
vector<ll>mp[M],v[M];
ll cnt,n,ans;
ll head[M],sz[M],d[M],md[M];
void add(ll u,ll v,ll w){
e[++cnt].to = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt;
} map<ll,ll>sum,sum1,num; void get_dis(ll u,ll fa){
Xor[++Xor[].num].num = d[u];
Xor[Xor[].num].id = u;
for(ll i = head[u];i;i=e[i].next){
ll v = e[i].to;
if(v != fa){
d[v] = d[u]^e[i].w;
get_dis(v,u);
}
}
return ;
} void get_siz(ll u,ll fa){
sz[u] = ;
for(ll i = head[u];i;i=e[i].next){
ll v = e[i].to;
if(v != fa){
get_siz(v,u);
sz[u] += sz[v];
}
}
}
void gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b) {d=a;x=;y=;}
else {gcd(b,a%b,d,y,x);y-=x*(a/b);}
}
ll finv(ll a,ll n)
{
ll d,x,y;
gcd(a,n,d,x,y);
return d==?(x+n)%n:-;
}
void cal(ll u){
d[u] = ; Xor[].num = ;
get_dis(u,);
sort(Xor+,Xor++Xor[].num,cmp);
ll st = -,idx = ;
for(ll i = ;i <= Xor[].num;i ++){
if(Xor[i].num != st){
st = Xor[i].num;
mp[++idx].push_back(Xor[i].id);
md[idx] = st;
}
else{
mp[idx].push_back(Xor[i].id);
}
}
ans = ;
for(ll i = ;i <= idx;i ++){
ll num1 = ,num2 = ;
for(ll j = ;j < mp[i].size();j ++){
num1 += sz[mp[i][j]];
num2 += sz[mp[i][j]]*sz[mp[i][j]]%mod;
num1%=mod; num2%=mod;
}
ans += ((num1*num1%mod+mod - num2)%mod)*finv(,mod)%mod;
ans %= mod;
}
for(ll i = ;i <= idx;i ++) mp[i].clear();
} void dfs(ll u,ll fa){
for(ll i = head[u];i;i=e[i].next){
ll v = e[i].to;
if(v == fa) continue;
sum1[d[u]] += (n - sz[v]+mod)%mod;
if(num[d[v]] >= ){
ans = (ans + mod - (sz[v]*sum[d[v]]%mod))%mod;
ans += sz[v]*sum1[d[v]]%mod;
ans %= mod;
}
sum[d[v]] += sz[v];
num[d[v]] += ;
sum[d[v]]%=mod;
sum1[d[v]]%=mod;
dfs(v,u);
sum[d[v]] -= sz[v]-mod;
sum1[d[u]] -= (n-sz[v])-mod;
sum[d[v]]%=mod;
sum1[d[v]]%=mod;
num[d[v]] -= ;
}
} int main()
{
ll v,w;
scanf("%lld",&n);
for(ll i = ;i <= n;i ++){
scanf("%lld%lld",&v,&w);
add(i,v,w); add(v,i,w);
}
get_siz(,);
cal();
sum[] += sz[];
num[] += ;
dfs(,);
ans %= mod;
num.clear(); sum.clear(); sum1.clear();
printf("%lld\n",ans);
}

The 2019 ACM-ICPC China Shannxi Provincial Programming Contest (西安邀请赛重现) J. And And And的更多相关文章

  1. 计蒜客 39272.Tree-树链剖分(点权)+带修改区间异或和 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest E.) 2019ICPC西安邀请赛现场赛重现赛

    Tree Ming and Hong are playing a simple game called nim game. They have nn piles of stones numbered  ...

  2. C.0689-The 2019 ICPC China Shaanxi Provincial Programming Contest

    We call a string as a 0689-string if this string only consists of digits '0', '6', '8' and '9'. Give ...

  3. B.Grid with Arrows-The 2019 ICPC China Shaanxi Provincial Programming Contest

    BaoBao has just found a grid with $n$ rows and $m$ columns in his left pocket, where the cell in the ...

  4. 计蒜客 39280.Travel-二分+最短路dijkstra-二分过程中保存结果,因为二分完最后的不一定是结果 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest M.) 2019ICPC西安邀请赛现场赛重现赛

    Travel There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. ...

  5. 计蒜客 39279.Swap-打表找规律 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest L.) 2019ICPC西安邀请赛现场赛重现赛

    Swap There is a sequence of numbers of length nn, and each number in the sequence is different. Ther ...

  6. 计蒜客 39270.Angel's Journey-简单的计算几何 ((The 2019 ACM-ICPC China Shannxi Provincial Programming Contest C.) 2019ICPC西安邀请赛现场赛重现赛

    Angel's Journey “Miyane!” This day Hana asks Miyako for help again. Hana plays the part of angel on ...

  7. 计蒜客 39268.Tasks-签到 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest A.) 2019ICPC西安邀请赛现场赛重现赛

    Tasks It's too late now, but you still have too much work to do. There are nn tasks on your list. Th ...

  8. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

    题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...

  9. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set

    Let's consider some math problems. JSZKC has a set A=A={1,2,...,N}. He defines a subset of A as 'Meo ...

随机推荐

  1. SIGAI机器学习第十四集 支持向量机1

    讲授线性分类器,分类间隔,线性可分的支持向量机原问题与对偶问题,线性不可分的支持向量机原问题与对偶问题,核映射与核函数,多分类问题,libsvm的使用,实际应用 大纲: 支持向量机简介线性分类器分类间 ...

  2. Oracle 后台进程(六)PMON进程

    一.PMON简介 二.PMON的工作内容如下: 1.监控后台进程运行状况 2.如果某些进程异常中断,PMON去释放会话资源以及占用的锁LOCK 3.更新事务表的标志以及清除事务XID的标记 4.清除异 ...

  3. Java进阶知识25 Spring与Hibernate整合到一起

    1.概述 1.1.Spring与Hibernate整合关键点 1) Hibernate的SessionFactory对象交给Spring创建.    2) hibernate事务交给spring的声明 ...

  4. 【线性代数】6-3:微分方程的应用(Applications to Differential Equations)

    title: [线性代数]6-3:微分方程的应用(Applications to Differential Equations) categories: Mathematic Linear Algeb ...

  5. 如何通过Thread查看一个方法被调用的顺序

    Test1 package com.dwz.concurrency.chapter11; public class Test1 { private Test2 test2 = new Test2(); ...

  6. 算法-java实现

    1. 质因数分解 public static List<Integer> factorize(int n){ List<Integer> factors = new Array ...

  7. 异步机制 - APC

    APC : An asynchronous procedure call,异步过程调用,是微软提供的一种在线程上下文中执行代码的机制.当向一个线程插入一个USER APC时,如果线程进入alertab ...

  8. 解决oracle服务占用内存过高的问题

    其实这是因为安装Oracle时,为了均衡电脑性能和数据库性能,默认内存大小为物理内存的1/8,自身内存比较大时,oracle所占的内存也会变大.而通常,我们自己的环境并不需要分配那么大的内存来支持Or ...

  9. 消灭WinRAR广告

    1. 问题描述 WinRAR每次弹出的广告真的令人厌烦至极,虽然软件公司也得恰饭,免费给你用总得看俩广告吧,但是像我这样经常用WinRAR的人来说广告弹出频率未免也太过分了.一开始还只是用火绒的弹窗拦 ...

  10. SourceTree软件

      A free Git client for Windows and Mac SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端管理工具,同时也是Mer ...