首页 > 其他分享 >Tree and Queries Dsu on tree

Tree and Queries Dsu on tree

时间:2023-01-09 15:56:34浏览次数:45  
标签:sz cnt int tree Dsu Tree son mp col

//题意:给出一棵树,树上每个点有一种颜色,给出多个询问,求以a为根的子树上,染色点数超过k的颜色有多少种
//思路:很明显可以dsu on tree,在写的时候要注意统计答案和清零的复杂度,第一个版本利用二分统计答案,复杂度达到O(nlogn*logn)会被卡掉,
//      再加上memset的O(n)复杂度和大常数更寄,所有要注意优化
//
/*#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, col[N], ans[N];
vector<int> mp[N];
vector<pair<int, int>> qus[N];//第一维k,第二维问题编号
int sz[N], fa[N], son[N], color, tim;
void dfs1(int x, int f) {
    sz[x] = 1;
    fa[x] = f;
    for (auto y : mp[x]) {
        if (y == f) continue;
        dfs1(y, x);
        sz[x] += sz[y];
        if (sz[y] > sz[son[x]]) son[x] = y;
    }
}
int cnt[N];
void dfs2(int x) {
    cnt[col[x]]++;
    for (auto y : mp[x]) {
        if (y == fa[x]) continue;
        dfs2(y);
    }
}
int temp[N];
void solve(int x, bool kep) {
    for (auto y : mp[x]) {
        if (y == fa[x] || y == son[x]) continue;
        solve(y, 0);
    }
    if (son[x]) solve(son[x], 1);
    
    for (auto y : mp[x]) {
        if (y == fa[x] || y == son[x]) continue;
        dfs2(y);
    }
    cnt[col[x]]++;

    for (int i = 1; i <= color; i++) temp[i] = cnt[i];

    sort(temp, temp + 1 + color);
    for (auto y : qus[x]) {
        int len = lower_bound(temp, temp + color + 1, y.first) - temp;
        ans[y.second] = color - len + 1;
    }

    if (!kep) memset(cnt, 0, sizeof(cnt));
    memset(temp, 0, sizeof(temp));
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> col[i]; 
        color = max(color, col[i]);
    }
    for (int i = 1; i <= n - 1; i++) {
        int a, b; cin >> a >> b;
        mp[a].push_back(b);
        mp[b].push_back(a);
    }
    for (int i = 1; i <= m; i++) {
        int a, b; cin >> a >> b;
        qus[a].push_back({ b,i });
    }
    dfs1(1, 0);
    solve(1, 0);
    for (int i = 1; i <= m; i++) cout << ans[i] << endl;
    return 0;
}*/


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, col[N], ans[N];
struct edge {
    int to, nt;
}mp[2 * N];
vector<pair<int, int>> qus[N];//第一维k,第二维问题编号
int sz[N], fa[N], son[N], head[N], num;
void add(int a, int b) {
    mp[++num].to = b;
    mp[num].nt = head[a];
    head[a] = num;
}

void dfs1(int x, int f) {
    sz[x] = 1;
    fa[x] = f;
    for (auto w = head[x]; w; w = mp[w].nt) {
        int y = mp[w].to;
        if (y == f) continue;
        dfs1(y, x);
        sz[x] += sz[y];
        if (sz[y] > sz[son[x]]) son[x] = y;
    }
}
int cnt[N], stk[N];
void dfs2(int x, bool fg) {
    if (fg) {
        cnt[col[x]]++;
        stk[cnt[col[x]]]++;
    }
    else {
        stk[cnt[col[x]]]--;
        cnt[col[x]]--;
    }
    for (auto w = head[x]; w; w = mp[w].nt) {
        int y = mp[w].to;
        if (y == fa[x]) continue;
        dfs2(y, fg);
    }
}
void solve(int x, bool kep) {
    for (auto w = head[x]; w; w = mp[w].nt) {
        int y = mp[w].to;
        if (y == fa[x] || y == son[x]) continue;
        solve(y, 0);
    }
    if (son[x]) solve(son[x], 1);

    for (auto w = head[x]; w; w = mp[w].nt) {
        int y = mp[w].to;
        if (y == fa[x] || y == son[x]) continue;
        dfs2(y, 1);
    }
    cnt[col[x]]++;
    stk[cnt[col[x]]]++;

    for (auto y : qus[x]) ans[y.second] = stk[y.first];

    if (!kep) {
        for (auto w = head[x]; w; w = mp[w].nt) {
            int y = mp[w].to;
            if (y == fa[x]) continue;
            dfs2(y, 0);
        }
        stk[cnt[col[x]]]--;
        cnt[col[x]]--;
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> col[i];
    for (int i = 1; i <= n - 1; i++) {
        int a, b; cin >> a >> b;
        add(a, b);
        add(b, a);
    }
    for (int i = 1; i <= m; i++) {
        int a, b; cin >> a >> b;
        qus[a].push_back({ b,i });
    }
    dfs1(1, 0);
    solve(1, 0);
    for (int i = 1; i <= m; i++) cout << ans[i] << endl;
    return 0;
}

 

标签:sz,cnt,int,tree,Dsu,Tree,son,mp,col
From: https://www.cnblogs.com/Aacaod/p/17037267.html

相关文章

  • git代码仓sourcetree忽略.xcuserstate或.xcscheme等文件设置
    首先编辑.gitignore文件如下然后将我们想忽略的文件加上然后终端输入如下代码cd项目路径gitrm-r--cached.gitadd.gitcommit-m''"之后重启source......
  • 1020 Tree Traversals (25)
    Supposethatallthekeysinabinarytreearedistinctpositiveintegers.Giventhepostorderandinordertraversalsequences,youaresupposedtooutputthe......
  • re | [MRCTF2020]VirtualTree
    re|[MRCTF2020]VirtualTree这个题是一个错题,是有多解的。原因是使用了abs函数考察了二叉树后序遍历,和一点基本花指令,还有一点点smc的内容。直接丢exp了:#include<s......
  • 【学习笔记】动态树 Link-Cut Tree
    -闲话LCT优秀博客:FlashHu大佬的cnblogs:https://www.cnblogs.com/flashhu/p/8324551.html-动态树Link-CutTree-前置知识「必学」Splay。「重要」树链剖分......
  • 二叉树 LC104.MaxDepth of Binary Tree
    最近看了labuladong讲二叉树,掌握了一种思路:拿到二叉树题目,思考三个维度——能不能遍历一遍就得出结果?如果可以,配合一个traverse函数+外部变量进行实现。——能不能定义......
  • Codeforces - 1656E - Equal Tree Sums(构造 + 树论 + 图论 + 搜索、结论题、*2200)
    1656E-EqualTreeSums(⇔源地址)目录1656E-EqualTreeSums(⇔源地址)tag题意思路AC代码错误次数:0tag⇔*2200、⇔构造、⇔树论、⇔图论、⇔搜......
  • [ABC264Ex] Perfect Binary Tree 题解
    [ABC264Ex]PerfectBinaryTreeSolution目录[ABC264Ex]PerfectBinaryTreeSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面存在一......
  • Tree 树分治
    //题意:询问一棵树上长度不超过k的简单路径有多少条//思路:貌似可以用dsuontree但好像要用到平衡树之类的,之后再看看//https://tqltqltqlorzorzorz.blog.luogu......
  • Race dsu on tree写法
    //跑不过,不知道为啥,感觉逻辑都很正确了//题意:给出一棵带边权的树,询问一条权值为k的路径经过点的最小值是多少//思路:因为涉及到最小值问题,所以树性dp好像不行(其实暂时......
  • 单细胞 | CNV和SNV(genome + MT)推测lineage tree
     正式进入cancergenomics领域,只不过是从scRNA-seq与scATAC-seq入手。我们的问题是如何从有限的SNV和CNV数据里推测出CRC的lineage的关系。 使用的工具:https://git......