# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1027982 | 2024-07-19T12:10:14 Z | anango | Stranded Far From Home (BOI22_island) | C++17 | 1000 ms | 27620 KB |
#include <bits/stdc++.h> #define int long long using namespace std; int INF = 1LL<<30; vector<int> people; vector<int> ans; class DSU { private: int n; vector<int> parent; vector<int> rank; vector<int> rank2; public: DSU(int sz) { n = sz; parent=vector<int>(n); iota(parent.begin(), parent.end(), (int)0); rank=vector<int>(n,1); for (int i=0; i<n; i++) rank[i] = people[i]; rank2=vector<int>(n,0); } int find(int node) { if (node==parent[node]) return node; return parent[node] = find(parent[node]); } void link(int u, int v) { u=find(u); v=find(v); if (u==v) { return; } if (rank2[u]<rank2[v]) swap(u,v); if (rank2[u]==rank2[v]) rank2[u]++; rank[u]+=rank[v]; parent[v] = u; } int getrank(int u) { return rank[find(u)]; } void destroy(int u) { for (int i=0; i<n; i++) { if (find(i)==find(u)) { ans[i] = 0; } } } }; signed main() { int local = 0; if (local) { // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); } /*#ifdef ONLINE_JUDGE ios_base::sync_with_stdio(false); cin.tie(NULL); #endif*/ //fast IO int n,m; cin >> n >> m; //want to calculate for each node, //the smallest w such that if we remove all nodes of weight > w, the connected component containing this node //has total weight at most w //use DSU //make w take on the weight of each node + 1, in increasing order //any set that has sum at most the current w is not allowable //actually it's easier to merge the sets of indices from small to large //so a set is marked as bad when it is merged into a larger set ans=vector<int>(n,1); people=vector<int>(n); for (int i=0; i<n; i++) cin >> people[i]; vector<int> order(n); iota(order.begin(), order.end(), (int)0); sort(order.begin(), order.end(), [&](const int i1, const int i2) { return people[i1]<people[i2]; }); vector<vector<int>> adjlist(n); for (int i=0; i<m; i++) { int u,v; cin >> u >> v; u--; v--; adjlist[u].push_back(v); adjlist[v].push_back(u); } DSU dsu(n); vector<int> active(n,0); for (int oi=0; oi<n; oi++) { int k = order[oi]; //cout << "adding node " << oi <<" " << k <<" " << people[k] << endl; for (int j:adjlist[k]) { if (active[j]) { int r1 = dsu.getrank(j); // cout << "adding edge " << k <<" " << j <<" " << r1 << endl; if (r1<people[k]) { dsu.destroy(dsu.find(j)); } dsu.link(k,j); } else { //cout << "inactive edge " << k <<" " << j << endl; } } active[k] = 1; } for (int i=0; i<n; i++) { cout << ans[i]; } cout << endl; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Correct | 0 ms | 344 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 4 ms | 668 KB | Output is correct |
5 | Correct | 6 ms | 604 KB | Output is correct |
6 | Correct | 2 ms | 604 KB | Output is correct |
7 | Correct | 4 ms | 444 KB | Output is correct |
8 | Correct | 5 ms | 604 KB | Output is correct |
9 | Correct | 1 ms | 604 KB | Output is correct |
10 | Correct | 5 ms | 604 KB | Output is correct |
11 | Correct | 6 ms | 504 KB | Output is correct |
12 | Correct | 5 ms | 500 KB | Output is correct |
13 | Correct | 1 ms | 604 KB | Output is correct |
14 | Correct | 5 ms | 576 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Execution timed out | 1096 ms | 27620 KB | Time limit exceeded |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 348 KB | Output is correct |
2 | Execution timed out | 1075 ms | 26564 KB | Time limit exceeded |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 344 KB | Output is correct |
2 | Execution timed out | 1059 ms | 27484 KB | Time limit exceeded |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Correct | 0 ms | 344 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 4 ms | 668 KB | Output is correct |
5 | Correct | 6 ms | 604 KB | Output is correct |
6 | Correct | 2 ms | 604 KB | Output is correct |
7 | Correct | 4 ms | 444 KB | Output is correct |
8 | Correct | 5 ms | 604 KB | Output is correct |
9 | Correct | 1 ms | 604 KB | Output is correct |
10 | Correct | 5 ms | 604 KB | Output is correct |
11 | Correct | 6 ms | 504 KB | Output is correct |
12 | Correct | 5 ms | 500 KB | Output is correct |
13 | Correct | 1 ms | 604 KB | Output is correct |
14 | Correct | 5 ms | 576 KB | Output is correct |
15 | Correct | 0 ms | 348 KB | Output is correct |
16 | Correct | 0 ms | 348 KB | Output is correct |
17 | Execution timed out | 1096 ms | 27620 KB | Time limit exceeded |
18 | Halted | 0 ms | 0 KB | - |