善用stl。。。。。。。。Codeforces Round #316 (Div. 2) D - Tree Requests

http://codeforces.com/contest/570/problem/D

题意:
给一颗树, 每一个节点上有一个字符, 有m次询问,每一个询问:rot,dep。 表示问:在节点rot的所有子树中,(离根节点)深度为dep 的点能不能组成回文的字符串。

一开始年轻的想法:
就是觉得太麻烦了,好难做。
然后看了看别人的算法,觉得自己好菜啊,好菜啊,好菜啊。

运用stl 很好解决这个问题:
vecotrans[][] 开一个二维的vector,实际就是三维 =。= ,我们 把dfs序跑出来, 把深度为dep,字符为ch 的节点放进ans[dep][ch-‘a’] ,里面,对于 rot 的子树不就是在 L[rot]…………R[rot]之间么,可得解

哦!!! 还有一个问题就是: 这道题q神的代码看起来差不多啊,但是只跑了500ms,一定要去学习一波!!!!!
7-18 回顾 : 只有一串666了

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define mem(a) memset(a,0,sizeof(a))
#define INF 0x7fffffff   //INT_MAX
#define inf 0x3f3f3f3f   //
const double PI = acos(-1.0);
const double e = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
bool cmpbig(int a,int b){return a>b;}
bool cmpsmall(int a,int b){return a<b;}
using namespace std;
#define maxn 500005

vector<int> v[maxn];
vector<int> ans[maxn][26];
int n, m;
char s[maxn];
int in[maxn], out[maxn], tot;
void dfs(int h, int x){

    in[x] = ++tot;
    ans[h][s[x-1]-'a'].push_back(tot);

    for(int i = 0; i < v[x].size(); i++)
      dfs(h+1, v[x][i]);

    out[x] = tot;
}
int main(){

//  freopen("in.txt", "r", stdin);
    int a;
    scanf("%d%d", &n, &m);
    for(int i = 2; i <= n; i++){
        scanf("%d", &a);
        v[a].push_back(i);
    }
    scanf("%s",s);
    tot = 0;
    dfs(1, 1);

    int v, h;
    for(int i = 0; i < m; i++){
        scanf("%d%d", &v, &h);
        int p = 0;
        for(int j = 0; j < 26; j++){
            int d = upper_bound(ans[h][j].begin(), ans[h][j].end(), out[v]) -
                    lower_bound(ans[h][j].begin(), ans[h][j].end(), in[v]);
            if(d&1)
             p++;
            if(p > 1)
             break;
        }
        if(p > 1)
          puts("No");
        else
          puts("Yes");
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值