| ||||||||||
| 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 | |||||||||
相同的代码,C++和G++时间差了1倍多。WHY?#include <cstdio>
#include <cctype>
#include <cstring>
const int maxN=1002,inf=(1<<31)-1;
char o;
int i,j,N,d,p,q,d2[maxN][maxN];
struct Point{
int x,y;
}cp[maxN];
class DSet{
private:
int root[maxN],rank[maxN];
int Root(int&);
public:
DSet();
bool Ed(int& i){
return root[i]!=0;
}
void New(int& i){
root[i]=i;
}
void Merge(int&,int&);
bool Check(int&,int&);
}c;
int sqr(int n){
return n*n;
}
int main(){
scanf("%d%d",&N,&d);
d*=d;
for(i=1;i<=N;++i) scanf("%d%d",&cp[i].x,&cp[i].y);
for(i=1;i<=N;++i){
d2[i][i]=inf;
for(j=i+1;j<=N;++j)
d2[j][i]=d2[i][j]=sqr(cp[i].x-cp[j].x)+sqr(cp[i].y-cp[j].y);
}
getchar();
while(isupper(o=getchar())){
switch(o){
case 'O':
scanf("%d",&p);
c.New(p);
for(i=1;i<=N;++i)
if(c.Ed(i)&&d2[p][i]<=d)
c.Merge(p,i);
break;
case 'S':
scanf("%d%d",&p,&q);
puts(c.Check(p,q)?"SUCCESS":"FAIL");
break;
}
getchar();
}
return 0;
}
int DSet::Root(int& i){
int f,p=i,r=i;
while(r!=root[r]) r=root[r];
while(p!=r){
f=root[p];
root[p]=r;
p=f;
}
return r;
}
DSet::DSet(){
memset(root,0,sizeof(root));
memset(rank,0,sizeof(rank));
}
void DSet::Merge(int& i,int& j){
int Ri=Root(i),Rj=Root(j);
if(Ri==Rj) return;
if(rank[Ri]>rank[Rj]){
root[Rj]=Ri;
++rank[Ri];
}
else{
root[Ri]=Rj;
++rank[Rj];
}
}
bool DSet::Check(int& i,int& j){
return (root[i]==0||root[j]==0)?false:Root(i)==Root(j);
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator