답안 #697794

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
697794 2023-02-11T05:57:28 Z vjudge1 Stranded Far From Home (BOI22_island) C++17
100 / 100
676 ms 49088 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array
#define fi first
#define se second
#define sz(x) (int)x.size()
#define rep(i, b, e) for (int i = (b); i <= (e); i++)
#define rrep(i, b, e) for (int i = (b); i >= (e); i--)

typedef long long ll;
typedef pair<ll, ll> ii;

template <class T>
bool minimize(T &a, const T &b) {
    if(a > b) {a = b; return 1;}
    return 0;
}
template <class T>
bool maximize(T &a, const T &b) {
    if(a < b) {a = b; return 1;}
    return 0;
}

const int N = 2e5 + 7;

int n, m, s[N];
vector<int> adj[N];

void read() {
    cin >> n >> m;
    rep(i, 1, n) cin >> s[i];
    rep(i, 1, m) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
}

void sub1() {
    rep(i, 1, n) {
        priority_queue<ii> pq;
        vector<bool> done(n + 1, 0);
        queue<int> q; q.push(i); done[i] = 1;
        ll cur = s[i], cnt = 0;
        while (sz(q)) {
            int u = q.front(); q.pop();
            for (int v: adj[u]) if (!done[v]) {
                pq.push({-s[v], v});
                done[v] = 1;
            }
            while (sz(pq)) {
                ii k = pq.top();
                if (cur >= -k.fi) {
                    ++cnt; pq.pop();
                    cur += -k.fi;
                    q.push(k.se);
                } else break;
            }
        }
        if (cnt == n - 1) cout << 1; else cout << 0;
    }
}

ll sum[N];
int dsu[N];
bool mark[N];
set<int> st, node[N];
int root(int x){
    return (dsu[x] < 0 ? x : dsu[x] = root(dsu[x]));
}

void join(int x, int y) {
    if ((x = root(x)) == (y = root(y))) return;
    if (sz(node[x]) < sz(node[y])) swap(x, y);
    for (int i: node[y]) node[x].insert(i);
    node[y].clear();

    dsu[x] += dsu[y]; sum[x] += sum[y];
    dsu[y] = x; sum[y] = 0;
    st.erase(y);
}

void full() {
    memset(dsu, -1, sizeof dsu);
    vector<int> idx;
    rep(i, 1, n) {
        idx.push_back(i);
        st.insert(i);
        sum[i] = s[i];
        node[i].insert(i);
    }
    sort(idx.begin(), idx.end(), [&](int i, int j){
         return s[i] < s[j];
    });
    rep(i, 0, n - 1) {
        int j = i;
        while (j < n && s[idx[j]] == s[idx[i]]) {
            int u = idx[j];
            for (int v: adj[u]) if (s[v] <= s[u]) {
                if (sum[root(v)] < s[u]) node[root(v)].clear();
                join(u, v);
            }
            j++;
        }
        i = j - 1;
    }
    for (int x: node[root(idx.back())]) mark[x] =1;
    rep(i, 1, n) cout << mark[i];
}

void solve() {
    if (n <= 2000) sub1();
    else full();
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    if (fopen("test.inp", "r")) {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    read();
    solve();
}


/**
+ check mod (gõ đúng số), limit, kiểu dữ liệu
+ check format output
+mấy bài multitest:
    quên reset biến, mảng, (nên dùng fill, memset, v.v)
    quên xuống dòng
    precompute đầu bài
+nhập hết dữ liệu trước khi return
+xóa debug
**/

Compilation message

island.cpp: In function 'int main()':
island.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14292 KB Output is correct
4 Correct 76 ms 14480 KB Output is correct
5 Correct 80 ms 14480 KB Output is correct
6 Correct 101 ms 14460 KB Output is correct
7 Correct 71 ms 14420 KB Output is correct
8 Correct 53 ms 14460 KB Output is correct
9 Correct 131 ms 14508 KB Output is correct
10 Correct 65 ms 14472 KB Output is correct
11 Correct 72 ms 14476 KB Output is correct
12 Correct 58 ms 14420 KB Output is correct
13 Correct 109 ms 14476 KB Output is correct
14 Correct 64 ms 14540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 236 ms 43848 KB Output is correct
4 Correct 321 ms 46632 KB Output is correct
5 Correct 393 ms 47084 KB Output is correct
6 Correct 424 ms 47984 KB Output is correct
7 Correct 421 ms 48172 KB Output is correct
8 Correct 539 ms 48184 KB Output is correct
9 Correct 300 ms 47976 KB Output is correct
10 Correct 264 ms 47904 KB Output is correct
11 Correct 224 ms 47900 KB Output is correct
12 Correct 408 ms 46580 KB Output is correct
13 Correct 296 ms 46420 KB Output is correct
14 Correct 291 ms 46588 KB Output is correct
15 Correct 241 ms 48220 KB Output is correct
16 Correct 200 ms 47204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 511 ms 43588 KB Output is correct
3 Correct 465 ms 48296 KB Output is correct
4 Correct 329 ms 48244 KB Output is correct
5 Correct 292 ms 46488 KB Output is correct
6 Correct 495 ms 48316 KB Output is correct
7 Correct 287 ms 48216 KB Output is correct
8 Correct 279 ms 48240 KB Output is correct
9 Correct 199 ms 47100 KB Output is correct
10 Correct 262 ms 47492 KB Output is correct
11 Correct 336 ms 46544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 506 ms 43648 KB Output is correct
3 Correct 513 ms 43660 KB Output is correct
4 Correct 581 ms 43824 KB Output is correct
5 Correct 655 ms 43736 KB Output is correct
6 Correct 549 ms 42280 KB Output is correct
7 Correct 237 ms 44496 KB Output is correct
8 Correct 217 ms 43636 KB Output is correct
9 Correct 223 ms 30472 KB Output is correct
10 Correct 673 ms 43604 KB Output is correct
11 Correct 342 ms 43508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14292 KB Output is correct
4 Correct 76 ms 14480 KB Output is correct
5 Correct 80 ms 14480 KB Output is correct
6 Correct 101 ms 14460 KB Output is correct
7 Correct 71 ms 14420 KB Output is correct
8 Correct 53 ms 14460 KB Output is correct
9 Correct 131 ms 14508 KB Output is correct
10 Correct 65 ms 14472 KB Output is correct
11 Correct 72 ms 14476 KB Output is correct
12 Correct 58 ms 14420 KB Output is correct
13 Correct 109 ms 14476 KB Output is correct
14 Correct 64 ms 14540 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14292 KB Output is correct
17 Correct 236 ms 43848 KB Output is correct
18 Correct 321 ms 46632 KB Output is correct
19 Correct 393 ms 47084 KB Output is correct
20 Correct 424 ms 47984 KB Output is correct
21 Correct 421 ms 48172 KB Output is correct
22 Correct 539 ms 48184 KB Output is correct
23 Correct 300 ms 47976 KB Output is correct
24 Correct 264 ms 47904 KB Output is correct
25 Correct 224 ms 47900 KB Output is correct
26 Correct 408 ms 46580 KB Output is correct
27 Correct 296 ms 46420 KB Output is correct
28 Correct 291 ms 46588 KB Output is correct
29 Correct 241 ms 48220 KB Output is correct
30 Correct 200 ms 47204 KB Output is correct
31 Correct 7 ms 14420 KB Output is correct
32 Correct 511 ms 43588 KB Output is correct
33 Correct 465 ms 48296 KB Output is correct
34 Correct 329 ms 48244 KB Output is correct
35 Correct 292 ms 46488 KB Output is correct
36 Correct 495 ms 48316 KB Output is correct
37 Correct 287 ms 48216 KB Output is correct
38 Correct 279 ms 48240 KB Output is correct
39 Correct 199 ms 47100 KB Output is correct
40 Correct 262 ms 47492 KB Output is correct
41 Correct 336 ms 46544 KB Output is correct
42 Correct 9 ms 14420 KB Output is correct
43 Correct 506 ms 43648 KB Output is correct
44 Correct 513 ms 43660 KB Output is correct
45 Correct 581 ms 43824 KB Output is correct
46 Correct 655 ms 43736 KB Output is correct
47 Correct 549 ms 42280 KB Output is correct
48 Correct 237 ms 44496 KB Output is correct
49 Correct 217 ms 43636 KB Output is correct
50 Correct 223 ms 30472 KB Output is correct
51 Correct 673 ms 43604 KB Output is correct
52 Correct 342 ms 43508 KB Output is correct
53 Correct 10 ms 14420 KB Output is correct
54 Correct 8 ms 14420 KB Output is correct
55 Correct 7 ms 14420 KB Output is correct
56 Correct 75 ms 14432 KB Output is correct
57 Correct 81 ms 14464 KB Output is correct
58 Correct 101 ms 14500 KB Output is correct
59 Correct 71 ms 14420 KB Output is correct
60 Correct 54 ms 14488 KB Output is correct
61 Correct 134 ms 14548 KB Output is correct
62 Correct 65 ms 14512 KB Output is correct
63 Correct 62 ms 14464 KB Output is correct
64 Correct 57 ms 14440 KB Output is correct
65 Correct 109 ms 14420 KB Output is correct
66 Correct 65 ms 14504 KB Output is correct
67 Correct 246 ms 48220 KB Output is correct
68 Correct 302 ms 46572 KB Output is correct
69 Correct 394 ms 47048 KB Output is correct
70 Correct 431 ms 48040 KB Output is correct
71 Correct 424 ms 48120 KB Output is correct
72 Correct 533 ms 48336 KB Output is correct
73 Correct 308 ms 47984 KB Output is correct
74 Correct 276 ms 47864 KB Output is correct
75 Correct 217 ms 47952 KB Output is correct
76 Correct 386 ms 46664 KB Output is correct
77 Correct 290 ms 46456 KB Output is correct
78 Correct 302 ms 46588 KB Output is correct
79 Correct 244 ms 48204 KB Output is correct
80 Correct 199 ms 47116 KB Output is correct
81 Correct 466 ms 47996 KB Output is correct
82 Correct 485 ms 48192 KB Output is correct
83 Correct 305 ms 48172 KB Output is correct
84 Correct 299 ms 46420 KB Output is correct
85 Correct 466 ms 48116 KB Output is correct
86 Correct 291 ms 48316 KB Output is correct
87 Correct 283 ms 48208 KB Output is correct
88 Correct 258 ms 47568 KB Output is correct
89 Correct 329 ms 46448 KB Output is correct
90 Correct 471 ms 48072 KB Output is correct
91 Correct 505 ms 48064 KB Output is correct
92 Correct 570 ms 48208 KB Output is correct
93 Correct 652 ms 48228 KB Output is correct
94 Correct 555 ms 46600 KB Output is correct
95 Correct 236 ms 49088 KB Output is correct
96 Correct 217 ms 46916 KB Output is correct
97 Correct 217 ms 33028 KB Output is correct
98 Correct 676 ms 46480 KB Output is correct
99 Correct 352 ms 46528 KB Output is correct
100 Correct 51 ms 19564 KB Output is correct
101 Correct 527 ms 48380 KB Output is correct
102 Correct 410 ms 41540 KB Output is correct
103 Correct 400 ms 43148 KB Output is correct
104 Correct 481 ms 47672 KB Output is correct