https://vjudge.net/problem/HDU-4467

大概就是,设一个块大小T

对于度数<=T的点,设为1类点,在改变颜色的时候暴力查询与其相邻点,更新答案

对于度数>T的点,设为2类点,分别维护与其相邻的颜色为0/1的点之间的边权和(记录与每个点相连的所有2类点,然后在任意点(注意2类点也要)改变颜色时维护一下与其相连2类点的这个值),改变颜色的时候根据维护的值O(1)可以计算出对答案的修改

说的再简单一点,1类点由自身去更新其他,2类点由其他去更新自身。。复杂度很容易发现是根号级别的(T=sqrt(m))

但是要注意,重边一定要去。。。不然可以被卡到$n^2$

错误记录:

1.没有在改变2类点颜色时改变相连其他2类点维护的值

2.没有去重边

3.d[a],d[b]和a,b没有极其仔细地区分

 //#pragma GCC optimize(3)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define N 100000
struct E
{
int to,nxt;ll d;
}e[*N+];
int f1[N+],ne;
int TT,n,m,mm,d[N+];
int in[N+],sz,q;
//1类点:in[i]<=sz;2类点:in[i]>sz
ll a1[N+][];
//a1[i][j]:i为2类点,i与相邻的颜色为j的点之间的所有边的权值和
//int num[100100];
char tmp[];
vector<int> ss[N+];//ss[i]:i点到相邻的2类点的边
ll ans[];//ans[0]:00边答案;ans[1]:11边答案;ans[2]:01边答案
map<pii,pii> ma;
ll &gg(int a,int b)
{
if(a==&&b==) return ans[];
if(a==&&b==) return ans[];
return ans[];
}
int main()
{
int a,b,c,i,k;pii t;
while(scanf("%d%d",&n,&m)==)
{
TT++;mm=;//num[0]=0;
memset(f1,,sizeof(f1));ne=;ma.clear();
memset(in,,sizeof(in));ans[]=ans[]=ans[]=;
memset(a1,,sizeof(a1));
for(i=;i<=n;i++) scanf("%d",&d[i]);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(a>b) swap(a,b);
if(ma.count(mp(a,b)))
{
t=ma[mp(a,b)];
e[t.fi].d+=c;e[t.se].d+=c;
}
else
{
e[++ne].to=b;e[ne].nxt=f1[a];f1[a]=ne;e[ne].d=c;
e[++ne].to=a;e[ne].nxt=f1[b];f1[b]=ne;e[ne].d=c;
in[a]++;in[b]++;
ma[mp(a,b)]=mp(ne-,ne);mm++;
}
gg(d[a],d[b])+=c;
}
sz=sqrt(*mm);
for(i=;i<=n;i++)
if(in[i]>sz)
{
//num[++num[0]]=i;
for(k=f1[i];k;k=e[k].nxt)
a1[i][d[e[k].to]]+=e[k].d;
}
for(i=;i<=n;i++)
{
ss[i].clear();
for(k=f1[i];k;k=e[k].nxt)
if(in[e[k].to]>sz)
ss[i].pb(k);
}
scanf("%d",&q);
printf("Case %d:\n",TT);
while(q--)
{
scanf("%s",tmp);
if(tmp[]=='A')
{
scanf("%d%d",&a,&b);
printf("%lld\n",gg(a,b));
}
else
{
scanf("%d",&a);
if(in[a]<=sz)
{
for(k=f1[a];k;k=e[k].nxt)
{
b=e[k].to;
gg(d[a],d[b])-=e[k].d;
if(in[b]>sz) a1[b][d[a]]-=e[k].d;
}
d[a]^=;
for(k=f1[a];k;k=e[k].nxt)
{
b=e[k].to;
gg(d[a],d[b])+=e[k].d;
if(in[b]>sz) a1[b][d[a]]+=e[k].d;
}
}
else
{
for(i=;i<ss[a].size();i++)
a1[e[ss[a][i]].to][d[a]]-=e[ss[a][i]].d;
gg(d[a],)-=a1[a][];gg(d[a],)-=a1[a][];
d[a]^=;
gg(d[a],)+=a1[a][];gg(d[a],)+=a1[a][];
for(i=;i<ss[a].size();i++)
a1[e[ss[a][i]].to][d[a]]+=e[ss[a][i]].d;
}
}
}
}
return ;
}

Graph HDU - 4467的更多相关文章

  1. hdu 4467 Graph

    P. T. Tigris is a student currently studying graph theory. One day, when he was studying hard, GS ap ...

  2. HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)

    Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...

  3. HDU 4467 分块

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

  4. Bipartite Graph hdu 5313 bitset 并查集 二分图

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset   ...

  5. The Shortest Path in Nya Graph HDU - 4725

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  6. B - Rikka with Graph HDU - 5631 (并查集+思维)

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...

  7. AC日记——The Shortest Path in Nya Graph hdu 4725

    4725 思路: 拆点建图跑最短路: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  8. Rikka with Graph hdu 6090

    题解:考虑贪心地一条一条边添加进去. 当 m \leq n-1m≤n−1 时,我们需要最小化距离为 nn 的点对数,所以肯定是连出一个大小为 m+1m+1 的联通块,剩下的点都是孤立点.在这个联通块中 ...

  9. 7.11 NOI模拟赛 graph 生成函数 dp 多项式

    LINK:graph HDU题库里的原题 没做过自闭. 考虑dp 设\(f_{i,j}\)表示前i个点构成j个联通块是树的方案数. 对于一次询问答案即为\(\sum_{j}f_{n,j}j^k\) 考 ...

随机推荐

  1. linux输入子系统(6)-input子系统介绍及结构图

    注:本系列转自: http://www.ourunix.org/post/290.html input子系统介绍         输入设备(如按键,键盘,触摸屏,鼠标,蜂鸣器等)是典型的字符设备,其一 ...

  2. LeetCode(67)题解: Add Binary

    https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a bin ...

  3. V-Play 文档翻译 Page

    V-Play 文档翻译 Page 翻译:qyvlik 应用的一个页面. VPlayApps 1.0 Inherits: MouseArea Inherited By: ListPage 属性 Item ...

  4. CrateDb

    CrateDB: Real-time SQL Database for Machine Data & IoT | Crate.io https://crate.io/

  5. Delphi中取得汉字的首字母简单方法(十分巧妙)

    //从朝闻道的博客里转载,原文定义AHzStr: String,发现结果为空,后来改成AHzStr: AnsiString就可以了 function GetHzPy(const AHzStr: Ans ...

  6. 微信小程序 新手入门教程

    因为工作需要,最近学习了一下微信小程序,在此分享一下大概的流程. 强烈建议大家先去看微信小程序简易教程:点我进入 起步: 安装微信web开发软件者工具,需要破解的同学可以网上找破解教程,很简单的,这里 ...

  7. 通过powershell操作eventlog

    relevant command list ~\Desktop> (Get-Command Write-EventLog).Parameters Key Value --- ----- Warn ...

  8. 织梦文章页调用当前栏目名称和url地址的方法

    其实织梦本身有这2个调用标签,可能大家没怎么注意,下面的代码就是织梦文章页调用当前栏目名称和url地址的方法: {dede:field name='typeurl' function=”GetType ...

  9. 花式GCD

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  10. Java中的ArrayList

    ArrayList是所谓的动态数组.用一个小例子: import java.util.ArrayList; import java.util.Iterator; import java.util.Li ...