P2420 让我们异或吧

Posted hahaha2124652975

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2420 让我们异或吧相关的知识,希望对你有一定的参考价值。

树链剖分

 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <vector>
 4 using namespace std;
 5 const int maxn = 100000 + 10;
 6 int n,m,r,p,dis[maxn],dep[maxn],father[maxn][18];
 7 vector<pair<int,int> > edges[maxn];
 8 inline void dfs(int now,int f,int Xor)   //dfs预处理
 9     dis[now] = Xor;
10     for (size_t i = 0;i < edges[now].size();i++)
11         if (edges[now][i].first != f) 
12             dep[edges[now][i].first] = dep[now]+1;
13             father[edges[now][i].first][0] = now;
14             dfs(edges[now][i].first,now,Xor^edges[now][i].second);
15         
16 
17 inline void init()   //倍增
18     for (int j = 1;j < 18;j++)
19         for (int i = 1;i <= n;i++) father[i][j] = father[father[i][j-1]][j-1];
20 
21 inline int lca(int a,int b)   //求lca
22     if (dep[a] < dep[b]) swap(a,b);
23     for (int i = 0;i < 18;i++)
24         if ((1<<i)&dep[a]-dep[b]) a = father[a][i];  //让深的点往上走
25     if (a == b) return a;  //走到同一点
26     for (int i = 18;i >= 0;i--)
27         if (father[a][i] != father[b][i])   //两个一起往上走
28             a = father[a][i];
29             b = father[b][i];
30         
31     return father[a][0];
32 
33 int main() 
34     scanf("%d",&n);
35     for (int i = 1,u,v,w;i < n;i++) 
36         scanf("%d%d%d",&u,&v,&w);
37         edges[u].push_back(make_pair(v,w));
38         edges[v].push_back(make_pair(u,w));
39     
40     dfs(1,1,1);
41     init();
42     scanf("%d",&m);
43     for (int i = 1,u,v;i <= m;i++) 
44         scanf("%d%d",&u,&v);
45         int tmp = lca(u,v);
46         printf("%d\n",(dis[u]^dis[tmp])^(dis[v]^dis[tmp]));
47     
48     return 0;
49 

 

#include <algorithm>#include <cstdio>#include <vector>usingnamespacestd; constint maxn = 100000 + 10; int n,m,r,p,dis[maxn],dep[maxn],father[maxn][18]; vector<pair<int,int> > edges[maxn]; inline void dfs(int now,int f,int Xor) //dfs预处理 dis[now] = Xor; for (size_t i = 0;i < edges[now].size();i++) if (edges[now][i].first != f) dep[edges[now][i].first] = dep[now]+1; father[edges[now][i].first][0] = now; dfs(edges[now][i].first,now,Xor^edges[now][i].second); inline void init() //倍增for (int j = 1;j < 18;j++) for (int i = 1;i <= n;i++) father[i][j] = father[father[i][j-1]][j-1]; inline int lca(int a,int b) //求lcaif (dep[a] < dep[b]) swap(a,b); for (int i = 0;i < 18;i++) if ((1<<i)&dep[a]-dep[b]) a = father[a][i]; //让深的点往上走if (a == b) return a; //走到同一点for (int i = 18;i >= 0;i--) if (father[a][i] != father[b][i]) //两个一起往上走 a = father[a][i]; b = father[b][i]; return father[a][0]; int main() scanf("%d",&n); for (int i = 1,u,v,w;i < n;i++) scanf("%d%d%d",&u,&v,&w); edges[u].push_back(make_pair(v,w)); edges[v].push_back(make_pair(u,w)); dfs(1,1,1); init(); scanf("%d",&m); for (int i = 1,u,v;i <= m;i++) scanf("%d%d",&u,&v); int tmp = lca(u,v); printf("%d\n",(dis[u]^dis[tmp])^(dis[v]^dis[tmp])); return0;

以上是关于P2420 让我们异或吧的主要内容,如果未能解决你的问题,请参考以下文章

洛谷 [P2420] 让我们异或吧

P2420 让我们异或吧

P2420 让我们异或吧

洛谷——P2420 让我们异或吧

Luogu P2420 让我们异或吧

AC日记——让我们异或吧 洛谷 P2420