例1. hdu 4858 项目管理
按点的度数划分为重点(deg>=sqrt(m))和轻点(deg<sqrt(m)), 轻点暴力更新, 重点只更新邻接的重点
#include <iostream> #include <algorithm> #include <math.h> #include <cstdio> #include <vector> #define pb push_back #define REP(i,a,n) for(int i=a;i<=n;++i) using namespace std; typedef long long ll; const int N = 1e5+10, INF = 0x3f3f3f3f; int n, m, q, sqn; int deg[N], u[N], v[N]; ll sum[N], val[N]; vector<int> g[N]; void work() { scanf("%d%d", &n, &m), sqn = sqrt(m); REP(i,1,n) g[i].clear(), deg[i]=sum[i]=val[i]=0; REP(i,1,m) { scanf("%d%d", u+i, v+i); ++deg[u[i]], ++deg[v[i]]; } REP(i,1,m) { if (deg[u[i]]>=sqn&°[v[i]]>=sqn) { g[u[i]].pb(v[i]),g[v[i]].pb(u[i]); } if (deg[u[i]]<sqn) g[u[i]].pb(v[i]); if (deg[v[i]]<sqn) g[v[i]].pb(u[i]); } scanf("%d", &q); REP(i,1,q) { int op, x, v; scanf("%d%d", &op, &x); if (op==0) { scanf("%d", &v); val[x] += v; for (int y:g[x]) if (deg[y]>=sqn) sum[y]+=v; } else { ll ans = 0; if (deg[x]<sqn) for (int y:g[x]) ans += val[y]; else ans = sum[x]; printf("%lld\n", ans); } } } int main() { int t; scanf("%d", &t); REP(i,1,t) work(); }
例2: hdu 4467 Graph
原文地址:https://www.cnblogs.com/uid001/p/10434786.html
时间: 2024-12-17 09:48:40