| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
两段代码为何一个WA,一个AC?#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=30010;
int p[MAXN],num[MAXN],c[MAXN];
void make_set()
{
for(int i=1;i<=MAXN;++i)
{
p[i]=i;
num[i]=1;
c[i]=0;
}
}
int find_set(int x)
{
if(p[x]!=x)
{
int px=p[x];
p[x]=find_set(p[x]);//就在这个位置,修改一下就AC了 ,但是逻辑上没有任何差别阿
c[x]+=c[px];
}
return p[x];
}
void union_set(int a,int b)
{
int x=find_set(a);
int y=find_set(b);
if(x==y)
{
return ;
}
p[y]=x;
c[y]+=num[x];
num[x]+=num[y];
}
int main()
{
freopen("in.txt","r",stdin);
char ch;
int p,a,b;
scanf("%d",&p);
make_set();
while(p--)
{
while(scanf("%c", &ch))
{
if(ch!='\n'&&ch!=' ')
break;
}
if(ch=='M')
{
scanf("%d%d",&a,&b);
union_set(a,b);
continue;
}
if(ch=='C')
{
scanf("%d",&a);
b=find_set(a);
printf("%d\n",num[b]-c[a]-1);
}
}
return 0;
}
————————————————————————————————————————
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=30010;
int p[MAXN],num[MAXN],c[MAXN];
void make_set()
{
for(int i=1;i<=MAXN;++i)
{
p[i]=i;
num[i]=1;
c[i]=0;
}
}
int find_set(int x)
{
if(p[x]!=x)
{
c[x]+=c[p[x]];
p[x]=find_set(p[x]);
}
return p[x];
}
void union_set(int a,int b)
{
int x=find_set(a);
int y=find_set(b);
if(x==y)
{
return ;
}
p[y]=x;
c[y]+=num[x];
num[x]+=num[y];
}
int main()
{
freopen("in.txt","r",stdin);
char ch;
int p,a,b;
scanf("%d",&p);
make_set();
while(p--)
{
while(scanf("%c", &ch))
{
if(ch!='\n'&&ch!=' ')
break;
}
if(ch=='M')
{
scanf("%d%d",&a,&b);
union_set(a,b);
continue;
}
if(ch=='C')
{
scanf("%d",&a);
b=find_set(a);
printf("%d\n",num[b]-c[a]-1);
}
}
return 0;
}
前面的AC了 后面的WA了 无语了 路过的大牛现身指点一下把
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator