Submission #703517

# Submission time Handle Problem Language Result Execution time Memory
703517 2023-02-27T14:55:49 Z Chal1shkan Stranded Far From Home (BOI22_island) C++14
100 / 100
284 ms 46408 KB
# include <bits/stdc++.h>
 
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x  << " = " << x << endl; 
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
const ll maxn = 2e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
 
using namespace std;

ll n, m, s[maxn];
vector <ll> g[maxn];
ll ans[maxn];

struct DSU
{
	ll par[maxn];
	vector <ll> zxc[maxn];
	ll sum[maxn];
	void init ()
	{
		for (ll i = 1; i <= n; ++i)
		{
			par[i] = i;
			sum[i] = s[i];
			zxc[i].pb(i);
		}
	}
	ll find_set (ll v)
	{
		if (v == par[v]) return v;
		else return par[v] = find_set(par[v]);
	}
	void union_sets(ll u, ll v)
	{
		u = find_set(u);
		v = find_set(v);
		if (u == v) return;
		if (sz(zxc[u]) < sz(zxc[v]))
		{
			swap(u, v);
		}
		for (ll qq : zxc[v]) zxc[u].pb(qq);
		zxc[v].clear();
		par[v] = u;
		sum[u] += sum[v];
	} 
	ll get_sum (ll v)
	{
		v = find_set(v);
		return sum[v];
	}
	void clear (ll v)
	{
		v = find_set(v);
		zxc[v].clear();
	}
	void last_dance (ll v)
	{
		for (ll to : zxc[v])
		{
			ans[to] = 1;
		}
	}
	void out ()
	{
		for (ll i = 1; i <= n; ++i)
		{
			cout << sum[i] << ' ' << sz(zxc[i]) << nl;
		}
	}
} dsu;

void ma1n (/* SABR */)
{
	cin >> n >> m;
	vector <pair <ll, ll> > ord;
	for (ll i = 1; i <= n; ++i)
	{
		cin >> s[i];
		ord.pb({s[i], i});
	}
	for (ll i = 1, u, v; i <= m; ++i)
	{
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);	
	}
	dsu.init();
	sort(ord.begin(), ord.end());
	for (auto i : ord)
	{
		ll v = i.ss;
		for (ll to : g[v])
		{
			if (s[to] <= s[v])
			{
				if (dsu.get_sum(to) < s[v])
				{
					dsu.clear(to);
				}
				dsu.union_sets(v, to);
			}
		}
	}
	dsu.last_dance(dsu.find_set(ord.back().ss));
	for (ll i = 1; i <= n; ++i) cout << ans[i];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//  freopen("file.in", "r", stdin);
//  freopen("file.out", "w", stdout);
    int ttt = 1;	
//  cin >> ttt;
    for (int test = 1; test <= ttt; ++test)
    {
//      cout << "Case " << test << ":" << ' ';
        ma1n();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9940 KB Output is correct
5 Correct 6 ms 9940 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 9920 KB Output is correct
8 Correct 8 ms 9928 KB Output is correct
9 Correct 6 ms 9940 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 6 ms 9940 KB Output is correct
12 Correct 6 ms 9940 KB Output is correct
13 Correct 6 ms 9940 KB Output is correct
14 Correct 6 ms 9940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 153 ms 35776 KB Output is correct
4 Correct 149 ms 37740 KB Output is correct
5 Correct 166 ms 42424 KB Output is correct
6 Correct 189 ms 43676 KB Output is correct
7 Correct 169 ms 43644 KB Output is correct
8 Correct 188 ms 40760 KB Output is correct
9 Correct 155 ms 45916 KB Output is correct
10 Correct 124 ms 35340 KB Output is correct
11 Correct 172 ms 40428 KB Output is correct
12 Correct 177 ms 36724 KB Output is correct
13 Correct 144 ms 36868 KB Output is correct
14 Correct 144 ms 36972 KB Output is correct
15 Correct 150 ms 39864 KB Output is correct
16 Correct 100 ms 38960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 194 ms 39804 KB Output is correct
3 Correct 228 ms 44604 KB Output is correct
4 Correct 167 ms 39864 KB Output is correct
5 Correct 144 ms 36472 KB Output is correct
6 Correct 211 ms 44280 KB Output is correct
7 Correct 158 ms 39920 KB Output is correct
8 Correct 177 ms 39920 KB Output is correct
9 Correct 98 ms 38844 KB Output is correct
10 Correct 146 ms 37392 KB Output is correct
11 Correct 165 ms 36600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 227 ms 36440 KB Output is correct
3 Correct 230 ms 41560 KB Output is correct
4 Correct 220 ms 43700 KB Output is correct
5 Correct 253 ms 46236 KB Output is correct
6 Correct 212 ms 42128 KB Output is correct
7 Correct 158 ms 41292 KB Output is correct
8 Correct 114 ms 38512 KB Output is correct
9 Correct 116 ms 27324 KB Output is correct
10 Correct 210 ms 45880 KB Output is correct
11 Correct 180 ms 36636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9940 KB Output is correct
5 Correct 6 ms 9940 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 9920 KB Output is correct
8 Correct 8 ms 9928 KB Output is correct
9 Correct 6 ms 9940 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 6 ms 9940 KB Output is correct
12 Correct 6 ms 9940 KB Output is correct
13 Correct 6 ms 9940 KB Output is correct
14 Correct 6 ms 9940 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 153 ms 35776 KB Output is correct
18 Correct 149 ms 37740 KB Output is correct
19 Correct 166 ms 42424 KB Output is correct
20 Correct 189 ms 43676 KB Output is correct
21 Correct 169 ms 43644 KB Output is correct
22 Correct 188 ms 40760 KB Output is correct
23 Correct 155 ms 45916 KB Output is correct
24 Correct 124 ms 35340 KB Output is correct
25 Correct 172 ms 40428 KB Output is correct
26 Correct 177 ms 36724 KB Output is correct
27 Correct 144 ms 36868 KB Output is correct
28 Correct 144 ms 36972 KB Output is correct
29 Correct 150 ms 39864 KB Output is correct
30 Correct 100 ms 38960 KB Output is correct
31 Correct 4 ms 9684 KB Output is correct
32 Correct 194 ms 39804 KB Output is correct
33 Correct 228 ms 44604 KB Output is correct
34 Correct 167 ms 39864 KB Output is correct
35 Correct 144 ms 36472 KB Output is correct
36 Correct 211 ms 44280 KB Output is correct
37 Correct 158 ms 39920 KB Output is correct
38 Correct 177 ms 39920 KB Output is correct
39 Correct 98 ms 38844 KB Output is correct
40 Correct 146 ms 37392 KB Output is correct
41 Correct 165 ms 36600 KB Output is correct
42 Correct 6 ms 9684 KB Output is correct
43 Correct 227 ms 36440 KB Output is correct
44 Correct 230 ms 41560 KB Output is correct
45 Correct 220 ms 43700 KB Output is correct
46 Correct 253 ms 46236 KB Output is correct
47 Correct 212 ms 42128 KB Output is correct
48 Correct 158 ms 41292 KB Output is correct
49 Correct 114 ms 38512 KB Output is correct
50 Correct 116 ms 27324 KB Output is correct
51 Correct 210 ms 45880 KB Output is correct
52 Correct 180 ms 36636 KB Output is correct
53 Correct 6 ms 9684 KB Output is correct
54 Correct 6 ms 9736 KB Output is correct
55 Correct 5 ms 9684 KB Output is correct
56 Correct 7 ms 10000 KB Output is correct
57 Correct 7 ms 9964 KB Output is correct
58 Correct 6 ms 9940 KB Output is correct
59 Correct 6 ms 9940 KB Output is correct
60 Correct 6 ms 9884 KB Output is correct
61 Correct 6 ms 9940 KB Output is correct
62 Correct 7 ms 10072 KB Output is correct
63 Correct 8 ms 9940 KB Output is correct
64 Correct 6 ms 10068 KB Output is correct
65 Correct 7 ms 9940 KB Output is correct
66 Correct 7 ms 9940 KB Output is correct
67 Correct 169 ms 40364 KB Output is correct
68 Correct 150 ms 37724 KB Output is correct
69 Correct 198 ms 42496 KB Output is correct
70 Correct 197 ms 43700 KB Output is correct
71 Correct 177 ms 43656 KB Output is correct
72 Correct 201 ms 40732 KB Output is correct
73 Correct 175 ms 45992 KB Output is correct
74 Correct 130 ms 35372 KB Output is correct
75 Correct 190 ms 40384 KB Output is correct
76 Correct 181 ms 36728 KB Output is correct
77 Correct 137 ms 36928 KB Output is correct
78 Correct 143 ms 36920 KB Output is correct
79 Correct 146 ms 39908 KB Output is correct
80 Correct 93 ms 38860 KB Output is correct
81 Correct 201 ms 44264 KB Output is correct
82 Correct 230 ms 44564 KB Output is correct
83 Correct 158 ms 39872 KB Output is correct
84 Correct 137 ms 36224 KB Output is correct
85 Correct 231 ms 44320 KB Output is correct
86 Correct 166 ms 39896 KB Output is correct
87 Correct 163 ms 39888 KB Output is correct
88 Correct 161 ms 37388 KB Output is correct
89 Correct 165 ms 36572 KB Output is correct
90 Correct 262 ms 40888 KB Output is correct
91 Correct 220 ms 41552 KB Output is correct
92 Correct 248 ms 43664 KB Output is correct
93 Correct 242 ms 46408 KB Output is correct
94 Correct 220 ms 42148 KB Output is correct
95 Correct 146 ms 41328 KB Output is correct
96 Correct 106 ms 38408 KB Output is correct
97 Correct 110 ms 27360 KB Output is correct
98 Correct 216 ms 45876 KB Output is correct
99 Correct 167 ms 36624 KB Output is correct
100 Correct 53 ms 16396 KB Output is correct
101 Correct 284 ms 42292 KB Output is correct
102 Correct 175 ms 36932 KB Output is correct
103 Correct 205 ms 35856 KB Output is correct
104 Correct 229 ms 39836 KB Output is correct