QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#259063#6723. Grid with ArrowsAH20RE 0ms17212kbC++142.9kb2023-11-20 16:12:082023-11-20 16:12:09

Judging History

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

  • [2023-11-20 16:12:09]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:17212kb
  • [2023-11-20 16:12:08]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
const int maxm = 2e5+7;
int n,m;
vector<vector<char>>dir;
vector<vector<int>>step;
vector<int>edge[maxm],ne[maxm];
int dfn[maxm],low[maxm],instk[maxm],stk[maxm],col[maxm];
int tot = 0,tp = 0,tc = 0;
int inv[maxm],vis[maxm];
int N;
void init(){
    dir.resize(n,vector<char>(m));
    step.resize(n,vector<int>(m,0));
    //重置几个关键的数组
    for(int i=0;i<=n*m;i++){
        edge[i].clear();
        ne[i].clear();
        //清空邻接表的边
        inv[i] = vis[i] = 0;
        instk[i] = 0;col[i] = 0;
        dfn[i] = 0;low[i] = 0;
        stk[i] = 0;
    }
    tot = tp = tc = 0;
    N = n*m;
}
void tarjan(int now){
    dfn[now] = low[now] = ++tot;
    stk[++tp] = now;
    instk[now] = 1;
    for(auto u:edge[now]){
        if(dfn[u]==0){
            tarjan(u);
            low[now] = min(low[now],low[u]);
        }
        else{
            if(instk[u]){
                low[now] = min(low[now],low[u]);
            }
        }
    }
    if(low[now] == dfn[now]){
        //说明该节点是强连通分量的起始点
        ++tc;
        while(stk[tp] != now){
            instk[stk[tp]] = 0;
            col[stk[tp]] = tc;
            --tp;
        }
        col[now] = tc;
        instk[now] = 0;
        --tp;
    }
}
void work(){
    //构造出新的图
    for(int i=1;i<=N;i++){
        for(auto u:edge[i]){
            if(col[i] == col[u]) continue;
            ne[col[i]].push_back(col[u]);
            inv[col[u]]++;
        }
    }
}
int id(int x,int y){
    return x*m + y+1;
}
void solve(){
    cin>>n>>m;
    init();
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>dir[i][j];
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>step[i][j];
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int ni = i,nj = j;
            switch (dir[i][j]){
            case 'u':
                ni = i - step[i][j];
                break;
            case 'd':
                ni = i + step[i][j];
                break;
            case 'l':
                nj = j - step[i][j];
                break;
            case 'r':
                nj = j + step[i][j]; 
            }
            if(ni>=0 and ni<n and nj>=0 and nj<m){
                edge[id(i,j)].push_back(id(ni,nj));
            }
        }
    }
    //完成建图,下面开始跑tarjan
    for(int i=1;i<=N;i++){
        if(dfn[i] == 0){
            tarjan(i);
        }
    }
    work();
    int sum = 0;
    for(int i=1;i<=tc;i++){
        if(inv[i] == 0){ ++sum;}
    }
    if(sum > 1){
        cout<<"No\n";
    }
    else{
        cout<<"Yes\n";
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int T;cin>>T;
    while(T--) solve();
    return 0;
}

詳細信息

Test #1:

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

input:

2
2 3
rdd
url
2 1 1
1 1 2
2 2
rr
rr
1 1
1 1

output:

Yes
No

result:

ok 2 token(s): yes count is 1, no count is 1

Test #2:

score: -100
Runtime Error

input:

1109
5 8
rddddldl
drruludl
rrldrurd
urrrlluu
uurrulrl
4 4 1 2 4 3 1 6
1 3 5 1 1 1 3 6
2 4 1 1 2 1 1 1
2 3 4 2 4 3 3 3
4 1 1 2 2 5 1 5
7 9
rdrddrdll
urrdruldl
ruullrulu
drrlrlddl
rrrdddlll
ruulururl
ruurrlluu
7 1 1 1 2 1 2 3 6
1 7 3 1 3 1 2 1 8
2 2 1 2 4 3 1 2 2
2 2 4 1 1 5 3 3 1
3 4 6 1 2 1 2 7 7
6 ...

output:


result: