UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214875#2642. colornaroto202220632ms63064kbC++1.8kb2024-11-22 22:46:352024-11-22 23:14:43

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int MN=1e5+5;
ll n,m,c[MN],fa[MN][25],vis[2][MN][25],head[MN],tot,deep[MN];
struct edge{ll to,nxt;}e[MN<<1];
void write(ll n){if(n<0){putchar('-');write(-n);return;}if(n>9)write(n/10);putchar(n%10+'0');}
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
char gc(){char ch=getchar();while(ch!='H'&&ch!='G')ch=getchar();return ch;}
void add(ll u, ll v){e[++tot].nxt=head[u];head[u]=tot;e[tot].to=v;}
void lca_dfs(ll u, ll f){
    deep[u]=deep[f]+1;fa[u][0]=f;vis[c[u]][u][0]=1;
    for(int i=1; i<25; i++){
        fa[u][i]=fa[fa[u][i-1]][i-1];
        for(int j=0; j<2; j++) vis[j][u][i]=vis[j][u][i-1]|vis[j][fa[u][i-1]][i-1];
    }
    for(int i=head[u]; i; i=e[i].nxt){
        ll v=e[i].to;
        if(v==f) continue;
        lca_dfs(v,u);
    }
}
ll lca(ll x, ll y, ll op){
    ll res=0;
    if(deep[x]<deep[y]) swap(x,y);
    for(int i=24; i>=0; i--) if(deep[x]-(1<<i)>=deep[y]){
        res|=vis[op][x][i];
        x=fa[x][i];
    }
    if(x==y) return res|(c[x]==op?1:0);
    for(int i=24; i>=0; i--) if(fa[x][i]!=fa[y][i]){
        res|=vis[op][x][i];res|=vis[op][y][i];
        x=fa[x][i];y=fa[y][i];
    }
    res|=(c[fa[x][0]]==op?1:0);
    return res;
}
int main(){
    n=read();m=read();
    for(int i=1; i<=n; i++){
        char ch=gc();
        c[i]=(ch=='H'?0:1);
    }
    for(int i=1; i<n; i++){
        ll u=read(),v=read();
        add(u,v);add(v,u);
    }
    lca_dfs(1,0);
    while(m--){
        ll s=read(),t=read(),op;char ch=gc();op=(ch=='H'?0:1);
        write(lca(s,t,op)|(c[s]==op?1:0)|(c[t]==op?1:0));
    }
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1732kb

input:

920 900
HHHHHHHHHHHHHHHHHHHHHHHHHGHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHHHHHHGHHGHHHHHHHGGHGHHHGHHGHGHHHHHH...

output:

0010100111100111101010001000010101100010001110110110001101100101111000101111100001111001110100001111...

result:

ok single line: '001010011110011110101000100001...1010100111010011101110111010101'

Test #2:

score: 10
Accepted
time: 2ms
memory: 1740kb

input:

927 949
HHHHGHGGGHHHHGHGHHHHHGHGGHGGGHHHGGHHHHHHGGGGGGHHHGGHHHHGHHHHGGGHHHGHGHHHHGGGGHHGHHGGHGHGGGGG...

output:

1111000011110111101101100110110011110101011000110111111111101110110011111011111110110011101010111111...

result:

ok single line: '111100001111011110110110011011...0011001011010111111011111001110'

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 1740kb

input:

934 998
HHHHGHHHGHHGGGHGGHGHGHHHGHHGHGGGHHHHHGHHGGHHHHHHGHHHGGHHHHHGGHHHGHHHHHHHGHHGHGHGGHHGHGHHHGGH...

output:

1101010111111010110101011111110111111101101100011111001101110111111111011111101001111011001010101111...

result:

wrong answer 1st lines differ - expected: '110101011111101011010101111111...101110111001110010111110...

Test #4:

score: 0
Wrong Answer
time: 0ms
memory: 1748kb

input:

941 947
HHHHHHHHHHHHHHHGHHHGHHHGHHHHGHHHGHHHHHHHHGHHHHHHHHHGHHHHHGHHHHHGHHGHHHHGHGGHHGHHHHHHHHHHGGHH...

output:

1111111011010110001101010101110100110001000101100101111101010001111111111110111011010101001111001111...

result:

wrong answer 1st lines differ - expected: '111111101101011000110101010111...010110001111011001100000...

Test #5:

score: 0
Wrong Answer
time: 115ms
memory: 60200kb

input:

92189 98896
HHHHHHHHHGHHHHGGGHGHHHHHHGHHHHHGHHHHHHHHHGHGHHHGHHHHHHGGHHHGHGGHHHHHHHHHGHHHHHHGHGHGHHHG...

output:

1110111111111111111111111111111111111011110111111111111111110111111101111111111111111111111111101111...

result:

wrong answer 1st lines differ - expected: '111011111111111111111111111111...111111111111111111111111...

Test #6:

score: 0
Wrong Answer
time: 112ms
memory: 62512kb

input:

95803 95747
HHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHGHHGHGHHHHGHGHHHHHHGGHHHHHHHHHHHHGHHHGHHHHHHHHHGHGGHHHHH...

output:

1011011101111111111111111101111111111011101101111110111111101111111011110111111101111111001100101111...

result:

wrong answer 1st lines differ - expected: '101101110111111111111111110111...111110110111111011111111...

Test #7:

score: 0
Wrong Answer
time: 102ms
memory: 60472kb

input:

92610 90996
HHGGGHHGHGGGHGHGHGGGGGGHGGGHGGHGHGHGGHHHHHGHGGHGHGGGGHGGGHGGHHGHHHGGHGHGGHGGGHGGGGGGGGGG...

output:

1111111111111111111011111111111111111110111111111111100111111111011111111111101101111111111111011111...

result:

wrong answer 1st lines differ - expected: '111111111111111111101111111111...111111001111111111111111...

Test #8:

score: 0
Wrong Answer
time: 101ms
memory: 62784kb

input:

96224 91494
HHHHHHHHGGHHGHHHHHHHHHHHHHGHHHGHHHHHHHHHHHHHHHHHGGHHHHHHGHHHHHHHHHHHHHHHHHGHGGGHGHGHHHHH...

output:

1111010100111100111111111111011011010101011111011101111011111011111011111111111111111111111011111101...

result:

wrong answer 1st lines differ - expected: '111101011011110011111111111101...110101101111111111111110...

Test #9:

score: 0
Wrong Answer
time: 89ms
memory: 60740kb

input:

93031 96743
HHHHHHHHHGHHHHHHHHHHHHHHHHHGGHHHHGHHHGHHHHHHHHHGHHHHHHHHHHHHHHGGHHHHHGGHGHHHGHHHGHHHHHHG...

output:

0100011111101111011111110111001111101111110101110110111111111111111101111110110001011110011111011111...

result:

wrong answer 1st lines differ - expected: '010001111110111101111111011100...111111110111001111110101...

Test #10:

score: 0
Wrong Answer
time: 111ms
memory: 63064kb

input:

96645 93594
HHHHHHHHHGHGHHHHHHHGHHHHGGGHHHHHHHHHHHHHHHHHHGHHHHHGHGHHHHHGHGHHHHHGHHHHGHHHHGGHHGHHHHGH...

output:

0101000110111110011111111111110111101011011101111110111111011011111111111110111110101111111111011111...

result:

wrong answer 1st lines differ - expected: '010100011011111001111111111111...111001111111111111011001...