QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#496511#4231. Rafting TripCelestialCoder#TL 2ms9764kbC++203.6kb2024-07-28 12:26:292024-07-28 12:26:29

Judging History

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

  • [2024-07-28 12:26:29]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:9764kb
  • [2024-07-28 12:26:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int r, c;
int arr[501][501];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};


vector<int> adj[300001];
int graph[300001];
vector<int> rev[300001];
int sid;

int visited[300001];
int cycle[300001];
int cid;
vector<int> ci[300001];
int init_dfs(int current) {
    visited[current] = 1;
    int next = graph[current];
    if (cycle[next]) {
        visited[current] = -1;
        return -1;
    }
    if (visited[next] == 1) {
        cycle[current] = ++cid;
        ci[cid].push_back(current);
        visited[current] = -1;
        return current != next ? next : -1;
    }
    int r = init_dfs(next);
    visited[current] = -1;
    if (r == -1)
        return -1;
    ci[cid].push_back(current);
    cycle[current] = cid;
    if (current == r)
        return -1;
    return r;
}

int in_cnt;
int in[300001];
void push_adj(int idx) {
    for (int i: adj[idx]) {
        in_cnt += !in[i]++;
    }
}
void pop_adj(int idx) {
    for (int i: adj[idx]) {
        in_cnt -= !--in[i];
    }
}

int dfs(int current) {
    push_adj(current);
    int ret = in_cnt;
    int size = rev[current].size();
    for (int i = 0; i < size; ++i) {
        int next = rev[current][i];
        if (cycle[next])
            continue;
        ret = max(ret, dfs(next));
    }
    pop_adj(current);
    return ret;
}

int track(int idx) {
    int size = ci[idx].size();
    for (int i = 0; i < size; ++i) {
        int p = ci[idx][i];
        push_adj(p);
    }

    int ret = in_cnt;
    for (int i = 0; i < size; ++i) {
        ret = max(ret, dfs(ci[idx][i]));
    }

    for (int i = 0; i < size; ++i) {
        int p = ci[idx][i];
        pop_adj(p);
    }
    return ret;
}

int main() {
    cin >> r >> c;
    sid = 4;
    for (int i = 1; i <= r; ++i) {
        for (int j = 1; j <= c; ++j) {
            char v;
            cin >> v;
            if (v == '<')
                arr[i][j] = 2;
            else if (v == '>')
                arr[i][j] = 0;
            else if (v == '^')
                arr[i][j] = 3;
            else if (v == 'v')
                arr[i][j] = 1;
            else if (v == '.')
                arr[i][j] = -1;
            else
                arr[i][j] = ++sid;
        }
    }

    for (int i = 1; i <= r; ++i) {
        for (int j = 1; j <= c; ++j) {
            for (int k = 0; k < 4; ++k) {
                int nx = i + dx[k], ny = j + dy[k];
                if (nx <= 0 || ny <= 0 || nx > r || ny > c)
                    continue;
                if (arr[nx][ny] >= 4) {
                    adj[i * c + j].push_back(arr[nx][ny]);
                }
            }

            if (arr[i][j] >= 0 && arr[i][j] < 4) {
                int nx = i + dx[arr[i][j]], ny = j + dy[arr[i][j]];

                if (nx <= 0 || ny <= 0 || nx > r || ny > c ||
                    arr[nx][ny] < 0 || arr[nx][ny] >= 4) {
                    graph[i * c + j] = (0);
                    rev[0].push_back(i * c + j);
                }
                else {
                    graph[i * c + j] = (nx * c + ny);
                    rev[nx * c + ny].push_back(i * c + j);
                }
            }
        }
    }


    for (int i = 1; i <= r; ++i) {
        for (int j = 1; j <= c; ++j) {
            if (arr[i][j] >= 0 && arr[i][j] < 4) {
                if (!visited[i * c + j])
                    init_dfs(i * c + j);
            }
        }
    }

    int ans = 0;
    for (int i = 1; i <= cid; ++i) {
        ans = max(ans, track(i));
    }
    cout << ans;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4

result:

ok single line: '4'

Test #2:

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

input:

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

output:

2

result:

ok single line: '2'

Test #3:

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

input:

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

output:

5

result:

ok single line: '5'

Test #4:

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

input:

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

output:

2

result:

ok single line: '2'

Test #5:

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

input:

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

output:

2

result:

ok single line: '2'

Test #6:

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

input:

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

output:

6

result:

ok single line: '6'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

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

input:

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

output:

4

result:

ok single line: '4'

Test #9:

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

input:

2 2
>.
.#

output:

0

result:

ok single line: '0'

Test #10:

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

input:

2 2
..
.v

output:

0

result:

ok single line: '0'

Test #11:

score: -100
Time Limit Exceeded

input:

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

output:


result: