你是下了数据么?
by moye到碗里来 @ 2017-08-24 21:23:11
恩
by 火羽 @ 2017-08-24 21:24:42
那就不知道了。。。
by moye到碗里来 @ 2017-08-24 21:25:44
freopen删没?
by c20191623 @ 2017-08-25 15:20:26
```cpp
#include<cstdio>
#include<cmath>
using namespace std;
int f[30001],a[30001],b[30001],n,i,a1,a2,x,y;
char c;
inline int read()
{
char c=getchar();int f=1,s=0;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
return s*f;
}
int find(int x)
{
int of=f[x];
if(f[x]!=x)
{
f[x]=find(f[x]);
if(a[x]==0) a[x]=1;
a[x]=a[of]+a[x];
}
return f[x];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
n=read();
for(i=1;i<=30000;i++)
{
f[i]=i;
b[i]=1;
}
for(i=1;i<=n;i++)
{
scanf("%c",&c);x=read();y=read();
a1=find(x);
a2=find(y);
if(c=='M')
{
if(a1!=a2)
{
f[a1]=a2;
a[a1]+=b[a2];
b[a2]+=b[a1];
}
}
else
{
if(a1!=a2) printf("-1\n");
else printf("%d\n",abs(a[x]-a[y])-1);
}
}
return 0;
}
这是新代码,原来的有点错误,不过前几个点是没问题的,不知道为什么一评测就爆零
```
by 火羽 @ 2017-08-25 20:30:24
freopen删了
by 火羽 @ 2017-08-25 20:31:04
玄(ren)学(pin)问题。。。
我也printf(“-1\n”)试了一下,,,没问题啊,,,
by 昤昽 @ 2017-08-26 10:54:14
```cpp
#include<cstdio>
#include<cmath>
#include <cctype>
#include <iostream>
using namespace std;
const int N = 30005;
int f[N],a[N],b[N],n,i,a1,a2,x,y;
char c;
inline int read()
{
char c=getchar();int f=1,s=0;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
return s*f;
}
inline char rchar()
{
char ch = getchar();
while (!isupper(ch)) ch = getchar();
return ch;
}
int find(int x)
{
int of=f[x];
if(f[x]!=x)
{
f[x]=find(f[x]);
if(a[x]==0) a[x]=1;
a[x]=a[of]+a[x];
}
return f[x];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
n=read();
for(i=1;i<=30000;i++)
{
f[i]=i;
b[i]=1;
}
for(i=1;i<=n;i++)
{
c = rchar();x=read();y=read();
a1=find(x);
a2=find(y);
if(c=='M')
{
if(a1!=a2)
{
f[a1]=a2;
a[a1]+=b[a2];
b[a2]+=b[a1];
}
}
else
{
int temp = (a[x] - a[y] > 0 ? a[x] - a[y] : a[y] - a[x]) - 1; //abs貌似有问题??
if(a1!=a2) cout <<-1 <<endl;
else cout <<temp <<endl;
}
}
return 0;
}
改你的代码AC了
```
by yxr811740686 @ 2017-08-29 09:54:13
什么原因我也不是很清楚, 交你的代码我爆0了两次..
反正读入c的时候肯定不能用scanf的%c, 然后就是%d配合绝对值就会有问题??(大雾)
by yxr811740686 @ 2017-08-29 10:01:47
在线IDE试一下
by Bzy_temp @ 2017-08-29 10:03:23