Bzoj 1040 server guard

Source: Internet
Author: User

This question is really uncomfortable. It's really uncomfortable to write a question about Wa and the browser is down.

So if we don't talk about it much, We can't judge whether it is a parent node. In this case, we can only use numbers to determine whether it is a parent node, (K ^ 1 )! = Fa.

You need to pay attention to a lot of details, ah!

Code:

#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <queue>#include <stack>#define N 1000100using namespace std;int n, power[N];int p[N] = {0}, next[N*2], v[N*2], bnum = -1;long long f1[N][2] = {0}, f2[N][2] = {0};void addbian(int x, int y){    bnum++; next[bnum] = p[x]; p[x] = bnum; v[bnum] = y;    bnum++; next[bnum] = p[y]; p[y] = bnum; v[bnum] = x;}int root, ban;int cannot;bool vis[N] = {0};void dfs(int now, int fa){    int k = p[now];    while (k != -1)    {        if ((k^1) != fa && v[k] != ban)        {            if (vis[v[k]])            {                root = v[k];                ban = now;                cannot = k^1;            }            else            {                vis[v[k]] = 1;                dfs(v[k], k);            }        }        k = next[k];    }}void makeans_root(int now, int fa){    int k = p[now];    if (now == ban)    {        while (k != -1)        {            if ((k^1) != fa && v[k] != root)            {                makeans_root(v[k], k);                f1[now][0] += max(f1[v[k]][0], f1[v[k]][1]);                f1[now][1] += f1[v[k]][0];            }            k = next[k];        }        return;    }    f1[now][1] = power[now];    while (k != -1)    {        if ((k^1) != fa && k != cannot)        {            makeans_root(v[k], k);            f1[now][0] += max(f1[v[k]][0], f1[v[k]][1]);            f1[now][1] += f1[v[k]][0];        }        k = next[k];    }    return;}void makeans_ban(int now, int fa){    int k = p[now];    if (now == ban)    {        f2[now][1] = power[now];        while (k != -1)        {            if ((k^1) != fa && v[k] != root)            {                makeans_ban(v[k], k);                f2[now][0] += max(f2[v[k]][0], f2[v[k]][1]);                f2[now][1] += f2[v[k]][0];            }            k = next[k];        }        return;    }    f2[now][1] = power[now];    while (k != -1)    {        if ((k^1) != fa && k != cannot)        {            makeans_ban(v[k], k);            f2[now][0] += max(f2[v[k]][0], f2[v[k]][1]);            f2[now][1] += f2[v[k]][0];        }        k = next[k];    }    return;}int main(){    scanf("%d", &n);    for (int i = 1; i <= n; ++i) p[i] = -1;    for (int i = 1; i <= n; ++i)    {        int x; scanf("%d%d", &power[i], &x);        addbian(x, i);    }    long long ans = 0;    for (int i = 1; i <= n; ++i)        if (!vis[i])        {            root = -1; ban = -1;            vis[i] = 1; dfs(i, -1);            makeans_root(root, -1);            makeans_ban(root, -1);            long long zans = 0;            zans = max(f1[root][1], zans);            zans = max(zans, f1[root][0]);            zans = max(f2[root][0], zans);            ans += zans;        }    printf("%lld\n", ans);}

 

Bzoj 1040 server guard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.