资本运动|竞争性编程
Capital Movement | Competitive Programming
问题
假设一个国家有n个城市,其中一个是首都。所有城市都通过"n-1"道路连接,使用这些道路可以在任何两个城市之间行驶。问题中提供了城市人口Pi。
现在,如果一个城市感染了病毒,与该城市相连的城市也会被感染。在感染的情况下,会选择一个新的首都。人口最多的未感染城市成为新首都。
输入
输入的第一行包含一个整数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使时间复杂度为二次型。我的解决方案超出了大量输入的时间限制。因此,我的程序需要改进执行时间。请帮忙。谢谢你抽出时间。
因为没有关于"更大的投入"意味着什么的信息,我猜它指的是拥有更多的城市。在这种情况下,你正在制作一个巨大的(nxn)图数组,这将导致更多的内存被占用,同时(大部分)是空的,因为只有n-1条路。考虑将有std::列表的城市连接起来,然后只查找与感染城市不在同一列表中的城市。
已解决!诀窍是预先对城市人口进行排序。
#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;
}
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 模板元编程:如何将参数包组合成新的参数包
- Qt Q串行端口未编程设备未关闭
- 模板元编程 - 尝试实现维度分析
- 我是编程新手
- C++编程从外部文本文件定义数组大小
- 了解算法的性能差异(如果以不同的编程语言实现)
- 使用 Gtkmm 以编程方式选择 Gtk::TextView 中的文本
- C++:竞争性编程技巧的解释
- 竞争性编程问题说错了答案
- 如何在视觉工作室社区中设置竞争性编程站点等环境?
- 模块化算术 - 竞争性编程
- 竞争性编程 - 代码对在线编译器给出了不同的答案
- 资本运动|竞争性编程
- 如何验证竞争性编程任务的动态编程解决方案的正确性
- 竞争性编程:解决方案运行速度太慢
- 竞争性编程中的快速输入/输出
- 竞争性编程头文件
- 我如何处理输入/输出格式的竞争性编程网站,如SPOJ