답안 #1082525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082525 2024-08-31T14:48:01 Z juicy Izlet (COI19_izlet) C++17
100 / 100
496 ms 83284 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 3000;

int n, m;
int c[N][N], pr[N], a[N], cnt[N];
bool vis[N], same[N][N];
vector<int> g[N];

void add(int u, int v) {
  g[u].push_back(v);
  g[v].push_back(u);
}

int find(int u) {
  return pr[u] < 0 ? u : pr[u] = find(pr[u]);
}

void mrg(int u, int v) {
  if ((u = find(u)) == (v = find(v))) {
    return;
  }
  if (pr[u] > pr[v]) {
    swap(u, v);
  }
  pr[u] += pr[v];
  pr[v] = u;
  add(u, v);
} 

int check(int s) {
  memset(cnt, 0, sizeof(cnt));
  int active = 0, res = -1;
  function<void(int, int)> dfs = [&](int u, int p) {
    active += cnt[a[u]]++ == 0;
    if (res == -1 && active != c[s][u]) {
      res = a[u];
    }
    for (int v : g[u]) {
      if (vis[v] && v != p) {
        dfs(v, u);
      }
    }
    active -= --cnt[a[u]] == 0;
  };
  dfs(s, s);
  return ~res ? res : ++m;
}

void dfs(int u) {
  a[u] = check(u);
  vis[u] = 1;
  for (int v : g[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  int _; cin >> _ >> n;
  memset(pr, -1, sizeof(pr));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> c[i][j];
      if (c[i][j] == 1) {
        mrg(i, j);
      }
    }
  }
  vector<int> root;
  for (int i = 0; i < n; ++i) {
    if (i == find(i)) {
      root.push_back(i);
    }
  }
  int m = root.size();
  for (int i = 0; i < m; ++i) {
    for (int j = i + 1; j < m; ++j) {
      int u = root[i], v = root[j];
      if (c[u][v] == 2 && !same[u][v]) {
        vector<int> ver{u, v};
        for (int k = j + 1; k < m; ++k) {
          int x = root[k];
          if (c[u][x] == 2 && c[v][x] == 2) {
            ver.push_back(x);
          }
        }
        for (int k = 1; k < ver.size(); ++k) {
          for (int l = 0; l < k; ++l) {
            same[ver[k]][ver[l]] = 1;
            same[ver[l]][ver[k]] = 1;
          } 
          add(ver[0], ver[k]);
        }
      }
    }
  }
  dfs(0);
  for (int i = 0; i < n; ++i) {
    cout << a[find(i)] << " \n"[i + 1 == n];
  }
  for (int i = 0; i < n; ++i) {
    for (int j : g[i]) {
      if (i < j) {
        cout << i + 1 << " " << j + 1 << "\n";
      }
    }
  }
  return 0;
}

Compilation message

izlet.cpp: In function 'int main()':
izlet.cpp:99:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         for (int k = 1; k < ver.size(); ++k) {
      |                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 380 ms 62292 KB Output is correct
3 Correct 360 ms 62288 KB Output is correct
4 Correct 335 ms 59408 KB Output is correct
5 Correct 346 ms 61476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 365 ms 61188 KB Output is correct
2 Correct 363 ms 60500 KB Output is correct
3 Correct 445 ms 82768 KB Output is correct
4 Correct 496 ms 83284 KB Output is correct
5 Correct 360 ms 56600 KB Output is correct
6 Correct 372 ms 65540 KB Output is correct
7 Correct 283 ms 53844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 380 ms 62292 KB Output is correct
3 Correct 360 ms 62288 KB Output is correct
4 Correct 335 ms 59408 KB Output is correct
5 Correct 346 ms 61476 KB Output is correct
6 Correct 365 ms 61188 KB Output is correct
7 Correct 363 ms 60500 KB Output is correct
8 Correct 445 ms 82768 KB Output is correct
9 Correct 496 ms 83284 KB Output is correct
10 Correct 360 ms 56600 KB Output is correct
11 Correct 372 ms 65540 KB Output is correct
12 Correct 283 ms 53844 KB Output is correct
13 Correct 384 ms 62800 KB Output is correct
14 Correct 480 ms 69972 KB Output is correct
15 Correct 346 ms 58944 KB Output is correct
16 Correct 403 ms 62036 KB Output is correct
17 Correct 417 ms 64588 KB Output is correct
18 Correct 378 ms 62208 KB Output is correct
19 Correct 364 ms 61520 KB Output is correct
20 Correct 354 ms 60544 KB Output is correct
21 Correct 376 ms 61268 KB Output is correct
22 Correct 371 ms 61608 KB Output is correct
23 Correct 369 ms 60852 KB Output is correct
24 Correct 395 ms 67372 KB Output is correct
25 Correct 361 ms 59216 KB Output is correct