QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#370407#4231. Rafting TripInfinityNSTL 1ms4104kbC++203.8kb2024-03-29 06:36:192024-03-29 06:36:21

Judging History

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

  • [2024-03-29 06:36:21]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:4104kb
  • [2024-03-29 06:36:19]
  • 提交

answer

#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int,int> pii;

const int mod=998244353;
inline int add(int x,int y){int ret=x+y;if(ret>=mod)ret-=mod;return ret;}
inline int sub(int x,int y){int ret=x-y;if(ret<0)ret+=mod;return ret;}
inline int mul(int x,int y){return ((ll)x*y)%mod;}
inline int step(int base,int pw){int ret=1;while(pw){if(pw&1)ret=mul(ret,base);base=mul(base,base);pw>>=1;}return ret;}
inline int invv(int x){return step(x,mod-2);}


const int maxd=510;
const int maxn=maxd*maxd;

int n,m;
int a[maxd][maxd];

/// dole,gore,desno,levo
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int tox(int x,int y){
    x--;y--;
    return x*m+y;
}
pii toxy(int x){
    int px=x/m;
    int py=x%m;
    px++;
    py++;
    return {px,py};
}
bool cell_bad(int x,int y){
    if(x<1 || y<1 || x>n || y>m)return true;
    if(a[x][y]>=4)return true;
    return false;
}

vector<int>vect[maxn],revg[maxn];
void add_edge(int x,int y){
    vect[x].pb(y);
    revg[y].pb(x);
}

int pos[maxn];
void dfs1(int x,vector<int>&stek,vector<int>&cyc){

    pos[x]=1;
    stek.pb(x);
    for(int i=0;i<vect[x].size();i++){
        int id=vect[x][i];
        if(pos[id]){
            for(int j=stek.size()-1;j>=0;j--){
                cyc.pb(stek[j]);
                pos[stek[j]]=2;
                if(stek[j]==id)break;
            }
            continue;
        }
        dfs1(id,stek,cyc);
    }
}

int rez=0;
int currez=0;
int cnt[maxd][maxd];
void inc(int x,int y){
    if(cnt[x][y]==0)currez++;
    cnt[x][y]++;
    rez=max(rez,currez);
}
void dec(int x,int y){
    cnt[x][y]--;
    if(cnt[x][y]==0)currez--;
}
void dodaj_cvor(int p){

    int x=p/m;
    int y=p%m;
    x++;
    y++;

    for(int i=0;i<4;i++){
        int idx=x+dx[i];
        int idy=y+dy[i];
        if(idx<1 || idy<1 || idx>n || idy>m)continue;
        if(a[idx][idy]==5){
            inc(idx,idy);
        }
    }

}
void skini_cvor(int p){

    int x=p/m;
    int y=p%m;
    x++;
    y++;

    for(int i=0;i<4;i++){
        int idx=x+dx[i];
        int idy=y+dy[i];
        if(idx<1 || idy<1 || idx>n || idy>m)continue;
        if(a[idx][idy]==5){
            dec(idx,idy);
        }
    }

}
void dfs2(int x){

    dodaj_cvor(x);

    for(int i=0;i<revg[x].size();i++){
        int id=revg[x][i];
        if(pos[id]==2)continue;
        dfs2(id);
    }

    skini_cvor(x);
}
void go(int x){

    vector<int>stek;
    vector<int>cyc;
    dfs1(x,stek,cyc);

    for(int i=0;i<cyc.size();i++){
        dodaj_cvor(cyc[i]);
    }

    for(int i=0;i<cyc.size();i++){
        int id=cyc[i];
        dfs2(id);
    }

    for(int i=0;i<cyc.size();i++){
        skini_cvor(cyc[i]);
    }

}

int main(){

    ///freopen("test.txt","r",stdin);

    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        for(int j=1;j<=m;j++){
            if(s[j-1]=='.')a[i][j]=4;
            if(s[j-1]=='#')a[i][j]=5;
            if(s[j-1]=='v')a[i][j]=0;
            if(s[j-1]=='^')a[i][j]=1;
            if(s[j-1]=='>')a[i][j]=2;
            if(s[j-1]=='<')a[i][j]=3;
        }
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){

            if(a[i][j]<4){

                int idx=i+dx[a[i][j]];
                int idy=j+dy[a[i][j]];

                if(cell_bad(idx,idy))add_edge(tox(i,j),tox(i,j));
                else add_edge(tox(i,j),tox(idx,idy));

            }

        }
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(pos[tox(i,j)])continue;
            go(tox(i,j));
        }
    }

    printf("%d\n",rez);

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4

result:

ok single line: '4'

Test #2:

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

input:

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

output:

2

result:

ok single line: '2'

Test #3:

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

input:

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

output:

5

result:

ok single line: '5'

Test #4:

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

input:

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

output:

2

result:

ok single line: '2'

Test #5:

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

input:

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

output:

2

result:

ok single line: '2'

Test #6:

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

input:

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

output:

6

result:

ok single line: '6'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

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

input:

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

output:

4

result:

ok single line: '4'

Test #9:

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

input:

2 2
>.
.#

output:

0

result:

ok single line: '0'

Test #10:

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

input:

2 2
..
.v

output:

0

result:

ok single line: '0'

Test #11:

score: -100
Time Limit Exceeded

input:

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

output:


result: