hdu 4695 Important Sisters

Posted AωD!

tags:

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

支配树模版题~

这东西似乎非常好写……

但是证明好长好长啊~

$$sdom(u) = min\{\{u|(v, u) \in E, dfn(v) < dfn(u)\}\cup\{sdom(q)|dfn(q)>dfn(u), \exists  (p, x) \in E, q \in path(S, p)\}\}$$

前一半是走前向边或者树边,后一半是走横叉边或者返祖边

$$idom(u) = \begin{cases}sdom(u)& \text{ if } sdom(v)= sdom(u)\\ idom(v) & \text{ if } sdom(v)<sdom(u) \end{cases}$$

其中\(v\)代表\((sdom(u), u]\)之中\(sdom\)最小的那一个点。

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 200000;
int n, m;
int dfn[N], lst[N], id[N], sd[N], paid[N], mns[N];
int anc[N], par[N], tot;
vector <int> bi[N], bp[N], bb[N];
LL sum[N];
void dfs(int t)
{
    dfn[t] = ++ tot; lst[tot] = t;
    for (int i = 0; i < bi[t].size(); ++ i)
        if (!dfn[bi[t][i]])
            dfs(bi[t][i]), par[bi[t][i]] = t;
}
int check_min(int a, int b)
{
    if (dfn[a] < dfn[b]) return a; else return b;
}
int find_anc(int t)
{
    if (anc[t] == t) return t;
    else
    {
        int a = find_anc(anc[t]);
        if (a != anc[t] && dfn[sd[mns[anc[t]]]] < dfn[sd[mns[t]]])
            mns[t] = mns[anc[t]];
        return anc[t] = a;
    }
}
int main()
{
    while (scanf("%d%d", &n, &m) == 2)
    {
        for (int i = 0; i <= n; ++ i)
            bi[i].clear(), bp[i].clear(), bb[i].clear();
        for (int i = 0; i <= n; ++ i)
            dfn[i] = lst[i] = par[i] = id[i] = sd[i] = paid[i] = sum[i] = 0;
        for (int i = 1; i <= n; ++ i) anc[i] = mns[i] = i;
        tot = 0;
        for (int i = 1; i <= m; ++ i)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            bi[a].push_back(b);
            bp[b].push_back(a);
        }
        dfs(n);
        for (int i = n; ; -- i)
        {
            for (int j = 0; j < bb[lst[i]].size(); ++ j)
            {
                find_anc(bb[lst[i]][j]);
                if (sd[mns[bb[lst[i]][j]]] == sd[bb[lst[i]][j]])
                    id[bb[lst[i]][j]] = sd[bb[lst[i]][j]];
                else
                    paid[bb[lst[i]][j]] = mns[bb[lst[i]][j]];
            }
            if (i == 1) break;
            sd[lst[i]] = par[lst[i]];
            for (int j = 0; j < bp[lst[i]].size(); ++ j) if (dfn[bp[lst[i]][j]])
            {
                if (dfn[bp[lst[i]][j]] < i)
                    sd[lst[i]] = check_min(sd[lst[i]], bp[lst[i]][j]);
                else
                {
                    find_anc(bp[lst[i]][j]);
                    sd[lst[i]] = check_min(sd[lst[i]], sd[mns[bp[lst[i]][j]]]);
                }
            }
            mns[lst[i]] = lst[i];
            anc[lst[i]] = par[lst[i]];
            bb[sd[lst[i]]].push_back(lst[i]);
        }
        for (int i = 1; i <= n; ++ i) if (paid[lst[i]])
            id[lst[i]] = id[paid[lst[i]]];
        // for (int i = 1; i <= n; ++ i)
            // printf("%d\n", id[i]);
        for (int i = 1; i <= n; ++ i)
            sum[lst[i]] = sum[id[lst[i]]] + lst[i];
        for (int i = 1; i <= n; ++ i)
            printf("%lld%c", sum[i], (i == n? \n:  ));
    }
}

 

以上是关于hdu 4695 Important Sisters的主要内容,如果未能解决你的问题,请参考以下文章

HDU.4694.Important Sisters(支配树)

bzoj4695 最假女选手

poj 3307 Smart Sister 打表解因子生成数问题

尝试通过 ASP.NET 中的命令文本在 SQL Server 2014 中插入“Rahul's Sister”时出错 [重复]

与同一模型相关联

母函数的一些简单想法(HDU2110)