QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#58001#3711. Floyd-Warshallzjy2022WA 5ms16884kbC++1.5kb2022-10-24 08:28:412022-10-24 08:29:03

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-24 08:29:03]
  • Judged
  • Verdict: WA
  • Time: 5ms
  • Memory: 16884kb
  • [2022-10-24 08:28:41]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
inline int read(){
	int x=0,f=1;char ch;
	while(!isdigit(ch=getchar()))if(ch=='-')f=-f;
	do x=(x<<3)+(x<<1)+(ch^48);while(isdigit(ch=getchar()));
	return x*f;
}
const int N=1e5+5;
int n,m,q,cnt;
int dis[105][N],vis[N];
int f[N][18];
vector<int>G[N];
void bfs(int st){
    ++cnt;
    queue<int>Q;
    Q.push(st);
    while(!Q.empty()){
        int x=Q.front();Q.pop();
        for(int v:G[x])
            if(v!=st&&!dis[cnt][v])
                dis[cnt][v]=dis[cnt][x]+1,Q.push(v);
    }
}
void dfs(int x,int fa){
    dis[0][x]=dis[0][fa]+1,f[x][0]=fa;
    for(int i=0;f[x][i];++i)
        f[x][i+1]=f[f[x][i]][i];
    for(int v:G[x]){
        if(v==fa)continue;
        if(dis[0][v]&&dis[0][v]<dis[0][x])bfs(x);
        else dfs(v,x);
    }
}
int LCA(int x,int y){
    if(dis[0][x]<dis[0][y])swap(x,y);
    for(int h=dis[0][x]-dis[0][y],i=0;h;h>>=1,++i)
        if(h&1)x=f[x][i];
    if(x==y)return x;
    for(int i=17;~i;--i)
        if(f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    return f[x][0];
}
int query(int u,int v){
    int res=dis[0][u]+dis[0][v]-2*dis[0][LCA(u,v)];
    for(int i=1;i<=cnt;++i)res=min(res,dis[i][u]+dis[i][v]);
    return res;
}
signed main(){
	n=read(),m=read(),q=read();
	for(int i=1;i<=m;++i){
        int u=read(),v=read();
        G[u].push_back(v),G[v].push_back(u);
	}
	dfs(1,0);
    for(int i=1;i<=q;++i){
        int u=read(),v=read();
        printf("%d\n",query(u,v));
    }
	return 0;
}

详细

Test #1:

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

input:

4 5 3
1 2
1 3
1 4
2 3
3 4
2 2
2 3
2 4

output:

0
1
2

result:

ok 3 number(s): "0 1 2"

Test #2:

score: 0
Accepted
time: 3ms
memory: 8440kb

input:

1 2 1
1 1
1 1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 5ms
memory: 16884kb

input:

5 7 10
5 3
1 2
4 1
2 5
3 4
2 2
1 3
5 5
4 5
3 2
4 4
5 3
3 5
2 4
4 2
5 3
4 2

output:

0
2
-4
0
1
1
-3
-3
1
-3

result:

wrong answer 3rd numbers differ - expected: '2', found: '-4'