hdu4467 Graph(图的分块)

Graph

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2736    Accepted Submission(s): 443


Problem Description
P. T. Tigris is a student currently studying graph theory. One day, when he was studying hard, GS appeared around the corner shyly and came up with a problem:
Given a graph with n nodes and m undirected weighted edges, every node having one of two colors, namely black (denoted as 0) and white (denoted as 1), you’re to maintain q operations of either kind:
* Change x: Change the color of x th node. A black node should be changed into white one and vice versa.
* Asksum A B: Find the sum of weight of those edges whose two end points are in color A and B respectively. A and B can be either 0 or 1.
P. T. Tigris doesn’t know how to solve this problem, so he turns to you for help.
 

Input
There are several test cases.
For each test case, the first line contains two integers, n and m (1 ≤ n,m ≤ 10 5), where n is the number of nodes and m is the number of edges.
The second line consists of n integers, the i th of which represents the color of the i th node: 0 for black and 1 for white.
The following m lines represent edges. Each line has three integer u, v and w, indicating there is an edge of weight w (1 ≤ w ≤ 2 31 - 1) between u and v (u != v).
The next line contains only one integer q (1 ≤ q ≤ 10 5), the number of operations.
Each of the following q lines describes an operation mentioned before.
Input is terminated by EOF.
 

Output
For each test case, output several lines.
The first line contains “Case X:”, where X is the test case number (starting from 1).
And then, for each “Asksum” query, output one line containing the desired answer.
 

Sample Input
  
  
4 3 0 0 0 0 1 2 1 2 3 2 3 4 3 4 Asksum 0 0 Change 2 Asksum 0 0 Asksum 0 1 4 3 0 1 0 0 1 2 1 2 3 2 3 4 3 4 Asksum 0 0 Change 3 Asksum 0 0 Asksum 0 1
 

Sample Output
  
  
Case 1: 6 3 3 Case 2: 3 0 4
 

Source
 


题目要做的是维护00,01,11这样的边的总数,用ans[0],ans[1],ans[2]去保存 00,01(10),11的情况。

最裸的思想是每次修改时都去找这个点所有相邻的边,再更新到ans里去,当这个点的度数很大时,复杂度就是n*m了;


令lim = sqrt(m)

1.所以对于度数小于lim的点的修改,我们都直接按上法去做算了,复杂度大约是n/2*sqrt(m);

2.那度数大于等于lim的点,我们希望不要每次去查和他相邻的每个点,那么就分别保存这个点的和它相邻是0的点之间的边权和sum[i][0] 以及 这个点的和它相邻是1的点之间的边权和sum[i][1]。 如果这个点的这个sum值是自己记录的,只会影响到周围度数大于等于lim的点,所以只要去修改这个点周围度数大于等于lim的点j的sum[j][0]和sum[j][1],复杂度大约是n/2*sqrt(m)。


所以总复杂度n*sqrt(m)还是可以接受的。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL ;
const int maxn = 100050 ;

struct Edge{
    int u, v;
    LL w;
    Edge(int u=0,int v=0, LL w=0):u(u),v(v),w(w){}
    bool operator < (const Edge& rhs)const{
        if (u == rhs.u) return v < rhs.v;
        return u < rhs.u;
    }
}e[maxn];

int du[maxn];
bool sp[maxn], type[maxn];
LL ans[3];


int head[maxn][2], v[maxn<<2], nxt[maxn<<2], tot;
LL w[maxn<<2];

void initGraph(){
    memset(head, 0, sizeof head);
    tot = 0;
}

inline void add(int t, int _u, int _v, LL _w){
    v[++tot] = _v; w[tot] = _w; nxt[tot] = head[_u][t]; head[_u][t] = tot;
}

LL sum[maxn][2];

int main()
{
//    freopen("data.in","r", stdin) ;
    int cas = 1, n, m;
    while(scanf("%d%d", &n, &m) == 2){
        for(int i=1, x; i<=n; i++){
            scanf("%d", &x);
            type[i] = x;
        }

        for(int i = 0; i < m; i++){
            scanf("%d%d%I64d", &e[i].u, &e[i].v, &e[i].w);
            if (e[i].u > e[i].v) swap(e[i].u, e[i].v);
        }
        sort(e, e+m);//unique
        int cnt = 0;
        for(int i = 0, j; i < m; i = j){
            for(j = i+1; j < m && e[i].u == e[j].u && e[i].v == e[j].v; j++){
                e[i].w += e[j].w;
            }
            e[cnt++] = e[i];
        }

        //build graph
        memset(du, 0, sizeof du);
        for(int i = 0; i < cnt; i++){
            du[e[i].u]++; du[e[i].v]++;
        }
        int lim = sqrt(cnt<<1);
        for(int i = 1; i <= n; i++){
            sp[i] = (du[i] >= lim);
        }
        initGraph();
        for(int i = 0; i < cnt; i++){
            int x = e[i].u, y = e[i].v; LL w = e[i].w;
            // 用add1 来访问重点
            // 用add0 来访问所有点
            if (sp[y]){//重点需要被访问
                add(1, x, y, w);
            }else{//轻点需要访问所有点
                add(0, y, x, w);
            }
            if (sp[x]){//重点需要被访问
                add(1, y, x, w);
            }else{//轻点需要访问所有点
                add(0, x, y, w);
            }
        }

        ans[0] = ans[1] = ans[2] = 0;
        memset(sum, 0, sizeof sum);
        for(int i = 0; i < cnt; i++){
            int x = e[i].u, y = e[i].v; LL w = e[i].w;
            if (sp[x]){
                sum[x][type[y]] += w;
            }
            if (sp[y]){
                sum[y][type[x]] += w;
            }
            ans[type[x]+type[y]] += w;
        }

        printf("Case %d:\n", cas++);
        int Q, a, b, x; scanf("%d", &Q); char s[12];
        while(Q--){
            scanf("%s", s);
            if (s[0] == 'A'){
                scanf("%d%d", &a, &b);
                printf("%I64d\n", ans[a+b]);
            }
            else{
                scanf("%d", &x);
                type[x] ^= 1;
                if (sp[x]){
                    for(int i = 0; i < 2; i++){
                        ans[(type[x] ^ 1) + i] -= sum[x][i];
                        ans[type[x] + i] += sum[x][i];
                    }
                }
                else{
                    for(int i = head[x][0]; i; i = nxt[i]){
                        ans[(type[x] ^ 1) + type[v[i]]] -= w[i];
                        ans[type[x] + type[v[i]]] += w[i];
                    }
                }

                for(int i = head[x][1]; i; i = nxt[i]){
                    sum[v[i]][type[x]^1] -= w[i];
                    sum[v[i]][type[x]] += w[i];
                }
            }
        }
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值