QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#522770#4231. Rafting TripRngBasedWA 35ms28688kbC++203.5kb2024-08-17 13:31:392024-08-17 13:31:39

Judging History

你现在查看的是最新测评结果

  • [2024-08-17 13:31:39]
  • 评测
  • 测评结果:WA
  • 用时:35ms
  • 内存:28688kb
  • [2024-08-17 13:31:39]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define F first
#define S second
#define N 250005
#define all(x) x.begin(), x.end()
using namespace std;
int r, c, dfn[N], low[N], t = 0, boss[N], nscc = 0, deg[N], in[N], out[N], val[N], dp[N];
bool ins[N], vis[N];
char ch[505][505];
stack<int> st;
vector<int> g[N], G[N];
vector<pii> sight;
int idx(int x, int y){
	return x * c + y;
}
void tarjan(int p){
	dfn[p] = low[p] = ++t;
	st.push(p); ins[p] = 1;
	for (auto i : g[p]){
		if (!dfn[i]){
			tarjan(i);
			low[p] = min(low[p], low[i]);
		}
		else if (dfn[p] > dfn[i] && ins[i])
			low[p] = min(low[p], dfn[i]);
	}
	if (low[p] == dfn[p]){
		nscc++;
		for (int x = -1; x != p; st.pop()){
			x = st.top();
			ins[x] = 0;
			boss[x] = nscc;
		}
	}
}
void dfs(int p){
	in[p] = ++t;
	for (auto i : G[p])
		dfs(i);
	out[p] = ++t;
}
bool check(int x, int y){
	return x >= 0 && x < r && y >= 0 && y < c;
}
bool isac(int a, int b){
	return in[a] < in[b] && out[a] > out[b];
}
void solve(int p){
	if (vis[p])
		return;
	vis[p] = 1;
	dp[p] += val[p];
	int add = 0;
	for (auto i : G[p]){
		solve(i);
		add = max(add, dp[i]);
	}
	dp[p] += add;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
	cin >> r >> c;
	for (int i = 0; i < r; i++){
		for (int j = 0; j < c; j++){
			cin >> ch[i][j];
		}
	}
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++){
			char c = ch[i][j];
			if (c == '.')
				continue;
			else if (c == '^' && i > 0 && ch[i - 1][j] != '.' && ch[i - 1][j] != '#')
				g[idx(i, j)].push_back(idx(i - 1, j));
			else if (c == 'v' && i < r - 1 && ch[i + 1][j] != '.' && ch[i + 1][j] != '#')
				g[idx(i, j)].push_back(idx(i + 1, j));
			else if (c == '>' && j < c - 1 && ch[i][j + 1] != '.' && ch[i][j + 1] != '#')
				g[idx(i, j)].push_back(idx(i, j + 1));
			else if (c == '<' && j > 0 && ch[i][j - 1] != '.' && ch[i][j - 1] != '#')
				g[idx(i, j)].push_back(idx(i, j - 1));
			else if (c == '#')
				sight.emplace_back(i, j);
		}
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
			if (!dfn[idx(i, j)] && ch[i][j] != '.' && ch[i][j] != '#')
				tarjan(idx(i, j));
	for (int i = 0; i < r * c; i++){
		for (auto j : g[i])
			if (boss[i] != boss[j] && boss[i] != 0 && boss[j] != 0)
				G[boss[j]].push_back(boss[i]), deg[boss[i]]++;
	}
	t = 0;
	for (int i = 1; i <= nscc; i++)
		if (deg[i] == 0)
			dfs(i);

	// for (int i = 0; i < r; i++)
	// 	for (int j = 0; j < c; j++)
	// 		cerr << boss[idx(i, j)] << " \n"[j == c - 1];

	for (auto [x, y]: sight){
		set<int> needin;
		vector<pii> d = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
		for (auto [dx, dy] : d){
			if (check(x + dx, y + dy)){
				bool no = 0;
				vector<int> rem;
				for (auto j : needin){
					// cerr << "check " << boss[idx(x + dx, y + dy)] << " " << j << " " << isac(boss[idx(x + dx, y + dy)], j) << '\n';
					if (isac(boss[idx(x + dx, y + dy)], j))
						rem.push_back(j);
					if (isac(j, boss[idx(x + dx, y + dy)]))
						no = 1;
				}
				assert(!(no == 1 && rem.size() > 0));
				if (no == 1)
					continue;
				for (auto j : rem)
					needin.erase(j);
				needin.insert(boss[idx(x + dx, y + dy)]);
			}
		}
		for (auto i : needin)
			val[i]++;
	}
	for (int i = 1; i <= nscc; i++)
		solve(i);
	cout << *max_element(dp, dp + N) << '\n';
}
/*
5 5
v<<<<
v#^.<
>>^<^
.....
.....

2 2
#.
.<
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10048kb

input:

5 6
v<<<#v
v#v<.>
>>v<<<
..v##^
#<<<.^

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 7736kb

input:

4 5
>v<<.
^<..#
#...#
.#>^#

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 2ms
memory: 8004kb

input:

4 6
>>v#>v
^#>>^v
^<<#v<
.#^<<#

output:

5

result:

ok single line: '5'

Test #4:

score: 0
Accepted
time: 0ms
memory: 8004kb

input:

6 6
^.>>>>
^.....
^....v
^....v
#....v
<<<<#v

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 2ms
memory: 9748kb

input:

6 7
^>>>>>v
^.....v
^.#^..v
^.#^<.v
^.....v
^<<<<<<

output:

2

result:

ok single line: '2'

Test #6:

score: 0
Accepted
time: 2ms
memory: 8000kb

input:

3 7
>v<<<<#
^<#####
#^<<<<<

output:

6

result:

ok single line: '6'

Test #7:

score: 0
Accepted
time: 0ms
memory: 7964kb

input:

3 5
><.v#
##.^#
...#.

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 2ms
memory: 7700kb

input:

7 3
###
#>#
###
...
###
#>.
###

output:

4

result:

ok single line: '4'

Test #9:

score: 0
Accepted
time: 2ms
memory: 7704kb

input:

2 2
>.
.#

output:

0

result:

ok single line: '0'

Test #10:

score: 0
Accepted
time: 0ms
memory: 7920kb

input:

2 2
..
.v

output:

0

result:

ok single line: '0'

Test #11:

score: -100
Wrong Answer
time: 35ms
memory: 28688kb

input:

498 498
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<...

output:

766

result:

wrong answer 1st lines differ - expected: '41195', found: '766'