标签:des style blog http color io os java ar
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1013 Accepted Submission(s): 360
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<vector> 6 using namespace std; 7 const int maxn=100005; 8 vector<vector<int> >arr; 9 //vector<int>arr[maxn]; 10 int ans[maxn]; 11 int n,m,i; 12 int main(){ 13 int cas,a,b,q,cmd,u,v; 14 scanf("%d",&cas); 15 while(cas--){ 16 scanf("%d%d",&n,&m); 17 arr.clear(); 18 arr.resize(n+1); 19 memset(ans,0,sizeof(int)*(n+1)); 20 for(i=0;i<m;i++){ 21 scanf("%d%d",&a,&b); 22 arr[b].push_back(a); 23 arr[a].push_back(b); 24 } 25 scanf("%d",&q); 26 while(q--){ 27 scanf("%d",&cmd); 28 if(cmd==1){ 29 scanf("%d",&u); 30 printf("%d\n",ans[u]); 31 } 32 else{ 33 scanf("%d%d",&u,&v); 34 int len=arr[u].size(); 35 for(i=0;i<len;i++) 36 ans[arr[u][i]]+=v; 37 } 38 } 39 //for(i=1;i<=n;i++)arr[i].clear(); 40 } 41 return 0; 42 }
优化之后:
1 //#pragma comment(linker,"\STACK:1024000000,1024000000"); 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<vector> 6 using namespace std; 7 const int maxn=100005; 8 vector<int>arr[maxn]; 9 int ans[maxn]; 10 int n,m,i; 11 int main(){ 12 int cas,a,b,q,cmd,u,v; 13 scanf("%d",&cas); 14 while(cas--){ 15 scanf("%d%d",&n,&m); 16 memset(ans,0,sizeof(int)*(n+1)); 17 for(i=0;i<m;i++){ 18 scanf("%d%d",&a,&b); 19 arr[b].push_back(a); 20 arr[a].push_back(b); 21 } 22 scanf("%d",&q); 23 while(q--){ 24 scanf("%d",&cmd); 25 if(cmd==1){ 26 scanf("%d",&u); 27 printf("%d\n",ans[u]); 28 } 29 else{ 30 scanf("%d%d",&u,&v); 31 int len=arr[u].size(); 32 for(i=0;i<len;i++) 33 ans[arr[u][i]]+=v; 34 } 35 } 36 //for(i=1;i<=n;i++)arr[i].clear(); 37 } 38 return 0; 39 }
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/gongxijun/p/3966590.html