Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

仅仅把cin改成scanf,就从3000+MS变成1000+MS

Posted by speedcell4 at 2011-08-02 13:50:58 on Problem 2236
#include<iostream>
#include<cmath>
#define SIZE 1010
using namespace std;
int n,d,x,y;
char ctrl;
struct node
{
    int x;
    int y;
    int pa;
    bool ok;
}a[SIZE];
void Init(int n)
{
    for(int i=1;n-i>=0;i++)
    {
        a[i].pa=i;
        a[i].ok=false;
    }
}
int findPa(int n)
{
    if(a[n].pa!=n) return a[n].pa=findPa(a[n].pa);
    else return n;
}
void Union(int x,int y)
{
    int xx=findPa(x);
    int yy=findPa(y);
    a[xx].pa=yy;
}
bool NotLess(double x,double y)
{
    if(x-y>1e-7) return true;
    else if(fabs(x-y)<1e-7) return true;
    else return false;
}
bool check(int x,int y)
{
    double Del_x=(a[x].x-a[y].x)*1.0;
    double Del_y=(a[x].y-a[y].y)*1.0;
    double distance=sqrt(Del_x*Del_x+Del_y*Del_y);
    return (a[x].ok&&a[y].ok&&NotLess(d*1.0,distance));
}
void joinIn(int x)
{
    for(int i=1;n-i>=0;i++)
    {
        if(check(i,x))
        {
            Union(i,x);
        }
    }
}
void printInfo(node a[],int n)
{
    for(int i=1;n-i>=0;i++) printf("%d ",a[i].pa);printf("\n");
    for(int i=1;n-i>=0;i++) printf("%d ",a[i].ok);printf("\n");
}
int main()
{
    scanf("%d %d",&n,&d);
    Init(n);
    for(int i=1;n-i>=0;i++) scanf("%d %d",&a[i].x,&a[i].y);getchar();
    while(scanf("%c",&ctrl)!=EOF)
    {
        if(ctrl=='S')
        {
            scanf("%d %d",&x,&y);
            if(a[x].ok&&a[y].ok&&findPa(x)==findPa(y)) printf("SUCCESS\n");
            else printf("FAIL\n");
        }
        else
        {
            scanf("%d",&x);
            a[x].ok=true;
            joinIn(x);
        }
        getchar();
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator