资本运动|竞争性规划

时间:2017-01-09 22:36:33

标签: c++ algorithm performance time-complexity space-complexity

问题

假设一个国家有'n'个城市,其中一个是首都。所有城市都通过“n-​​1”道路相连,可以使用这些道路在任意两个城市之间旅行。城市人口P i 在问题中提供。

现在,如果某个城市感染了病毒,与该城市相关的城市也会受到感染。在感染的情况下,选择新的资本。人口最多的非感染城市成为新的首都。

INPUT

输入的第一行包含一个整数T,表示测试用例的数量。 T测试用例的描述如下。

每个测试用例的第一行包含一个整数N,表示城市数量。

下一行包含N个以空格分隔的整数P1,P2,...,PN,表示每个城市的人口。

接下来的N-1行包含两个以空格分隔的整数,每个V和U表示城市V和U之间有一条道路。

输出

对于每个测试用例,输出包含N个整数A1,A2,...,AN的单行,用空格分隔。在这里,Ai表示在感染开始从城市i传播的情况下被选为新资本的城市的数量。如果感染影响所有城市输出0。

示例

输入

1

6

5 10 15 20 25 30

1 3

2 3

3 4

4 5

4 6

输出

6 6 6 2 6 5

我的C ++解决方案

#include <iostream>
#include <vector>
#include <list>
#include <stdio.h>
#include <algorithm>
using namespace std;

int main() {
    int t;
    scanf("%d", &t); //No. of test cases
    while(t--) {
        int n;
        scanf("%d", &n);
        vector<int> p(n);   //vector for storing population of each city
        for(int i = 0; i < n; i++)
            scanf("%d", &p[i]);

        vector<list<int> > graph(n); //vector of lists for storing connection between cities


        for(int i = 0; i < n-1; i++) { 
            int a, b;
            scanf("%d %d", &a, &b);
            graph[--a].push_back(--b);
            graph[b].push_back(a);
        }

        for(int i = 0; i < n; i++) {
            vector<int> temp = p;             //Temporary vector 

        /*All the infected cities are assigned a population
          of -1 so that they don't compete to become the largest capital*/

            temp[i] = -1;
            if(graph[i].size() == n-1) {//Printing "0" if all cities have connection with the infected city.
                cout<<"0 ";
                continue;
            }
            int s = graph[i].size();
            for(int j = 0; j < s; j++) {
                temp[graph[i].front()] = -1;
                graph[i].pop_front();
            }

            /*Finding max. population*/
            int maxindex = std::distance(temp.begin(), std::max_element(temp.begin(), temp.end()));

            printf("%d ", maxindex+1);
        }
        printf("\n");
    }
    return 0;
} 

函数max_element使时间复杂度呈二次方。 我的解决方案是超出大输入的时间限制。因此,我的计划需要在执行时间上进行改进。请帮忙。谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

解决!诀窍是事先对城市人口进行排序

#include <iostream>
#include <vector>
#include <list>
#include <stdio.h>
#include <algorithm>
using namespace std;
bool myfnc(pair<int, int> i, pair<int, int> j) {
    return i.first > j.first;
}
int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, d;
        scanf("%d", &n);
        vector<int> p(n);
        int m = 0, mi = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &d);
            p[i] = d;
        }
        vector<pair<int, int> > p1(n);
        for(int i = 0; i < n; i++) {
            p1[i].first = p[i];
            p1[i].second = i;
        }
        sort(p1.begin(), p1.end(), myfnc);

        vector<vector<bool> > graph(n, vector<bool> (n));
        for(int i = 0; i < n-1; i++) {
            int a, b;
            scanf("%d %d", &a, &b);
            graph[--a][--b] = 1;
            graph[b][a] = 1;
            graph[i][i] = 1;
        }
        graph[n-1][n-1] = 1;
        for(int i = 0; i < n; i++) {
            int j;
            for(j = 0; j < n; j++) {
                if(graph[i][p1[j].second] == 0) {
                    printf("%d ", p1[j].second+1);
                    break;
                }
            }
            if(j == n)
                printf("0 ");
        }
        printf("\n");
    }
    return 0;
}

答案 1 :(得分:-1)

因为没有关于什么&#34;更大输入的信息&#34;意思是我猜它指的是有更多的城市。在这种情况下,你正在制作一个巨大的(nxn)图形数组,这将导致更多的内存被占用,同时(大多数)是空的,因为只有n-1条道路。 考虑使用std :: list连接城市,然后仅查看与受感染城市不在同一列表中的城市。