这题我本来想用floyd做,然后全WA
#include <bitset>
#include <cstdio>
#include <iostream>
#include <memory.h>
#include <vector>
using namespace std;
const int maxn=1145,inf=0x3F3F3F3F;
inline int read(){
int sum=0,sign=1;
char tmp=getchar();
while(tmp<'0'||tmp>'9'){
sign=tmp=='-'?-1:1;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return sum*sign;
}
bitset<maxn> calc[maxn][maxn];
int n,m,dist[maxn][maxn];
int main(){
//freopen("LG6328.in","r",stdin);
//freopen("LG6328.out","w",stdout);
memset(dist,inf,sizeof(dist));
n=read();
for(int i=1;i<=n;i++){
dist[i][i]=0;
}
m=read();
int q=read();
for(int i=0;i<m;i++){
int x=read(),y=read();
dist[x][y]=dist[y][x]=1;
}
for(int mid=1;mid<=n;mid++){
for(int s=1;s<=n;s++){
for(int t=1;t<=n;t++){
if(dist[s][mid]+dist[mid][t]<dist[s][t]){
dist[s][t]=dist[t][s]=dist[s][mid]+dist[mid][t];
}
}
}
}
for(int s=1;s<=n;s++){
for(int t=1;t<=n;t++){
if(dist[s][t]!=inf){
calc[s][dist[s][t]].set(t,1);
}
}
for(int d=1;d<=n;d++){
calc[s][d]|=calc[s][d-1];
}
}
while(q--){
int a=read();
bitset<maxn> rslt;
rslt.reset();
while(a--){
int x=read(),y=read();
if(y>n){
y=n;
}
rslt|=calc[x][y];
}
cout<<rslt.count()<<endl;
}
return 0;
}
改成BFS就AC了
#include <bitset>
#include <cstdio>
#include <iostream>
#include <memory.h>
#include <queue>
#include <vector>
using namespace std;
const int maxn=1145,inf=0x3F3F3F3F;
inline int read(){
int sum=0,sign=1;
char tmp=getchar();
while(tmp<'0'||tmp>'9'){
sign=tmp=='-'?-1:1;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return sum*sign;
}
bitset<maxn> calc[maxn][maxn];
vector<int> graf[maxn];
queue<int> que;
int n,m,dist[maxn];
bool flags[maxn];
void bfs(int x){
memset(flags,0,sizeof(flags));
dist[x]=0;
flags[x]=1;
que.push(x);
while(!que.empty()){
int pos=que.front();
que.pop();
calc[x][dist[pos]].set(pos,1);
for(unsigned int i=0;i<graf[pos].size();i++){
if(flags[graf[pos][i]]){
continue;
}
dist[graf[pos][i]]=dist[pos]+1;
flags[graf[pos][i]]=1;
que.push(graf[pos][i]);
}
}
for(int i=1;i<=n;i++){
calc[x][i]|=calc[x][i-1];
}
}
int main(){
//freopen("LG6328.in","r",stdin);
//freopen("LG6328.out","w",stdout);
n=read();
m=read();
int q=read();
for(int i=0;i<m;i++){
int x=read(),y=read();
graf[x].push_back(y);
graf[y].push_back(x);
}
for(int i=1;i<=n;i++){
bfs(i);
}
bitset<maxn> rslt;
while(q--){
int a=read();
rslt.reset();
while(a--){
int x=read(),y=read();
if(y>n){
y=n;
}
rslt|=calc[x][y];
}
cout<<rslt.count()<<endl;
}
return 0;
}
难道我floyd写假了?