BestCoder Round #1


    今天开始去做BC 的AB 就像和CF 的ABC 一样吧,后面的题目,有能力也尽量去做做、、、

    附上今天在群里群巨发的一条链接,可以去看看、、如何成为阿里巴巴算法工程师? - 计算机 - 知乎

    

逃生



Problem Description
糟糕的事情发生啦,现在大家都忙着逃命。但是逃命的通道很窄,大家只能排成一行。

现在有n个人,从1标号到n。同时有一些奇怪的约束条件,每个都形如:a必须在b之前。
同时,社会是不平等的,这些人有的穷有的富。1号最富,2号第二富,以此类推。有钱人就贿赂负责人,所以他们有一些好处。

负责人现在可以安排大家排队的顺序,由于收了好处,所以他要让1号尽量靠前,如果此时还有多种情况,就再让2号尽量靠前,如果还有多种情况,就让3号尽量靠前,以此类推。

那么你就要安排大家的顺序。我们保证一定有解。
 

Input
第一行一个整数T(1 <= T <= 5),表示测试数据的个数。
然后对于每个测试数据,第一行有两个整数n(1 <= n <= 30000)和m(1 <= m <= 100000),分别表示人数和约束的个数。

然后m行,每行两个整数a和b,表示有一个约束a号必须在b号之前。a和b必然不同。
 

Output
对每个测试数据,输出一行排队的顺序,用空格隔开。
 

Sample Input
1 5 10 3 5 1 4 2 5 1 2 3 4 1 4 2 3 1 5 3 5 1 2
 

Sample Output
1 2 3 4 5
 

    这是一道拓扑排序的题目,我一开始以为是模版题了,敲了一下,先是用的邻接矩阵,果断超内存了30000*30000的数组、、、然后用的vector和for循环,在敲完之前,我也已经预料到可能会超时的,结果、、、然后我去百度了下,发现我题目要求就没搞清楚了!最后看了他们的方法,是优先队列+ 拓扑排序,处理方法也是比较好的、、

    

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define N 30010

vector<int>mpt[N];
int in[N];
int n, m;

int main()
{
    int a, b;
    int t;
    while(~scanf("%d",&t))
    {
        while(t --)
        {
            scanf("%d%d",&n,&m);
            memset(in, 0, sizeof(in));

            for(int i = 1; i <= n; i ++)
            {
                mpt[i].clear();
            }

            for(int i = 0; i < m; i ++)
            {
                scanf("%d%d",&a,&b);
                in[a] ++;
                mpt[b].push_back(a);
            }

            priority_queue<int> qe;
            vector<int> ans;

            for(int i = 1; i <= n; i ++)
            {
                if(in[i] == 0)
                qe.push(i);
            }

            while(!qe.empty())
            {
                int k = qe.top();
                ans.push_back(k);
                qe.pop();
                for(int i = 0; i < mpt[k].size(); i ++)
                {
                    int v = mpt[k][i];
                    in[v] --;
                    if(in[v] == 0)
                        qe.push(v);
                }
            }
            for(int i = ans.size() - 1; i > 0; i --)
            {
                printf("%d ",ans[i]);
            }
            printf("%d\n",ans[0]);
        }
    }
    return 0;
}

项目管理


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
  

    中文题,也不用解释题意了 - -# . 暴力过的,不过题解说是有关图的,有优化版,下次补上、、、 然后现在大一点的数据,多维数组也不敢开了,不知道可不可以一直用着stl,其实我也不知道stl 优化在哪里 - -#

    

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define N 100010

vector<int> ve[N];
int sum[N];
int n, m;

int main()
{
    int t, q;
    int a, b;
    int qq, v, u;
    while(~scanf("%d",&t))
    {
        while(t --)
        {
            scanf("%d%d",&n,&m);

            for(int i = 0; i <= n; i ++)
            {
                ve[i].clear();
            }
            memset(sum , 0, sizeof(sum));
            for(int i = 0; i < m; i ++)
            {
                scanf("%d%d",&a,&b);
                ve[a].push_back(b);
                ve[b].push_back(a);
            }
            scanf("%d",&qq);
            while(qq --)
            {
                scanf("%d",&q);
                if(q == 0)
                {
                    scanf("%d%d",&u,&v);
                    sum[u] += v;
                }
                else
                {
                    scanf("%d",&u);
                    int ans = 0;
                    for(int i = 0; i < ve[u].size(); i ++)
                    {
                        ans += sum[ve[u][i]];
                    }
                    printf("%d\n",ans);
                }
            }
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章