제출 #647428

#제출 시각아이디문제언어결과실행 시간메모리
647428PetyStranded Far From Home (BOI22_island)C++14
100 / 100
351 ms47300 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; const int INF = 1e9; const int MOD = 1e9 + 7; int n, m, val[200002], ind[200002], p[200002], ok[200002]; ll sz[200002]; vector<int>G2[200002]; vector<int>G[200002]; int find (int x) { if (p[x] == x) return x; return p[x] = find(p[x]); } void Union (int a, int b) { a = find(a); b = find(b); if (a != b) { G[a].push_back(b); G[b].push_back(a); p[b] = a; } } bool cmp (int a, int b) { if (val[a] == val[b]) return a < b; return val[a] < val[b]; } void dfs (int nod, int par) { assert(par == 0 || val[nod] <= val[par]); sz[nod] = val[nod]; for (auto it : G[nod]) { if (it == par) continue; dfs(it, nod); sz[nod] += sz[it]; } ok[nod] = (sz[nod] >= val[par]); } void dfs2 (int nod, int par, bool idk) { for (auto it : G[nod]) { if (it == par) continue; dfs2(it, nod, (idk & ok[it])); } ok[nod] = idk; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> val[i]; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; G2[x].push_back(y); G2[y].push_back(x); } for (int i = 1; i <= n; i++) ind[i] = p[i] = i; sort(ind + 1, ind + n + 1, cmp); for (int i = 1; i <= n; i++) for (auto it : G2[ind[i]]) if (make_pair(val[it], it) <= make_pair(val[ind[i]], ind[i])) Union(ind[i], it); dfs(find(1), 0); dfs2(find(1), 0, 1); for (int i = 1; i <= n; i++) cout << ok[i]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...