cf1009F. Dominant Indices

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf1009F. Dominant Indices相关的知识,希望对你有一定的参考价值。

cf1009F. Dominant Indices

题意:

1号节点为根,问对于以每个点为根的子树种,求某个深度节点最多的层数。如果多个相等,输出深度小的。

题解:

直接dsu就完事了,相当于是求子树的众数,但是注意常数,一开始因为常数过大在第100点wa了
将可以合并的dfs套在一起写,减少常数
好像线段树合并也可以做,但我还不会emm

代码:

// Problem: F. Dominant Indices
// Contest: Codeforces - Educational Codeforces Round 47 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1009/problem/F
// Memory Limit: 512 MB
// Time Limit: 4500 ms
// Data:2021-09-03 11:43:05
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
int n;
const int maxn= 2e6 + 9;
vector<int> vec[maxn];
int dep[maxn];
int siz[maxn];
int son[maxn];
int Son;
void dfs(int u, int fa)
{
    dep[u]= dep[fa] + 1;
    siz[u]= 1;
    for (auto v : vec[u]) {
        if (v == fa)
            continue;
        dfs(v, u);
        siz[u]+= siz[v];
        if (siz[v] > siz[son[u]])
            son[u]= v;
    }
}
int ans[maxn];
int num[maxn];
int Ans, Num;
void cal(int u, int fa, int val)
{
    num[dep[u]]+= val;
    if (val == 1) {
        if (num[dep[u]] > Num || (num[dep[u]] == Num && dep[u] < Ans)) {
            Ans= dep[u];
            Num= num[dep[u]];
        }
    }

    for (auto v : vec[u]) {
        if (v == fa || v == Son)
            continue;
        cal(v, u, val);
    }
}
// PII solve(int u, int fa, int rt)
// {
// PII maxx;
// maxx.first= num[dep[u]];
// maxx.second= dep[u] - dep[rt];
// for (auto v : vec[u]) {
// if (v == fa)
// continue;
// PII ans= solve(v, u, rt);
// if (ans.first > maxx.first) {
// maxx.first= ans.first;
// maxx.second= ans.second;
// }
// if (ans.first == maxx.first) {
// maxx.second= min(maxx.second, ans.second);
// }
// }
// return maxx;
// }
void dfs2(int u, int fa, int keep)
{
    for (auto v : vec[u]) {
        if (v == fa || v == son[u])
            continue;
        dfs2(v, u, 0);
    }
    if (son[u]) {
        dfs2(son[u], u, 1);
        Son= son[u];
    }
    cal(u, fa, 1);
    // PII p= solve(u, fa, u);
    ans[u]= Ans - dep[u];
    Son= 0;
    if (!keep) {
        cal(u, fa, -1);
        Num= 0;
        Ans= 0;
    }
}
int main()
{
    //rd_test();
    read(n);
    for (int i= 1; i < n; i++) {
        int x, y;
        read(x, y);
        vec[x].push_back(y);
        vec[y].push_back(x);
    }
    dfs(1, 0);
    dfs2(1, 0, 0);
    for (int i= 1; i <= n; i++)
        printf("%d\\n", ans[i]);
    //Time_test();
}

以上是关于cf1009F. Dominant Indices的主要内容,如果未能解决你的问题,请参考以下文章

CF1009F Dominant Indices 解题报告

CF1009F Dominant Indices [长链剖分]

CF1009F Dominant Indices [线段树合并]

F. Dominant Indices

Codeforces ECR47F Dominant Indices(线段树合并)

Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并