https://blog.csdn.net/hpu2022/article/details/81910490

https://blog.csdn.net/qq_39670434/article/details/78425125

https://www.cnblogs.com/gj-Acit/p/3236843.html

https://blog.csdn.net/iamzky/article/details/18923587

void dfs(int u) {
in[u] = ans;
for(int i = ; i < edge[u].size(); i++) {
ans++;
dfs(edge[u][i]);
}
out[u] = ans;
}
https://vjudge.net/contest/230809#status/xiayuyang/I/0/

https://cn.vjudge.net/contest/209473#problem/D

该题给出前n个节点有多少个子节点,然后要很多次查询,看是否为父子关系。

用dfs序做,如果in[x] < in[y] && out[x] > out[y] 则x是y的父节点。用栈模拟来标记每个节点的in于out值。

https://www.cnblogs.com/zyb993963526/p/7301444.html

https://www.cnblogs.com/L-Excalibur/p/8511527.html#include<iostream>

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
int start[maxn];
int c[maxn];
int in[maxn];
int out[maxn]; void dfs(int u)
{
stack<int> S;
int dfs_clock=;
in[u]=;
S.push(u);
while(!S.empty())
{
int x=S.top();
if(in[x])
{
out[x]=++dfs_clock;
S.pop();
continue;
//out值,标记玩就要pop该点。
}
in[x]=++dfs_clock;
for(int i=start[x];i<start[x]+c[x];i++)
{
if(i<n) {in[i]=;S.push(i);}
else {in[i]=++dfs_clock;out[i]=++dfs_clock;}
//如果小于n则把该点push进去,相当于bfs,然后标记in值,如果大于n则进去就出来所以可以直接标号
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int T; int kase=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int s=;
for(int i=;i<n;i++)
{
scanf("%d",&c[i]);
start[i]=s; s+=c[i];
}
dfs();
printf("Case %d:\n",++kase);
int q;
scanf("%d",&q);
while(q--)
{
int u,v;
scanf("%d%d",&u,&v);
if(in[u]<in[v] && out[u]>in[v]) puts("Yes");
else puts("No");
}
if(T) puts("");
}
return ;
}
/* by Lstg */
/* 2018-03-05 21:02:03 */ #include<stdio.h>
#include<iostream>
#include<queue>
#define MAXN 20000005
using namespace std; int dfn[MAXN],dfm[MAXN],stk[MAXN],n;
queue<int>g[];//这里太大就会RE void _mydfs(int x){ int top=,num=,y;
dfn[x]=num++;
stk[++top]=x;
while(top){
x=stk[top];
if(x>n||g[x].empty()){/*先判断x的大小防RE和WA(有时溢出不会告诉你是RE,而会返回WA)*/
dfm[x]=num++;
top--;
}
else{
y=g[x].front();
g[x].pop();
dfn[y]=num++;
stk[++top]=y;
}
}
} int main(){ int T,tot,i,x,y;
scanf("%d",&T);
for(int cc=;cc<=T;cc++){
scanf("%d",&n);
tot=; for(i=;i<n;i++){
while(!g[i].empty())g[i].pop();
scanf("%d",&x);
while(x--)
g[i].push(++tot);
}
_mydfs();
printf("Case %d:\n",cc);
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d%d",&x,&y);
if(dfn[x]<dfn[y]&&dfm[x]>dfm[y])
puts("Yes");
else puts("No");
}
if(cc!=T)putchar();
}
return ;
}

https://vjudge.net/contest/218179#problem/Q 求子树大小(染色的)

https://blog.csdn.net/baidu_19306071/article/details/52005557

#include <bits/stdc++.h>

using namespace std;
const int maxm = 2e5 + ;
const int maxn = 2e5 + ;
typedef long long ll;
struct Edges{
int u,v;
}edge[maxm];
vector<int> G[maxn]; int cnt[maxn];
int dfs(int cur,int from){
for(int i = ; i < G[cur].size() ; i ++){
int v = G[cur][i];
if(v == from) continue;
cnt[cur] += dfs(v,cur);
}
return cnt[cur];
} int main (){
int n,k;
scanf("%d %d", &n,&k);
for(int i = ; i <= k* ; i ++){
int tmp;
scanf("%d", &tmp);
cnt[tmp] = ;
}
int cn = ;
for(int i = ; i < n- ; i ++){
int u,v;
scanf("%d %d", &u,&v);
edge[i].u = u;
edge[i].v = v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(,-);
ll exp = ;
for(int i = ; i < n- ; i ++){
Edges & e = edge[i];
int tt = min(cnt[e.u],cnt[e.v]);
int tmp = min(*k-tt,tt);
exp += tmp;
}
printf("%lld\n",exp);
}

dfs序与求子树子节点(染了色)的个数的更多相关文章

  1. dfs序+RMQ求LCA详解

    首先安利自己倍增求LCA的博客,前置(算不上)知识在此. LCA有3种求法:倍增求lca(上面qwq),树链剖分求lca(什么时候会了树链剖分再说.),还有,标题. 是的你也来和我一起学习这个了qwq ...

  2. Codeforces - 570D 离散DFS序 特殊的子树统计 (暴力出奇迹)

    题意:给定一棵树,树上每个节点有对应的字符,多次询问在\(u\)子树的深度为\(d\)的所有节点上的字符任意组合能否凑成一个回文串 把dfs序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度 ...

  3. C++求树子节点权重最大的和

    #include <iostream> #include <vector> using namespace std; int n; const int MaxN = 1e5; ...

  4. bzoj2434 fail树 + dfs序 + 树状数组

    https://www.lydsy.com/JudgeOnline/problem.php?id=2434 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...

  5. DFS序和7种模型

    DFS序就是将树的节点按照先根的顺序遍历得到的节点顺序 性质:一个子树全在一个连续的区间内,可以与线段树和树状数组搭配使用 很好写,只需在dfs中加几行代码即可. 代码: void dfs(ll u, ...

  6. 【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1146 第一种做法(时间太感人): 第二种做法(rank5,好开心) ================ ...

  7. BZOJ.2434.[NOI2011]阿狸的打字机(AC自动机 树状数组 DFS序)

    题目链接 首先不需要存储每个字符串,可以将所有输入的字符依次存进Trie树,对于每个'P',记录该串结束的位置在哪,以及当前节点对应的是第几个串(当前串即根节点到当前节点):对于'B',只需向上跳一个 ...

  8. dfs序七个经典问题

    update-2018.07.23: 原文问题五思路描述有误,已更正. 参考自:<数据结构漫谈>-许昊然 dfs序是树在dfs先序遍历时的序列,将树形结构转化成序列问题处理. dfs有一个 ...

  9. 【转载】dfs序七个经典问题

    作者:weeping 出处:www.cnblogs.com/weeping/ 原文链接 https://www.cnblogs.com/weeping/p/6847112.html 参考自:<数 ...

随机推荐

  1. 利用Marshal来管理非托管资源

    void MarshalChartDemo() { string name = "xuwei"; IntPtr pName = Marshal.AllocHGlobal(name. ...

  2. git push出错的解决办法

    今天push代码到线上的时候怎么都不行,尝试了很多办法报了好几种错比如: 反正就是各种错,然后其实不管什么错,你全部Git init 一下然后重新配置 git config --global user ...

  3. ubuntu apt

    一些命令: sudo apt-get update  更新源 sudo apt-get install package --reinstall  重新安装包 sudo apt-get upgrade ...

  4. Grafana展示zabbix监控数据

    一.安装步骤 (1)进入官网选择合适的操作系统版本下载Grafana:https://grafana.com/grafana/download?platform=linux [root@zabbix- ...

  5. apache配置跨域请求代理

    1.配置允许跨域请求 Header always set Access-Control-Allow-Origin "*"Header always set Access-Contr ...

  6. ASP.NET Razor 语法

    主要的 Razor C# 语法规则 Razor 代码块包含在 @{ ... } 中 内联表达式(变量和函数)以 @ 开头 代码语句用分号结束 变量使用 var 关键字声明 字符串用引号括起来 C# 代 ...

  7. 大数据-HBase

    HBase HBase(Hadoop Database)基于Google的BigTable论文,依赖HDFS进行存储.适合存储大体量数据.HBase是高可靠性(数据安全).高性能(存取效率).面向列. ...

  8. C++的多态总结(静态&动态)

    什么是多态 顾名思义就是同一个事物在不同场景下的多种形态. 静态多态 我们以前说过的函数重载就是一个简单的静态多态,静态多态是编译器在编译期间完成的,编译器会根据实参类型来选择调用合适的函数,如果有合 ...

  9. rancher三节点k8s集群部署例子

    rancher三节点k8s集群部署例子 待办 https://rorschachchan.github.io/2019/07/25/使用Rancher2-1部署k8s/

  10. 解决报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: TLS handshaketimeout

    报错: [root@localhost /]# sudo docker pull ubuntuError response from daemon: Get https://registry-1.do ...