Submission #958671

# Submission time Handle Problem Language Result Execution time Memory
958671 2024-04-06T10:11:05 Z shenfe1 Virus Experiment (JOI19_virus) C++17
20 / 100
646 ms 262144 KB
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #include <x86intrin.h>
 
#include <bits/stdc++.h>
#include <chrono>
#include <random>
 
// @author: Vlapos
 
namespace operators
{
	template <typename T1, typename T2>
	std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
	{
		in >> x.first >> x.second;
		return in;
	}
 
	template <typename T1, typename T2>
	std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
	{
		out << x.first << " " << x.second;
		return out;
	}
 
	template <typename T1>
	std::istream &operator>>(std::istream &in, std::vector<T1> &x)
	{
		for (auto &i : x)
			in >> i;
		return in;
	}
 
	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}
 
	template <typename T1>
	std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
	{
		for (auto &i : x)
			out << i << " ";
		return out;
	}
}
 
// name spaces
using namespace std;
using namespace operators;
// end of name spaces
 
// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines
 
// usefull stuff
 
void boost()
{
	ios_base ::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}
 
inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }
 
const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};
 
const ll INF = (1e18) + 500;
const int BIG = (1e9) + 100;
const int MAXN = (1e5) + 5;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;
 
// #define int ll
 
const int maxN = 800 * 800+10;
// const int maxN = 810 * 810;
 
bool rules[maxN][16];
 
struct DSU
{
	map<int, bool> is[maxN];
	int pr[maxN];
 
	void init(int n)
	{
		for (int i = 0; i < n; ++i)
		{
			pr[i] = i;
			is[i][i];
		}
	}
 
	int getRoot(int c)
	{
		return pr[c] == c ? c : pr[c] = getRoot(pr[c]);
	}
};
 
struct test
{
	int m, r, c;
 
	bool good(int x, int y)
	{
		return x >= 0 and y >= 0 and x < r and y < c;
	}
	vector<int> tin, fup, added;
	DSU dsu;
 
	int ans = BIG, cnt = 0;
	int timer = 0;
 
	bool dfs(int v)
	{
		tin[v] = fup[v] = ++timer;
		queue<int> toV;
		{
			for (int d = 0; d < 4; ++d)
			{
				int tox = (v / c) + dx4[d];
				int toy = (v % c) + dy4[d];
				if (good(tox, toy))
				{
					int mask = 0;
					for (int D = 0; D < 4; ++D)
					{
						int totox = tox + dx4[D];
						int totoy = toy + dy4[D];
						if (good(totox, totoy))
							if (dsu.is[v].find(totox * c + totoy) != dsu.is[v].end())
								mask |= (1 << D);
					}
					if (rules[tox * c + toy][mask])
					{
						toV.push(tox * c + toy);
					}
				}
			}
		}
 
		bool isGood = true;
		unordered_map<int, int> HELP;
		while (toV.size())
		{
			int to = toV.front();
			to = dsu.getRoot(to);
			toV.pop();
			if (dsu.getRoot(to) == dsu.getRoot(v))
				continue;
			// cout << v << " -> " << to << '\n';
			if (!tin[to])
			{
				isGood &= dfs(to);
				fup[v] = min(fup[v], fup[to]);
				if (fup[to] <= tin[v])
				{
					assert(dsu.getRoot(to) == to);
					assert(dsu.getRoot(v) == v);
					// merge prev and cur
					if (dsu.is[v].size() < dsu.is[to].size())
						swap(dsu.is[v], dsu.is[to]);
 
					for (auto &[el, bf] : dsu.is[to])
						for (int d = 0; d < 4; ++d)
						{
							int tox = (el / c) + dx4[d];
							int toy = (el % c) + dy4[d];
							if (good(tox, toy))
							{
								int cnt1 = 0, cnt2 = 0;
								int mask = 0;
								for (int D = 0; D < 4; ++D)
								{
									int totox = tox + dx4[D];
									int totoy = toy + dy4[D];
									if (good(totox, totoy))
										if (dsu.is[v].find(totox * c + totoy) != dsu.is[v].end())
										{
											cnt1++;
											mask |= (1 << D);
										}
										else if (dsu.is[to].find(totox * c + totoy) != dsu.is[to].end())
										{
											cnt2++;
											mask |= (1 << D);
										}
								}
								if (rules[tox * c + toy][mask] and cnt1 and cnt2 and dsu.getRoot(tox * c + toy) != dsu.getRoot(v) and !HELP[dsu.getRoot(tox * c + toy)])
								{
									int cur = dsu.getRoot(tox * c + toy);
									HELP[cur] = 1;
									toV.push(cur);
								}
							}
						}
 
					for (auto &[el, bf] : dsu.is[to])
						dsu.is[v][el];
					dsu.pr[to] = v;
					dsu.is[to].clear();
				}
				else
					isGood = false;
			}
			else if (tin[v] > tin[to])
			{
				fup[v] = min(tin[to], fup[v]);
			}
			else
				isGood = false;
		}
 
		if (tin[v] == fup[v] and isGood)
		{
			assert(dsu.getRoot(v) == v);
			int VAL = (int)dsu.is[v].size();
			if (ans > VAL)
			{
				ans = VAL;
				cnt = VAL;
			}
			else if (ans == VAL)
			{
				cnt += VAL;
			}
		}
		tin[v] = BIG;
		return isGood;
	}
 
	void solve(int testcase)
	{
		boost();
 
		cin >> m >> r >> c;
 
		dsu.init(r * c);
		tin.resize(r * c);
		fup.resize(r * c);
		added.resize(r * c);
		string s;
		cin >> s;
		s = s + s;
		m *= 2;
 
		// const int dx4[4] = {-1, 0, 0, 1};
		// const int dy4[4] = {0, -1, 1, 0};
		map<char, int> dist;
		dist['N'] = 0;
		dist['W'] = 1;
		dist['E'] = 2;
		dist['S'] = 3;
 
		vector<int> longest(1 << 4);
		for (int i = 1; i < (1 << 4); ++i)
		{
			int cnt = 0, cur = 0;
			for (int j = 0; j < m; ++j)
			{
				if ((i >> (dist[s[j]])) & 1)
					cur++;
				else
					cur = 0;
				cnt = max(cnt, cur);
			}
			if (cnt == m)
				longest[i] = BIG;
			else
				longest[i] = cnt;
		}
 
		vector<vector<int>> u(r, vector<int>(c));
		cin >> u;
		for (int i = 0; i < r; ++i)
			for (int j = 0; j < c; ++j)
				if (!u[i][j])
					u[i][j] = BIG + 10;
 
		for (int i = 0; i < r; ++i)
			for (int j = 0; j < c; ++j)
				for (int mask = 0; mask < (1 << 4); ++mask)
				{
					bool cur = true;
					for (int val = 0; val < 4; ++val)
					{
						int tox = i + dx4[val];
						int toy = j + dy4[val];
						cur &= (good(tox, toy) or !((mask >> val) & 1));
					}
					if (cur and u[i][j] <= longest[mask])
					{
						rules[i * c + j][mask] = 1;
					}
				}
 
		for (int i = 0; i < r; ++i)
			for (int j = 0; j < c; ++j)
				if (!tin[i * c + j] and u[i][j] != BIG + 10)
					dfs(i * c + j);
		if (ans == BIG)
			ans = 0, cnt = 0;
 
		cout << ans << '\n'
			 << cnt << '\n';
	}
};
 
main()
{
	boost();
	int q = 1;
	// cin >> q;
	for (int i = 0; i < q; i++)
	{
		test t;
		t.solve(i);
	}
	return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
//                                                                                    //
//                               Coded by Der_Vlἀpos                                  //
//                                                                                    //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//

Compilation message

virus.cpp: In member function 'bool test::dfs(int)':
virus.cpp:197:13: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
  197 |          if (good(totox, totoy))
      |             ^
virus.cpp: At global scope:
virus.cpp:329:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  329 | main()
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 33116 KB Output is correct
2 Correct 242 ms 83028 KB Output is correct
3 Correct 261 ms 83892 KB Output is correct
4 Correct 228 ms 83032 KB Output is correct
5 Correct 241 ms 83144 KB Output is correct
6 Correct 21 ms 32860 KB Output is correct
7 Correct 646 ms 84212 KB Output is correct
8 Correct 143 ms 53136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 32996 KB Output is correct
2 Correct 39 ms 33372 KB Output is correct
3 Correct 60 ms 33368 KB Output is correct
4 Correct 30 ms 33372 KB Output is correct
5 Correct 59 ms 33360 KB Output is correct
6 Correct 72 ms 35428 KB Output is correct
7 Correct 22 ms 32860 KB Output is correct
8 Correct 65 ms 33372 KB Output is correct
9 Correct 30 ms 33364 KB Output is correct
10 Correct 34 ms 33372 KB Output is correct
11 Correct 23 ms 33116 KB Output is correct
12 Correct 26 ms 34140 KB Output is correct
13 Correct 53 ms 33556 KB Output is correct
14 Correct 57 ms 33536 KB Output is correct
15 Correct 55 ms 35992 KB Output is correct
16 Correct 53 ms 33528 KB Output is correct
17 Correct 40 ms 33380 KB Output is correct
18 Correct 25 ms 33260 KB Output is correct
19 Correct 60 ms 33780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 33116 KB Output is correct
2 Correct 242 ms 83028 KB Output is correct
3 Correct 261 ms 83892 KB Output is correct
4 Correct 228 ms 83032 KB Output is correct
5 Correct 241 ms 83144 KB Output is correct
6 Correct 21 ms 32860 KB Output is correct
7 Correct 646 ms 84212 KB Output is correct
8 Correct 143 ms 53136 KB Output is correct
9 Correct 21 ms 32996 KB Output is correct
10 Correct 39 ms 33372 KB Output is correct
11 Correct 60 ms 33368 KB Output is correct
12 Correct 30 ms 33372 KB Output is correct
13 Correct 59 ms 33360 KB Output is correct
14 Correct 72 ms 35428 KB Output is correct
15 Correct 22 ms 32860 KB Output is correct
16 Correct 65 ms 33372 KB Output is correct
17 Correct 30 ms 33364 KB Output is correct
18 Correct 34 ms 33372 KB Output is correct
19 Correct 23 ms 33116 KB Output is correct
20 Correct 26 ms 34140 KB Output is correct
21 Correct 53 ms 33556 KB Output is correct
22 Correct 57 ms 33536 KB Output is correct
23 Correct 55 ms 35992 KB Output is correct
24 Correct 53 ms 33528 KB Output is correct
25 Correct 40 ms 33380 KB Output is correct
26 Correct 25 ms 33260 KB Output is correct
27 Correct 60 ms 33780 KB Output is correct
28 Runtime error 308 ms 262144 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -