답안 #984183

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
984183 2024-05-16T11:13:28 Z OAleksa Stranded Far From Home (BOI22_island) C++14
0 / 100
112 ms 56148 KB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 2e5 + 69;
int n, m, a[N], p[N], sz[N], sum[N], vis[N], ans[N];
vector<int> g[N], can[N];
int root(int v) {
	if (p[v] == v)
		return v;
	return p[v] = root(p[v]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
  	cin >> n >> m;
  	for (int i = 1;i <= n;i++) {
  		cin >> a[i];
  		p[i] = i, sz[i] = 1;
  		sum[i] = a[i];
  		can[i].push_back(i);
  	}
  	for (int i = 1;i <= m;i++) {
  		int a, b;
  		cin >> a >> b;
  		g[a].push_back(b);
  		g[b].push_back(a);
  	}   
  	vector<int> ord(n);
  	iota(ord.begin(), ord.end(), 1);
  	sort(ord.begin(), ord.end(), [&](int i, int j) {
  		return a[i] < a[j];
  	});
  	for (int j = 0;j < n;j++) {
  		int i = ord[j];
  		vis[i] = 1;
  		for (auto u : g[i]) {
				if (vis[u]) {
  				int x = root(u);
  				int y = root(i);
  				int ok = 0;
  				if (x != y) {
  					if (sum[x] >= a[i])
  						ok = 1;
  					if (sz[x] < sz[y]) {
  						assert(!ok);
  						swap(x, y);
  					}
  					else
  						swap(can[y], can[x]);
  					p[y] = x;
  					sz[x] += sz[y];
  					sum[x] += sum[y];
  					if (ok) {
  						for (auto t : can[y])
  							can[x].push_back(t);
  					}
  				}
  			}
  		}
  	}
  	int ok = 1;
  	for (int i = 2;i <= n;i++)
  		ok &= (root(i) == root(i - 1));
  	assert(ok);
  	for (auto j : can[root(1)])
  		ans[j] = 1;
  	for (int i = 1;i <= n;i++)
  		cout << ans[i];
  	cout << '\n';
  }
  return 0; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Runtime error 12 ms 26200 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13300 KB Output is correct
2 Correct 2 ms 12888 KB Output is correct
3 Runtime error 98 ms 56148 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Runtime error 112 ms 55784 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12952 KB Output is correct
2 Runtime error 99 ms 56028 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Runtime error 12 ms 26200 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -