Submission #709566

# Submission time Handle Problem Language Result Execution time Memory
709566 2023-03-13T23:20:08 Z aryan12 Stranded Far From Home (BOI22_island) C++17
100 / 100
346 ms 56020 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 2e5 + 5;
vector<int> g[N];
int a[N];

struct node
{
    int index, sum, parent;
    bool merge;
    pair<int, int> children;

    node(int i, int s, int p, bool m)
    {
        index = i;
        sum = s;
        parent = p;
        merge = m;
        children = {-1, -1};
    }
} *reach[2 * N];

int Find(int idx)
{
    if(reach[idx]->parent == idx)
    {
        return idx;
    }
    return reach[idx]->parent = Find(reach[idx]->parent);
}

void Unite(int node1, int node2, int parent_node)
{
    reach[node1]->parent = parent_node;
    reach[node2]->parent = parent_node;
    reach[parent_node]->sum = reach[node1]->sum + reach[node2]->sum;
    reach[parent_node]->children = {node1, node2};
    reach[parent_node]->merge = false;
}

void dfs(int node, bool merge_parents)
{
    reach[node]->merge &= merge_parents;
    if(reach[node]->children.first == -1)
    {
        return;
    }
    dfs(reach[node]->children.first, reach[node]->merge);
    dfs(reach[node]->children.second, reach[node]->merge);
}

void Solve() 
{
    int n, m;
    cin >> n >> m;
    for(int i = n + 1; i <= 2 * n; i++)
    {
        reach[i] = new node(i, 0, i, false);
    }
    vector<array<int, 2> > order;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        reach[i] = new node(i, a[i], i, false);
        order.push_back({a[i], i});
    }
    for(int i = 1; i <= m; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    sort(order.begin(), order.end());
    int new_index = n + 1;
    for(int i = 0; i < n; i++)
    {
        // cout << "i = " << i << endl;
         // cout << reach[6]->merge << "\n";
        int current_index = order[i][1]; 
        // cout << "current_index = " << current_index << endl;
        for(int to: g[current_index])
        {
            if(a[to] <= a[current_index])
            {
                int parent_current_index = Find(current_index);
                int parent_index = Find(to);
                if(parent_index == parent_current_index)
                {
                    continue;
                }
                reach[parent_current_index]->merge = true;
                // cout << "parent index = " << parent_index << ", " << reach[parent_index]->merge << "\n";
                if(reach[parent_index]->sum >= a[current_index])
                {
                    reach[parent_index]->merge = true;
                }
                // cout << "merging " << parent_current_index << " and " << parent_index << " to " << new_index << ".\n";
                // cout << reach[parent_index]->sum << ", " << a[current_index] << "\n";
                // cout << "holyshit: " << reach[6]->merge << "\n";
                Unite(parent_current_index, parent_index, new_index);
                new_index = new_index + 1;
            }
        }
    }
    reach[new_index - 1]->merge = true;
    // for(int i = 1; i < new_index; i++)
    // {
    //     cout << "current index: " << i << "\n";
    //     cout << "sum: " << reach[i]->sum << ", parent: " << reach[i]->parent << ", merge: " << reach[i]->merge << "\n";
    // }
    dfs(new_index - 1, true);
    for(int i = 1; i <= n; i++)
    {
        cout << ((reach[i]->merge) ? ('1') : ('0'));
    }
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 5 ms 5332 KB Output is correct
5 Correct 5 ms 5424 KB Output is correct
6 Correct 5 ms 5388 KB Output is correct
7 Correct 5 ms 5296 KB Output is correct
8 Correct 4 ms 5332 KB Output is correct
9 Correct 4 ms 5460 KB Output is correct
10 Correct 4 ms 5460 KB Output is correct
11 Correct 4 ms 5432 KB Output is correct
12 Correct 4 ms 5420 KB Output is correct
13 Correct 4 ms 5424 KB Output is correct
14 Correct 5 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5024 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 178 ms 50472 KB Output is correct
4 Correct 193 ms 54056 KB Output is correct
5 Correct 219 ms 48300 KB Output is correct
6 Correct 277 ms 49600 KB Output is correct
7 Correct 256 ms 49756 KB Output is correct
8 Correct 315 ms 56020 KB Output is correct
9 Correct 171 ms 51120 KB Output is correct
10 Correct 161 ms 55724 KB Output is correct
11 Correct 216 ms 55596 KB Output is correct
12 Correct 298 ms 49584 KB Output is correct
13 Correct 147 ms 53520 KB Output is correct
14 Correct 152 ms 53040 KB Output is correct
15 Correct 164 ms 48788 KB Output is correct
16 Correct 107 ms 47656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 239 ms 48540 KB Output is correct
3 Correct 242 ms 48956 KB Output is correct
4 Correct 231 ms 55044 KB Output is correct
5 Correct 159 ms 51508 KB Output is correct
6 Correct 236 ms 48840 KB Output is correct
7 Correct 246 ms 48784 KB Output is correct
8 Correct 169 ms 48776 KB Output is correct
9 Correct 103 ms 47676 KB Output is correct
10 Correct 177 ms 49596 KB Output is correct
11 Correct 178 ms 47992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4920 KB Output is correct
2 Correct 339 ms 49780 KB Output is correct
3 Correct 324 ms 49616 KB Output is correct
4 Correct 316 ms 49780 KB Output is correct
5 Correct 338 ms 49952 KB Output is correct
6 Correct 331 ms 48300 KB Output is correct
7 Correct 197 ms 53824 KB Output is correct
8 Correct 119 ms 52312 KB Output is correct
9 Correct 190 ms 29940 KB Output is correct
10 Correct 326 ms 48472 KB Output is correct
11 Correct 194 ms 47924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 5 ms 5332 KB Output is correct
5 Correct 5 ms 5424 KB Output is correct
6 Correct 5 ms 5388 KB Output is correct
7 Correct 5 ms 5296 KB Output is correct
8 Correct 4 ms 5332 KB Output is correct
9 Correct 4 ms 5460 KB Output is correct
10 Correct 4 ms 5460 KB Output is correct
11 Correct 4 ms 5432 KB Output is correct
12 Correct 4 ms 5420 KB Output is correct
13 Correct 4 ms 5424 KB Output is correct
14 Correct 5 ms 5460 KB Output is correct
15 Correct 3 ms 5024 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 178 ms 50472 KB Output is correct
18 Correct 193 ms 54056 KB Output is correct
19 Correct 219 ms 48300 KB Output is correct
20 Correct 277 ms 49600 KB Output is correct
21 Correct 256 ms 49756 KB Output is correct
22 Correct 315 ms 56020 KB Output is correct
23 Correct 171 ms 51120 KB Output is correct
24 Correct 161 ms 55724 KB Output is correct
25 Correct 216 ms 55596 KB Output is correct
26 Correct 298 ms 49584 KB Output is correct
27 Correct 147 ms 53520 KB Output is correct
28 Correct 152 ms 53040 KB Output is correct
29 Correct 164 ms 48788 KB Output is correct
30 Correct 107 ms 47656 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 239 ms 48540 KB Output is correct
33 Correct 242 ms 48956 KB Output is correct
34 Correct 231 ms 55044 KB Output is correct
35 Correct 159 ms 51508 KB Output is correct
36 Correct 236 ms 48840 KB Output is correct
37 Correct 246 ms 48784 KB Output is correct
38 Correct 169 ms 48776 KB Output is correct
39 Correct 103 ms 47676 KB Output is correct
40 Correct 177 ms 49596 KB Output is correct
41 Correct 178 ms 47992 KB Output is correct
42 Correct 3 ms 4920 KB Output is correct
43 Correct 339 ms 49780 KB Output is correct
44 Correct 324 ms 49616 KB Output is correct
45 Correct 316 ms 49780 KB Output is correct
46 Correct 338 ms 49952 KB Output is correct
47 Correct 331 ms 48300 KB Output is correct
48 Correct 197 ms 53824 KB Output is correct
49 Correct 119 ms 52312 KB Output is correct
50 Correct 190 ms 29940 KB Output is correct
51 Correct 326 ms 48472 KB Output is correct
52 Correct 194 ms 47924 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 5024 KB Output is correct
55 Correct 3 ms 5028 KB Output is correct
56 Correct 5 ms 5332 KB Output is correct
57 Correct 5 ms 5460 KB Output is correct
58 Correct 4 ms 5292 KB Output is correct
59 Correct 4 ms 5332 KB Output is correct
60 Correct 4 ms 5248 KB Output is correct
61 Correct 4 ms 5392 KB Output is correct
62 Correct 4 ms 5460 KB Output is correct
63 Correct 4 ms 5460 KB Output is correct
64 Correct 6 ms 5460 KB Output is correct
65 Correct 4 ms 5424 KB Output is correct
66 Correct 5 ms 5416 KB Output is correct
67 Correct 163 ms 50472 KB Output is correct
68 Correct 159 ms 54052 KB Output is correct
69 Correct 244 ms 48276 KB Output is correct
70 Correct 266 ms 49628 KB Output is correct
71 Correct 276 ms 49780 KB Output is correct
72 Correct 316 ms 56020 KB Output is correct
73 Correct 158 ms 50992 KB Output is correct
74 Correct 163 ms 55704 KB Output is correct
75 Correct 236 ms 55536 KB Output is correct
76 Correct 260 ms 49632 KB Output is correct
77 Correct 162 ms 53424 KB Output is correct
78 Correct 157 ms 52984 KB Output is correct
79 Correct 155 ms 48748 KB Output is correct
80 Correct 114 ms 47592 KB Output is correct
81 Correct 229 ms 48460 KB Output is correct
82 Correct 245 ms 48748 KB Output is correct
83 Correct 218 ms 55052 KB Output is correct
84 Correct 164 ms 51476 KB Output is correct
85 Correct 267 ms 48744 KB Output is correct
86 Correct 182 ms 48800 KB Output is correct
87 Correct 179 ms 48776 KB Output is correct
88 Correct 175 ms 49676 KB Output is correct
89 Correct 187 ms 47896 KB Output is correct
90 Correct 332 ms 49664 KB Output is correct
91 Correct 319 ms 49584 KB Output is correct
92 Correct 322 ms 49816 KB Output is correct
93 Correct 346 ms 49676 KB Output is correct
94 Correct 307 ms 48248 KB Output is correct
95 Correct 209 ms 53768 KB Output is correct
96 Correct 115 ms 52244 KB Output is correct
97 Correct 165 ms 30032 KB Output is correct
98 Correct 316 ms 48220 KB Output is correct
99 Correct 181 ms 47920 KB Output is correct
100 Correct 45 ms 12048 KB Output is correct
101 Correct 343 ms 49840 KB Output is correct
102 Correct 286 ms 42672 KB Output is correct
103 Correct 268 ms 44132 KB Output is correct
104 Correct 323 ms 49328 KB Output is correct