博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces - 570D 离散DFS序 特殊的子树统计 (暴力出奇迹)
阅读量:4488 次
发布时间:2019-06-08

本文共 2097 字,大约阅读时间需要 6 分钟。

题意:给定一棵树,树上每个节点有对应的字符,多次询问在\(u\)子树的深度为\(d\)的所有节点上的字符任意组合能否凑成一个回文串

把dfs序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度

因为dfs序是单调递增的,所以每个二维表的值也是单调递增的
那么只需用两次二分把合法的儿子搞出来就行

感觉是个比较奇怪的姿势,总之学习了

//时间空间都这么暴力真的大丈夫?

#include
#define rep(i,j,k) for(register int i=j;i<=k;i++)#define rrep(i,j,k) for(register int i=j;i>=k;i--)#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])#define print(a) printf("%lld",(ll)(a))#define println(a) printf("%lld\n",(ll)(a))#define printbk(a) printf("%lld ",(ll)(a))using namespace std;const int MAXN = 5e5+11;typedef long long ll;ll read(){ ll x=0,f=1;register char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int to[MAXN<<1],nxt[MAXN<<1],head[MAXN],tot;int CLOCK,st[MAXN],ed[MAXN],n,m;char str[MAXN];vector
save[MAXN][27];void init(){ memset(head,-1,sizeof head); tot=CLOCK=0;}void add(int u,int v){ to[tot]=v; nxt[tot]=head[u]; head[u]=tot++; swap(u,v); to[tot]=v; nxt[tot]=head[u]; head[u]=tot++;}void dfs(int u,int p,int d){ st[u]=++CLOCK; save[d][str[u]-'a'].push_back(st[u]); erep(i,u){ int v=to[i]; if(v==p)continue; dfs(v,u,d+1); } ed[u]=CLOCK;}int main(){ while(cin>>n>>m){ init(); rep(i,2,n) add(i,read()); scanf("%s",str+1); memset(save,0,sizeof save); dfs(1,-1,1); rep(i,1,m){ int u=read(); int d=read(); bool flag=0,fflag=0; rep(j,0,25){ vector
::iterator it1=lower_bound(save[d][j].begin(),save[d][j].end(),st[u]); vector
::iterator it2=upper_bound(save[d][j].begin(),save[d][j].end(),ed[u]); int cha=it2-it1; if((cha&1)&&!flag) flag=1; else if((cha&1)&&flag){ fflag=1;break; } } if(fflag) printf("No\n"); else printf("Yes\n"); } } return 0;}

转载于:https://www.cnblogs.com/caturra/p/8986811.html

你可能感兴趣的文章
Hook基本知识
查看>>
Hook CreateProcess
查看>>
C#与C++之间类型的对应
查看>>
(C++C#类型互转工具)使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明...
查看>>
面试题
查看>>
Powershell + HTA
查看>>
IFG以太网帧间隙
查看>>
WINDOWS API 大全(一)
查看>>
Wmic
查看>>
WINDOWS API 大全(二)
查看>>
SetWindowsHookEx失败
查看>>
C/C++判断字符串是否包含某个字符串
查看>>
[C#菜鸟]C# Hook
查看>>
easyhook源码分析二——注入
查看>>
C#调用C++的库 P/Invoke工具集
查看>>
easyhook源码分析三——申请钩子
查看>>
easyhook源码分析一
查看>>
对“XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们...
查看>>
VC中MessageBox与AfxMessageBox用法与区别
查看>>
Web开发使用jsTree实例
查看>>