接下来M行每行包含两个正整数a、b,表示询问a结点和b结点的最近公共祖先。 输出格式: 输出包含M行,每行包含一个正整数,依次为每一个询问的结果。 输入样例#1: 5 5 4 3 1 2 4 5 1 1 4 2 4 3 2 3 5 1 2 4 5 输出样例#1: 4 4 1 4 4 模板:时间复杂度nlogn #include<iostream>#include<cs...
LCA模板【倍增法】1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<ctime> 6 #include<cmath> 7 #include<algorithm> 8using namespace std;9#define MAXN 100010 10struct node{int y,next;}e[MAXN*2];11int n,m,len,Link[MAXN],deep[MAXN],f[...
LCA模板 【倍增法】 1#include<iostream>2#include<cstdio>3#include<cstring>4#include<cstdlib>5#include<ctime>6#include<cmath>7#include<algorithm>8usingnamespacestd;9#defineMAXN 10001010structnode{inty,next;}e[MAXN*2];11intn,m,len,Link[MAXN],deep[MAXN],f[MAXN],anc[MAXN][25];12inli...
char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ?
#include<cstdio> #include<cstring> using namespace std; const int maxn=500007,ln=20; struct E { int to,next; }e[maxn*2]; int p[maxn][ln+1],d[maxn],list[maxn],cnt=1,n,q,u; void add(int u,int v) { e[++cnt].to=v; e[cnt].next=list[u]; list[u]=cnt; ...
模板:时间复杂度nlogn 代码语言:javascript 复制 #include<iostream>#include<cstdio>using namespace std;struct yyy{int t,nex;}e[2*500001];int deepth[500001],fa[500001][22],lg[500001],head[500001];int tot;voidadd(int x,int y)//邻接表存树{e[++tot].t=y;e[tot].nex=head[x];head[x...
[idx]=c,h[a]=idx++;}voiddfs(introot){memset(depth,0x3f,sizeofdepth);depth[root]=1;depth[0]=0;queue<int>q;q.push(root);while(q.size()){autot=q.front();q.pop();for(inti=h[t];i!=-1;i=ne[i]){intj=e[i];if(depth[j]>depth[t]+1){depth[j]=depth[t]+1;fa[j][...
一种面向中小企业的LCA软件开发思想及开发实践
1799 1 6:17 App C52 可持久化线段树 P1972 [SDOI2009] HH的项链 2352 1 3:20 App A29 贪心算法 P1803 线段覆盖 3426 -- 4:45 App C57 可持久化线段树+二分 P2839 [国家集训队] middle 9163 -- 5:48 App G42 快速傅里叶变换 FFT算法 高精度乘法 3166 -- 3:12 App C46【模板】权值...
60}61voidread(int&x)62{63char c=getchar();x=0;64while(c<'0'||c>'9')c=getchar();65while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();// 读入优化,必须要有!66}67intmain()68{69//scanf("%d%d%d",&n,&m,&root);70read(n);read(m);read(root);71for(int i=1;i<=n...