답안 #858410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
858410 2023-10-08T12:33:33 Z evenvalue 가장 긴 여행 (IOI23_longesttrip) C++17
60 / 100
871 ms 1072 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;

using int64 = long long;
using ld = long double;

constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;

class dsu {
  int n;
  std::vector<int> e;

  int pfind(const int x) {
    return (e[x] < 0 ? x : e[x] = pfind(e[x]));
  }

public:
  dsu() : n(0), comp(0) {}
  explicit dsu(const int n) : n(n), comp(n), e(n, -1) {}

  int comp;

  int find(const int x) {
    assert(0 <= x and x < n);
    return pfind(x);
  }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return false;
    if (e[x] > e[y]) swap(x, y);
    e[x] += e[y];
    e[y] = x;
    comp--;
    return true;
  }

  bool same(const int x, const int y) {
    return (find(x) == find(y));
  }

  int size(const int x) {
    return -e[find(x)];
  }

  std::vector<std::vector<int>> components() {
    std::vector<std::vector<int>> res(n);
    for (int x = 0; x < n; x++) {
      res[pfind(x)].push_back(x);
    }
    res.erase(
        std::remove_if(res.begin(), res.end(), [&](const std::vector<int> &v) { return v.empty(); }),
        res.end());
    return res;
  }
};

std::vector<int> longest_trip(const int n, int D) {
  vector<vector<bool>> edge(n, vector<bool>(n));
  dsu g(n);
  for (int x = 0; x < n; x++) {
    for (int y = x + 1; y < n; y++) {
      if (not are_connected({x}, {y})) continue;
      edge[x][y] = edge[y][x] = true;
      g.unite(x, y);
    }
  }

  if (g.comp == 2) {
    auto comps = g.components();
    return (comps[0].size() > comps[1].size() ? comps[0] : comps[1]);
  }

  vector<int> path;
  vector<int> nbr;
  vector<bool> visit(n);

  path.push_back(0);
  visit[0] = true;

  for (int x = 0; x < n; x++) {
    if (edge[0][x]) nbr.push_back(x);
  }

  while (not nbr.empty()) {
    const int x = nbr.back();
    nbr.pop_back();

    if (visit[x]) continue;
    visit[x] = true;

    if (edge[x][path.front()]) {
      path.insert(path.begin(), x);
    } else if (edge[x][path.back()]) {
      path.insert(path.end(), x);
    } else {
      int y = 0;
      for (; y < n; y++) {
        if (not visit[y]) continue;
        if (not edge[x][y]) continue;
        break;
      }

      const auto it = find(path.begin(), path.end(), y);
      rotate(path.begin(), it, path.end());
      path.insert(path.begin(), x);
    }

    for (int y = 0; y < n; y++) {
      if (edge[y][x]) nbr.push_back(y);
    }
  }

  return path;
}

Compilation message

longesttrip.cpp: In constructor 'dsu::dsu(int)':
longesttrip.cpp:29:7: warning: 'dsu::comp' will be initialized after [-Wreorder]
   29 |   int comp;
      |       ^~~~
longesttrip.cpp:19:20: warning:   'std::vector<int> dsu::e' [-Wreorder]
   19 |   std::vector<int> e;
      |                    ^
longesttrip.cpp:27:12: warning:   when initialized here [-Wreorder]
   27 |   explicit dsu(const int n) : n(n), comp(n), e(n, -1) {}
      |            ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 176 ms 696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 22 ms 344 KB Output is correct
3 Correct 122 ms 600 KB Output is correct
4 Correct 367 ms 344 KB Output is correct
5 Correct 705 ms 820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 20 ms 344 KB Output is correct
3 Correct 124 ms 600 KB Output is correct
4 Correct 350 ms 420 KB Output is correct
5 Correct 749 ms 820 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 20 ms 344 KB Output is correct
8 Correct 120 ms 600 KB Output is correct
9 Correct 274 ms 600 KB Output is correct
10 Correct 777 ms 824 KB Output is correct
11 Correct 741 ms 696 KB Output is correct
12 Correct 715 ms 572 KB Output is correct
13 Correct 692 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 29 ms 344 KB Output is correct
3 Correct 121 ms 600 KB Output is correct
4 Correct 355 ms 592 KB Output is correct
5 Correct 773 ms 820 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 30 ms 344 KB Output is correct
8 Correct 119 ms 600 KB Output is correct
9 Correct 256 ms 424 KB Output is correct
10 Correct 678 ms 820 KB Output is correct
11 Correct 716 ms 692 KB Output is correct
12 Correct 691 ms 692 KB Output is correct
13 Correct 719 ms 692 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 12 ms 344 KB Output is correct
16 Correct 36 ms 600 KB Output is correct
17 Correct 87 ms 600 KB Output is correct
18 Correct 114 ms 600 KB Output is correct
19 Correct 280 ms 848 KB Output is correct
20 Correct 267 ms 592 KB Output is correct
21 Correct 720 ms 704 KB Output is correct
22 Correct 699 ms 700 KB Output is correct
23 Correct 690 ms 564 KB Output is correct
24 Correct 729 ms 564 KB Output is correct
25 Correct 10 ms 344 KB Output is correct
26 Correct 8 ms 344 KB Output is correct
27 Correct 22 ms 344 KB Output is correct
28 Correct 22 ms 344 KB Output is correct
29 Correct 26 ms 344 KB Output is correct
30 Correct 163 ms 444 KB Output is correct
31 Correct 166 ms 420 KB Output is correct
32 Correct 152 ms 436 KB Output is correct
33 Correct 257 ms 344 KB Output is correct
34 Correct 249 ms 420 KB Output is correct
35 Correct 256 ms 592 KB Output is correct
36 Correct 647 ms 440 KB Output is correct
37 Correct 721 ms 444 KB Output is correct
38 Correct 681 ms 444 KB Output is correct
39 Correct 695 ms 448 KB Output is correct
40 Correct 678 ms 436 KB Output is correct
41 Correct 655 ms 444 KB Output is correct
42 Correct 713 ms 448 KB Output is correct
43 Correct 701 ms 444 KB Output is correct
44 Correct 703 ms 440 KB Output is correct
45 Correct 9 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 32 ms 344 KB Output is correct
48 Correct 24 ms 344 KB Output is correct
49 Correct 22 ms 344 KB Output is correct
50 Correct 149 ms 436 KB Output is correct
51 Correct 154 ms 448 KB Output is correct
52 Correct 165 ms 444 KB Output is correct
53 Correct 246 ms 592 KB Output is correct
54 Correct 262 ms 436 KB Output is correct
55 Correct 249 ms 416 KB Output is correct
56 Correct 688 ms 704 KB Output is correct
57 Correct 684 ms 688 KB Output is correct
58 Correct 679 ms 568 KB Output is correct
59 Correct 698 ms 564 KB Output is correct
60 Correct 683 ms 560 KB Output is correct
61 Correct 686 ms 588 KB Output is correct
62 Correct 674 ms 436 KB Output is correct
63 Correct 709 ms 548 KB Output is correct
64 Correct 696 ms 536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 20 ms 344 KB Output is correct
3 Partially correct 120 ms 852 KB Output is partially correct
4 Partially correct 342 ms 424 KB Output is partially correct
5 Partially correct 730 ms 1072 KB Output is partially correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 23 ms 344 KB Output is correct
8 Partially correct 121 ms 436 KB Output is partially correct
9 Partially correct 259 ms 420 KB Output is partially correct
10 Partially correct 696 ms 824 KB Output is partially correct
11 Partially correct 695 ms 848 KB Output is partially correct
12 Partially correct 686 ms 692 KB Output is partially correct
13 Partially correct 697 ms 692 KB Output is partially correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 36 ms 600 KB Output is correct
17 Partially correct 70 ms 600 KB Output is partially correct
18 Partially correct 117 ms 344 KB Output is partially correct
19 Partially correct 239 ms 676 KB Output is partially correct
20 Partially correct 256 ms 592 KB Output is partially correct
21 Correct 9 ms 344 KB Output is correct
22 Correct 8 ms 344 KB Output is correct
23 Correct 20 ms 344 KB Output is correct
24 Correct 23 ms 344 KB Output is correct
25 Correct 24 ms 344 KB Output is correct
26 Partially correct 158 ms 436 KB Output is partially correct
27 Partially correct 150 ms 440 KB Output is partially correct
28 Partially correct 158 ms 436 KB Output is partially correct
29 Partially correct 246 ms 344 KB Output is partially correct
30 Partially correct 247 ms 600 KB Output is partially correct
31 Partially correct 242 ms 420 KB Output is partially correct
32 Correct 10 ms 344 KB Output is correct
33 Correct 8 ms 344 KB Output is correct
34 Correct 21 ms 344 KB Output is correct
35 Correct 25 ms 344 KB Output is correct
36 Correct 16 ms 344 KB Output is correct
37 Partially correct 164 ms 436 KB Output is partially correct
38 Partially correct 156 ms 440 KB Output is partially correct
39 Partially correct 166 ms 440 KB Output is partially correct
40 Partially correct 246 ms 592 KB Output is partially correct
41 Partially correct 255 ms 420 KB Output is partially correct
42 Partially correct 262 ms 432 KB Output is partially correct
43 Partially correct 740 ms 564 KB Output is partially correct
44 Partially correct 695 ms 564 KB Output is partially correct
45 Partially correct 702 ms 708 KB Output is partially correct
46 Partially correct 715 ms 588 KB Output is partially correct
47 Partially correct 695 ms 448 KB Output is partially correct
48 Partially correct 680 ms 440 KB Output is partially correct
49 Partially correct 685 ms 444 KB Output is partially correct
50 Partially correct 698 ms 440 KB Output is partially correct
51 Partially correct 649 ms 432 KB Output is partially correct
52 Partially correct 698 ms 440 KB Output is partially correct
53 Partially correct 715 ms 444 KB Output is partially correct
54 Partially correct 738 ms 440 KB Output is partially correct
55 Partially correct 751 ms 440 KB Output is partially correct
56 Partially correct 727 ms 452 KB Output is partially correct
57 Partially correct 701 ms 452 KB Output is partially correct
58 Partially correct 745 ms 444 KB Output is partially correct
59 Partially correct 719 ms 452 KB Output is partially correct
60 Partially correct 780 ms 592 KB Output is partially correct
61 Partially correct 780 ms 440 KB Output is partially correct
62 Partially correct 800 ms 688 KB Output is partially correct
63 Partially correct 790 ms 596 KB Output is partially correct
64 Partially correct 751 ms 564 KB Output is partially correct
65 Partially correct 750 ms 568 KB Output is partially correct
66 Partially correct 764 ms 556 KB Output is partially correct
67 Partially correct 762 ms 436 KB Output is partially correct
68 Partially correct 722 ms 440 KB Output is partially correct
69 Partially correct 757 ms 552 KB Output is partially correct
70 Partially correct 771 ms 536 KB Output is partially correct
71 Partially correct 760 ms 692 KB Output is partially correct
72 Partially correct 773 ms 564 KB Output is partially correct
73 Partially correct 802 ms 740 KB Output is partially correct
74 Partially correct 768 ms 568 KB Output is partially correct
75 Partially correct 795 ms 548 KB Output is partially correct
76 Partially correct 699 ms 556 KB Output is partially correct
77 Partially correct 871 ms 692 KB Output is partially correct
78 Partially correct 768 ms 888 KB Output is partially correct
79 Partially correct 769 ms 560 KB Output is partially correct
80 Partially correct 734 ms 568 KB Output is partially correct
81 Partially correct 708 ms 432 KB Output is partially correct
82 Partially correct 794 ms 436 KB Output is partially correct