QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#394138#4231. Rafting Tripkevinshan#WA 0ms24852kbC++175.4kb2024-04-20 06:16:102024-04-20 06:16:10

Judging History

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

  • [2024-04-20 06:16:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:24852kb
  • [2024-04-20 06:16:10]
  • 提交

answer

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

#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define f first
#define s second

int xm[4] = {-1, 0, 0, 1};
int ym[4] = {0, -1, 1, 0};

const int MAXN = 505 * 505;
vector<int> adj[MAXN];
vector<int> radj[MAXN];
int inc[MAXN];
int mark[MAXN];
int vis[MAXN];

bool is_river[MAXN];

vector<int> val[MAXN];
map<int, int> res;

int answer;

void dfs(int x, int p) {
    vis[x] = 1;
    for(int i:val[x]) {
        if(res.count(i) == 0) res[i] = 0;
        res[i]++;
    }

    for(int i:radj[x]) {
        if(i == p) continue;
        dfs(i,x);
    }

    answer = max(answer, (int)res.size());
    for(int i:val[x]) {
        res[i]--;
        if(res[i] == 0) {
            res.erase(i);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    if (fopen("input.in", "r")) {
        freopen("input.in", "r", stdin);
        freopen("output.out", "w", stdout);
    }
    answer = 0;
    int n, m;
    cin>>n>>m;
    char symbol[n][m];
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin>>symbol[i][j];
    int grid[n][m];
    int CNT = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++) {
            grid[i][j] = (CNT++);
        }
    }
    vector<char> riv = {'^','<','>','v'};
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            for(int k=0; k<4; k++){
                if(symbol[i][j] == riv[k]) {
                    int nx = i + xm[k];
                    int ny = j + ym[k];
                    if(nx >= 0 && nx < n && ny >= 0 && ny < m) {
                        if(symbol[nx][ny] == riv[0] || symbol[nx][ny] == riv[1] || symbol[nx][ny] == riv[2] || symbol[nx][ny] == riv[3]) {
                            adj[grid[i][j]].pb(grid[nx][ny]);
                            radj[grid[nx][ny]].pb(grid[i][j]);
                            inc[grid[nx][ny]]++;
                        }
                    }
                }
            }
        }
    }


    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(symbol[i][j] == '#'){
                for(int k=0; k<4; k++){
                    int nx = i + xm[k];
                    int ny = j + ym[k];
                    if(nx >= 0 && nx < n && ny >= 0 && ny < m) {
                        val[grid[nx][ny]].pb(grid[i][j]);
                    }
                }
            }
        }
    }

    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
                is_river[grid[i][j]] = 1;
            }
        }
    }

    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
            } else {
                mark[grid[i][j]] = 1;
            }
        }
    }
    queue<int> q;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
                if(inc[grid[i][j]] == 0) {
                    q.push(grid[i][j]);
                    mark[grid[i][j]] = 1;
                }
            }
        }
    }


    while(q.size()) {
        int c = q.front();
        q.pop();
        for(int i:adj[c]) {
            inc[i]--;
            if(inc[i] == 0) {
                mark[i] = 1;
                q.push(i);
            }
        }
    }

    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
                if(adj[grid[i][j]].size() == 0) mark[grid[i][j]] = 0;
            }
        }
    }
    // for(int i=0; i<n; i++){
    //     for(int j=0; j<m; j++){
    //         cout<<mark[grid[i][j]]<<" ";
    //     }
    //     cout<<"\n";
    // }
    // cout<<"\n";

    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(mark[grid[i][j]] == 0 && !vis[grid[i][j]]) {
                // is cyclical component!
                vector<int> comp;
                if(adj[grid[i][j]].size() == 0) comp.pb(grid[i][j]);
                else {
                    int cur = grid[i][j];
                    while(1){
                        comp.pb(cur);
                        for(int a:adj[cur]) {
                            if(is_river[a]) {
                                cur = a;
                            }
                        }
                        if(cur == grid[i][j]) break;
                    }
                }
                for(int x:comp) {
                    // cout<<x<<" ";
                    for(int v:val[x]) {
                        if(res.count(v) == 0) res[v] = 1;
                        res[v]++;
                    }
                    vis[x] = 1;
                }
                // cout<<"\n";
                for(int x:comp) {
                    for(int a:radj[x]) {
                        if(!vis[a]) {
                            dfs(a, x);
                        }
                    }
                }
                res.clear();
                // cout<<res.size();
            }
        }
    }

    cout<<answer;    
}


詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 24852kb

input:

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

output:

4

result:

ok single line: '4'

Test #2:

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

input:

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

output:

2

result:

ok single line: '2'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 24756kb

input:

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

output:

0

result:

wrong answer 1st lines differ - expected: '5', found: '0'