链接: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. leetcode解题报告(7):Valid Parentheses

    描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  2. 【CTS2019】珍珠【生成函数,二项式反演】

    题目链接:洛谷 pb大佬说这是sb题感觉好像有点过fan...(我还是太弱了) 首先,设$i$这个数在序列中出现$a_i$次,要求$\sum_{i=1}^D[a_i \ mod \ 2]\leq n- ...

  3. windows游戏编程封装窗口类

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22451353 作者:jadeshu   邮箱: jades ...

  4. Dubbo——配置

    一.配置原则 JVM 启动 -D 参数优先,这样可以使用户在部署和启动时进行参数重写,比如在启动时需改变协议的端口. XML 次之,如果在 XML 中有配置,则 dubbo.properties 中的 ...

  5. MySQL数据分析实战-朱元禄-专题视频课程

    MySQL数据分析实战-496人已学习 课程介绍        本套课程由知名数据分析博主jacky老师录制,深入浅出讲解MySQL数据分析,从实战角度出发,帮助大家制胜职场!课程收益    1.学会 ...

  6. gitlab配置邮箱postfix(新用户激活邮件)

    亲测可用 https://www.cnblogs.com/yoyoketang/p/10287345.html

  7. js的dom测试及实例代码

    js的dom测试及实例代码 一.总结 一句话总结: 1.需要记得 创建 标签和创建文本节点都是document的活:document.createTextNode("Rockets的姚明&q ...

  8. Linux ldd -- 查看可执行文件所依赖的动态链接库

    我们知道“ldd”这个命令主要是被程序员或是管理员用来查看可执行文件所依赖的动态链接库的.是的,这就是这个命令的用处.可是,这个命令比你想像的要危险得多,也许很多黑客通过ldd的安全问题来攻击你的服务 ...

  9. API网关的用处

    API网关我的分析中会用到以下三种场景. Open API. 企业需要将自身数据.能力等作为开发平台向外开放,通常会以rest的方式向外提供,最好的例子就是淘宝开放平台.腾讯公司的QQ开发平台.微信开 ...

  10. kotlin 类的继承

    与Java不同,kotlin 使用冒号,而Java 中使用extends, 注意冒号后面需要调用夫类的构造器.属于单继承,使用open 关键字允许继承class package loaderman.d ...