HDU 4858 project management chart governance (Composite Algorithm)

Source: Internet
Author: User

HDU 4858 Project Management


Question: give n (<= 100000) vertices and m edges (<= N + 10). There may be duplicate edges. Each vertex has a value of Val, which is first recognized as 0.

Two operations.

Operation 1: Add addx to the value of vertex x.

Operation 2: output the Val sum of the neighboring points of X.


Analysis: simple optimization operations 1 or 2 won't work.

Method 1: vertices in the graph are divided into two types of vertices. For vertices whose degree is greater than SQRT (N), for vertices whose degree is less than or equal to SQRT (N), it is light. The number of key points is less than SQRT (n. Focuses and light points are handled separately.

Method 2: You can also classify neighboring points for each vertex. Vertices greater than this degree are classified into one type, which is equal to the degree of this point. vertices smaller than this point are classified into one type. Process them separately.

Method 1:

const int maxn = 100100;vector<int>v[maxn], vgt[maxn];int d[maxn];int val[maxn];int sum[maxn];int n, m;const int MM = 3333;int main (){    int T;    cin >> T;    while (T--)    {        scanf("%d%d", &n, &m);        memset(d, 0, sizeof(d));        memset(val, 0, sizeof(val));        memset(sum, 0, sizeof(sum));        for (int i = 1; i <= n; i++)        {            v[i].clear(); vgt[i].clear();        }        for (int i = 0; i < m; i++)        {            int x, y;            scanf("%d%d", &x, &y);            v[x].push_back(y); v[y].push_back(x);            d[x]++; d[y]++;        }        for (int i = 1; i <= n; i++)        {            for (int j = 0; j < v[i].size(); j++)            {                int r = v[i][j];                if (d[r] > MM) vgt[i].push_back(r);            }        }        int Q;        cin >> Q;        while (Q--)        {            int op;            int x, addx;            scanf("%d", &op);            if (!op)            {                scanf("%d%d", &x, &addx);                val[x] += addx;                if (d[x] <= MM)                {                    for (int i = 0; i < vgt[x].size(); i++)                    {                        int y = vgt[x][i];                        sum[y] += addx;                    }                }            }            else            {                scanf("%d", &x);                int ans = 0;                if (d[x] <= MM)                {                    for (int i = 0; i < v[x].size(); i++)                    {                        ans += val[v[x][i]];                    }                }                else                {                    ans += sum[x];                    for (int i = 0; i < vgt[x].size(); i++)                    {                        int y = vgt[x][i];                        ans += val[y];                    }                }                printf("%d\n", ans);            }        }    }    return 0;}/**43 21 21 360 1 150 3 41 11 30 2 331 23 21 21 260 1 151 10 2 151 11 3*/


Method 2:
const int maxn = 100100;vector<int>v[maxn], vgt[maxn], veq[maxn];int d[maxn];int val[maxn];int sum[maxn];int n, m;const int MM = 3333;int main (){    int T;    cin >> T;    while (T--)    {        scanf("%d%d", &n, &m);        memset(d, 0, sizeof(d));        memset(val, 0, sizeof(val));        memset(sum, 0, sizeof(sum));        for (int i = 1; i <= n; i++)        {            v[i].clear(); vgt[i].clear(); veq[i].clear();        }        for (int i = 0; i < m; i++)        {            int x, y;            scanf("%d%d", &x, &y);            v[x].push_back(y); v[y].push_back(x);            d[x]++; d[y]++;        }        for (int i = 1; i <= n; i++)        {            for (int j = 0; j < v[i].size(); j++)            {                int r = v[i][j];                if (d[r] > d[i]) vgt[i].push_back(r);                else if (d[r] == d[i]) veq[i].push_back(r);            }        }        int Q;        cin >> Q;        while (Q--)        {            int op;            int x, addx;            scanf("%d", &op);            if (!op)            {                scanf("%d%d", &x, &addx);                val[x] += addx;                for (int i = 0; i < vgt[x].size(); i++)                    sum[vgt[x][i]] += addx;            }            else            {                scanf("%d", &x);                int ans = sum[x];                for (int i = 0; i < vgt[x].size(); i++)                    ans += val[vgt[x][i]];                for (int i = 0; i < veq[x].size(); i++)                    ans += val[veq[x][i]];                printf("%d\n", ans);            }        }    }    return 0;}/**43 21 21 360 1 150 3 41 11 30 2 331 23 21 21 260 1 151 10 2 151 11 3*/


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.