QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#726385#5476. Remodeling the Dungeonshstyle#WA 0ms20156kbC++233.5kb2024-11-08 23:32:472024-11-08 23:32:47

Judging History

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

  • [2024-11-08 23:32:47]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:20156kb
  • [2024-11-08 23:32:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int N=1e6+10;

ll n,m,k;

vector<int> e[N];
vector<PII> v;
char s[1010][1010];
int id[1010][1010];
int dep[N];
bool tf[N];
ll cnt[N];
typedef struct{
    int fa,len,tf;
}Node;

Node fa[N][20];

ll ans;

void dfs(int x,int f){
    fa[x][0].fa=f;
    fa[x][0].len=1;
    for(auto j:e[x]){
        if(j==f) continue;
        dep[j]=dep[x]+1;
        dfs(j,x);
    }
}


int LCA(int u,int v){
    if(dep[u]<dep[v]) swap(u,v);
    for(int i=19;i>=0;i--){
        if(dep[fa[u][i].fa]>=dep[v]) u=fa[u][i].fa;
    }
    // cout<<u<<" "<<v<<endl;
    if(u==v) return u;
    for(int i=19;i>=0;i--){
        if(dep[fa[u][i].fa]!=dep[fa[v][i].fa]){
            u=fa[u][i].fa;
            v=fa[v][i].fa;
        }
    }
    
    return fa[u][0].fa;
}



void HuangGuangsheng(int u,int v){
    // cout<<u<<" "<<v<<endl;
    int lca=LCA(u,v);
    // cout<<u<<" "<<v<<" "<<lca<<endl;
    int ru=u,rv=v;
    if(u!=lca)
    if(tf[ru]){
        ans=max(ans,dep[rv]+1+cnt[ru]);
        // cout<<"--"<<dep[rv]+1+cnt[ru]<<endl;
    }else
    {
        int len=0;
    for(int i=19;i>=0;i--){
        if(fa[u][i].tf||dep[fa[u][i].fa]<=dep[lca]) continue;
        len+=fa[u][i].len;
        u=fa[u][i].fa;
    }
    u=fa[u][0].fa;
    len++;
    if(u!=lca){
        ans=max(ans,dep[rv]+1+len+cnt[u]);
        // cout<<dep[rv]+1+len+cnt[u]<<endl;
    }
    }
    
    if(v!=lca)
    if(tf[rv]){
        ans=max(ans,dep[ru]+1+cnt[rv]);
        // cout<<dep[ru]<<" "<<1<<" "<<cnt[rv]<<endl;
    }else{
        int len=0;
    for(int i=19;i>=0;i--){
        if(fa[v][i].tf||dep[fa[v][i].fa]<=dep[lca]) continue;
        len+=fa[v][i].len;
        v=fa[v][i].fa;
    }
    v=fa[v][0].fa;
    len++;
    // cout<<" "<<v<<endl;
    if(v!=lca){
        ans=max(ans,dep[ru]+1+len+cnt[v]);
        // cout<<dep[ru]+1+len+cnt[v]<<endl;
    }
    }
}

int main(){
//    ios::sync_with_stdio(0);
//    cin.tie(0);
    memset(cnt,-0x3f,sizeof cnt);
    cin>>n>>m;
    for(int i=1;i<=n*2+1;i++){
        scanf("%s",s[i]+1);
    }
    int idx=0;
    dep[1]=1;
    for(int i=2;i<=2*n+1;i+=2){
        for(int j=2;j<=2*m+1;j+=2){
            id[i][j]=++idx;
        }
    }

    for(int i=2;i<=2*n+1;i+=2){
        for(int j=3;j<2*m+1;j+=2){
            int l=id[i][j-1],r=id[i][j+1];
            if(s[i][j]=='.'){
                e[l].push_back(r);
                e[r].push_back(l);
                // cout<<l<<" "<<r<<endl;
            }else{
                v.push_back({l,r});
            }
        }
    }
    for(int j=2;j<=2*m+1;j+=2){
        for(int i=3;i<2*n+1;i+=2){
            int l=id[i-1][j],r=id[i+1][j];
            if(s[i][j]=='.'){
                e[l].push_back(r);
                e[r].push_back(l);


                // cout<<l<<" "<<r<<endl;
            }else{
                v.push_back({l,r});
            }
        }
    }
    dfs(1,0);
    // ans=dep[n*m];
    // for(int i=1;i<=n;i++) cout<<dep[i]<<" ";
    // cout<<endl;
    int x=n*m;
    int res=0;
    while(x!=0){
        tf[x]=1;
        cnt[x]=res;
        res++;
        x=fa[x][0].fa;
    }
    for(int i=1;i<=n*m;i++){
        fa[i][0].tf=tf[fa[i][0].fa];
    }
    for(int i=1;i<20;i++){
        for(int j=1;j<=n*m;j++){
            int id1=j,id2=fa[j][i-1].fa;
            fa[j][i].fa=fa[id2][i-1].fa;
            fa[j][i].len=fa[id1][i-1].len+fa[id2][i-1].len;
            fa[j][i].tf=fa[id1][i-1].tf|fa[id2][i-1].tf;
        }
    }

    for(auto [x,y]:v){
        HuangGuangsheng(x,y);
    }
    assert(ans<=n*m);
    cout<<ans<<endl;
}

详细

Test #1:

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

input:

2 3
+-+-+-+
|.....|
+.+.+.+
|.|.|.|
+-+-+-+

output:

6

result:

ok single line: '6'

Test #2:

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

input:

2 3
+-+-+-+
|...|.|
+.+.+.+
|.|...|
+-+-+-+

output:

4

result:

ok single line: '4'

Test #3:

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

input:

5 5
+-+-+-+-+-+
|...|...|.|
+-+.+.+.+.+
|...|.|.|.|
+.+.+.+-+.+
|.|...|.|.|
+.+.+-+.+.+
|.|.....|.|
+-+.+.+-+.+
|...|.....|
+-+-+-+-+-+

output:

13

result:

wrong answer 1st lines differ - expected: '15', found: '13'