| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 869437 | lolismek | Stranded Far From Home (BOI22_island) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define pii pair <int, int>
using namespace std;
#ifdef LOCAL
ifstream fin("input.in");
ofstream fout("output.out");
#elseif
#define fin cin
#define fout cout
#endif
const int NMAX = 2e5;
int v[NMAX + 1];
vector <int> adj[NMAX + 1];
int par[NMAX + 1];
int sum[NMAX + 1];
int Find(int x){
if(x == par[x]){
return x;
}
return par[x] = Find(par[x]);
}
void Join(int x, int y){
x = par[x];
y = par[y];
par[x] = y;
sum[y] += sum[x];
}
bool ins[NMAX + 1];
void Insert(int x){
ins[x] = true;
for(int vec : adj[x]){
if(ins[vec]){
Join(vec, x);
}
}
}
bool ans[NMAX + 1];
int main(){
int n, m;
fin >> n >> m;
vector <pii> aux;
priority_queue <pii, vector <pii>, greater <pii>> pq;
for(int i = 1; i <= n; i++){
fin >> v[i];
par[i] = i;
sum[i] = v[i];
aux.push_back({v[i], i});
pq.push({v[i], i});
ans[i] = 1;
}
for(int i = 1; i <= m; i++){
int a, b;
fin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
sort(aux.begin(), aux.end());
for(int i = 0; i < (int)aux.size(); i++){
cout << i << endl;
int val = aux[i].first;
Insert(aux[i].second);
//cout << "-- " << aux[i].second << endl;
while(i + 1 < (int)aux.size() && aux[i + 1].first == aux[i].first){
Insert(aux[++i].second);
//cout << "-- " << aux[i].second << endl;
}
while(!pq.empty() && pq.top().first <= val){
int node = pq.top().second;
int x = pq.top().first;
pq.pop();
int s = sum[Find(node)];
if(s == x){
// cout << ">> " << node << ' ' << val << ' ' << x << ' ' << s << endl;
ans[node] = 0;
}else{
pq.push({s, node});
}
}
}
for(int i = 1; i <= n; i++){
fout << ans[i];
}
fout << '\n';
return 0;
}
