Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int belong_group_num[100001] = {0};
int group_member_cnt[100001] = {0};

vector<vector<int>> frineds_group;

void make_group(const int person, const int group_num,
                vector<vector<int>> friends_link) {
  if (belong_group_num[person] != 0)
    return;

  belong_group_num[person] = group_num;
  group_member_cnt[group_num]++;
  for (int i = 0; i < friends_link[person].size(); i++) {
    int next_member = friends_link[person][i];
    make_group(next_member, group_num, friends_link);
  }
}

int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<int>> friends_link(N + 1);
  vector<vector<int>> blocks(N + 1);

  for (int i = 1; i <= N; i++) {
    belong_group_num[i] = 0;
    group_member_cnt[i] = 0;
  }

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    friends_link[A].push_back(B);
    friends_link[B].push_back(A);
  }

  for (int i = 0; i < K; i++) {
    int A, B;
    cin >> A >> B;
    blocks[A].push_back(B);
    blocks[B].push_back(A);
  }

  int group_cnt = 0;
  frineds_group.clear();
  for (int i = 1; i <= N; i++) {
    if (belong_group_num[i] != 0)
      continue;

    group_cnt++;
    make_group(i, group_cnt, friends_link);
  }

  // output answer
  for (int i = 1; i <= N; i++) {
    int ans = group_member_cnt[belong_group_num[i]];
    ans -= friends_link[i].size(); // 直接のお友達を引く
    ans -= 1;                      // 自分自身を引く

    // blockしている人を引く
    for (int j = 0; j < blocks[i].size(); j++) {
      int block_member = blocks[i][j];
      if (belong_group_num[block_member] == belong_group_num[i]) {
        ans--;
      }
    }
    cout << ans << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int belong_group_num[100001] = {0};
int group_member_cnt[100001] = {0};

vector<vector<int>> frineds_group;

void make_group(const int person, const int group_num,
                const vector<vector<int>> &friends_link) {
  if (belong_group_num[person] != 0)
    return;

  belong_group_num[person] = group_num;
  group_member_cnt[group_num]++;
  for (int i = 0; i < friends_link[person].size(); i++) {
    int next_member = friends_link[person][i];
    make_group(next_member, group_num, friends_link);
  }
}

int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<int>> friends_link(N + 1);
  vector<vector<int>> blocks(N + 1);

  for (int i = 1; i <= N; i++) {
    belong_group_num[i] = 0;
    group_member_cnt[i] = 0;
  }

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    friends_link[A].push_back(B);
    friends_link[B].push_back(A);
  }

  for (int i = 0; i < K; i++) {
    int A, B;
    cin >> A >> B;
    blocks[A].push_back(B);
    blocks[B].push_back(A);
  }

  int group_cnt = 0;
  frineds_group.clear();
  for (int i = 1; i <= N; i++) {
    if (belong_group_num[i] != 0)
      continue;

    group_cnt++;
    make_group(i, group_cnt, friends_link);
  }

  // output answer
  for (int i = 1; i <= N; i++) {
    int ans = group_member_cnt[belong_group_num[i]];
    ans -= friends_link[i].size(); // 直接のお友達を引く
    ans -= 1;                      // 自分自身を引く

    // blockしている人を引く
    for (int j = 0; j < blocks[i].size(); j++) {
      int block_member = blocks[i][j];
      if (belong_group_num[block_member] == belong_group_num[i]) {
        ans--;
      }
    }
    cout << ans << " ";
  }
  cout << endl;
  return 0;
}