UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214848#2642. colorFilberte100424ms17008kbC++112.2kb2024-11-22 19:13:002024-11-22 23:11:56

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
vector<int> e[N];
int col[N];
struct ST_LCA{
    int nods;
    int fa[N], dep[N];
    int dfn[N], clk = 0, dfnst[N][21];
    int ch[N], cg[N];
    void dfs(int u, int f){
        dfn[u] = ++clk;dfnst[clk][0] = u;
        fa[u] = f, dep[u] = dep[f] + 1;
        ch[u] = ch[f], cg[u] = cg[f];
        if(col[u] == 0) ch[u]++;
        else cg[u]++;
        for(int v : e[u]){
            if(v == f) continue;
            dfs(v, u);
        } 
    }
    void pre_st(){
        int _k = __lg(nods);
        for(int j = 1;j <= _k;j++){
            for(int i = 1;i + (1 << j) - 1 <= nods;i++){
                if(dep[dfnst[i][j - 1]] < dep[dfnst[i + (1 << (j - 1))][j - 1]]) dfnst[i][j] = dfnst[i][j - 1];
                else dfnst[i][j] = dfnst[i + (1 << (j - 1))][j - 1];   
            }
        }
    }
    void init(int nnod, int rt = 1){
        nods = nnod;
        dfs(rt, 0);
        pre_st();
    }
    int qry(int x, int y){
        int Lg = __lg(y - x + 1);
        int q = dfnst[x][Lg], p = dfnst[y - (1 << Lg) + 1][Lg];
        if(dep[q] < dep[p]) return q;
        else return p; 
    }
    int getlca(int u, int v){
        if(u == v) return u;
        if(dfn[u] > dfn[v]) swap(u, v);
        return fa[qry(dfn[u] + 1, dfn[v])];
    }
    int cnt_h(int u, int v){
        int lc = getlca(u, v);
        return ch[u] + ch[v] - ch[lc] - ch[fa[lc]];
    }
    int cnt_g(int u, int v){
        int lc = getlca(u, v);
        return cg[u] + cg[v] - cg[lc] - cg[fa[lc]];
    }
}T;
using namespace std;
int n, m;
char s[N];
int main(){
    scanf("%d%d",&n,&m);   
    scanf("%s",s + 1);
    for(int i = 1;i <= n;i++) col[i] = (s[i] == 'G') ? 1 : 0;
    for(int i = 1;i < n;i++){
        int u, v;scanf("%d%d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    T.init(n);
    while(m--){
        int u, v; char ch;
        scanf("%d%d %c",&u,&v,&ch);
        if(ch == 'H'){
            if(T.cnt_h(u, v) > 0) putchar('1');
            else putchar('0');
        }
        else{
            if(T.cnt_g(u, v) > 0) putchar('1');
            else putchar('0');
        }
    }
}

详细

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

Test #1:

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

input:

920 900
HHHHHHHHHHHHHHHHHHHHHHHHHGHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHHHHHHGHHGHHHHHHHGGHGHHHGHHGHGHHHHHH...

output:

0010100111100111101010001000010101100010001110110110001101100101111000101111100001111001110100001111...

result:

ok single line: '001010011110011110101000100001...1010100111010011101110111010101'

Test #2:

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

input:

927 949
HHHHGHGGGHHHHGHGHHHHHGHGGHGGGHHHGGHHHHHHGGGGGGHHHGGHHHHGHHHHGGGHHHGHGHHHHGGGGHHGHHGGHGHGGGGG...

output:

1111000011110111101101100110110011110101011000110111111111101110110011111011111110110011101010111111...

result:

ok single line: '111100001111011110110110011011...0011001011010111111011111001110'

Test #3:

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

input:

934 998
HHHHGHHHGHHGGGHGGHGHGHHHGHHGHGGGHHHHHGHHGGHHHHHHGHHHGGHHHHHGGHHHGHHHHHHHGHHGHGHGGHHGHGHHHGGH...

output:

1101010111111010110101011111110111111101101100011111001101110111111111011111101001111011001010101111...

result:

ok single line: '110101011111101011010101111111...1011101110011100101111100101110'

Test #4:

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

input:

941 947
HHHHHHHHHHHHHHHGHHHGHHHGHHHHGHHHGHHHHHHHHGHHHHHHHHHGHHHHHGHHHHHGHHGHHHHGHGGHHGHHHHHHHHHHGGHH...

output:

1111111011010110001101010101110100110001000101100101111101110001111111111110111011010101001111101111...

result:

ok single line: '111111101101011000110101010111...0101100011110110011000000000011'

Test #5:

score: 10
Accepted
time: 66ms
memory: 16400kb

input:

92189 98896
HHHHHHHHHGHHHHGGGHGHHHHHHGHHHHHGHHHHHHHHHGHGHHHGHHHHHHGGHHHGHGGHHHHHHHHHGHHHHHHGHGHGHHHG...

output:

1110111111111111111111111111111111111011110111111111111111110111111101111111111111111111111111101111...

result:

ok single line: '111011111111111111111111111111...1111111111111111111111111111111'

Test #6:

score: 10
Accepted
time: 68ms
memory: 16900kb

input:

95803 95747
HHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHGHHGHGHHHHGHGHHHHHHGGHHHHHHHHHHHHGHHHGHHHHHHHHHGHGGHHHHH...

output:

1011011101111111111111111101111111111011101101111110111111101111111011110111111101111111001100101111...

result:

ok single line: '101101110111111111111111110111...1111101101111110111111111001111'

Test #7:

score: 10
Accepted
time: 72ms
memory: 16448kb

input:

92610 90996
HHGGGHHGHGGGHGHGHGGGGGGHGGGHGGHGHGHGGHHHHHGHGGHGHGGGGHGGGHGGHHGHHHGGHGHGGHGGGHGGGGGGGGGG...

output:

1111111111111111111011111111111111111110111111111111100111111111011111111111101101111111111111011111...

result:

ok single line: '111111111111111111101111111111...1111110011111111111111111111111'

Test #8:

score: 10
Accepted
time: 74ms
memory: 16960kb

input:

96224 91494
HHHHHHHHGGHHGHHHHHHHHHHHHHGHHHGHHHHHHHHHHHHHHHHHGGHHHHHHGHHHHHHHHHHHHHHHHHGHGGGHGHGHHHHH...

output:

1111010110111100111111111111011011010101011111011101111011111011111011111111111111111111111011111101...

result:

ok single line: '111101011011110011111111111101...1101011011111111111111101101111'

Test #9:

score: 10
Accepted
time: 64ms
memory: 16512kb

input:

93031 96743
HHHHHHHHHGHHHHHHHHHHHHHHHHHGGHHHHGHHHGHHHHHHHHHGHHHHHHHHHHHHHHGGHHHHHGGHGHHHGHHHGHHHHHHG...

output:

0100011111101111011111110111001111101111110101110110111111111111111101111110110001011110011111011111...

result:

ok single line: '010001111110111101111111011100...1111111101110011111101011111110'

Test #10:

score: 10
Accepted
time: 78ms
memory: 17008kb

input:

96645 93594
HHHHHHHHHGHGHHHHHHHGHHHHGGGHHHHHHHHHHHHHHHHHHGHHHHHGHGHHHHHGHGHHHHHGHHHHGHHHHGGHHGHHHHGH...

output:

0101000110111110011111111111110111101011011101111110111111011011111111111110111110101111111111111111...

result:

ok single line: '010100011011111001111111111111...1110011111111111110110011111111'