QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#155412 | #3711. Floyd-Warshall | Ice_teapoy | WA | 4ms | 14080kb | C++14 | 1.6kb | 2023-09-01 16:41:49 | 2023-09-01 16:41:50 |
Judging History
answer
//#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define lowbit(x) (x&-x)
using namespace std;
using LL=long long;
const int N=1e5+5;
int n,m,q,f[N],fa[N][20],dep[N];
int dis[201][N],que[N],h,t,vis[N];
set<int> st;
vector<int> e[N],w[N];//e是树边
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void dfs(int u,int f)
{
for (auto v:e[u])
{
if (v==f) continue;
dep[v]=dep[u]+1;
fa[v][0]=u;
dfs(v,u);
}
}
int LCA(int x,int y)
{
if (dep[x]<dep[y]) swap(x,y);
int dt=dep[x]-dep[y];
for (int i=0;i<20;++i)
if (dt&1) x=fa[x][i];
if (x==y) return x;
for (int i=19;i>=0;--i)
if (fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
return fa[x][0];
}
void bfs(int id,int st)
{
h=t=0;
que[++t]=st;
memset(vis,0,sizeof(vis));
vis[st]=1;
while (h!=t)
{
++h;
for (auto v:w[que[h]])
{
if (vis[v]) continue;
vis[v]=1;
dis[id][v]=dis[id][que[h]]+1;
que[++t]=v;
}
}
}
int main()
{
// cin.tie(0);
// cout.tie(0);
// ios::sync_with_stdio(0);
cin>>n>>m>>q;
for (int i=1;i<=n;++i) f[i]=i;
for (int i=1,x,y;i<=m;++i)
{
cin>>x>>y;
if (x==y) continue;
w[x].push_back(y);
w[y].push_back(x);
if (find(x)==find(y))
{
st.insert(x);
st.insert(y);
continue;
}
f[f[x]]=f[y];
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1,0);
for (int i=0;i<19;++i)
for (int j=1;j<=n;++j) fa[j][i+1]=fa[fa[j][i]][i];
int tot=0;
for (auto v:st) bfs(++tot,v);
for (int x,y;q--;)
{
cin>>x>>y;
int ans=dep[x]+dep[y]-2*dep[LCA(x,y)];
for (int i=1;i<=tot;++i)
ans=min(ans,dis[i][x]+dis[i][y]);
cout<<ans<<"\n";
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 13768kb
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: 0ms
memory: 9056kb
input:
1 2 1 1 1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 9540kb
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 2 0 1 1 2 2 1 2
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 3ms
memory: 10056kb
input:
5 8 10 5 1 1 3 4 5 2 3 1 5 4 2 2 2 2 3 2 1 2 3 4 5 1 1 2 2 5 3 3 5 3 4 4 5 1 1
output:
2 1 1 0 0 2 2 2 1 0
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 3ms
memory: 9100kb
input:
5 8 10 4 1 5 2 3 5 3 1 5 1 5 1 4 2 2 5 3 1 3 2 2 1 3 2 3 3 1 5 4 5 4 3 4 3 2 5
output:
1 2 2 2 0 1 2 2 2 1
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 0ms
memory: 9464kb
input:
5 6 10 3 5 5 1 1 4 5 2 5 4 4 1 3 3 4 5 1 5 1 4 2 5 1 4 3 3 2 1 5 1 1 4
output:
0 1 1 1 1 1 0 2 1 1
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 0ms
memory: 10136kb
input:
5 8 10 2 3 5 3 1 4 3 1 5 5 1 2 2 5 1 2 3 5 2 5 3 5 3 2 3 2 2 5 2 4 1 2 2 2 5 2
output:
1 1 1 1 1 1 2 1 0 1
result:
ok 10 numbers
Test #8:
score: 0
Accepted
time: 1ms
memory: 10288kb
input:
5 7 10 1 3 4 1 4 2 5 4 4 1 5 4 3 5 2 3 1 1 5 4 4 3 1 1 3 3 3 1 2 3 5 5 2 5
output:
3 0 1 2 0 0 1 3 0 2
result:
ok 10 numbers
Test #9:
score: 0
Accepted
time: 4ms
memory: 13852kb
input:
5 8 10 1 2 1 5 4 1 2 3 4 5 3 3 4 1 2 5 5 1 5 3 3 4 1 5 1 4 3 5 1 4 1 3 5 4 2 4
output:
1 2 3 1 1 2 1 2 1 2
result:
ok 10 numbers
Test #10:
score: 0
Accepted
time: 3ms
memory: 9604kb
input:
5 7 10 5 2 2 1 5 3 3 4 1 5 1 1 5 3 4 5 3 1 3 1 3 1 2 1 2 1 5 1 4 4 5 3 3 2
output:
2 2 2 2 1 1 1 0 1 2
result:
ok 10 numbers
Test #11:
score: 0
Accepted
time: 0ms
memory: 9456kb
input:
5 8 10 1 5 3 5 2 5 1 4 4 1 3 3 5 5 5 2 1 5 2 3 2 2 3 4 3 2 1 1 5 3 3 4 5 4 2 4
output:
1 2 0 3 2 0 1 3 2 3
result:
ok 10 numbers
Test #12:
score: 0
Accepted
time: 2ms
memory: 8800kb
input:
5 7 10 5 1 5 3 2 5 4 2 3 3 3 2 2 2 1 5 2 3 2 2 5 4 3 3 2 5 5 5 3 4 2 2 5 5
output:
1 1 0 2 0 1 0 2 0 0
result:
ok 10 numbers
Test #13:
score: 0
Accepted
time: 2ms
memory: 9704kb
input:
10 15 10 5 9 1 6 8 1 1 10 10 3 2 5 5 4 2 8 7 5 10 7 1 10 3 3 4 2 3 7 2 4 7 2 10 8 1 9 6 8 4 5 2 4 8 9 3 7 3 7 6 8
output:
2 2 4 2 1 1 3 1 1 2
result:
ok 10 numbers
Test #14:
score: 0
Accepted
time: 3ms
memory: 9508kb
input:
10 15 10 1 3 2 8 3 5 2 6 9 3 7 4 9 2 7 6 10 5 10 3 7 1 8 6 4 8 7 8 5 9 10 8 7 5 1 1 4 10 1 3 9 2 2 2 3 6 2 8 4 4
output:
4 3 0 4 1 1 0 3 1 0
result:
ok 10 numbers
Test #15:
score: 0
Accepted
time: 0ms
memory: 14080kb
input:
10 14 10 6 10 10 1 9 10 1 5 10 3 3 8 9 4 10 2 8 7 9 3 6 9 9 2 10 1 8 1 5 8 6 1 8 9 5 1 1 6 10 7 1 4 1 8 9 9 6 2
output:
2 2 2 1 2 3 3 1 0 2
result:
ok 10 numbers
Test #16:
score: 0
Accepted
time: 3ms
memory: 9484kb
input:
10 13 10 6 7 5 10 2 7 5 4 8 2 5 9 1 10 5 2 3 9 5 7 8 2 4 2 7 5 2 2 5 2 8 6 5 2 10 3 4 10 3 7 5 5 4 4 6 3
output:
0 1 3 1 3 2 3 0 0 4
result:
ok 10 numbers
Test #17:
score: 0
Accepted
time: 3ms
memory: 9228kb
input:
10 17 10 10 9 8 4 5 2 7 6 9 6 8 2 3 7 6 8 5 1 3 2 9 4 6 8 4 10 8 9 1 2 5 1 7 1 10 6 10 2 7 6 1 1 6 10 7 1 9 9 4 1 4 3 5 1
output:
2 3 1 0 2 1 0 3 3 1
result:
ok 10 numbers
Test #18:
score: -100
Wrong Answer
time: 0ms
memory: 9084kb
input:
10 12 10 2 1 7 6 8 2 8 10 4 10 8 6 6 3 3 5 10 9 3 3 3 5 4 10 9 5 9 9 1 1 4 5 6 7 4 6 10 4 2 10 7 6 9 10
output:
5 0 0 5 3 3 1 2 3 1
result:
wrong answer 5th numbers differ - expected: '1', found: '3'