답안 #977638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
977638 2024-05-08T08:00:39 Z Ning07 자매 도시 (APIO20_swap) C++17
37 / 100
2000 ms 13760 KB
#include "swap.h"

#include <vector>
#include<bits/stdc++.h>
using namespace std;

#define L(i, j, k) for (int i = (j); i <= k; i++) 
#define R(i, j, k) for (int i = (j); i >= k; i--) 

struct dsu {
   vector<int> fa, sz;
   vector<bool> ok;
   dsu(int n) {
      fa.resize(n + 10);
      iota(fa.begin(), fa.end(), 0);
      sz.assign(n + 10, 1);
      ok.assign(n + 10, false);
   }
   int find(int x) {
      while (x != fa[x]) x = fa[x] = fa[fa[x]];
      return x;
   }
   bool merge(int x, int y) {
      int px = find(x), py = find(y);
      if (px == py) return false;
      if (sz[py] > sz[px]) swap(px, py);
      sz[px] += sz[py]; fa[py] = px;
      ok[px] = ok[px] || ok[py];
      return true;
   }
   bool same(int x, int y) {
      int px = find(x), py = find(y);
      return px == py;
   }
};

const int nax = 100050;

int N, M;
vector<pair<int, int>> G[nax];
vector<pair<int, pair<int, int>>> edges; 

void init(int _N, int _M, std::vector<int> _U, std::vector<int> _V, std::vector<int> _W) {
   N = _N; M = _M;
   L(i, 0, M - 1) edges.push_back(make_pair(_W[i], make_pair(_U[i], _V[i])));
   sort(edges.begin(), edges.end());
}

int getMinimumFuelCapacity(int X, int Y) {
   // in order to speed up,
   // we want to check connectivity, cycle, and whether the degree >= 3

   dsu D(N);
   vector<int> deg(N);
   for (int i = 0; i < edges.size(); i++) {
      int w = edges[i].first;
      int u = edges[i].second.first, v = edges[i].second.second;
      if (D.same(u, v)) D.ok[D.find(u)] = true; // cycle is formed
      deg[u]++; deg[v]++;
      if (deg[u] >= 3) D.ok[D.find(u)] = true; // degree >= 3
      if (deg[v] >= 3) D.ok[D.find(v)] = true; // degree >= 3
      D.merge(u, v); // merge them
      if (D.same(X, Y) && D.ok[D.find(X)] == true)  // if they are in the same component and its ok to swap them
         return w;
   }
   return -1;
}

Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |    for (int i = 0; i < edges.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2812 KB Output is correct
9 Correct 30 ms 7116 KB Output is correct
10 Correct 50 ms 7748 KB Output is correct
11 Correct 48 ms 7852 KB Output is correct
12 Correct 49 ms 7880 KB Output is correct
13 Correct 44 ms 7968 KB Output is correct
14 Execution timed out 2025 ms 7636 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Execution timed out 2095 ms 10600 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2812 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2908 KB Output is correct
25 Correct 2 ms 2904 KB Output is correct
26 Correct 2 ms 2904 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2812 KB Output is correct
10 Correct 30 ms 7116 KB Output is correct
11 Correct 50 ms 7748 KB Output is correct
12 Correct 48 ms 7852 KB Output is correct
13 Correct 49 ms 7880 KB Output is correct
14 Correct 44 ms 7968 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2908 KB Output is correct
30 Correct 2 ms 2904 KB Output is correct
31 Correct 2 ms 2904 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 2 ms 2908 KB Output is correct
34 Correct 6 ms 3672 KB Output is correct
35 Correct 43 ms 7952 KB Output is correct
36 Correct 44 ms 7908 KB Output is correct
37 Correct 44 ms 7912 KB Output is correct
38 Correct 44 ms 7856 KB Output is correct
39 Correct 47 ms 7628 KB Output is correct
40 Correct 42 ms 7540 KB Output is correct
41 Correct 46 ms 7880 KB Output is correct
42 Correct 46 ms 8160 KB Output is correct
43 Correct 38 ms 7884 KB Output is correct
44 Correct 48 ms 8016 KB Output is correct
45 Correct 59 ms 11208 KB Output is correct
46 Correct 43 ms 7880 KB Output is correct
47 Correct 55 ms 7880 KB Output is correct
48 Correct 46 ms 8136 KB Output is correct
49 Correct 50 ms 11208 KB Output is correct
50 Correct 40 ms 8652 KB Output is correct
51 Correct 50 ms 8908 KB Output is correct
52 Correct 71 ms 12488 KB Output is correct
53 Correct 74 ms 13760 KB Output is correct
54 Correct 76 ms 12740 KB Output is correct
55 Correct 39 ms 7884 KB Output is correct
56 Correct 78 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2812 KB Output is correct
9 Correct 30 ms 7116 KB Output is correct
10 Correct 50 ms 7748 KB Output is correct
11 Correct 48 ms 7852 KB Output is correct
12 Correct 49 ms 7880 KB Output is correct
13 Correct 44 ms 7968 KB Output is correct
14 Execution timed out 2025 ms 7636 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2812 KB Output is correct
10 Correct 30 ms 7116 KB Output is correct
11 Correct 50 ms 7748 KB Output is correct
12 Correct 48 ms 7852 KB Output is correct
13 Correct 49 ms 7880 KB Output is correct
14 Correct 44 ms 7968 KB Output is correct
15 Execution timed out 2025 ms 7636 KB Time limit exceeded
16 Halted 0 ms 0 KB -