[codeforces246E]Blood Cousins Return(dsu on the tree+STL)
来源:互联网 发布:java volatile atomic 编辑:程序博客网 时间:2024/05/29 01:56
题目:
我是超链接
题意:
一棵树,每一个点有一个颜色(字符串),每一次询问以某一个点为根的子树中与其距离为k的点有多少种颜色。
题解:
哇,相比裸题来说,这道题把颜色换成字符串了呢,运用set好了(其实很想知道不用set怎么办
读入人名的话一波map
代码:
#include <cstdio>#include <map>#include <set>#include <iostream>#define N 100005#define sz 19 using namespace std;map<string,int>hash;set<int>cnt[N];int tot,nxt[N*2],point[N],v[N*2],tot1,nxt1[N*2],point1[N],v1[N*2],id[N];int ans[N],h[N],size[N],son[N],Son,a[N],num,n;bool vis[N];void addline(int x,int y){++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;}void addline1(int x,int y,int i){++tot1; nxt1[tot1]=point1[x]; point1[x]=tot1; v1[tot1]=y; id[tot1]=i;}void getson(int x,int fa){ size[x]=1;h[x]=h[fa]+1; for (int i=point[x];i;i=nxt[i]) { getson(v[i],x); if (size[v[i]]>size[son[x]]) son[x]=v[i]; size[x]+=size[v[i]]; }}void add(int x,int vv){ if (vv==1) cnt[h[x]].insert(a[x]); else cnt[h[x]].clear(); for (int i=point[x];i;i=nxt[i]) if (v[i]!=Son) add(v[i],vv);}void dfs(int x,int k){ vis[x]=1; for (int i=point[x];i;i=nxt[i]) if (v[i]!=son[x]) dfs(v[i],0); if (son[x]) dfs(son[x],1),Son=son[x]; add(x,1); Son=0; for (int i=point1[x];i;i=nxt1[i]) { if (v1[i]>n) continue; ans[id[i]]=cnt[v1[i]].size(); } if (!k) add(x,-1);}int main(){ int i,m; scanf("%d",&n); for (i=1;i<=n;i++) { int fa;string s; cin>>s; if (!hash[s]) hash[s]=++num,a[i]=num; else a[i]=hash[s]; scanf("%d",&fa); if (fa) addline(fa,i); } for (i=1;i<=n;i++) if (!size[i]) getson(i,0); scanf("%d",&m); for (i=1;i<=m;i++) { int vv,hh; scanf("%d%d",&vv,&hh); addline1(vv,hh+h[vv],i); } for (i=1;i<=n;i++) if (!vis[i]) dfs(i,0); for (i=1;i<=m;i++) printf("%d\n",ans[i]);}
阅读全文
0 0
- [codeforces246E]Blood Cousins Return(dsu on the tree+STL)
- [Codeforces246E]Blood Cousins Return(dsu on the tree+set)
- codeforces 246 E. Blood Cousins Return (set+dsu on the tree)
- codeforces 208 E. Blood Cousins (dsu on the tree)
- [Codeforces208E]Blood Cousins(dsu on the tree+倍增)
- [codeforces208E]Blood Cousins(dsu on the tree+倍增)
- codeforces 570 D. Tree Requests (dsu on the tree)
- [Codeforces570D]Tree Requests(dsu on the tree)
- [Codeforces375D]Tree and Queries(dsu on the tree+bit)
- [codeforces570D]Tree Requests(dsu on the tree)
- [codeforces375D]Tree and Queries(dsu on the tree+bit)
- cf 151.div2 E Blood Cousins Return
- Codeforces 246E Blood Cousins Return
- codeforces 600 E. Lomsat gelral (dsu on the tree)
- [Codeforces600E]Lomsat gelral(dsu on the tree)
- [codeforces600E]Lomsat gelral(dsu on the tree+讲解)
- dsu on the tree 学习笔记
- Codeforces 246E Blood Cousins Return 树上启发式合并
- Redis is configured to save RDB snapshots
- 习题7-10:守卫棋盘(dfs迭代加深)
- 百度地图的使用
- Numpy学习笔记
- 设计一个栈实现最大值函数
- [codeforces246E]Blood Cousins Return(dsu on the tree+STL)
- Fresco图片加载框架
- 初识construct 2
- LauncherRootView和DragLayer的布局过程
- 前端工程化
- SpringMvc Controller介绍和常用注解
- java AWT菜单
- (UART 与 USART有什么区别)&(SPI、I2C、UART三种串行总线协议的区别)
- 初探SpringBoot