链接: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. Oracle 审计 部署监控 user DML操作

    1.移动audit表及索引到dbadmin表空间 alter table aud$ move tablespace DBADMIN;alter table AUDIT$ move tablespace ...

  2. 百度ueditor中复制word图文时图片转存任然保持灰色不可用

    官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...

  3. luogu 1903 [国家集训队]数颜色 / 维护队列 带修改莫队

    十分玄学的数据结构~ code: #include <bits/stdc++.h> #define N 1000006 #define setIO(s) freopen(s".i ...

  4. python if else elif statement

    name = input('what is your name?')if name.endswith('zd'): print("hello panzidong") name = ...

  5. mysql报错码code=exited,status=2的解决方案

    由于电脑死机,导致MySQL无法重启. 解决方案看官方文档,设置完后重启失败,再把innodb_force_recovery = 1去掉就可以了 https://dev.mysql.com/doc/r ...

  6. Flask-第三方插件

    Flask-Session 因为flask自带的session是将session存在cookie中: 所以才有了第三方Flask_session插件,可以将session存储在我们想存储的数据库中(r ...

  7. [题解] [BZOJ4152] The Captain

    题面 题解 将所有点根据

  8. mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法

    1.执行whereis mysql会有如下打印: mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql ...

  9. Django中的 返回json对象的方式

    在返回json对象的几种方式: 1 from django.shortcuts import render, HttpResponse # Create your views here. from d ...

  10. Linux中查看系统资源占用情况的命令

    用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...