제출 #342031

#제출 시각아이디문제언어결과실행 시간메모리
342031freshminttTraffic (IOI10_traffic)C++14
50 / 100
603 ms262148 KiB
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <string.h>
#include <iterator> 
#include <list>
#include <cmath>
#include <math.h>
#include <unordered_map> 
#include <numeric>

using namespace std;
typedef pair<int,int> pi;
vector<int> adj[1000001]; // change
int people[1000001]; // change
set<int> sums[1000001]; // change
int subtree[1000001]; // change

void dfsSubtree(int node, int parent, int parentSum) {
  // sums[node].insert(parentSum); // record the cumulative sum from root
  subtree[node] += people[node];
  for (auto u: adj[node]) {
    if (u != parent) { // traverse to the next node, adding on the parentSum
    // the subtree sum of the next node must be returned and added
    dfsSubtree(u, node, parentSum + people[node]);
    subtree[node] += subtree[u]; // subtree u is the sum for the child
    sums[node].insert(subtree[u]);
  }
}
}

void dfsParents(int node, int parent, int parentSum) {
  sums[node].insert(parentSum);
  for (auto u : adj[node]) {
    if (u != parent) {

      int res = subtree[0]-subtree[u];
      // cout << "going from:  "<< node << " to " << u << ", parentsum: " << res <<  endl;
      // must send parentSum = entire tree sum - dest node subtree
      
      dfsParents(u, node, res);
      // subtree[node] += subtree[u]; // subtree u is the sum for the child
      // sums[node].insert(subtree[u]);
    }
  }
}

int LocateCentre(int N,int P[],int S[], int D[]) {
  for (int i = 0; i <N-1; i ++) {
    adj[S[i]].push_back(D[i]);
    adj[D[i]].push_back(S[i]);
    
  }
  for (int i = 0; i < N; i ++) {
    people[i] = P[i];
  }
  dfsSubtree(0, -1, 0);
  
  dfsParents(0, -1, 0);
  int minRoad = 1E12;
  int city = -1;
  for (int i = 0; i < N; i++) {
    
    if (*sums[i].rbegin() < minRoad) {
      minRoad = *sums[i].rbegin();
      city = i;
    }
  }
  return city;
}

// int main() {
//   int P[5] = {10,10,10,20,20};
//   int S[4] = {1, 2, 2, 3};
//   int D[4] = {2, 0, 3, 4};
//   cout << LocateCentre(5, P, S, D);
// }

컴파일 시 표준 에러 (stderr) 메시지

traffic.cpp: In function 'int LocateCentre(int, int*, int*, int*)':
traffic.cpp:66:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+12' to '2147483647' [-Woverflow]
   66 |   int minRoad = 1E12;
      |                 ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...