제출 #1321210

#제출 시각아이디문제언어결과실행 시간메모리
1321210adscodingStranded Far From Home (BOI22_island)C++20
100 / 100
138 ms26828 KiB
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)
template<typename T>
void __print_one(const char *&s, const T &x)
{
	while (*s == ' ') ++s;
	const char *p = s;
	int bal = 0;
	while (*s)
	{
		if (*s == '(') ++bal;
		else if (*s == ')') --bal;
		else if (*s == ',' && bal == 0) break;
		++s;
	}
	cerr.write(p, s - p) << " = " << x;
	if (*s == ',')
	{
		cerr << "  ,  ";
		++s;
	}
}

template<typename... Args>
void debug(const char *s, Args... args)
{
	cerr << "[  ";
	int dummy[] = { 0, (__print_one(s, args), 0)... };
	(void)dummy;
	cerr << "  ]\n\n";
}

// ----------------------------------------------------------------------------------------------------

const int maxn = 2e5 + 3;
int n, m, a[maxn], dsu[maxn];
ll sum[maxn];
vector<int> g[maxn], tree[maxn];
bool ans[maxn], vis[maxn];

// ----------------------------------------------------------------------------------------------------

int getRoot(int u) { return dsu[u] == u ? u : dsu[u] = getRoot(dsu[u]); }

void solve()
{
	cin >> n >> m;
	FOR(i, 1, n)
		cin >> a[i];
	FOR(i, 1, m)
	{
		int u, v; cin >> u >> v;
		if (u > v) swap(u, v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
		
	FOR(i, 1, n)
	{
		dsu[i] = i;
		sum[i] = a[i];
		ans[i] = 1;
	}

	vector<int> sorted(n);
	iota(all(sorted), 1);
	sort(all(sorted), [](const int &x, const int &y) {
		return a[x] < a[y];
	});

	for (int u : sorted)
	{
		vis[u] = true;
		for (int v : g[u])
		{
			if (!vis[v]) continue;

			v = getRoot(v);

			if (u == v) continue;
			if (sum[v] < a[u])
			{
				ans[v] = 0;
			}

			dsu[v] = u;
			sum[u] += sum[v];
			tree[u].push_back(v);
		}
	}

	FORD(i, n - 1, 0)
	{
		int u = sorted[i];
		if (ans[u])
			continue;

		for (int v : tree[u])
			ans[v] = 0;
	}

	FOR(i, 1, n)
		cout << ans[i];
}

signed main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#define TASK "TEST"
	if (fopen(TASK".INP", "r"))
	{
		freopen(TASK".INP", "r", stdin);
		freopen(TASK".OUT", "w", stdout);
	}
	solve();
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

island.cpp: In function 'int main()':
island.cpp:127:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |                 freopen(TASK".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:128:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |                 freopen(TASK".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...