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. 题解【洛谷P2003】平板

    题面 由于本题中\(n\)很小,\(\Theta(n^2)\)的暴力也可以通过. 具体可参照洛谷题解区 #include <bits/stdc++.h> #define itn int # ...

  2. 【Python】天天向上的力量

    每天进步一点点会有什么不同呢? Q1:每天进步千分之一或退步千分之一会怎么样? #DayDayUp,每天进步一千分之一 print("基础为1") dayup=pow(1.001, ...

  3. Java基本语法--关键字&标识符

    本篇博客主要介绍了Java基本语法中的关键字.保留字和标识符. 关键字与保留字 关键字(keyword的定义及特点) ✄ 定义:被Java 语言赋予了特殊含义,用做专门用途的字符串 ✄ 特点:关键字中 ...

  4. 剑指offer 面试题. 按之字形顺序打印二叉树

    题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推.   方法1: 正常层次遍历,利用普通队列.逢 ...

  5. 从bbs.3dmgame.com与qq的登录解析oauth2.0协议

    点击3dm上的qq图标,浏览器跳转到,地址为: https://graph.qq.com/oauth2.0/show ?which=Login &display=pc &respons ...

  6. php多版本使用composer

    适用多版本的方法 1:下载composer.phar,官网有直接下载的链接,https://getcomposer.org/download/ 2:composer.phar 复制到项目根目录 3:p ...

  7. 在Linux系统上安装Git

    Git是目前流行的非常好用的版本控制工具,这里介绍两种安装方式,1.yum安装,2.从github上下载最新的源码编译后安装 一.yum安装 1.在Linux上是有yum安装Git,非常简单,只需要一 ...

  8. Axure 8.1.0.3377 激活码 授权码 (亲测有效)

    适用版本 Axure 8.1.0.3377 zdfans.com gP5uuK2gH+iIVO3YFZwoKyxAdHpXRGNnZWN8Obntqv7++FF3pAz7dTu8B61ySxli 亲测 ...

  9. 用户 'sa' 登录失败。该用户与可信 SQL Server 连接无关联'。错误代码:18452 解决办法

    原文:https://blog.csdn.net/wuxianwei/article/details/6330270 SQLSERVER 2005采用'SQLSERVER身份验证'去登录, 出错的原因 ...

  10. Python爬取微博热搜以及链接

    基本操作,不再详述 直接贴源码(根据当前时间创建文件): import requests from bs4 import BeautifulSoup import time def input_to_ ...