Submission #18265545


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;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; 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;
#define SZ(x) (int)((x).size())
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 = 1e5 + 10;
vector<ii> a[N];
int c[N];
bool visit[N];

void DFS(int u, int up) {
  visit[u] = true;
  for (auto& [v, w] : a[u]) {
    if (visit[v]) continue;
    if (w != up || c[u] == 0) c[v] = w;
    DFS(v, w);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    --x; --y;
    a[x].push_back({y, z});
    a[y].push_back({x, z});
  }
  DFS(0, 0);
  set<int> unused;
  for (int i = 1; i <= n; ++i) unused.insert(i);
  for (int i = 0; i < n; ++i) {
    if (c[i]) unused.erase(c[i]);
  }
  for (int i = 0; i < n; ++i) {
    if (!c[i]) {
      auto it = unused.begin();
      c[i] = *it;
      unused.erase(it);
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << c[i] << '\n';
  }

  return 0;
}

Submission Info

Submission Time
Task C - Keep Graph Connected
User cuiaoxiang
Language C++ (Clang 10.0.0)
Score 500
Code Size 3308 Byte
Status AC
Exec Time 399 ms
Memory 18136 KB

Compile Error

./Main.cpp:78:11: warning: unused variable 'INF' [-Wunused-const-variable]
const int INF = 1 << 29;
          ^
./Main.cpp:79:11: warning: unused variable 'MOD' [-Wunused-const-variable]
const int MOD = 1e9 + 7;
          ^
2 warnings generated.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 86
Set Name Test Cases
Sample sample_01.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, hand_16.txt, hand_17.txt, hand_18.txt, hand_19.txt, hand_20.txt, hand_21.txt, hand_22.txt, hand_23.txt, hand_24.txt, hand_25.txt, hand_26.txt, hand_27.txt, hand_28.txt, hand_29.txt, hand_30.txt, hand_31.txt, hand_32.txt, hand_33.txt, hand_34.txt, hand_35.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, random_41.txt, random_42.txt, random_43.txt, random_44.txt, random_45.txt, random_46.txt, random_47.txt, random_48.txt, random_49.txt, random_50.txt, sample_01.txt
Case Name Status Exec Time Memory
hand_01.txt AC 17 ms 5512 KB
hand_02.txt AC 7 ms 5516 KB
hand_03.txt AC 8 ms 5440 KB
hand_04.txt AC 10 ms 5484 KB
hand_05.txt AC 6 ms 5496 KB
hand_06.txt AC 5 ms 5516 KB
hand_07.txt AC 7 ms 5496 KB
hand_08.txt AC 7 ms 5516 KB
hand_09.txt AC 7 ms 5448 KB
hand_10.txt AC 6 ms 5440 KB
hand_11.txt AC 232 ms 15484 KB
hand_12.txt AC 188 ms 12064 KB
hand_13.txt AC 189 ms 14484 KB
hand_14.txt AC 88 ms 8600 KB
hand_15.txt AC 112 ms 9992 KB
hand_16.txt AC 124 ms 9372 KB
hand_17.txt AC 59 ms 7900 KB
hand_18.txt AC 204 ms 14012 KB
hand_19.txt AC 116 ms 10432 KB
hand_20.txt AC 154 ms 10808 KB
hand_21.txt AC 97 ms 10232 KB
hand_22.txt AC 182 ms 13040 KB
hand_23.txt AC 160 ms 13072 KB
hand_24.txt AC 190 ms 12344 KB
hand_25.txt AC 200 ms 15012 KB
hand_26.txt AC 40 ms 6588 KB
hand_27.txt AC 53 ms 7560 KB
hand_28.txt AC 235 ms 14000 KB
hand_29.txt AC 175 ms 13284 KB
hand_30.txt AC 71 ms 7932 KB
hand_31.txt AC 265 ms 17320 KB
hand_32.txt AC 238 ms 14264 KB
hand_33.txt AC 243 ms 17172 KB
hand_34.txt AC 203 ms 14276 KB
hand_35.txt AC 266 ms 17184 KB
random_01.txt AC 7 ms 5460 KB
random_02.txt AC 8 ms 5496 KB
random_03.txt AC 6 ms 5456 KB
random_04.txt AC 9 ms 5456 KB
random_05.txt AC 6 ms 5456 KB
random_06.txt AC 10 ms 5464 KB
random_07.txt AC 5 ms 5544 KB
random_08.txt AC 12 ms 5456 KB
random_09.txt AC 6 ms 5456 KB
random_10.txt AC 5 ms 5376 KB
random_11.txt AC 10 ms 5368 KB
random_12.txt AC 6 ms 5416 KB
random_13.txt AC 6 ms 5556 KB
random_14.txt AC 7 ms 5404 KB
random_15.txt AC 5 ms 5364 KB
random_16.txt AC 5 ms 5408 KB
random_17.txt AC 4 ms 5516 KB
random_18.txt AC 5 ms 5380 KB
random_19.txt AC 5 ms 5500 KB
random_20.txt AC 8 ms 5464 KB
random_21.txt AC 18 ms 5624 KB
random_22.txt AC 14 ms 5560 KB
random_23.txt AC 27 ms 5544 KB
random_24.txt AC 16 ms 5648 KB
random_25.txt AC 9 ms 5512 KB
random_26.txt AC 14 ms 5584 KB
random_27.txt AC 9 ms 5456 KB
random_28.txt AC 13 ms 5644 KB
random_29.txt AC 16 ms 5604 KB
random_30.txt AC 23 ms 5696 KB
random_31.txt AC 90 ms 8536 KB
random_32.txt AC 35 ms 6400 KB
random_33.txt AC 46 ms 6764 KB
random_34.txt AC 146 ms 10992 KB
random_35.txt AC 95 ms 8780 KB
random_36.txt AC 75 ms 8008 KB
random_37.txt AC 78 ms 8308 KB
random_38.txt AC 182 ms 12472 KB
random_39.txt AC 134 ms 10152 KB
random_40.txt AC 190 ms 12084 KB
random_41.txt AC 187 ms 12056 KB
random_42.txt AC 211 ms 13564 KB
random_43.txt AC 246 ms 14228 KB
random_44.txt AC 399 ms 18136 KB
random_45.txt AC 256 ms 14824 KB
random_46.txt AC 103 ms 8800 KB
random_47.txt AC 376 ms 17664 KB
random_48.txt AC 386 ms 18112 KB
random_49.txt AC 228 ms 14872 KB
random_50.txt AC 163 ms 11628 KB
sample_01.txt AC 9 ms 5500 KB