E - More is better

本文介绍了一种通过并查集算法解决的最大好友群组数量问题。该问题要求从1到10000000编号的男孩中,根据直接好友关系找出能够形成的最大互为直接或间接好友的群组规模。文章提供了完整的C++代码实现,利用并查集对群组进行合并,并最终找出人数最多的群组。

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang’s selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
Sample Input
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
Sample Output
4
2

Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect),
then A and C are also friends(indirect).

In the first sample {1,2,5,6} is the result.
In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define MAX 10000000+10
using namespace std;

int pre[MAX];
int rankk[MAX];
int maxx;
void init(int n)
{
    for(int i=0;i<n;i++)
    {
        pre[i]=i;
        rankk[i]=1;
    }
}

int find(int a)
{
    if(pre[a]==a)
        return a;
    return pre[a] = find(pre[a]);
}
void mix(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y) 
    {
        return ;
    }  
    if(rankk[x]>=rankk[y])  
    {  
        pre[y]=x;  
        rankk[x]=rankk[x]+rankk[y]; 
    }
    else  
    {  
        pre[x]=y;  
        rankk[y]=rankk[y]+rankk[x];  
    }  
}
int main()
{
    int n,u,v,max;
    while(scanf("%d",&n)!=EOF)
    {
        memset(pre,0,sizeof(pre));
        memset(rankk,0,sizeof(rankk));
        init(n);
        maxx=0;
        if(n==0)
            printf("1\n");
        else
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&u,&v);
                mix(u,v);
                if(u>maxx)
                    maxx=u;
                if(v>maxx)
                    maxx=v;
            }
            sort(rankk,rankk+maxx);
            printf("%d\n",rankk[maxx-1]);
        }

    }

    return 0;
}
the result of cl and cd is not acceptable and i need to modify the file the result of the code which give not coverge Writing the forces breakdown file (forces_breakdown.dat). +-----------------------------------------------------------------------+ +----------------------------------------------------------------+ | Inner_Iter| Time(sec)| Time_Iter| CL| CD| +----------------------------------------------------------------+ | 19510| 6.6939e-02| 0| 0.004081| 0.156430| | 19520| 6.6938e-02| 0| 0.011526| 0.146708| | 19530| 6.6936e-02| 0| 0.020145| 0.136572| | 19540| 6.6936e-02| 0| 0.029911| 0.126056| | 19550| 6.6936e-02| 0| 0.040790| 0.115191| | 19560| 6.6937e-02| 0| 0.052748| 0.104011| | 19570| 6.6936e-02| 0| 0.065747| 0.092552| | 19580| 6.6937e-02| 0| 0.079750| 0.080846| | 19590| 6.6937e-02| 0| 0.094710| 0.068930| | 19600| 6.6937e-02| 0| 0.110578| 0.056842| | 19610| 6.6936e-02| 0| 0.127301| 0.044617| | 19620| 6.6936e-02| 0| 0.144820| 0.032294| | 19630| 6.6936e-02| 0| 0.163072| 0.019912| | 19640| 6.6936e-02| 0| 0.181996| 0.007507| | 19650| 6.6938e-02| 0| 0.201529| -0.004880| | 19660| 6.6938e-02| 0| 0.221605| -0.017212| | 19670| 6.6939e-02| 0| 0.242152| -0.029450| | 19680| 6.6939e-02| 0| 0.263099| -0.041557| | 19690| 6.6939e-02| 0| 0.284374| -0.053493| | 19700| 6.6938e-02| 0| 0.305902| -0.065223| | 19710| 6.6939e-02| 0| 0.327610| -0.076709| | 19720| 6.6939e-02| 0| 0.349426| -0.087915| | 19730| 6.6940e-02| 0| 0.371270| -0.098806| | 19740| 6.6939e-02| 0| 0.393065| -0.109348| | 19750| 6.6938e-02| 0| 0.414740| -0.119507| | 19760| 6.6937e-02| 0| 0.436215| -0.129251| | 19770| 6.6936e-02| 0| 0.457421| -0.138551| | 19780| 6.6935e-02| 0| 0.478285| -0.147374| | 19790| 6.6936e-02| 0| 0.498731| -0.155695| | 19800| 6.6935e-02| 0| 0.518687| -0.163486| | 19810| 6.6934e-02| 0| 0.538085| -0.170724| | 19820| 6.6933e-02| 0| 0.556863| -0.177383| | 19830| 6.6933e-02| 0| 0.574961| -0.183444| | 19840| 6.6933e-02| 0| 0.592319| -0.188885| | 19850| 6.6933e-02| 0| 0.608878| -0.193688| | 19860| 6.6932e-02| 0| 0.624578| -0.197837| | 19870| 6.6934e-02| 0| 0.639369| -0.201318| | 19880| 6.6934e-02| 0| 0.653203| -0.204118| | 19890| 6.6933e-02| 0| 0.666041| -0.206226| | 19900| 6.6932e-02| 0| 0.677843| -0.207635| | 19910| 6.6932e-02| 0| 0.688573| -0.208341| | 19920| 6.6932e-02| 0| 0.698202| -0.208341| | 19930| 6.6932e-02| 0| 0.706704| -0.207633| | 19940| 6.6932e-02| 0| 0.714061| -0.206219| | 19950| 6.6932e-02| 0| 0.720254| -0.204105| | 19960| 6.6932e-02| 0| 0.725271| -0.201298| | 19970| 6.6932e-02| 0| 0.729098| -0.197808| | 19980| 6.6931e-02| 0| 0.731734| -0.193645| | 19990| 6.6930e-02| 0| 0.733176| -0.188822| | 19999| 6.6930e-02| 0| 0.733451| -0.183929| ----------------------------- Solver Exit ------------------------------- Maximum number of iterations reached (ITER = 20000) before convergence. +-----------------------------------------------------------------------+ | Convergence Field | Value | Criterion | Converged | +-----------------------------------------------------------------------+ | rms[P]| -4.74136| < -12| No| +-----------------------------------------------------------------------+ ------------------------------------------------------------------------- +-----------------------------------------------------------------------+ | File Writing Summary | Filename | +-----------------------------------------------------------------------+ |SU2 binary restart |restart_flow.dat | | | (3796.74 MB/s)| Writing the forces breakdown file (forces_breakdown.dat). |Paraview surface |surface_flow.vtu | | | (385.216 MB/s)| Writing the forces breakdown file (forces_breakdown.dat). +-----------------------------------------------------------------------+ --------------------------- Finalizing Solver --------------------------- Deleted CNumerics container. Deleted CIntegration container. Deleted CSolver container. Deleted CIteration container. Deleted CInterface container. Deleted CGeometry container. Deleted CFreeFormDefBox class. Deleted CSurfaceMovement class. Deleted CVolumetricMovement class. Deleted CConfig container. Deleted nInst container. Deleted COutput class. ------------------------------------------------------------------------- -------------------------- Performance Summary -------------------------- Simulation totals: Wall-clock time (hrs): 0.371884 | Core-hrs: 0.371884 Cores: 1 | DOFs/point: 6 Points/core: 38442 | Ghost points/core: 0 Ghost/Owned Point Ratio: 0 | Preprocessing phase: Preproc. Time (s): 0.156802 | Preproc. Time (%): 0.0117123 Compute phase: Compute Time (s): 1338.6 | Compute Time (%): 99.9867 Iteration count: 20000 | Avg. s/iter: 0.0669302 Core-s/iter/Mpoints: 1.74107 | Mpoints/s: 0.574359 Output phase: Output Time (s): 0.021299 | Output Time (%): 0.00159092 Output count: 1 | Avg. s/output: 0.021299 Restart Aggr. BW (MB/s): 142429 | MB/s/core: 142429 ------------------------------------------------------------------------- ------------------------- Exit Success (SU2_CFD) ------------------------ the code i use it as foolowing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % ------------- DIRECT, ADJOINT, AND LINEARIZED PROBLEM DEFINITION ------------% % SOLVER= INC_RANS % KIND_TURB_MODEL= SST KIND_TRANS_MODEL= LM %FREESTREAM_TURBULENCEINTENSITY= 0.001 %FREESTREAM_TURB2LAMVISCRATIO = 2.0 %HROUGHNESS = 3.3E-6 % MATH_PROBLEM= DIRECT % RESTART_SOL= NO % OBJECTIVE_FUNCTION=DRAG % -------------------- INCOMPRESSIBLE FREE-STREAM DEFINITION ------------------% % INC_DENSITY_INIT= 1.225 INC_TEMPERATURE_INIT= 288.15 % -------------- COMPRESSIBLE AND INCOMPRESSIBLE FLUID CONSTANTS --------------% % %GAMMA_VALUE= 1.4 %GAS_CONSTANT= 287.87 % % AoA 0.0 deg INC_VELOCITY_INIT= ( 14.60799973614140 , 0.00277648930504, 0.0 ) % INC_NONDIM= INITIAL_VALUES % --------------------------- VISCOSITY MODEL ---------------------------------% % VISCOSITY_MODEL= CONSTANT_VISCOSITY % MU_CONSTANT= 1.789e-05 % ---------------------- REFERENCE VALUE DEFINITION ---------------------------% % REF_ORIGIN_MOMENT_X= 0.25 REF_ORIGIN_MOMENT_Y = 0.00 REF_ORIGIN_MOMENT_Z = 0.00 % REF_LENGTH= 1.0 % REF_AREA= 1.0 % -------------------------- CL DRIVER DEFINITION -----------------------------% % % Activate fixed lift mode (specify a CL instead of AoA, NO/YES) FIXED_CL_MODE= NO % % Target coefficient of lift for fixed lift mode (0.80 by default) TARGET_CL= 0.724 % % Estimation of dCL/dAlpha (0.2 per degree by default) DCL_DALPHA= 0.2 % % Maximum number of iterations between AoA updates UPDATE_AOA_ITER_LIMIT= 100 % % Number of iterations to evaluate dCL/dAlpha at the end of the simulation ITER_DCL_DALPHA= 500 % % Evaluate dObjFunc/dCL during runtime (YES) or use the value stored in the % direct solution file (NO). EVAL_DOF_DCX= NO % -------------------- BOUNDARY CONDITION DEFINITION --------------------------% % MARKER_HEATFLUX= ( AIRFOIL, 0.0 ) % MARKER_FAR= ( FARFIELD ) % MARKER_PLOTTING= ( AIRFOIL ) % MARKER_MONITORING= ( AIRFOIL ) % ------------- COMMON PARAMETERS DEFINING THE NUMERICAL METHOD ---------------% % NUM_METHOD_GRAD= WEIGHTED_LEAST_SQUARES % CFL_NUMBER= 1.0 % %MAX_DELTA_TIME= 1E10 % CFL_ADAPT= NO % CFL_ADAPT_PARAM= ( 0.1, 2.0, 40.0, 1e10, 0.001 ) %CFL_ADAPT_PARAM= ( 0.5, 1.0, 0.5, 1000, 0.001 ) % Less aggressive adaptation % ITER= 20000 % ------------------------ LINEAR SOLVER DEFINITION ---------------------------% % LINEAR_SOLVER= FGMRES % LINEAR_SOLVER_PREC= LU_SGS % LINEAR_SOLVER_ERROR= 1E-6 % LINEAR_SOLVER_ITER= 5 % -------------------- FLOW NUMERICAL METHOD DEFINITION -----------------------% % % CONV_NUM_METHOD_FLOW= FDS % MUSCL_FLOW= YES % SLOPE_LIMITER_FLOW= VENKATAKRISHNAN % JST_SENSOR_COEFF= ( 0.5, 0.02 ) % TIME_DISCRE_FLOW= EULER_IMPLICIT % ----------------------- SLOPE LIMITER DEFINITION ----------------------------% % VENKAT_LIMITER_COEFF= 0.05 % LIMITER_ITER= 99999 % -------------------- TURBULENT NUMERICAL METHOD DEFINITION ------------------% % CONV_NUM_METHOD_TURB= SCALAR_UPWIND % MUSCL_TURB= NO % SLOPE_LIMITER_TURB= NONE % TIME_DISCRE_TURB= EULER_IMPLICIT % CFL_REDUCTION_TURB= 1.0 % --------------------------- CONVERGENCE PARAMETERS --------------------------% % % %CONV_CRITERIA= CAUCHY % CONV_RESIDUAL_MINVAL= -12 % CONV_FIELD= ( RMS_PRESSURE ) % % Start convergence criteria at iteration number CONV_STARTITER= 500 % ------------------------- INPUT/OUTPUT INFORMATION --------------------------% % %HISTORY_WRT_FREQ_INNER=0 MESH_FILENAME= mesh_211_ffd.su2 % MESH_FORMAT= SU2 % SOLUTION_FILENAME= restart_flow.dat % OUTPUT_FILES= (RESTART, SURFACE_PARAVIEW) WRT_FORCES_BREAKDOWN= YES VOLUME_OUTPUT= (COORDINATES, SOLUTION, PRIMITIVE, RESIDUAL) HISTORY_OUTPUT= (ITER, LINSOL, RMS_RES, MAX_RES, AERO_COEFF, CFL_NUMBER) % CONV_FILENAME= history % RESTART_FILENAME= restart_flow.dat % SOLUTION_ADJ_FILENAME = solution_adj.dat TABULAR_FORMAT= CSV VOLUME_FILENAME= flow % SURFACE_FILENAME= surface_flow % OUTPUT_WRT_FREQ= 500 % SCREEN_WRT_FREQ_INNER= 10 % %SCREEN_OUTPUT=(INNER_ITER, WALL_TIME, RMS_PRESSURE, RMS_NU_TILDE, RMS_RE_THETA_T, LIFT, DRAG, LINSOL_ITER_TRANS, LINSOL_RESIDUAL_TRANS, TAVG_DRAG) SCREEN_OUTPUT=(INNER_ITER,WALL_TIME, TIME_ITER, RMS_ADJ_DENSITY, RMS_DENSITY, , REL_RMS_DENSITY,REL_RMS_ADJ_DENSITY, LIFT,DRAG) % WRT_PERFORMANCE = YES % ----------------------- DESIGN VARIABLE PARAMETERS --------------------------% % DV_KIND= FFD_CONTROL_POINT_2D DV_MARKER= ( AIRFOIL ) DV_PARAM= ( AIRFOIL_BOX, 0.0, 0.0, 1.0, 0.0) DV_VALUE= 0.01 %DV_FILENAME= mesh_motion.dat % % ------------------------ GRID DEFORMATION PARAMETERS ------------------------% % DEFORM_LINEAR_SOLVER_ITER= 1000 DEFORM_NONLINEAR_ITER= 1 DEFORM_CONSOLE_OUTPUT= YES DEFORM_LINEAR_SOLVER_ERROR = 1E-6 DEFORM_STIFFNESS_TYPE= INVERSE_VOLUME OPT_ITERATIONS= 100 OPT_OBJECTIVE= DRAG * 1.0 OPT_GRADIENT_FACTOR= 0.1 OPT_RELAX_FACTOR= 1 OPT_BOUND_UPPER= 0.01 OPT_BOUND_LOWER= -0.01 %OPT_CONSTRAINT= ( AIRFOIL_THICKNESS > 0.21 ) * 1.0 OPT_CONSTRAINT= ( LIFT > 0.724 ) * 1.0; ( AIRFOIL_THICKNESS > 0.21 ) * 1.0 DEFINITION_DV= ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 0, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 1, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 2, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 3, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 4, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 5, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 6, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 7, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 8, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 9, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 10, 0, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 0, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 1, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 2, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 3, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 4, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 5, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 6, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 7, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 8, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 9, 1, 0.0, 1.0 ); ( 19, 1.0 | AIRFOIL | AIRFOIL_BOX, 10, 1, 0.0, 1.0 )
最新发布
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值