题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 #define inf 0x7fffffff 10 using namespace std; 11 const int maxn=100000+10; 12 13 int n,m; 14 vector<int> vec[maxn],HG[maxn]; 15 int deg[maxn],sum[maxn],value[maxn]; 16 17 int main() 18 { 19 int t ;scanf("%d",&t); 20 while (t--) 21 { 22 scanf("%d%d",&n,&m); 23 for (int i=1 ;i<=n ;i++) vec[i].clear(),deg[i]=0,sum[i]=0,value[i]=0; 24 int a,b; 25 for (int i=1 ;i<=m ;i++) 26 { 27 scanf("%d%d",&a,&b); 28 vec[a].push_back(b); 29 vec[b].push_back(a); 30 deg[a] ++ ;deg[b] ++ ; 31 } 32 int d=(int)sqrt(n); 33 for (int i=1 ;i<=n ;i++) 34 { 35 HG[i].clear(); 36 int num=vec[i].size(); 37 for (int j=0 ;j<num ;j++) 38 { 39 int v=vec[i][j]; 40 if (deg[v]>d) HG[i].push_back(v); 41 } 42 } 43 int q,cmd,u,w; 44 scanf("%d",&q); 45 while (q--) 46 { 47 scanf("%d",&cmd); 48 if (cmd==1) 49 { 50 scanf("%d",&u); 51 int ans=sum[u]; 52 int num=HG[u].size(); 53 for (int j=0 ;j<num ;j++) 54 ans += value[HG[u][j] ]; 55 printf("%d\n",ans); 56 } 57 else 58 { 59 scanf("%d%d",&u,&w); 60 if (deg[u]<=d) 61 { 62 int num=vec[u].size(); 63 for (int j=0 ;j<num ;j++) 64 sum[vec[u][j] ] += w; 65 } 66 else value[u] += w; 67 } 68 } 69 } 70 return 0; 71 }
原文:http://www.cnblogs.com/huangxf/p/4382108.html