Luogu P5836 [USACO19DEC]Milk Visits S
数据合并的LCA,写的时间不算太长,查错查了好久,出现的问题有:
1.jph与jpg手误打错
2.双向边只加了一遍
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<cmath> 6 #include<stack> 7 #include<vector> 8 #include<queue> 9 #include<ctime> 10 #include<iostream> 11 #define rep(i,a,n) for(register int i = a;i <= n;++i) 12 #define per(i,n,a) for(register int i = n;i >= a;--i) 13 14 using namespace std; 15 typedef long long ll; 16 17 const int Maxn = 1e5+10; 18 19 ll read(){ 20 ll a = 0;char c = getchar(),l = c; 21 while(c < '0'||c > '9')l = c,c = getchar(); 22 while('0' <= c&&c <= '9')a = a*10+c-'0',c = getchar(); 23 if(l =='-')return -a;return a; 24 } 25 26 struct Edge{ 27 int to,ne; 28 }edges[Maxn<<1]; 29 30 int jph[Maxn][21],jpg[Maxn][21],jp[Maxn][21]; 31 int first[Maxn],vis[Maxn],dep[Maxn]; 32 int cnte = 0,n,m,x,y; 33 char milk[Maxn]; 34 char c; 35 36 void add_edge(int fr,int to){ 37 edges[++cnte] = (Edge){to,first[fr]}; 38 first[fr] = cnte; 39 } 40 41 void dfs(int s){ 42 for(int i = first[s];i;i = edges[i].ne){ 43 Edge &e = edges[i]; 44 if(vis[e.to])continue; 45 vis[e.to] = 1; 46 jp[e.to][0] = s; 47 if(milk[s] == 'H')jph[e.to][0] = 1; 48 else jpg[e.to][0] = 1; 49 dep[e.to] = dep[s]+1; 50 dfs(e.to); 51 } 52 } 53 54 bool checkh(int x,int y){ 55 if(milk[x] == 'H'||milk[y] == 'H')return true; 56 if(dep[y] > dep[x])swap(x,y); 57 per(i,20,0)if(dep[x]-(1<<i) >= dep[y]){ 58 if(jph[x][i])return true; 59 x = jp[x][i]; 60 } 61 if(x == y)return false; 62 per(i,20,0)if(dep[x] >= (1<<i)&&jp[x][i] != jp[y][i]){ 63 if(jph[x][i]|jph[y][i])return true; 64 x = jp[x][i],y = jp[y][i]; 65 } 66 return jph[x][0]; 67 } 68 69 bool checkg(int x,int y){ 70 if(milk[x] == 'G'||milk[y] == 'G')return true; 71 if(dep[y] > dep[x])swap(x,y); 72 per(i,20,0)if(dep[x]-(1<<i) >= dep[y]){ 73 if(jpg[x][i])return true; 74 x = jp[x][i]; 75 } 76 if(x == y)return false; 77 per(i,20,0)if(jp[x][i] != jp[y][i]){ 78 if(jpg[x][i]|jpg[y][i])return true; 79 x = jp[x][i],y = jp[y][i]; 80 } 81 return jpg[x][0]; 82 } 83 84 int main(){ 85 n = read(),m = read(); 86 scanf("%s",milk+1); 87 rep(i,1,n) 88 if(milk[i] == 'H')jph[i][0] = 1; 89 else jpg[i][0] = 1; 90 rep(i,1,n-1){ 91 x = read(),y = read(); 92 add_edge(y,x); 93 add_edge(x,y); 94 } 95 96 dfs(1); 97 rep(i,1,20){ 98 rep(j,1,n)if(dep[j] >= (1<<i)){ 99 jp[j][i] = jp[ jp[j][i-1] ][i-1]; 100 jph[j][i] = jph[j][i-1] | jph[ jp[j][i-1] ][i-1]; 101 jpg[j][i] = jpg[j][i-1] | jpg[ jp[j][i-1] ][i-1]; 102 } 103 } 104 105 rep(i,1,m){ 106 x = read(),y = read(); 107 cin >> c; 108 if(c == 'H')printf("%d",checkh(x,y)?1:0); 109 else printf("%d",checkg(x,y)?1:0); 110 } 111 return 0; 112 }