hdu4467 Graph
Graph
Problem Description
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.
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.
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.
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
6
3
3
Case 2:
3
0
4
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 100005
struct nodeedge{
int s,e;
__int64 val;
}edge[40*MAXN];
struct nodelist{
int next,to,flag;
}l[200*MAXN];
char str[300];
int head[MAXN],p[MAXN],index[MAXN],limit;//判定是否是超极点
__int64 sum[3],sumsuper[MAXN][2];
void change(int x)
{
int i,j;
int temp=p[x]^1,t; for(j=head[x];j!=-1;j=l[j].next)
{
i=l[j].to;
sum[p[x]+p[i]]-=edge[j].val;
sum[temp+p[i]]+=edge[j].val;
if((!l[x].flag)&&l[i].flag)
{
sumsuper[i][p[x]]-=edge[j].val;
sumsuper[i][temp]+=edge[j].val;
}
}
if(l[x].flag)
{
sum[p[x]]-=sumsuper[x][0];
sum[temp]+=sumsuper[x][0];
sum[p[x]+1]-=sumsuper[x][1];
sum[temp+1]+=sumsuper[x][1];
}
p[x]=p[x]^1;//取反
}
void build(int i,int s,int e,__int64 val)//建邻接表
{
if(l[s].flag&&(!l[e].flag))
{
sumsuper[s][p[e]]+=val;
}
else
{
l[i].next=head[s];
l[i].to=e;
head[s]=i;
}
}
bool cmp(nodeedge a,nodeedge b)
{
if(a.s != b.s)
return a.s < b.s;
else
return a.e < b.e;
}
int main ()
{
int n,m,i,edgem,m2,asnum,a1,a2,tcase;
tcase=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
limit=350;m2=2*m;
sum[0]=sum[1]=sum[2]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
index[i]=0;head[i]=-1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%I64d",&edge[i].s,&edge[i].e,&edge[i].val);
edge[i+m].s=edge[i].e,edge[i+m].e=edge[i].s,edge[i+m].val=edge[i].val;//建反向边
sum[p[edge[i].s]+p[edge[i].e]]+=edge[i].val;
}
sort(edge+1,edge+m2+1,cmp);//边从1开始计数
edgem=1;
for(i=2;i<=m2;i++)//去掉重边
{
if((edge[i].s==edge[edgem].s)&&(edge[i].e==edge[edgem].e))
{
edge[edgem].val+=edge[i].val;
}
else{
edgem++;
edge[edgem]=edge[i];
}
}
for(i=1;i<=edgem;i++)
{
index[edge[i].s]++;
}
for(i=1;i<=n;i++)//统计超极结点
{
if(index[i]>=limit)
{
l[i].flag=1;//是超极结点
sumsuper[i][0]=sumsuper[i][1]=0;
} else
l[i].flag=0;
} for(i=1;i<=edgem;i++)//建领接表
{
build(i,edge[i].s,edge[i].e,edge[i].val);
}
scanf("%d",&asnum);
printf("Case %d:\n",tcase++);
while(asnum--)
{
scanf("%s",str);
if(str[0]=='A')
{
scanf("%d%d",&a1,&a2);
printf("%I64d\n",sum[a1+a2]);
}
else{
scanf("%d",&a1);
change(a1);
}
} }
return 0;
}
hdu4467 Graph的更多相关文章
- HDU4467:Graph(点的度数分块)
传送门 题意 给出一张n个点m条边的无向图,点的颜色为0/1,每次有两种操作: 1.Asksum x y,查询两点颜色为x和y的边的权值之和 2.Change x,将x颜色取反 分析 最直接的做法是每 ...
- HDU4467 Graph【轻重点维护】
HDU4467 Graph 题意: 给出一张染色图,\(n\)个点每个点是黑色或者白色,\(m\)条带权边,\(q\)次操作,有两种操作: 改变一个点的颜色 问所有边中两个端点的颜色为给定情况的边权和 ...
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
随机推荐
- Caffe 碎碎念
Window Data Layer window data layer 的数据是存在硬盘上的图片, 需要在一个txt里指定用于训练或测试的图片以及bounding box, bounding box ...
- IL反编译的实用工具Ildasm.exe
初识Ildasm.exe——IL反编译的实用工具 https://www.cnblogs.com/yangmingming/archive/2010/02/03/1662307.html 学 ...
- 基于theano的多层感知机的实现
1.引言 一个多层感知机(Multi-Layer Perceptron,MLP)可以看做是,在逻辑回归分类器的中间加了非线性转换的隐层,这种转换把数据映射到一个线性可分的空间.一个单隐层的MLP就可以 ...
- Linux内核中的printf实现【转】
转自:http://www.cnblogs.com/chenglei/archive/2009/08/06/1540702.html 从main.c中的printf开始读这个函数. 首先看printf ...
- oracle锁表查询,资源占用,连接会话,低效SQL等性能检查
查询oracle用户名,机器名,锁表对象 select l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, l.os_user ...
- Android 4.4 API
Android 4.4 (KITKAT) 是新的 Android 平台版本,为用户和应用开发者提供了新功能.本文旨在介绍其中最值得关注的新 API. 作为应用开发者,您应尽快从 SDK 管理器下载 A ...
- Android 7.0 新增功能和api
Android 7.0 Nougat 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 请务必查阅 Android 7.0 行为变更以了解平台变更可能影响您的应用的领域. 要详细了解 A ...
- Java 接口关键字 interface
interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体的实现,它允许创建者确定方法名.参数列表和返回类型,但没有任何方法体,接口只提供了形式,而未提供任何具体实现 一个接口表示:& ...
- CentOS 6 安装chromium
由于centos 6对C++11支持不足的缘故,目前chromium已经不再支持CentOS 6系列. 这里介绍如何在centos 6系列安装chromium. 1.添加chromium源 cd /e ...
- NOIP 2000 计算器的改良
题面 NCL是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在该公司某型号的计算器上加上解一元一次方程的功能.实验室将这个任务交给了一个刚进入的新手ZL先生. 为 ...