1 条题解
-
1
#include<bits/stdc++.h> #define us unsigned short #define ll long long const int N=20010,M=200010; int n,m,i,x,y; ll a[N]; int t[N]; struct edge { int to,next; }l[N+M<<1];int e; int qt[N]; ll ans[M];bool have[M]; bool vis[N]; us q[N],head,tail; namespace kcz { us sz[N],f[N]; us get_g(int x) { q[tail=1]=x;f[x]=0; for(head=1;head<=tail;++head) { x=q[head];sz[x]=1; for(i=t[x];y=l[i].to;i=l[i].next) if(!vis[y]&&y!=f[x]) { f[y]=x; q[++tail]=y; } } for(head=tail;head;--head) { x=q[head]; if(sz[x]>=(tail>>1)) return x; sz[f[x]]+=sz[x]; } } }; us belong[N]; ll g[61],f[N][61]; void ins(ll *f,ll x) { short i; for(i=60;x;--i) if((x>>i)&1) if(!f[i]){f[i]=x;return ;} else x^=f[i]; } ll qiu(ll *f) { ll ans=0; for(short i=60;i>=0;--i) if((ans^f[i])>ans) ans^=f[i]; return ans; } us now; void dfs(us x,us fr) { belong[x]=now; int i;us y; for(i=0;i<=60;++i) f[x][i]=f[fr][i]; ins(f[x],a[x]); for(i=t[x];y=l[i].to;i=l[i].next) if(y!=fr&&!vis[y]) dfs(y,x); } void merge(ll *g,ll *f1,ll *f2) { int i; for(i=0;i<=60;++i) g[i]=f1[i]; for(i=0;i<=60;++i) ins(g,f2[i]); } void solve(int x) { vis[x]=1; belong[x]=0; for(i=0;i<=60;++i) f[x][i]=0; ins(f[x],a[x]); for(i=t[x];now=l[i].to;i=l[i].next) dfs(now,x); for(head=1;head<=tail;++head) { x=q[head]; for(i=qt[x];y=l[i].to;i=l[i].next) if(!have[i>>1]&&belong[x]!=belong[y]) { have[i>>1]=1; merge(g,f[x],f[y]); ans[i>>1]=qiu(g); } } } #define ch_top 10000000 char ch[ch_top],*now_r=ch,*now_w=ch-1; void read(int &x) { while(*now_r<'0') ++now_r; for(x=*now_r-'0';*++now_r>='0';) x=(x<<1)+(x<<3)+*now_r-'0'; } void read(ll &x) { while(*now_r<'0') ++now_r; for(x=*now_r-'0';*++now_r>='0';) x=(x<<1)+(x<<3)+*now_r-'0'; } void write(ll &x) { static us st[30],top; if(!x) *++now_w='0'; else { for(;x;x/=10) st[++top]=x%10; for(;top;--top) *++now_w='0'+st[top]; } *++now_w='\n'; } int main() { freopen("1.in","r",stdin);freopen("1.out","w",stdout); fread(ch,1,ch_top,stdin); read(n);read(m); int i; for(i=1;i<=n;++i) read(a[i]); e=M<<1; for(i=1;i<n;++i) { read(x);read(y); l[++e]=(edge){y,t[x]};t[x]=e; l[++e]=(edge){x,t[y]};t[y]=e; } e=1; for(i=1;i<=m;++i) { read(x);read(y); l[++e]=(edge){y,qt[x]};qt[x]=e; l[++e]=(edge){x,qt[y]};qt[y]=e; if(x==y) {have[i]=1;ans[i]=a[x];} } for(i=1;i<=n;++i) while(!vis[i]) solve(kcz::get_g(i)); for(i=1;i<=m;++i) write(ans[i]); fwrite(ch,1,now_w-ch+1,stdout); }
- 1
信息
- ID
- 2228
- 时间
- 2000~6000ms
- 内存
- 250MiB
- 难度
- 6
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者