- 银河英雄传说
- 2017-10-20 15:55:46 @
表示把样例粘本地能对然而vj自测都是WA???
喵喵喵???
#include <cstring>
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 33000;
int x,y,t;
int num[N],dis[N],f[N];
int ser(int x){
if (x==f[x]) return x;
int r=ser(f[x]);
dis[x] += dis[f[x]];
return f[x] = r;
}
void merge(int x, int y){
int fx = ser(x);
int fy = ser(y);
f[fx] = fy;
dis[fx] = num[fy];
num[fy]+=num[fx];
return ;
}
void ques(int x,int y){
int fx = ser(x); int fy = ser(y);
if(fx!=fy){
printf("-1\n");
return;
}
if(dis[x]>=dis[y]) printf("%d\n",dis[x]-dis[y]-1);
else printf("%d\n",dis[y]-dis[x]-1);
return ;
}
int main(){
//freopen("universe.in", "r", stdin);
//freopen("test.out", "w", stdout);
cin >> t;
memset(dis, 0, sizeof dis);
for (int i=1; i<=N; ++i)
num[i] = 1, f[i] = i;
for(int i=1;i<=t;i++)
{
char c=getchar();
while(c!='M'&&c!='C')c=getchar();
scanf("%d%d",&x,&y);
if(c=='M')
{
merge(x,y);
continue;
}
ques(x, y);
}
return 0;
}
1 条评论
-
q234rty LV 10 @ 2017-10-20 23:06:35
/in/foo.cc:42:16: warning: iteration 32999 invokes undefined behavior [-Waggressive-loop-optimizations] num[i] = 1, f[i] = i; ~~~~~~~^~~ /in/foo.cc:41:20: note: within this loop for (int i=1; i<=N; ++i) ~^~~
根据Vijos编译信息中的这一段,可以发现您的数组少开了\(1\)。
- 1