传送门

题意

给出一张n个点m条边的无向图,点的颜色为0/1,每次有两种操作:

1.Asksum x y,查询两点颜色为x和y的边的权值之和

2.Change x,将x颜色取反

分析

最直接的做法是每次改变点的颜色豆浆与该点所连的边更新,\(O(q*m)\),超时

那么我们考虑将点根据度数分类,将度数\(<sqrt(m)\)的点称为普通点,否则为超级点。设置sum[i][0/1]代表第i个点(超级点)与颜色为0/1相连的边的权值和。普通点颜色改变,暴力修改与普通点相连的所有的点对答案的贡献\(O(\sqrt{m})\);超级点颜色改变,直接修改对答案的贡献\(O(1)\);最后修改与变颜色的点相连的所有超级点的sum,具体细节见代码

trick

1.用vector写会wa,不知道为什么

2.一些写的更详细的blog,对于答案的贡献修改有清晰的介绍

hdu4467 Graph(构造法求解)

hdu4467 Graph(图的分块)

代码

//vector(wa)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; #define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
const int N = 100100;
int n,m,q,cas;
int color[N],du[N],super[N];
ll sum[N][2],ans[3];
struct edge
{
int u,v;
ll w;
bool operator<(const edge &p)const
{
return u==p.u?v<p.v:u<p.u;
}
}e[N];
struct node
{
int v;
ll w;
node(int _v,ll _w):v(_v),w(_w){}
};
vector<node>ve[N];
void change(int x)
{
color[x]^=1;
if(super[x])
{
ans[(color[x]^1)+0]-=sum[x][0];
ans[(color[x]^1)+1]-=sum[x][1];
ans[(color[x])+0]+=sum[x][0];
ans[(color[x])+1]+=sum[x][1];
}
else
{
for(int i=0;i<ve[x].size();++i)
{
int y=ve[x][i].v;ll z=ve[x][i].w;
ans[(color[x]^1)+color[y]]-=z;
ans[(color[x])+color[y]]+=z;
}
}
for(int i=0;i<ve[x].size();++i)
{
int y=ve[x][i].v;ll z=ve[x][i].w;
if(super[y])
{
sum[y][color[x]^1]-=z;
sum[y][color[x]]+=z;
}
}
}
/*
void check()
{
for(int i=1;i<=n;++i) printf("%d%c",color[i],i==n?'\n':' ');
for(int i=1;i<=n;++i) printf("%lld %lld%c",sum[i][0],sum[i][1],i==n?'\n':' ');
}
*/
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
mem(du,0);mem(ans,0);mem(sum,0);
F(i,0,n) ve[i].clear();
F(i,1,n) scanf("%d",color+i);
F(i,1,m) {scanf("%d %d %lld",&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+1,e+1+m);
int cnt=0;
for(int i=1,j;i<=m;i=j){
for(j=i+1;j<=m;++j) if(e[i].u==e[j].u&&e[i].v==e[j].v) e[i].w+=e[j].w;else break;
e[++cnt]=e[i];
}
F(i,1,cnt) du[e[i].u]++,du[e[i].v]++;
int judge=sqrt(cnt);
F(i,1,n) super[i]=(du[i]>=judge);
F(i,1,cnt)
{
if(super[e[i].u]) {ve[e[i].v].push_back(node(e[i].u,e[i].w));sum[e[i].u][color[e[i].v]]+=e[i].w;}else ve[e[i].u].push_back(node(e[i].v,e[i].w));
if(super[e[i].v]) {ve[e[i].u].push_back(node(e[i].v,e[i].w));sum[e[i].v][color[e[i].u]]+=e[i].w;}else ve[e[i].v].push_back(node(e[i].u,e[i].w));
ans[color[e[i].u]+color[e[i].v]]+=e[i].w;
}
printf("Case %d:\n", ++cas);
for(scanf("%d",&q);q--;)
{
char s[10];scanf("%s",s);
if(s[0]=='A'){ int x,y;scanf("%d %d",&x,&y);printf("%lld\n",ans[x+y]); }
else
{
int x;scanf("%d",&x);change(x);
}
}
// check();
}
return 0;
}
//邻接表(ac)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std; #define F(i,a,b) for(int i=a;i<=b;++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
const int N = 100100;
int n,m,q,cas;
int color[N],du[N],super[N];
ll sum[N][2],ans[3];
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 &p)const
{
return u==p.u?v<p.v:u<p.u;
}
}e[N];
int head[N][2], v[N<<2], nxt[N<<2], tot;
ll w[N<<2];
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; }
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
mem(du,0);
F(i,1,n) scanf("%d",color+i);
F(i,1,m) {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+1,e+m+1);
//对边去重
int cnt=0;
for(int i=1,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];
}
F(i,1,cnt) du[e[i].u]++,du[e[i].v]++;//计算度数
int judge=sqrt(cnt);//设定界限,高于界限为超级点,否则为普通点
F(i,1,n) super[i]=(du[i]>=judge);
mem(head,0);tot=0;mem(ans,0);mem(sum,0);
F(i,1,cnt)//构建邻接表
{
int x=e[i].u,y=e[i].v;ll z=e[i].w;
//如果为超级点,就要放到被访问的位置,否则放到访问的位置,对超级点相连的边记录sum值
if(super[x]) {add(1,y,x,z);sum[x][color[y]]+=z;}else add(0,x,y,z);
if(super[y]) {add(1,x,y,z);sum[y][color[x]]+=z;}else add(0,y,x,z);
ans[color[x]+color[y]]+=z;//将边权记录到答案中
}
printf("Case %d:\n", ++cas);
for(scanf("%d",&q);q--;)
{
char s[10];scanf("%s",s);
if(s[0]=='A'){ int x,y;scanf("%d %d",&x,&y);printf("%I64d\n",ans[x+y]); }
else
{
int x;scanf("%d",&x);
color[x]^=1;//先取反
if(super[x])
{
for(int i = 0; i < 2; i++)//点的状态改变,将答案中的与点有关的部分转移
{
ans[(color[x] ^ 1) + i] -= sum[x][i];
ans[color[x] + i] += sum[x][i];
}
}
else
{
for(int i=head[x][0];i;i=nxt[i])//普通点暴力更新答案
{
ans[(color[x]^1)+color[v[i]]]-=w[i];
ans[color[x]+color[v[i]]]+=w[i];
} }
for(int i=head[x][1];i;i=nxt[i])//暴力修改与点相连的超级点sum
{
sum[v[i]][color[x]^1]-=w[i];
sum[v[i]][color[x]]+=w[i];
}
}
}
}
return 0;
}

HDU4467:Graph(点的度数分块)的更多相关文章

  1. HDU4467 Graph【轻重点维护】

    HDU4467 Graph 题意: 给出一张染色图,\(n\)个点每个点是黑色或者白色,\(m\)条带权边,\(q\)次操作,有两种操作: 改变一个点的颜色 问所有边中两个端点的颜色为给定情况的边权和 ...

  2. hdu4467 Graph

    Graph Problem Description P. T. Tigris is a student currently studying graph theory. One day, when h ...

  3. 2019牛客多校三 A. Graph Games (图分块)

    大意: 给定无向图, 定义$S(x)$为$x$的邻接点集合. 每次操作翻转$[L,R]$范围的边, 询问$S(x)$与$S(y)$是否相等. 快速判断集合相等可以用$CF 799F$的技巧. 采用$h ...

  4. HihoCoder 1629 Graph (2017 ACM-ICPC 北京区域赛 C题,回滚莫队 + 启发式合并 + 可撤销并查集)

    题目链接  2017 ACM-ICPC Beijing Regional Contest Problem C 题意  给定一个$n$个点$m$条边的无向图.现在有$q$个询问,每次询问格式为$[l, ...

  5. Comet OJ - Contest #5 迫真图论 (图分块)

    大意: 给定无向图, 点$i$点权$b_i$, 边$(x,y,z)$对序列贡献是把$A[b_x \oplus b_y]$加上$z$. 多组询问, 一共三种操作: 1. 修改点权. 2.修改边权. 3. ...

  6. 2019牛客多校第三场A Graph Games 分块思想

    题意:给你一张无向图,设s(x)为与x直接相连的点的集合,题目中有两种操作: 1:1 l r 将读入的边的序列中第l个到第r个翻转状态(有这条边 -> 没这条边, 没这条边 -> 有这条边 ...

  7. Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)

    题目链接  Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$    涉及的点的个数 $<= ...

  8. 2019牛客暑期多校训练营(第三场)A.Graph Games (分块)

    题意:给你n个点 m条边的一张图 现在有q次操作 每次操作可以选择反转l~r的边号 也可以询问S(l)和S(r) 连接成的点集是否相同 思路:我们把m条边分块 用一个S数组维护每块对一个点的贡献 然后 ...

  9. HDU 4467 分块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4467 题意:给定n个点m条边的无向图,点被染色(黑0/白1),边带边权.然后q个询问.询问分为两种: ...

随机推荐

  1. c++ struct与class的差别

    从语法上,在C++中(仅仅讨论C++中).class和struct做类型定义时仅仅有两点差别: (一)默认继承权限. 假设不明白指定,来自class的继承依照private继承处理.来自struct的 ...

  2. vue2.0 自定义过滤器(filter)实例

    一.过滤器简介 (1)过滤器创建 过滤器的本质 是一个有参数 有返回值的方法 new Vue({ filters:{ myCurrency:function(myInput){ return 处理后的 ...

  3. Firefox OS开发指南

    在海外社区Leanpub上线了<Firefox OS App开发>高速指南,指引开发人员尝试新技术. 这款<Firefox OS App开发>高速指南现已开放下载.HTML5开 ...

  4. Linux input子系统实例分析(二)

    紧接着上一节的实例我们来分析调用的input子系统的接口: 1. input_dev,用来标识输入设备 1: struct input_dev { 2: const char *name; //设备名 ...

  5. mac上pydev

    转自:http://m.blog.csdn.net/blog/yangfu132/23689823 本来网上有教程,但是往往又一些不周到的地方,让人走了不少弯路. 使用 PyDev 进行调试 第一步: ...

  6. 6.游戏特别离不开脚本(3)-JS脚本操作java(3)(直接操作JS文件或者调用函数)

    java直接运行JS脚本文件的语句,游戏开发时,策划的配置文件什么的就可以分开管理了,游戏逻辑也是一样,比如:一个功能一个脚本或者一个系统一个脚本. import java.io.FileNotFou ...

  7. sql语句,无法绑定由多个部分组成的标识符 "xxx"

    String sql = "select TOP 7 news_id,news_title,news_addtime,news_url from web_news_info a" ...

  8. IE8 下背景图标不显示

    如图所示 : 微博微信前方各应有个图标,但是IE8下图标没有显示 css如下 .weibo_icon{background: url(../ieImages/weibo_icon.png)no-rep ...

  9. 分享一个好用的函数吧,将js中的对象转成url参数

    JavaScript&jQuery获取url参数方法 这个函数呢是自己在写基于Vue+ElementUI管理后台时用到的,,下面列出来两种使用方式: 最普通的,封装一个js函数 /** * 对 ...

  10. 学习html5 中的canvas(一)

    1.canvas画直线 <!doctype html> <html> <head> <meta charset="UTF-8"> & ...