Problem p03026 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

vector<vector<int>> to;
vector<int> c;
vector<bool> done;
vector<int> nodes;
vector<pair<int, int>> ab;
int nodecount;

queue<int> q;

/*

void bfs(int id)
{
        done[id]=true;
        nodes[id]=c[nodecount];
        nodecount++;

//cout << id << endl;

        for(int i=0;i<to[id].size();i++)
        {
                int nxid=to[id][i];
                if(done[nxid]!=true)
                        bfs(nxid);
        }

}

*/

void bfs(int id) {
  q.push(id);
  while (!q.empty()) {
    int now = q.front();
    q.pop();

    done[now] = true;
    nodes[now] = c[nodecount];
    nodecount++;
    for (int i = 0; i < to[now].size(); i++) {
      int nxid = to[now][i];
      if (done[nxid] != true)
        q.push(nxid);
    }
  }
}

int val(void) {
  int ans = 0;
  for (int i = 0; i < ab.size(); i++)
    ans += min(nodes[ab[i].first], nodes[ab[i].second]);
  return ans;
}

int main() {
  int n;
  cin >> n;

  to = vector<vector<int>>(n);

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ab.push_back(make_pair(a, b));
    to[a].push_back(b);
    to[b].push_back(a);
  }
  c = vector<int>(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());

  int ans = 0;
  int ansid = 0;

  for (int i = 0; i < n; i++) {
    done = vector<bool>(n, false);
    nodes = vector<int>(n, 0);
    nodecount = 0;
    bfs(i);
    int tmp = val();
    if (tmp > ans) {
      ans = tmp;
      ansid = i;
    }
  }

  cout << ans << endl;

  done = vector<bool>(n, false);
  nodes = vector<int>(n, 0);
  nodecount = 0;
  bfs(ansid);
  for (int i = 0; i < n; i++)
    cout << nodes[i] << " ";
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

vector<vector<int>> to;
vector<int> c;
vector<bool> done;
vector<int> nodes;
vector<pair<int, int>> ab;
int nodecount;

queue<int> q;

/*

void bfs(int id)
{
        done[id]=true;
        nodes[id]=c[nodecount];
        nodecount++;

//cout << id << endl;

        for(int i=0;i<to[id].size();i++)
        {
                int nxid=to[id][i];
                if(done[nxid]!=true)
                        bfs(nxid);
        }

}

*/

void bfs(int id) {
  q.push(id);
  while (!q.empty()) {
    int now = q.front();
    q.pop();

    done[now] = true;
    nodes[now] = c[nodecount];
    nodecount++;
    for (int i = 0; i < to[now].size(); i++) {
      int nxid = to[now][i];
      if (done[nxid] != true)
        q.push(nxid);
    }
  }
}

int val(void) {
  int ans = 0;
  for (int i = 0; i < ab.size(); i++)
    ans += min(nodes[ab[i].first], nodes[ab[i].second]);
  return ans;
}

int main() {
  int n;
  cin >> n;

  to = vector<vector<int>>(n);

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ab.push_back(make_pair(a, b));
    to[a].push_back(b);
    to[b].push_back(a);
  }
  c = vector<int>(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());

  int ans = 0;
  int ansid = 0;

  for (int i = 0; i < 1; i++) {
    done = vector<bool>(n, false);
    nodes = vector<int>(n, 0);
    nodecount = 0;
    bfs(i);
    int tmp = val();
    if (tmp > ans) {
      ans = tmp;
      ansid = i;
    }
  }

  cout << ans << endl;

  done = vector<bool>(n, false);
  nodes = vector<int>(n, 0);
  nodecount = 0;
  bfs(ansid);
  for (int i = 0; i < n; i++)
    cout << nodes[i] << " ";
  cout << endl;
}