Submission #10684129


Source Code Expand

Copy
// #define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 2e5 + 10;
int c[N];
vector<int> a[N];
vector<int> A[3];

void DFS(int u, int parent) {
  for (auto& v : a[u]) {
    if (v == parent) continue;
    c[v] = 3 - c[u];
    DFS(v, u);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  // n = 10;
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x; --y;
    // x = 0; y = i;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  c[0] = 1;
  DFS(0, -1);
  for (int i = 0; i < n; ++i) {
    A[c[i]].push_back(i);
  }
  vector<int> cnt(3);
  vector<int> ret(n);
  vector<bool> used(n + 1);
  for (int i = 1; i <= n; ++i) cnt[i % 3]++;
  trace(A[0].size(), A[1].size(), A[2].size(), cnt[0], cnt[1], cnt[2]);
  if (A[1].size() <= cnt[0]) {
    int x = 3;
    for (auto& u : A[1]) {
      ret[u] = x;
      used[x] = true;
      x += 3;
    }
    x = 1;
    for (int i = 0; i < n; ++i) {
      if (ret[i]) continue;
      while (used[x]) x += 1;
      ret[i] = x++;
    }
  } else if (A[2].size() <= cnt[0]) {
    int x = 3;
    for (auto& u : A[2]) {
      ret[u] = x;
      used[x] = true;
      x += 3;
    }
    x = 1;
    for (int i = 0; i < n; ++i) {
      if (ret[i]) continue;
      while (used[x]) x += 1;
      ret[i] = x++;
    }
  } else {
    trace(A[1], A[2], cnt[1], cnt[2]);
    int x = 3;
    for (int i = 0; i < cnt[1]; ++i) {
      ret[A[1][i]] = 3 * i + 1;
    }
    for (int i = cnt[1]; i < A[1].size(); ++i) {
      ret[A[1][i]] = x;
      x += 3;
    }
    for (int i = 0; i < cnt[2]; ++i) {
      ret[A[2][i]] = 3 * i + 2;
    }
    for (int i = cnt[2]; i < A[2].size(); ++i) {
      ret[A[2][i]] = x;
      x += 3;
    }
  }
  for (int i = 0; i < n; ++i) {
    printf("%d%c", ret[i], " \n"[i + 1 == n]);
  }
  return 0;
}

Submission Info

Submission Time
Task C - ThREE
User cuiaoxiang
Language C++14 (GCC 5.4.1)
Score 600
Code Size 3992 Byte
Status AC
Exec Time 108 ms
Memory 22260 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:99:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
./Main.cpp:103:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &x, &y);
                          ^

Judge Result

Set Name All sample
Score / Max Score 600 / 600 0 / 0
Status
AC × 57
AC × 1
Set Name Test Cases
All 00_sample_01, 20_small_01, 20_small_02, 20_small_03, 20_small_04, 20_small_05, 30_large_01, 30_large_02, 30_large_03, 40_line_01, 40_line_02, 40_line_03, 50_star_01, 50_star_02, 50_star_03, 60_hand_01, 60_hand_02, 60_hand_03, 60_hand_04, 60_hand_05, 60_hand_06, 60_hand_07, 60_hand_08, 60_hand_09, 60_hand_10, 60_hand_11, 60_hand_12, 60_hand_13, 60_hand_14, 60_hand_15, 60_hand_16, 60_hand_17, 60_hand_18, 60_hand_19, 60_hand_20, 60_hand_21, 60_hand_22, 60_hand_23, 60_hand_24, 60_hand_25, 60_hand_26, 60_hand_27, 60_hand_28, 60_hand_29, 60_hand_30, 70_dense_01, 70_dense_02, 70_dense_03, 70_dense_04, 70_dense_05, 70_dense_06, 70_dense_07, 70_dense_08, 70_dense_09, 70_dense_10, 90_hand_01, 90_hand_02
sample 00_sample_01
Case Name Status Exec Time Memory
00_sample_01 AC 3 ms 4992 KB
20_small_01 AC 3 ms 4992 KB
20_small_02 AC 3 ms 4992 KB
20_small_03 AC 3 ms 4992 KB
20_small_04 AC 3 ms 4992 KB
20_small_05 AC 3 ms 4992 KB
30_large_01 AC 108 ms 15048 KB
30_large_02 AC 108 ms 15032 KB
30_large_03 AC 108 ms 15048 KB
40_line_01 AC 69 ms 22260 KB
40_line_02 AC 53 ms 15400 KB
40_line_03 AC 41 ms 13348 KB
50_star_01 AC 40 ms 11128 KB
50_star_02 AC 45 ms 11000 KB
50_star_03 AC 7 ms 5504 KB
60_hand_01 AC 24 ms 8124 KB
60_hand_02 AC 26 ms 8188 KB
60_hand_03 AC 6 ms 5504 KB
60_hand_04 AC 7 ms 5504 KB
60_hand_05 AC 34 ms 9496 KB
60_hand_06 AC 40 ms 10360 KB
60_hand_07 AC 32 ms 9212 KB
60_hand_08 AC 29 ms 8828 KB
60_hand_09 AC 13 ms 6400 KB
60_hand_10 AC 24 ms 8188 KB
60_hand_11 AC 44 ms 10868 KB
60_hand_12 AC 57 ms 12660 KB
60_hand_13 AC 35 ms 9468 KB
60_hand_14 AC 12 ms 6400 KB
60_hand_15 AC 25 ms 8316 KB
60_hand_16 AC 9 ms 5888 KB
60_hand_17 AC 18 ms 7168 KB
60_hand_18 AC 65 ms 13300 KB
60_hand_19 AC 8 ms 5760 KB
60_hand_20 AC 68 ms 14072 KB
60_hand_21 AC 26 ms 8060 KB
60_hand_22 AC 69 ms 13388 KB
60_hand_23 AC 66 ms 13392 KB
60_hand_24 AC 87 ms 15856 KB
60_hand_25 AC 50 ms 11616 KB
60_hand_26 AC 10 ms 5888 KB
60_hand_27 AC 43 ms 10912 KB
60_hand_28 AC 50 ms 11556 KB
60_hand_29 AC 6 ms 5504 KB
60_hand_30 AC 6 ms 5376 KB
70_dense_01 AC 28 ms 8572 KB
70_dense_02 AC 37 ms 9592 KB
70_dense_03 AC 30 ms 8824 KB
70_dense_04 AC 39 ms 9464 KB
70_dense_05 AC 36 ms 9228 KB
70_dense_06 AC 29 ms 8568 KB
70_dense_07 AC 32 ms 9336 KB
70_dense_08 AC 27 ms 8572 KB
70_dense_09 AC 11 ms 6144 KB
70_dense_10 AC 25 ms 7676 KB
90_hand_01 AC 3 ms 4992 KB
90_hand_02 AC 3 ms 4992 KB