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 |
Re:贴个282ms的代码,求2位数代码写法In Reply To:贴个282ms的代码,求2位数代码写法 Posted by:Belldandy at 2013-10-23 13:01:18 #include <stdio.h> int student[50001],n=0; int count; inline int read(void) { int x = 0; static char ch; while ((ch = getchar()) < '0' || (ch > '9')); x += ch - '0'; while ((ch = getchar()) >= '0' && (ch <= '9')) { x *= 10; x += ch - '0'; } return x; } inline int find(int x) { while(student[x]!=x) { x=student[x]; } return x; } void change(int x,int y) { int i=0; int px=find(x),py=find(y); if(px!=py) { --count; student[px]=py; } return; } int main(void) { int k=0,m=0; int i=0,x=0,y=0; while(scanf("%d%d",&n,&m)&&n+m) { for(i=1;i<=n;++i) { student[i]=i; } count=n; for(i=0;i<m;++i) { x = read(); y = read(); //scanf("%d%d",&x,&y); change(x,y); } printf("Case %d: %d\n",++k,count); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator