QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#58071#3711. Floyd-Warshallxyk2333WA 5ms15324kbC++141.5kb2022-10-24 18:30:172022-10-24 18:30:20

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 18:30:20]
  • Judged
  • Verdict: WA
  • Time: 5ms
  • Memory: 15324kb
  • [2022-10-24 18:30:17]
  • Submitted

answer

//QOJ #3711
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=1e5+110;
int U[M],V[M],from[N],d[N],pos[N],spe[M-N],dis[M-N][N],lg[N<<1],f[N<<1][20],E=0;
vector<int> G[N],T[N];
queue<int> q;
int find(int x)
{
	if(x!=from[x])from[x]=find(from[x]);
	return from[x];
}
void bfs(int x)
{
	q.push(spe[x]);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int v:G[u])
		{
			if(v==spe[x]||dis[x][v])continue;
			dis[x][v]=dis[x][u]+1;
			q.push(v);
		}
	}
	return;
}
void dfs(int u,int fa)
{
	f[pos[u]=++E][0]=d[u]=d[fa]+1;
	for(int v:T[u])if(v!=fa)dfs(v,u),f[++E][0]=d[u];
	return;
}
int MIN(int L,int R)
{
	if(L>R)swap(L,R);
	int k=lg[R-L+1];
	return min(f[L][k],f[R-(1<<k)+1][k]);
}
int main()
{
	int n,m,q,tot=0;
	scanf("%d %d %d",&n,&m,&q);
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d",&U[i],&V[i]);
		G[U[i]].push_back(V[i]);
		G[V[i]].push_back(U[i]);
	}
	for(int i=1;i<=n;i++)from[i]=i;
	for(int i=1;i<=m;i++)
	{
		int fu=find(U[i]),fv=find(V[i]);
		if(fu==fv)
		{
			spe[++tot]=U[i];
			continue;
		}
		T[U[i]].push_back(V[i]);
		T[V[i]].push_back(U[i]);
		from[fu]=fv;
	}
	for(int i=1;i<=tot;i++)bfs(i);
	dfs(1,0);
	for(int i=1;i<=E;i++)lg[i]=lg[i>>1]+1;
	for(int j=1;j<=lg[E];j++)for(int i=1;i+(1<<j)-1<=E;i++)f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
	while(q--)
	{
		int u,v;
		scanf("%d %d",&u,&v);
		int ans=d[u]+d[v]-2*MIN(pos[u],pos[v]);
		for(int i=1;i<=tot;i++)ans=min(ans,dis[i][u]+dis[i][v]);
		printf("%d\n",ans);
	}
	return 0;
}

詳細信息

Test #1:

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

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: 5ms
memory: 13608kb

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: 2ms
memory: 14716kb

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:

2
2
2
2
1
1
2
2
1
2

result:

wrong answer 1st numbers differ - expected: '0', found: '2'