답안 #598016

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
598016 2022-07-17T10:29:58 Z slime Stranded Far From Home (BOI22_island) C++14
100 / 100
591 ms 126684 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { int x; cin >> x; return (ll)x; }
long long bm(long long b, long long p) {
  if(p==0) return 1 % MOD;
  long long r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
long long inv(long long b) { 
  return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) { 
  long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
  ans *= inv(f[n-r]); ans %= MOD; return ans;
}
long long fib[MAXN], lucas[MAXN];
void precomp() { 
  for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD); 
  lucas[0] = 2;
  lucas[1] = 1;
  for(int i=2; i<MAXN; i++) lucas[i] = (lucas[i-2] + lucas[i-1]) % MOD;
  fib[0] = 0;
  fib[1] = 1;
  for(int i=2; i<MAXN; i++) fib[i] = (fib[i-2] + fib[i-1]) % MOD;
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void gay(int i) { cout << "Case #" << i << ": "; }
int csb(int l, int r, int k) { // count number of [l, r] such that i & 2^k > 0
  if(l > r) return 0;
  if(l == 0) {
    int s = r / (1ll << (k+1)); // number of complete cycles
    int t = r % (1ll << (k+1));
    int ans = s * (1ll << k);
    ans += (t >= (1ll << k) ? t - (1ll << k) + 1 : 0);
    return ans;
  }
  else return csb(0, r, k) - csb(0, l - 1, k);
}
int lis(vector<int> a) {
  int n = a.size();
  int bucket[n+1];
  for(int i=1; i<=n; i++) bucket[i] = 1e18;
  int ans = 1;
  for(int x: a) {
    auto it = lower_bound(bucket + 1, bucket +n +1, x);
    int d = distance(bucket, it);
    ans = max(ans, d);
    bucket[d] = min(bucket[d], x);
  }
  return ans;
}
struct disjoint_sets {
  vector<int> dsu;
  vector<int> sizes;
  public:
  void resize(int n) {
    dsu.resize(n + 10);
    sizes.resize(n + 10);
    for(int i=0; i<n+10; i++) dsu[i] = i;
  }
  void set_size(int i, int x) {
    sizes[i] = x;
  }
  int get_size(int i) {
    return sizes[set_of(i)];
  }
  int set_of(int u) {
    if(dsu[u] == u) return u;
    return dsu[u] = set_of(dsu[u]);
  }
  void union_sets(int u, int v) {
    int o = sizes[set_of(u)] + sizes[set_of(v)];
    dsu[set_of(u)] = set_of(v);
    sizes[set_of(u)] = o;
  }
};
 
void solve(int tc) {
  int n, m;
  cin >> n >> m;
  disjoint_sets dsu;
  dsu.resize(n);
  pair<int, int> s[n+1];
  for(int i=1; i<=n; i++) {
    cin >> s[i].first;
    s[i].second = i;
    dsu.set_size(i, s[i].first);
 
  }
  vector<int >adj[n+1];
  for(int i=1; i<=m; i++) {
    int a, b; cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  sort(s+1, s+n+1);
  bool done[n+1], bad[n+1];
  for(int i=1; i<=n; i++) done[i] = 0, bad[i] = 0;


  set<int> indices[n+1];
  int rep[n+1];
  for(int i=1; i<=n; i++) {
    rep[i] = i;
    indices[i].insert(i);
  }
  for(int i=1; i<=n; i++) {
    int node = s[i].second, val = s[i].first;
    //cout << val << " " << node << " o\n";
    for(int x: adj[node]) {
      if(dsu.set_of(node) != dsu.set_of(x) && done[x]) {
        //cout << "dsu size (" << x << ") = " << dsu.get_size(x) << ", val = " << val << "\n";
        if(dsu.get_size(x) < val) {
          for(int j: indices[rep[dsu.set_of(x)]]) {
            bad[j] = 1;
          }
        }
        int nw;
        if(indices[rep[dsu.set_of(node)]].size() < indices[rep[dsu.set_of(x)]].size()) {
          nw = rep[dsu.set_of(x)];
          for(int y: indices[rep[dsu.set_of(node)]]) {
            indices[rep[dsu.set_of(x)]].insert(y);
          }
        }
        else {
          nw = rep[dsu.set_of(node)];
          for(int y: indices[rep[dsu.set_of(x)]]) {
            indices[rep[dsu.set_of(node)]].insert(y);
          }
        }

        dsu.union_sets(node, x);
        rep[dsu.set_of(node)] = nw;
        //cout << "union " << node << " " << x << "\n";
      }
    }
    done[node] = 1;
  }
  for(int i=1; i<=n; i++) cout << (bad[i] ? 0 : 1);
  cout << "\n";
}
 
/*
Set is bad if:
Sum(Set) < Min(Outgoing S)
 
If set bad, answer = 0 for all in set
Use KRT to update
 
4 3
5 7 3 5
1 2
2 3
3 4
*/
int32_t main() {
  precomp();
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// I don't know geometry.
// Teaming is unfair.
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7252 KB Output is correct
2 Correct 8 ms 7252 KB Output is correct
3 Correct 8 ms 7276 KB Output is correct
4 Correct 10 ms 7892 KB Output is correct
5 Correct 12 ms 7940 KB Output is correct
6 Correct 9 ms 7764 KB Output is correct
7 Correct 11 ms 7808 KB Output is correct
8 Correct 9 ms 7764 KB Output is correct
9 Correct 9 ms 7764 KB Output is correct
10 Correct 10 ms 8140 KB Output is correct
11 Correct 11 ms 8020 KB Output is correct
12 Correct 9 ms 8148 KB Output is correct
13 Correct 8 ms 7764 KB Output is correct
14 Correct 10 ms 8020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7252 KB Output is correct
2 Correct 8 ms 7252 KB Output is correct
3 Correct 271 ms 61676 KB Output is correct
4 Correct 216 ms 58500 KB Output is correct
5 Correct 421 ms 86596 KB Output is correct
6 Correct 471 ms 91916 KB Output is correct
7 Correct 470 ms 91036 KB Output is correct
8 Correct 268 ms 58140 KB Output is correct
9 Correct 429 ms 126580 KB Output is correct
10 Correct 352 ms 58576 KB Output is correct
11 Correct 201 ms 58568 KB Output is correct
12 Correct 430 ms 66128 KB Output is correct
13 Correct 200 ms 56092 KB Output is correct
14 Correct 226 ms 58108 KB Output is correct
15 Correct 216 ms 57220 KB Output is correct
16 Correct 166 ms 56496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7252 KB Output is correct
2 Correct 485 ms 102696 KB Output is correct
3 Correct 483 ms 103888 KB Output is correct
4 Correct 279 ms 57024 KB Output is correct
5 Correct 242 ms 59992 KB Output is correct
6 Correct 509 ms 103052 KB Output is correct
7 Correct 236 ms 57072 KB Output is correct
8 Correct 259 ms 57172 KB Output is correct
9 Correct 160 ms 55956 KB Output is correct
10 Correct 195 ms 59532 KB Output is correct
11 Correct 262 ms 68920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7288 KB Output is correct
2 Correct 500 ms 75364 KB Output is correct
3 Correct 524 ms 81900 KB Output is correct
4 Correct 591 ms 88536 KB Output is correct
5 Correct 506 ms 83332 KB Output is correct
6 Correct 439 ms 68584 KB Output is correct
7 Correct 256 ms 58116 KB Output is correct
8 Correct 161 ms 58732 KB Output is correct
9 Correct 228 ms 40012 KB Output is correct
10 Correct 476 ms 88392 KB Output is correct
11 Correct 270 ms 66128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7252 KB Output is correct
2 Correct 8 ms 7252 KB Output is correct
3 Correct 8 ms 7276 KB Output is correct
4 Correct 10 ms 7892 KB Output is correct
5 Correct 12 ms 7940 KB Output is correct
6 Correct 9 ms 7764 KB Output is correct
7 Correct 11 ms 7808 KB Output is correct
8 Correct 9 ms 7764 KB Output is correct
9 Correct 9 ms 7764 KB Output is correct
10 Correct 10 ms 8140 KB Output is correct
11 Correct 11 ms 8020 KB Output is correct
12 Correct 9 ms 8148 KB Output is correct
13 Correct 8 ms 7764 KB Output is correct
14 Correct 10 ms 8020 KB Output is correct
15 Correct 7 ms 7252 KB Output is correct
16 Correct 8 ms 7252 KB Output is correct
17 Correct 271 ms 61676 KB Output is correct
18 Correct 216 ms 58500 KB Output is correct
19 Correct 421 ms 86596 KB Output is correct
20 Correct 471 ms 91916 KB Output is correct
21 Correct 470 ms 91036 KB Output is correct
22 Correct 268 ms 58140 KB Output is correct
23 Correct 429 ms 126580 KB Output is correct
24 Correct 352 ms 58576 KB Output is correct
25 Correct 201 ms 58568 KB Output is correct
26 Correct 430 ms 66128 KB Output is correct
27 Correct 200 ms 56092 KB Output is correct
28 Correct 226 ms 58108 KB Output is correct
29 Correct 216 ms 57220 KB Output is correct
30 Correct 166 ms 56496 KB Output is correct
31 Correct 8 ms 7252 KB Output is correct
32 Correct 485 ms 102696 KB Output is correct
33 Correct 483 ms 103888 KB Output is correct
34 Correct 279 ms 57024 KB Output is correct
35 Correct 242 ms 59992 KB Output is correct
36 Correct 509 ms 103052 KB Output is correct
37 Correct 236 ms 57072 KB Output is correct
38 Correct 259 ms 57172 KB Output is correct
39 Correct 160 ms 55956 KB Output is correct
40 Correct 195 ms 59532 KB Output is correct
41 Correct 262 ms 68920 KB Output is correct
42 Correct 8 ms 7288 KB Output is correct
43 Correct 500 ms 75364 KB Output is correct
44 Correct 524 ms 81900 KB Output is correct
45 Correct 591 ms 88536 KB Output is correct
46 Correct 506 ms 83332 KB Output is correct
47 Correct 439 ms 68584 KB Output is correct
48 Correct 256 ms 58116 KB Output is correct
49 Correct 161 ms 58732 KB Output is correct
50 Correct 228 ms 40012 KB Output is correct
51 Correct 476 ms 88392 KB Output is correct
52 Correct 270 ms 66128 KB Output is correct
53 Correct 9 ms 7372 KB Output is correct
54 Correct 7 ms 7272 KB Output is correct
55 Correct 8 ms 7252 KB Output is correct
56 Correct 10 ms 7764 KB Output is correct
57 Correct 10 ms 8020 KB Output is correct
58 Correct 9 ms 7708 KB Output is correct
59 Correct 10 ms 7764 KB Output is correct
60 Correct 9 ms 7764 KB Output is correct
61 Correct 9 ms 7796 KB Output is correct
62 Correct 11 ms 8100 KB Output is correct
63 Correct 11 ms 8044 KB Output is correct
64 Correct 10 ms 8208 KB Output is correct
65 Correct 10 ms 7764 KB Output is correct
66 Correct 11 ms 8016 KB Output is correct
67 Correct 206 ms 63036 KB Output is correct
68 Correct 220 ms 58864 KB Output is correct
69 Correct 452 ms 85872 KB Output is correct
70 Correct 428 ms 91964 KB Output is correct
71 Correct 429 ms 90312 KB Output is correct
72 Correct 268 ms 58116 KB Output is correct
73 Correct 437 ms 126684 KB Output is correct
74 Correct 308 ms 57644 KB Output is correct
75 Correct 223 ms 57952 KB Output is correct
76 Correct 405 ms 65688 KB Output is correct
77 Correct 217 ms 56136 KB Output is correct
78 Correct 236 ms 57104 KB Output is correct
79 Correct 242 ms 57100 KB Output is correct
80 Correct 167 ms 56496 KB Output is correct
81 Correct 478 ms 104172 KB Output is correct
82 Correct 476 ms 103820 KB Output is correct
83 Correct 220 ms 54988 KB Output is correct
84 Correct 215 ms 60100 KB Output is correct
85 Correct 488 ms 103168 KB Output is correct
86 Correct 279 ms 56980 KB Output is correct
87 Correct 235 ms 57060 KB Output is correct
88 Correct 190 ms 58728 KB Output is correct
89 Correct 255 ms 67004 KB Output is correct
90 Correct 461 ms 76652 KB Output is correct
91 Correct 534 ms 81880 KB Output is correct
92 Correct 519 ms 89240 KB Output is correct
93 Correct 506 ms 83316 KB Output is correct
94 Correct 438 ms 68568 KB Output is correct
95 Correct 267 ms 58640 KB Output is correct
96 Correct 176 ms 58668 KB Output is correct
97 Correct 206 ms 40396 KB Output is correct
98 Correct 457 ms 89120 KB Output is correct
99 Correct 259 ms 66852 KB Output is correct
100 Correct 50 ms 13980 KB Output is correct
101 Correct 536 ms 86844 KB Output is correct
102 Correct 285 ms 54152 KB Output is correct
103 Correct 403 ms 55776 KB Output is correct
104 Correct 501 ms 70472 KB Output is correct