链接: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. git error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

    原因:git 版本过低 解决方法:卸载旧版本,重装新版本

  2. java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK

    在pom.xml文件中添加如下依赖: <!-- https://mvnrepository.com/artifact/cn.easyproject/orai18n --> <depe ...

  3. (WA)BZOJ 4821: [Sdoi2017]相关分析

    二次联通门 : BZOJ 4821: [Sdoi2017]相关分析 2017.8.23 Updata 妈妈!!这道题卡我!!!就是不然我过!!!!! #include <cstdio> # ...

  4. python3.5+win7 安装 numpy 和scipy的总结

    1.安装numpy.      官网下载numpy.下载地址为https://pypi.Python.org/pypi/numpy.选择与Python版本相对应的numpy版本.之后在python35 ...

  5. fiddler在小米8下抓取https数据包.

    问题,在小米8下一直报 证书链问题,爬了半天帖子发现可能是Android版本问题,有的说用Charles没问题. 没有测试,网上接着爬帖子... 稍稍说下导入证书的问题吧. 可以使用浏览器下载证书,也 ...

  6. sqlserver 存储过程的新建与执行

    if Exists(select * from sysobjects where NAME = 'insert_custominfo' and type='P') drop procedure ins ...

  7. JavaWeb_(Spring框架)注解配置

    系列博文 JavaWeb_(Spring框架)xml配置文件  传送门 JavaWeb_(Spring框架)注解配置 传送门 Spring注解配置 a)导包和约束:基本包.aop包+context约束 ...

  8. Python 中操作 MySQL

    引入模块 在py文件中引入pymysql模块 from pymysql import * Connection 对象 用于建立与数据库的连接 创建对象:调用connect()方法 conn=conne ...

  9. 重读APUE(9)-SIG_ERR、SIG_DFL、SIG_IGN定义无参数

    下面这几个函数定义,每次看到都会纠结一阵子,奇怪的是为什么没有参数? #define SIG_ERR (void (*)())-1 #define SIG_DFL (void (*)())0 #def ...

  10. @Transactional注解不生效的原因总结(整理网上和自己遇到的解决方案)

    1.问题背景 今天做项目,发现配置好@Transactional后,没有生效,事务没有回滚,即便在网上查资料,也没有解决,好像网上没有人发过我遇见的这种情况的帖子. 2.自己遇到的情况分析 代码结构图 ...