Submission #1209904

#TimeUsernameProblemLanguageResultExecution timeMemory
1209904wazj2024Traffic (IOI10_traffic)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h> using namespace std; static int N,P[1000000],S[1000000],D[1000000]; int peso[1000000]; long long cost[1000000]; vector<int> c[1000000]; int tp; void dfs1(int u, int p, long long d, long long& total_cost) { peso[u] = P[u]; total_cost += (long long)P[u] * d; for (int v : c[u]) { if (v != p) { dfs1(v, u, d + 1, total_cost); peso[u] += peso[v]; } } } void dfs2(int u, int p) { for (int v : c[u]) { if (v != p) { cost[v] = cost[u] + (long long)(tp - 2 * tp[v]); dfs2(v, u); } } } int LocateCentre(int N, int P[], int S[], int D[]) { ::N = N; tp = 0; for (int i = 0; i < N; ++i) { c[i].clear(); tp += P[i]; } for (int i = 0; i < N - 1; ++i) { c[S[i]].push_back(D[i]); c[D[i]].push_back(S[i]); } long long total_cost = 0; dfs1(0, -1, 0, total_cost); cost[0] = total_cost; dfs2(0, -1); int best = 0; for (int i = 1; i < N; ++i) { if (cost[i] < cost[best]) best = i; } return best; } int main(){ int i; scanf("%d",&N); for (i=0;i<N;i++) scanf("%d",&P[i]); for (i=0;i<N-1;i++) scanf("%d%d",&S[i],&D[i]); int r = LocateCentre(N,P,S,D); printf("%d\n",r); return 0; }

Compilation message (stderr)

traffic.cpp: In function 'void dfs2(int, int)':
traffic.cpp:21:56: error: invalid types 'int[int]' for array subscript
   21 |             cost[v] = cost[u] + (long long)(tp - 2 * tp[v]);
      |                                                        ^
traffic.cpp: In function 'int main()':
traffic.cpp:49:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |    scanf("%d",&N);
      |    ~~~~~^~~~~~~~~
traffic.cpp:50:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |    for (i=0;i<N;i++) scanf("%d",&P[i]);
      |                      ~~~~~^~~~~~~~~~~~
traffic.cpp:51:29: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |    for (i=0;i<N-1;i++) scanf("%d%d",&S[i],&D[i]);
      |                        ~~~~~^~~~~~~~~~~~~~~~~~~~