(Jizhong) 2169. Fractional statistics [disjoint-set]

(File IO): input: score.in output: score.out
time limit: 1000 ms space constraints: 262144 KB specific restrictions


Title Description
After completion friend statistical situation, Xiao Ming and everyone interested in graduate school, but he felt alone people counting is a very silly thing, so he designed an algorithm, the same school graduates, the first 1 1 Ge will get 1 1 minute, first 2 2 Ge obtained 2 2 points, the first 3 3 Ge obtained 4 4 points ... first i i a will 2 ( i 1 ) 2^(i-1) points out that this elementary school scores, Xiao Ming would like to know how many schools have the highest scores points.


Enter the
input file s c The r e . i n score.in first row there are two integers n n and m m n n represents the total number of people, m m represents the number of known relationship with the school.
Next m m rows, each row having 2 2 by spaces separated integers a a and b b , represents a a and b are from the same school, a a and b b are 1 1 to n n an integer between. We will not give duplicate information.

Output
Output file score.out only one line for all schools in the highest score. The final score may be very large, 100 after you only need to output, less than 100, please direct output.


Sample input
. 5. 3
. 1 2
. 3. 4
. 1. 3

Sample output
15


Data range limit
60 60 % of the data, 1 < = n < = 10 1 <= n <= 10
80 80 % of the data, 1 < = n < = 70 1 <= n <= 70
100 100 % of the data, 1 < = n < = 10000 1 < = m < = 100000 1 <= n <= 10000,1 <= m <= 100000


prompt
1 2 3 4 1、2、3、4 resulting from the same school school score for 1 + 2 + 4 + 8 = 15 1+2+4+8=15


Problem-solving ideas
had started AC mentality to play this problem, the results of 0!
I have been saying runtime error, change a full two hours, and finally found the title is changed in the function missed one r e t u r n return . . . Low-level errors
in fact this question is to hit a disjoint-set, then calculate the results can be. Because the data is large, to use high precision.


Code

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int n,m,a[20010],x,y,xx,yy,fat[20010],ans[20010],l,maxn,k,x1;
int getfather(int z){
    if(fat[z]==0)
	return z;
   return fat[z]=getfather(fat[z]);
}
int main()
{
    freopen("score.in","r",stdin);
    freopen("score.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
        a[i]=1;
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&x,&y);
        xx=getfather(x);
        yy=getfather(y);
        if(xx!=yy)
        {
            fat[xx]=yy;
            a[yy]=a[xx]+a[yy];
            if(a[yy]>maxn)
                maxn=a[yy];
        }
    }
    l=1;
    ans[1]=1;
    for(int i=1; i<=maxn; i++)
    {
        x1=0;
        for(int j=1; j<=l; j++)
        {
            ans[j]=ans[j]*2+x1;
            x1=ans[j]/10;
            ans[j]=ans[j]%10;
        }
        if(x1>0)
        {
            l++;
            ans[l]=x1;
        }
        if(l>100)
           l=100;
    }
    ans[1]=ans[1]-1;
    k=1;
    while(ans[k]<0)
    {
    	ans[k]+=10;
    	ans[k+1]--;
    	k++;
	}
    for(int i=l;i>=1;i--) 
    {
    	if(ans[l]!=0)
    	printf("%d",ans[i]);
	}
}
Published 119 original articles · won praise 8 · views 4937

Guess you like

Origin blog.csdn.net/kejin2019/article/details/104527444