标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2181 Accepted Submission(s): 814
#include <stdio.h> #include <math.h> #include <iostream> #include <algorithm> #include <string.h> #include <vector> using namespace std; const int N = 100000+20; vector <int> g[N]; int v[N]; int tot; int n,m; int ans = 0; int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ v[i] = 0; g[i].clear(); } for(int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } int Q; scanf("%d",&Q); while(Q--){ int k,a,b; scanf("%d",&k); if(k){ scanf("%d",&a); ans = 0; for(int i=0;i<g[a].size();i++){ ans+=v[g[a][i]]; } printf("%d\n",ans); }else{ scanf("%d%d",&a,&b); v[a]+=b; } } } }
#include <stdio.h> #include <math.h> #include <iostream> #include <algorithm> #include <string.h> using namespace std; const int N = 100000+20; struct Edge{ int v,next; }edge[2*N]; int head[N]; int v[N]; int tot; int n,m; int ans = 0; void init(){ memset(head,-1,sizeof(head)); memset(v,0,sizeof(v)); tot=0; } void addEdge(int u,int v,int &k){ edge[k].v = v,edge[k].next = head[u],head[u] = k++; } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%d%d",&n,&m); init(n); for(int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); addEdge(u,v,tot); addEdge(v,u,tot); } int Q; scanf("%d",&Q); while(Q--){ int k,a,b; scanf("%d",&k); if(k){ scanf("%d",&a); ans = 0; for(int i = head[a];i!=-1;i=edge[i].next){ ans+=v[edge[i].v]; } printf("%d\n",ans); }else{ scanf("%d%d",&a,&b); v[a]+=b; } } } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5627842.html