QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#625555#4231. Rafting TripNYCU_CartesianTree#TL 45ms35704kbC++203.6kb2024-10-09 19:49:062024-10-09 19:49:10

Judging History

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

  • [2024-10-09 19:49:10]
  • 评测
  • 测评结果:TL
  • 用时:45ms
  • 内存:35704kb
  • [2024-10-09 19:49:06]
  • 提交

answer

//============================================================================
// Name        : G.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<bits/stdc++.h>
using namespace std;
const int N = 505;

char a[N][N];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0 , 1}}; // up down left right
int degree[N * N];
int to[N * N];
vector<int> graph[N * N];
bool vis[N * N], dfs_vis[N * N], visit_site[N][N], not_circle[N * N];
int n, m;
int cnt = 0;
int dfs(int v){
//	cerr << v << '\n';

	int x = (v - 1) / m + 1, y = (v - 1) % m + 1;
	dfs_vis[v] = 1;
	vector<pair<int, int> > add_site;
	for(int j = 0; j < 4; j++){
		int tox = x + dir[j][0], toy = y + dir[j][1];
		if(tox <= 0 || tox > n || toy <= 0 || toy > m)
			continue;
		if(a[tox][toy] == '#' && !visit_site[tox][toy]){
			visit_site[tox][toy] = 1;
			cnt++;
			add_site.push_back({tox, toy});
		}
	}
	int maxn = cnt;
	for(auto i : graph[v]){
		if(dfs_vis[i] || !not_circle[i]){
			continue;
		}
		maxn = max(maxn, dfs(i));
	}
	for(auto i : add_site){
		visit_site[i.first][i.second] = 0;
		cnt--;
	}
	return maxn;
}

int main() {
	cin >> n >> m;
	for(int i = 1; i <= n * m; i++){
		not_circle[i] = 1;
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> a[i][j];
		}
	}
	vector<int> lien;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(a[i][j] == '.' || a[i][j] == '#')
				continue;
			int id = (i - 1) * m + j;
			int x, y;
			if(a[i][j] == '^'){
				x = i + dir[0][0];
				y = j + dir[0][1];
			}else if(a[i][j] == 'v'){
				x = i + dir[1][0];
				y = j + dir[1][1];
			}else if(a[i][j] == '<'){
				x = i + dir[2][0];
				y = j + dir[2][1];
			}else if(a[i][j] == '>'){
				x = i + dir[3][0];
				y = j + dir[3][1];
			}
			int id2 = (x - 1) * m + y;
			if(x <= 0 || x > n || y <= 0 || y > m || a[x][y] == '.' || a[x][y] == '#'){
				lien.push_back(id);
				continue;
			}


			graph[id2].push_back(id);

			to[id] = id2;
			degree[id]++;
//			cerr << id2 << ' ' << id << '\n';
		}

	}
//	return 0;
	queue<int> que;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			int id = (i - 1) * m + j;
			if(degree[id] == 0){
				que.push(id);
			}
		}
	}
	while(!que.empty()){
		int top = que.front();
		que.pop();
		for(auto i : graph[top]){
			degree[i]--;
			if(degree[i] == 0){
				que.push(i);
			}
		}
	}
	int ans = 0;
	for(int i = 1; i <= n * m; i++){
		if(degree[i] != 0 && not_circle[i]){
			not_circle[i] = 0;
			int now = i, circle_cnt = 0;
			vector<pair<int, int> > add_site;
			while(!vis[now]){
				vis[now] = 1;
				not_circle[now] = 0;
				int x = (now - 1) / m + 1, y = (now - 1) % m + 1;
				for(int j = 0; j < 4; j++){
					int tox = x + dir[j][0], toy = y + dir[j][1];
					if(tox <= 0 || tox > n || toy <= 0 || toy > m)
						continue;
					if(a[tox][toy] == '#' && !visit_site[tox][toy]){
						visit_site[tox][toy] = 1;
						circle_cnt++;
						add_site.push_back({tox, toy});
					}
				}
				now = to[now];
			}
			ans = max(ans, circle_cnt);
			now = i;
			while(vis[now]){
//				cerr << now << '\n';
				vis[now] = 0;
				for(auto j : graph[now]){
					ans = max(ans, circle_cnt + dfs(j));
				}
				now = to[now];
			}
			for(auto j : add_site){
				visit_site[j.first][j.second] = 0;
			}
		}
	}

	for(auto i : lien){
		ans = max(ans, dfs(i));
	}
	cout << ans << '\n';

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7592kb

input:

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

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 1ms
memory: 5556kb

input:

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

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 1ms
memory: 5688kb

input:

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

output:

5

result:

ok single line: '5'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5648kb

input:

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

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 1ms
memory: 5656kb

input:

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

output:

2

result:

ok single line: '2'

Test #6:

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

input:

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

output:

6

result:

ok single line: '6'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3604kb

input:

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

output:

4

result:

ok single line: '4'

Test #9:

score: 0
Accepted
time: 1ms
memory: 3832kb

input:

2 2
>.
.#

output:

0

result:

ok single line: '0'

Test #10:

score: 0
Accepted
time: 1ms
memory: 3820kb

input:

2 2
..
.v

output:

0

result:

ok single line: '0'

Test #11:

score: 0
Accepted
time: 45ms
memory: 30960kb

input:

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

output:

41195

result:

ok single line: '41195'

Test #12:

score: 0
Accepted
time: 31ms
memory: 35704kb

input:

500 500
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...

output:

24926

result:

ok single line: '24926'

Test #13:

score: -100
Time Limit Exceeded

input:

500 500
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...

output:


result: