hdu 4858(简单模拟)

项目管理

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2181    Accepted Submission(s): 814

Problem Description

我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的!
两个节点间可能有多条边,不过一条边的两端必然是不同的节点。
每个节点都有一个能量值。

现在我们要编写一个项目管理软件,这个软件呢有两个操作:
1.给某个项目的能量值加上一个特定值。
2.询问跟一个项目相邻的项目的能量值之和。(如果有多条边就算多次,比如a和b有2条边,那么询问a的时候b的权值算2次)。

Input

第一行一个整数T(1 <= T <= 3),表示测试数据的个数。
然后对于每个测试数据,第一行有两个整数n(1 <= n <= 100000)和m(1 <= m <= n + 10),分别表示点数和边数。

然后m行,每行两个数a和b,表示a和b之间有一条边。
然后一个整数Q。

然后Q行,每行第一个数cmd表示操作类型。如果cmd为0,那么接下来两个数u v表示给项目u的能量值加上v(0 <= v <= 100)。
如果cmd为1,那么接下来一个数u表示询问u相邻的项目的能量值之和。

所有点从1到n标号。

Output

对每个询问,输出一行表示答案。

Sample Input

1
3 2
1 2
1 3
6
0 1 15
0 3 4
1 1
1 3
0 2 33
1 2

Sample Output

4
15
15

Author

CLJ

Source

BestCoder Round #1

链式前向星会超时,普通邻接表可以AC

AC:

#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;
            }
        }
    }
}

TLE:

#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;
            }
        }
    }
}
时间: 2024-08-06 11:56:35

hdu 4858(简单模拟)的相关文章

HDU 4891 简单模拟

The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1035    Accepted Submission(s): 355 Problem Description As a programming contest addict, Waybl is always happy to take part in vario

HDU 1234 简单模拟题

题目很简单不多说了,我只是觉得这题目的输入方式还是很有特点的 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 7 struct People{ 8 char name[20]; 9 int h1 , h2 , m1 , m2 , s1 , s2; 10 }p[10005]; 11 12

HDU 1707 简单模拟 Spring-outing Decision

Spring-outing Decision Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 676    Accepted Submission(s): 220 Problem Description Now is spring ! The sunshine is warm , the flowers is coming out . H

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

hdu 4858 项目管理(vector模拟)

# include <stdio.h> # include <algorithm> # include <string.h> # include <vector> # define N 100005 using namespace std; vector<int>g[N]; int node[N]; int slove(int x) { int sum=0,i; for(i=0;i<g[x].size();i++) { sum+=node[

HDU 4858 项目管理(邻接表 暴力模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值. 现在我们要编写一个项目管理软件,这个软件呢有两个操作:1.给某个项目的能量值加上一个特定值.2.询问跟一个项目相邻的项目的能量值之和.(如果有多条边就算多次,比如a和b有2条边,那么询问a的时候b的权值算2次). 解题报告:这个

HDU 4772 Zhuge Liang&#39;s Password (简单模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1404    Accepted Submission(s): 926 Problem Description In the anc

hdu 1800 Flying to the Mars(简单模拟,string,字符串)

题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于string的修改增加的用法 #include <cstdio> #include<iostream> #include <cstring> #include <algorithm> #include<string> using namespace std

HDU ACM 1057 A New Growth Industry 简单模拟

题意:给一个天数N,求20*20方阵内细菌的变化情况.每次变化加上一个d[k],d数组有给定的16个数.k是某个格子它本身加上它上下左右的四个数. 简单模拟题. 分析: #include<iostream> using namespace std; int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; char den[]=".!X#"; int D[16]; int map[20][20],tmp[20][20]; void Fun() {