zoukankan      html  css  js  c++  java
  • NBUT 1218 You are my brother(带权并查集)

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<algorithm>
    #include<stdlib.h>
    #define LL __int64
    using namespace std;
    int fa[3000];
    int val[3000];
    int find(int x)
    {
        if(x==fa[x])
            return x;
        int s=fa[x];
        fa[x]=find(fa[x]);
        val[x]+=val[s];
        return fa[x];
    }
    void bincha(int x,int y)  // x xiao y da
    {
        int fx=find(x);
        int fy=find(y);
        if(fx!=fy)
        {
            fa[fx]=fy;
            val[fx]=val[y]+val[x]+1;
            //val[x]=val[y]+1;
        }
    }
    int main()
    {
        int n;
        int i,j,k;
        int xiao,da;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=0;i<=2000;i++)
            {
                fa[i]=i;
            }
            memset(val,0,sizeof(val));
            for(i=0;i<n;i++)
            {
                scanf("%d%d",&xiao,&da);
                bincha(xiao,da);
            }
            find(1);
            find(2);
            if(val[1]<val[2])
            {
                printf("You are my younger
    ");
            }
            else if(val[1]>val[2])
            {
                printf("You are my elder
    ");
            }
            else
            {
                printf("You are my brother
    ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    局部组件
    flex布局
    Websocket
    关于Javascript夜里再来分析下
    go build、go mod等命令
    websocket
    FileSystemWatcher使用
    DataGridView双缓冲
    C#读INI文件
    c 通过 COM接口调用 Excel.Application 问题终于解决
  • 原文地址:https://www.cnblogs.com/sola1994/p/4738016.html
Copyright © 2011-2022 走看看