Submission #642324

# Submission time Handle Problem Language Result Execution time Memory
642324 2022-09-19T08:39:18 Z andecaandeci Stranded Far From Home (BOI22_island) C++17
100 / 100
317 ms 42624 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
typedef long long ll;
// const ll mod = 1e9 + 7;
const ll MAXN = 1e6 + 5;
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define sc second
#define endl '\n'
#define gl                        ios_base::sync_with_stdio(0);   cin.tie(0);                     cout.tie(0)

int n, m;
vi par, s, r;
vll val;
vector<vi> g;

int fpar(int a)
{
  return par[a] = (par[a] == a ? a : fpar(par[a]));
}

void merge(int a, int b)
{
  a = fpar(a), b = fpar(b);
  if (a == b)
    return;
  if (r[a] < r[b])
    swap(a, b);
  if (r[a] == r[b])
    r[a]++;
  par[b] = a;
}

bool cmp(pii a, pii b)
{
  return max(s[a.fi], s[a.sc]) <= max(s[b.fi], s[b.sc]);
}

int main()
{
  gl;
  cin >> n >> m;
  s.resize(n + 1);
  set<int> st;
  for (int i = 1; i <= n; i++)
  {
    cin >> s[i];
    st.insert(s[i]);
  }

  vi v(st.begin(), st.end());
  vector<vector<pii>> e(v.size() + 1);

  for (int i = 0; i < m; i++)
  {
    int a, b;
    cin >> a >> b;
    e[lb(v.begin(), v.end(), max(s[a], s[b])) - v.begin()].push_back({a, b});
  }

  g.resize(n + 1), val.resize(n + 1), par.resize(n + 1), r.resize(n + 1);

  for (int i = 1; i <= n; i++)
  {
    g[i].pb(i);
    val[i] = s[i];
    par[i] = i;
  }

  for (int i = 0; i < v.size(); i++)
  {
    for (pii edge : e[i])
    {
      int a = edge.fi, b = edge.sc;
      int para = fpar(a), parb = fpar(b);
      if (para == parb)
        continue;
      merge(para, parb);
      int p = fpar(a);
      if (val[para] < s[b])
        swap(g[p], g[parb]);
      else if (val[parb] < s[a])
        swap(g[p], g[para]);
      else
      {
        if (g[para].size() < g[parb].size())
          swap(g[para], g[parb]);
        for (int x : g[parb])
          g[para].pb(x);
        swap(g[p], g[para]);
      }
      val[p] = val[para] + val[parb];
    }
  }

  vector<bool> ans(n + 1, 0);
  for (int v : g[fpar(1)])
    ans[v] = 1;
  for (int i = 1; i <= n; i++)
    cout << ans[i];
  cout << endl;
  return 0;
}

Compilation message

island.cpp: In function 'int main()':
island.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for (int i = 0; i < v.size(); i++)
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 243 ms 40208 KB Output is correct
4 Correct 132 ms 27068 KB Output is correct
5 Correct 217 ms 40408 KB Output is correct
6 Correct 215 ms 37380 KB Output is correct
7 Correct 233 ms 41708 KB Output is correct
8 Correct 128 ms 26940 KB Output is correct
9 Correct 221 ms 37828 KB Output is correct
10 Correct 125 ms 31684 KB Output is correct
11 Correct 99 ms 22104 KB Output is correct
12 Correct 119 ms 22016 KB Output is correct
13 Correct 131 ms 28180 KB Output is correct
14 Correct 119 ms 27768 KB Output is correct
15 Correct 198 ms 41956 KB Output is correct
16 Correct 149 ms 40804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 289 ms 42308 KB Output is correct
3 Correct 261 ms 38468 KB Output is correct
4 Correct 137 ms 29540 KB Output is correct
5 Correct 124 ms 27968 KB Output is correct
6 Correct 313 ms 42624 KB Output is correct
7 Correct 194 ms 41932 KB Output is correct
8 Correct 196 ms 41864 KB Output is correct
9 Correct 151 ms 40768 KB Output is correct
10 Correct 153 ms 32120 KB Output is correct
11 Correct 132 ms 25644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 146 ms 23200 KB Output is correct
3 Correct 121 ms 23580 KB Output is correct
4 Correct 133 ms 24920 KB Output is correct
5 Correct 130 ms 26176 KB Output is correct
6 Correct 128 ms 23828 KB Output is correct
7 Correct 103 ms 23096 KB Output is correct
8 Correct 72 ms 21552 KB Output is correct
9 Correct 63 ms 12868 KB Output is correct
10 Correct 132 ms 25432 KB Output is correct
11 Correct 113 ms 25016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 243 ms 40208 KB Output is correct
18 Correct 132 ms 27068 KB Output is correct
19 Correct 217 ms 40408 KB Output is correct
20 Correct 215 ms 37380 KB Output is correct
21 Correct 233 ms 41708 KB Output is correct
22 Correct 128 ms 26940 KB Output is correct
23 Correct 221 ms 37828 KB Output is correct
24 Correct 125 ms 31684 KB Output is correct
25 Correct 99 ms 22104 KB Output is correct
26 Correct 119 ms 22016 KB Output is correct
27 Correct 131 ms 28180 KB Output is correct
28 Correct 119 ms 27768 KB Output is correct
29 Correct 198 ms 41956 KB Output is correct
30 Correct 149 ms 40804 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 289 ms 42308 KB Output is correct
33 Correct 261 ms 38468 KB Output is correct
34 Correct 137 ms 29540 KB Output is correct
35 Correct 124 ms 27968 KB Output is correct
36 Correct 313 ms 42624 KB Output is correct
37 Correct 194 ms 41932 KB Output is correct
38 Correct 196 ms 41864 KB Output is correct
39 Correct 151 ms 40768 KB Output is correct
40 Correct 153 ms 32120 KB Output is correct
41 Correct 132 ms 25644 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 146 ms 23200 KB Output is correct
44 Correct 121 ms 23580 KB Output is correct
45 Correct 133 ms 24920 KB Output is correct
46 Correct 130 ms 26176 KB Output is correct
47 Correct 128 ms 23828 KB Output is correct
48 Correct 103 ms 23096 KB Output is correct
49 Correct 72 ms 21552 KB Output is correct
50 Correct 63 ms 12868 KB Output is correct
51 Correct 132 ms 25432 KB Output is correct
52 Correct 113 ms 25016 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 2 ms 596 KB Output is correct
57 Correct 2 ms 600 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
59 Correct 2 ms 600 KB Output is correct
60 Correct 2 ms 596 KB Output is correct
61 Correct 1 ms 472 KB Output is correct
62 Correct 2 ms 612 KB Output is correct
63 Correct 2 ms 724 KB Output is correct
64 Correct 2 ms 604 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 2 ms 468 KB Output is correct
67 Correct 233 ms 40104 KB Output is correct
68 Correct 123 ms 26956 KB Output is correct
69 Correct 232 ms 40420 KB Output is correct
70 Correct 251 ms 37400 KB Output is correct
71 Correct 230 ms 41644 KB Output is correct
72 Correct 138 ms 26920 KB Output is correct
73 Correct 189 ms 37836 KB Output is correct
74 Correct 125 ms 31792 KB Output is correct
75 Correct 110 ms 22160 KB Output is correct
76 Correct 136 ms 22016 KB Output is correct
77 Correct 127 ms 28132 KB Output is correct
78 Correct 122 ms 27840 KB Output is correct
79 Correct 197 ms 41900 KB Output is correct
80 Correct 148 ms 40836 KB Output is correct
81 Correct 296 ms 42336 KB Output is correct
82 Correct 247 ms 38544 KB Output is correct
83 Correct 149 ms 29604 KB Output is correct
84 Correct 126 ms 27952 KB Output is correct
85 Correct 295 ms 42528 KB Output is correct
86 Correct 221 ms 41920 KB Output is correct
87 Correct 182 ms 41960 KB Output is correct
88 Correct 154 ms 32112 KB Output is correct
89 Correct 130 ms 25568 KB Output is correct
90 Correct 136 ms 23156 KB Output is correct
91 Correct 139 ms 23612 KB Output is correct
92 Correct 132 ms 24992 KB Output is correct
93 Correct 133 ms 26088 KB Output is correct
94 Correct 126 ms 23812 KB Output is correct
95 Correct 100 ms 23084 KB Output is correct
96 Correct 70 ms 21512 KB Output is correct
97 Correct 61 ms 12928 KB Output is correct
98 Correct 130 ms 25540 KB Output is correct
99 Correct 114 ms 25036 KB Output is correct
100 Correct 46 ms 5292 KB Output is correct
101 Correct 263 ms 36928 KB Output is correct
102 Correct 113 ms 19968 KB Output is correct
103 Correct 210 ms 30444 KB Output is correct
104 Correct 317 ms 39536 KB Output is correct