답안 #306811

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
306811 2020-09-26T10:15:13 Z quocnguyen1012 자매 도시 (APIO20_swap) C++14
100 / 100
664 ms 53488 KB
#ifndef LOCAL
#include "swap.h"
#endif // LOCAl
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 2e5 + 5;

class DSU
{
public:
  class node
  {
  public:
    int pa, max_deg, cycle, updated;
    bool operator < (const node & other) const
    {
      return updated < other.updated;
    }
  };
  vector<node> nodes[maxn];
  vector<int> memb[maxn];
  int deg[maxn];
  void init(int n)
  {
    for(int i = 0; i < n; ++i){
      memb[i].clear();
      nodes[i].clear();
      memb[i].pb(i);
      nodes[i].pb({i, 0, 0, -1});
    }
  }
  int finds(int u)
  {
    return nodes[u].back().pa;
  }
  void add_edge(int u, int v, int i)
  {
    deg[u]++; deg[v]++;
    int new_deg = max(deg[u], deg[v]);
    u = finds(u); v = finds(v);
    if(u != v){
      if(memb[u].size() < memb[v].size()) swap(u, v);
      for(auto & x : memb[v]){
        node prv = nodes[x].back();
        prv.pa = u;
        prv.updated = i;
        nodes[x].pb(prv);
        memb[u].pb(x);
      }
      node prv = nodes[u].back();
      prv.max_deg = max(prv.max_deg, nodes[v].back().max_deg);
      prv.cycle |= nodes[v].back().cycle;
      prv.updated = i;
      nodes[u].pb(prv);
      memb[v].clear();
    }
    node prv = nodes[u].back();
    if(u == v)
      prv.cycle = true;
    prv.max_deg = max(prv.max_deg, new_deg);
    prv.updated = i;
    nodes[u].pb(prv);
  }
  node get_node(int u, int t)
  {
    return *--upper_bound(nodes[u].begin(), nodes[u].end(), (node){-1, -1, 0, t});
  }
}dsu;

vector<ar<int, 3>> edge;
int N, M;

bool isValid(int u, int v, int t)
{
  auto pu = dsu.get_node(u, t), pv = dsu.get_node(v, t);
  if(pu.pa != pv.pa) return false;
  auto val = dsu.get_node(pu.pa, t);
  return val.max_deg >= 3 || val.cycle;
}

void init(int _N, int _M,
		std::vector<int> U, std::vector<int> V, std::vector<int> W) {
  N = _N; M = _M;
  dsu.init(N);
  edge.resize(M);
  for(int i = 0; i < M; ++i){
    edge[i] = {W[i], U[i], V[i]};
  }
  sort(edge.begin(), edge.end());
  for(int i = 0; i < M; ++i){
    dsu.add_edge(edge[i][1], edge[i][2], i);
  }
}

int getMinimumFuelCapacity(int X, int Y) {
  int lo = 0, hi = M - 1;
  while(lo <= hi){
    int mid = (lo + hi) / 2;
    if(isValid(X, Y, mid)) hi = mid - 1;
    else lo = mid + 1;
  }
  if(lo == M) return -1;
  return edge[lo][0];
}

#ifdef LOCAL
signed main(void){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  int N, M; cin >> N >> M;
  vector<int> U(M), V(M), W(M);
  for(int i = 0; i < M; ++i){
    cin >> U[i];
  }
  for(int i = 0; i < M; ++i){
    cin >> V[i];
  }
  for(int i = 0; i < M; ++i){
    cin >> W[i];
  }
  init(N, M, U, V, W);
  int Q; cin >> Q;
  while(Q--){
    int x, y; cin >> x >> y;
    cout << getMinimumFuelCapacity(x, y) << '\n';
  }
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 9 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 9 ms 9984 KB Output is correct
8 Correct 8 ms 9984 KB Output is correct
9 Correct 222 ms 37740 KB Output is correct
10 Correct 292 ms 43136 KB Output is correct
11 Correct 270 ms 42612 KB Output is correct
12 Correct 289 ms 44656 KB Output is correct
13 Correct 148 ms 31956 KB Output is correct
14 Correct 236 ms 37488 KB Output is correct
15 Correct 520 ms 45944 KB Output is correct
16 Correct 493 ms 44704 KB Output is correct
17 Correct 516 ms 47192 KB Output is correct
18 Correct 352 ms 33728 KB Output is correct
19 Correct 172 ms 19064 KB Output is correct
20 Correct 509 ms 47084 KB Output is correct
21 Correct 509 ms 46000 KB Output is correct
22 Correct 530 ms 48340 KB Output is correct
23 Correct 356 ms 35152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 452 ms 31276 KB Output is correct
4 Correct 437 ms 31968 KB Output is correct
5 Correct 467 ms 31772 KB Output is correct
6 Correct 437 ms 31952 KB Output is correct
7 Correct 449 ms 31840 KB Output is correct
8 Correct 440 ms 31260 KB Output is correct
9 Correct 439 ms 31592 KB Output is correct
10 Correct 439 ms 31004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 9 ms 9984 KB Output is correct
7 Correct 8 ms 9984 KB Output is correct
8 Correct 9 ms 9984 KB Output is correct
9 Correct 8 ms 9984 KB Output is correct
10 Correct 8 ms 9984 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
12 Correct 8 ms 9984 KB Output is correct
13 Correct 9 ms 9984 KB Output is correct
14 Correct 9 ms 9916 KB Output is correct
15 Correct 8 ms 9984 KB Output is correct
16 Correct 8 ms 9984 KB Output is correct
17 Correct 8 ms 9984 KB Output is correct
18 Correct 8 ms 9984 KB Output is correct
19 Correct 8 ms 9968 KB Output is correct
20 Correct 9 ms 9984 KB Output is correct
21 Correct 9 ms 9984 KB Output is correct
22 Correct 8 ms 9856 KB Output is correct
23 Correct 8 ms 9856 KB Output is correct
24 Correct 9 ms 10112 KB Output is correct
25 Correct 9 ms 9984 KB Output is correct
26 Correct 9 ms 10112 KB Output is correct
27 Correct 8 ms 9984 KB Output is correct
28 Correct 9 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 9 ms 9984 KB Output is correct
7 Correct 8 ms 9984 KB Output is correct
8 Correct 9 ms 9984 KB Output is correct
9 Correct 8 ms 9984 KB Output is correct
10 Correct 222 ms 37740 KB Output is correct
11 Correct 292 ms 43136 KB Output is correct
12 Correct 270 ms 42612 KB Output is correct
13 Correct 289 ms 44656 KB Output is correct
14 Correct 148 ms 31956 KB Output is correct
15 Correct 8 ms 9984 KB Output is correct
16 Correct 9 ms 9984 KB Output is correct
17 Correct 8 ms 9984 KB Output is correct
18 Correct 9 ms 9984 KB Output is correct
19 Correct 9 ms 9916 KB Output is correct
20 Correct 8 ms 9984 KB Output is correct
21 Correct 8 ms 9984 KB Output is correct
22 Correct 8 ms 9984 KB Output is correct
23 Correct 8 ms 9984 KB Output is correct
24 Correct 27 ms 13816 KB Output is correct
25 Correct 285 ms 44400 KB Output is correct
26 Correct 289 ms 44016 KB Output is correct
27 Correct 276 ms 42736 KB Output is correct
28 Correct 269 ms 41200 KB Output is correct
29 Correct 256 ms 40688 KB Output is correct
30 Correct 243 ms 37496 KB Output is correct
31 Correct 295 ms 45548 KB Output is correct
32 Correct 295 ms 45548 KB Output is correct
33 Correct 149 ms 31336 KB Output is correct
34 Correct 253 ms 40176 KB Output is correct
35 Correct 8 ms 9968 KB Output is correct
36 Correct 9 ms 9984 KB Output is correct
37 Correct 9 ms 9984 KB Output is correct
38 Correct 8 ms 9856 KB Output is correct
39 Correct 8 ms 9856 KB Output is correct
40 Correct 9 ms 10112 KB Output is correct
41 Correct 9 ms 9984 KB Output is correct
42 Correct 9 ms 10112 KB Output is correct
43 Correct 8 ms 9984 KB Output is correct
44 Correct 9 ms 9984 KB Output is correct
45 Correct 206 ms 30992 KB Output is correct
46 Correct 279 ms 43704 KB Output is correct
47 Correct 242 ms 38024 KB Output is correct
48 Correct 201 ms 32356 KB Output is correct
49 Correct 98 ms 20712 KB Output is correct
50 Correct 83 ms 18288 KB Output is correct
51 Correct 169 ms 27888 KB Output is correct
52 Correct 330 ms 45440 KB Output is correct
53 Correct 296 ms 39636 KB Output is correct
54 Correct 369 ms 51184 KB Output is correct
55 Correct 146 ms 31448 KB Output is correct
56 Correct 272 ms 37728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 9 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 9 ms 9984 KB Output is correct
8 Correct 8 ms 9984 KB Output is correct
9 Correct 222 ms 37740 KB Output is correct
10 Correct 292 ms 43136 KB Output is correct
11 Correct 270 ms 42612 KB Output is correct
12 Correct 289 ms 44656 KB Output is correct
13 Correct 148 ms 31956 KB Output is correct
14 Correct 236 ms 37488 KB Output is correct
15 Correct 520 ms 45944 KB Output is correct
16 Correct 493 ms 44704 KB Output is correct
17 Correct 516 ms 47192 KB Output is correct
18 Correct 352 ms 33728 KB Output is correct
19 Correct 452 ms 31276 KB Output is correct
20 Correct 437 ms 31968 KB Output is correct
21 Correct 467 ms 31772 KB Output is correct
22 Correct 437 ms 31952 KB Output is correct
23 Correct 449 ms 31840 KB Output is correct
24 Correct 440 ms 31260 KB Output is correct
25 Correct 439 ms 31592 KB Output is correct
26 Correct 439 ms 31004 KB Output is correct
27 Correct 8 ms 9984 KB Output is correct
28 Correct 9 ms 9984 KB Output is correct
29 Correct 8 ms 9984 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
31 Correct 9 ms 9916 KB Output is correct
32 Correct 8 ms 9984 KB Output is correct
33 Correct 8 ms 9984 KB Output is correct
34 Correct 8 ms 9984 KB Output is correct
35 Correct 8 ms 9984 KB Output is correct
36 Correct 27 ms 13816 KB Output is correct
37 Correct 285 ms 44400 KB Output is correct
38 Correct 289 ms 44016 KB Output is correct
39 Correct 276 ms 42736 KB Output is correct
40 Correct 269 ms 41200 KB Output is correct
41 Correct 256 ms 40688 KB Output is correct
42 Correct 243 ms 37496 KB Output is correct
43 Correct 295 ms 45548 KB Output is correct
44 Correct 295 ms 45548 KB Output is correct
45 Correct 149 ms 31336 KB Output is correct
46 Correct 253 ms 40176 KB Output is correct
47 Correct 43 ms 14032 KB Output is correct
48 Correct 537 ms 47416 KB Output is correct
49 Correct 518 ms 45892 KB Output is correct
50 Correct 533 ms 45608 KB Output is correct
51 Correct 509 ms 44644 KB Output is correct
52 Correct 483 ms 41760 KB Output is correct
53 Correct 404 ms 33508 KB Output is correct
54 Correct 525 ms 47452 KB Output is correct
55 Correct 529 ms 48600 KB Output is correct
56 Correct 424 ms 34368 KB Output is correct
57 Correct 522 ms 42968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 9 ms 9984 KB Output is correct
7 Correct 8 ms 9984 KB Output is correct
8 Correct 9 ms 9984 KB Output is correct
9 Correct 8 ms 9984 KB Output is correct
10 Correct 222 ms 37740 KB Output is correct
11 Correct 292 ms 43136 KB Output is correct
12 Correct 270 ms 42612 KB Output is correct
13 Correct 289 ms 44656 KB Output is correct
14 Correct 148 ms 31956 KB Output is correct
15 Correct 236 ms 37488 KB Output is correct
16 Correct 520 ms 45944 KB Output is correct
17 Correct 493 ms 44704 KB Output is correct
18 Correct 516 ms 47192 KB Output is correct
19 Correct 352 ms 33728 KB Output is correct
20 Correct 452 ms 31276 KB Output is correct
21 Correct 437 ms 31968 KB Output is correct
22 Correct 467 ms 31772 KB Output is correct
23 Correct 437 ms 31952 KB Output is correct
24 Correct 449 ms 31840 KB Output is correct
25 Correct 440 ms 31260 KB Output is correct
26 Correct 439 ms 31592 KB Output is correct
27 Correct 439 ms 31004 KB Output is correct
28 Correct 8 ms 9984 KB Output is correct
29 Correct 9 ms 9984 KB Output is correct
30 Correct 8 ms 9984 KB Output is correct
31 Correct 9 ms 9984 KB Output is correct
32 Correct 9 ms 9916 KB Output is correct
33 Correct 8 ms 9984 KB Output is correct
34 Correct 8 ms 9984 KB Output is correct
35 Correct 8 ms 9984 KB Output is correct
36 Correct 8 ms 9984 KB Output is correct
37 Correct 27 ms 13816 KB Output is correct
38 Correct 285 ms 44400 KB Output is correct
39 Correct 289 ms 44016 KB Output is correct
40 Correct 276 ms 42736 KB Output is correct
41 Correct 269 ms 41200 KB Output is correct
42 Correct 256 ms 40688 KB Output is correct
43 Correct 243 ms 37496 KB Output is correct
44 Correct 295 ms 45548 KB Output is correct
45 Correct 295 ms 45548 KB Output is correct
46 Correct 149 ms 31336 KB Output is correct
47 Correct 253 ms 40176 KB Output is correct
48 Correct 43 ms 14032 KB Output is correct
49 Correct 537 ms 47416 KB Output is correct
50 Correct 518 ms 45892 KB Output is correct
51 Correct 533 ms 45608 KB Output is correct
52 Correct 509 ms 44644 KB Output is correct
53 Correct 483 ms 41760 KB Output is correct
54 Correct 404 ms 33508 KB Output is correct
55 Correct 525 ms 47452 KB Output is correct
56 Correct 529 ms 48600 KB Output is correct
57 Correct 424 ms 34368 KB Output is correct
58 Correct 522 ms 42968 KB Output is correct
59 Correct 172 ms 19064 KB Output is correct
60 Correct 509 ms 47084 KB Output is correct
61 Correct 509 ms 46000 KB Output is correct
62 Correct 530 ms 48340 KB Output is correct
63 Correct 356 ms 35152 KB Output is correct
64 Correct 8 ms 9968 KB Output is correct
65 Correct 9 ms 9984 KB Output is correct
66 Correct 9 ms 9984 KB Output is correct
67 Correct 8 ms 9856 KB Output is correct
68 Correct 8 ms 9856 KB Output is correct
69 Correct 9 ms 10112 KB Output is correct
70 Correct 9 ms 9984 KB Output is correct
71 Correct 9 ms 10112 KB Output is correct
72 Correct 8 ms 9984 KB Output is correct
73 Correct 9 ms 9984 KB Output is correct
74 Correct 206 ms 30992 KB Output is correct
75 Correct 279 ms 43704 KB Output is correct
76 Correct 242 ms 38024 KB Output is correct
77 Correct 201 ms 32356 KB Output is correct
78 Correct 98 ms 20712 KB Output is correct
79 Correct 83 ms 18288 KB Output is correct
80 Correct 169 ms 27888 KB Output is correct
81 Correct 330 ms 45440 KB Output is correct
82 Correct 296 ms 39636 KB Output is correct
83 Correct 369 ms 51184 KB Output is correct
84 Correct 146 ms 31448 KB Output is correct
85 Correct 272 ms 37728 KB Output is correct
86 Correct 145 ms 19184 KB Output is correct
87 Correct 525 ms 44892 KB Output is correct
88 Correct 533 ms 45400 KB Output is correct
89 Correct 558 ms 33796 KB Output is correct
90 Correct 369 ms 22760 KB Output is correct
91 Correct 427 ms 23788 KB Output is correct
92 Correct 537 ms 30276 KB Output is correct
93 Correct 641 ms 47020 KB Output is correct
94 Correct 664 ms 41964 KB Output is correct
95 Correct 639 ms 53488 KB Output is correct
96 Correct 420 ms 34368 KB Output is correct
97 Correct 620 ms 37156 KB Output is correct