답안 #654580

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
654580 2022-10-31T19:05:51 Z atigun Stranded Far From Home (BOI22_island) C++14
100 / 100
298 ms 37996 KB
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

struct dsu{
  vector<int> parent;
  vector<ll> sum;
  vector<vector<int>> S;
  int n;
  dsu(int dsu_size) : n(dsu_size){
    parent.resize(n+5);
    iota(parent.begin(), parent.end(), 0);
    sum.resize(n+5, 0);
    S.resize(n+5);
  }
  int find(int v){
    return parent[v] = (parent[v] == v ? v : find(parent[v]));
  }
  void merge(int v, int u){
    if(S[find(u)].size() > S[find(v)].size())
      swap(u, v);
    if(find(u) == find(v))
      return;
    for(int x : S[find(u)])
      S[find(v)].push_back(x);
    S[find(u)].clear();
    sum[find(v)]+= sum[find(u)];
    parent[find(u)] = parent[find(v)];
  }
  ll comp_sum(int v){
    return sum[find(v)];
  }
};

const int maxn = 2e5;

int n, m;
dsu dsu(maxn+5);
vector<ll> a(maxn+5);
vector<vector<int>> g(maxn+5), new_g(maxn+5);
vector<bool> added(maxn+5), bad(maxn+5), vis(maxn+5);

void solve(){
  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    cin >> a[i];
    dsu.sum[dsu.find(i)] = a[i];
    dsu.S[dsu.find(i)] = {i};
  }
  for(int i = 0; i < m; i++){
    int v, u;
    cin >> v >> u;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  vector<pair<int, int>> query;
  query.reserve(n);
  for(int i = 1; i <= n; i++)
    query.push_back({a[i], i});
  sort(query.begin(), query.end());
  for(int i = 0; i < n; i++){
    int v = query[i].second;
    // cout << v << ": ";
    for(int u : g[v]){
      if(make_pair(a[u], u) < make_pair(a[v], v)){
        // cout << dsu.comp_sum(u) << "\n";
        if(dsu.comp_sum(u) < a[v]){
          dsu.S[dsu.find(u)].clear();
        }
      }
    }
    for(int u : g[v])
      if(make_pair(a[u], u) < make_pair(a[v], v))
        dsu.merge(u, v);
  }
  vector<bool> ans(n+5, 0);
  for(int x : dsu.S[dsu.find(n)])
    ans[x] = 1;
  for(int i = 1; i <= n; i++)
    cout << ans[i];
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  // cin >> tt;
  while(tt--){
    solve();
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 18388 KB Output is correct
2 Correct 13 ms 18384 KB Output is correct
3 Correct 11 ms 18388 KB Output is correct
4 Correct 11 ms 18536 KB Output is correct
5 Correct 11 ms 18572 KB Output is correct
6 Correct 12 ms 18516 KB Output is correct
7 Correct 10 ms 18560 KB Output is correct
8 Correct 13 ms 18552 KB Output is correct
9 Correct 11 ms 18568 KB Output is correct
10 Correct 11 ms 18680 KB Output is correct
11 Correct 11 ms 18516 KB Output is correct
12 Correct 14 ms 18564 KB Output is correct
13 Correct 11 ms 18516 KB Output is correct
14 Correct 11 ms 18516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 18388 KB Output is correct
2 Correct 12 ms 18388 KB Output is correct
3 Correct 198 ms 34504 KB Output is correct
4 Correct 200 ms 34348 KB Output is correct
5 Correct 222 ms 35780 KB Output is correct
6 Correct 248 ms 36448 KB Output is correct
7 Correct 226 ms 36268 KB Output is correct
8 Correct 211 ms 34492 KB Output is correct
9 Correct 213 ms 37560 KB Output is correct
10 Correct 131 ms 34204 KB Output is correct
11 Correct 168 ms 35572 KB Output is correct
12 Correct 206 ms 34608 KB Output is correct
13 Correct 167 ms 34416 KB Output is correct
14 Correct 182 ms 34504 KB Output is correct
15 Correct 190 ms 34336 KB Output is correct
16 Correct 108 ms 34292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 18388 KB Output is correct
2 Correct 248 ms 36940 KB Output is correct
3 Correct 244 ms 37168 KB Output is correct
4 Correct 202 ms 34372 KB Output is correct
5 Correct 182 ms 34228 KB Output is correct
6 Correct 268 ms 37272 KB Output is correct
7 Correct 168 ms 34340 KB Output is correct
8 Correct 176 ms 34376 KB Output is correct
9 Correct 111 ms 34216 KB Output is correct
10 Correct 203 ms 33936 KB Output is correct
11 Correct 213 ms 34472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 18352 KB Output is correct
2 Correct 252 ms 34964 KB Output is correct
3 Correct 258 ms 35424 KB Output is correct
4 Correct 276 ms 36408 KB Output is correct
5 Correct 273 ms 37096 KB Output is correct
6 Correct 224 ms 34996 KB Output is correct
7 Correct 148 ms 35556 KB Output is correct
8 Correct 110 ms 34372 KB Output is correct
9 Correct 151 ms 27756 KB Output is correct
10 Correct 237 ms 37872 KB Output is correct
11 Correct 193 ms 34436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 18388 KB Output is correct
2 Correct 13 ms 18384 KB Output is correct
3 Correct 11 ms 18388 KB Output is correct
4 Correct 11 ms 18536 KB Output is correct
5 Correct 11 ms 18572 KB Output is correct
6 Correct 12 ms 18516 KB Output is correct
7 Correct 10 ms 18560 KB Output is correct
8 Correct 13 ms 18552 KB Output is correct
9 Correct 11 ms 18568 KB Output is correct
10 Correct 11 ms 18680 KB Output is correct
11 Correct 11 ms 18516 KB Output is correct
12 Correct 14 ms 18564 KB Output is correct
13 Correct 11 ms 18516 KB Output is correct
14 Correct 11 ms 18516 KB Output is correct
15 Correct 10 ms 18388 KB Output is correct
16 Correct 12 ms 18388 KB Output is correct
17 Correct 198 ms 34504 KB Output is correct
18 Correct 200 ms 34348 KB Output is correct
19 Correct 222 ms 35780 KB Output is correct
20 Correct 248 ms 36448 KB Output is correct
21 Correct 226 ms 36268 KB Output is correct
22 Correct 211 ms 34492 KB Output is correct
23 Correct 213 ms 37560 KB Output is correct
24 Correct 131 ms 34204 KB Output is correct
25 Correct 168 ms 35572 KB Output is correct
26 Correct 206 ms 34608 KB Output is correct
27 Correct 167 ms 34416 KB Output is correct
28 Correct 182 ms 34504 KB Output is correct
29 Correct 190 ms 34336 KB Output is correct
30 Correct 108 ms 34292 KB Output is correct
31 Correct 9 ms 18388 KB Output is correct
32 Correct 248 ms 36940 KB Output is correct
33 Correct 244 ms 37168 KB Output is correct
34 Correct 202 ms 34372 KB Output is correct
35 Correct 182 ms 34228 KB Output is correct
36 Correct 268 ms 37272 KB Output is correct
37 Correct 168 ms 34340 KB Output is correct
38 Correct 176 ms 34376 KB Output is correct
39 Correct 111 ms 34216 KB Output is correct
40 Correct 203 ms 33936 KB Output is correct
41 Correct 213 ms 34472 KB Output is correct
42 Correct 10 ms 18352 KB Output is correct
43 Correct 252 ms 34964 KB Output is correct
44 Correct 258 ms 35424 KB Output is correct
45 Correct 276 ms 36408 KB Output is correct
46 Correct 273 ms 37096 KB Output is correct
47 Correct 224 ms 34996 KB Output is correct
48 Correct 148 ms 35556 KB Output is correct
49 Correct 110 ms 34372 KB Output is correct
50 Correct 151 ms 27756 KB Output is correct
51 Correct 237 ms 37872 KB Output is correct
52 Correct 193 ms 34436 KB Output is correct
53 Correct 10 ms 18388 KB Output is correct
54 Correct 10 ms 18388 KB Output is correct
55 Correct 8 ms 18388 KB Output is correct
56 Correct 10 ms 18556 KB Output is correct
57 Correct 11 ms 18588 KB Output is correct
58 Correct 12 ms 18556 KB Output is correct
59 Correct 13 ms 18512 KB Output is correct
60 Correct 11 ms 18516 KB Output is correct
61 Correct 13 ms 18556 KB Output is correct
62 Correct 11 ms 18552 KB Output is correct
63 Correct 11 ms 18516 KB Output is correct
64 Correct 11 ms 18516 KB Output is correct
65 Correct 11 ms 18560 KB Output is correct
66 Correct 11 ms 18560 KB Output is correct
67 Correct 156 ms 34448 KB Output is correct
68 Correct 159 ms 34376 KB Output is correct
69 Correct 189 ms 35764 KB Output is correct
70 Correct 221 ms 36288 KB Output is correct
71 Correct 196 ms 36264 KB Output is correct
72 Correct 171 ms 34436 KB Output is correct
73 Correct 181 ms 37552 KB Output is correct
74 Correct 129 ms 34212 KB Output is correct
75 Correct 158 ms 35600 KB Output is correct
76 Correct 214 ms 34576 KB Output is correct
77 Correct 142 ms 34376 KB Output is correct
78 Correct 172 ms 34400 KB Output is correct
79 Correct 167 ms 34336 KB Output is correct
80 Correct 105 ms 34244 KB Output is correct
81 Correct 253 ms 36960 KB Output is correct
82 Correct 249 ms 37068 KB Output is correct
83 Correct 183 ms 34404 KB Output is correct
84 Correct 177 ms 34492 KB Output is correct
85 Correct 249 ms 37184 KB Output is correct
86 Correct 177 ms 34360 KB Output is correct
87 Correct 189 ms 34404 KB Output is correct
88 Correct 170 ms 33940 KB Output is correct
89 Correct 191 ms 34612 KB Output is correct
90 Correct 254 ms 34976 KB Output is correct
91 Correct 250 ms 35280 KB Output is correct
92 Correct 257 ms 36428 KB Output is correct
93 Correct 267 ms 37120 KB Output is correct
94 Correct 215 ms 35056 KB Output is correct
95 Correct 151 ms 35496 KB Output is correct
96 Correct 120 ms 34420 KB Output is correct
97 Correct 139 ms 27860 KB Output is correct
98 Correct 231 ms 37996 KB Output is correct
99 Correct 191 ms 34656 KB Output is correct
100 Correct 58 ms 21344 KB Output is correct
101 Correct 250 ms 35588 KB Output is correct
102 Correct 195 ms 31948 KB Output is correct
103 Correct 230 ms 31924 KB Output is correct
104 Correct 298 ms 34312 KB Output is correct