zoukankan      html  css  js  c++  java
  • [算法设计与分析] 奶酪 (并查集)

    luogu P3958

    tag:并查集

    noip2017 day2 t1

    中规中矩的并查集板子题

    没什么坑点

    我不李姐为什么luogu的题解写的如此高深莫测,以至于我起初看题解吓得一批

     1 //
     2 //  main.cpp
     3 //  奶酪
     4 //
     5 //  Created by sylvia on 2021/11/2.
     6 //  Copyright © 2021 apple. All rights reserved.
     7 //
     8 
     9 
    10 #include <iostream>
    11 #include <stdio.h>
    12 #include <math.h>
    13 #include <algorithm>
    14 #include <string.h>
    15 using namespace std;
    16 #define M 1000+5
    17 int n,h,rr,T;
    18 int father[M],rankk[M];
    19 int judge(int x1,int y1,int z1,int x2,int y2,int z2){
    20     double t=sqrt((double)(pow(x1-x2,2)+pow(y1-y2,2)+pow(z1-z2,2)));
    21     if (t<=(2*rr)) {
    22         return 1;
    23     }
    24     else return 0;
    25 }
    26 void init(int n){//并查集初始化
    27     for (int i=0;i<n;i++){
    28         father[i]=i;
    29         rankk[i]=0;
    30     }
    31 }
    32 int find(int x){
    33     int j,k,r;
    34     r=x;
    35     while (r!=father[r]) r=father[r];
    36     k=x;
    37     while (k!=r){
    38         j=father[k];
    39         father[k]=r;
    40         k=j;
    41     }
    42     return r;
    43 }
    44 void unite(int x,int y){  //合并集合
    45     x=find(x);
    46     y=find(y);
    47     if(x==y) return;
    48     if (rankk[x]<rankk[y]) father[x]=y;
    49     else {
    50         father[y]=x;
    51         if(rankk[x]==rankk[y]) rankk[x]++;
    52     }
    53 }
    54 int same(int x,int y){  //判断是否在一个集合
    55     return find(x)==find(y);
    56 }
    57 
    58 
    59 int main(){
    60     int x[M],y[M],z[M];
    61 
    62     cin>>T;
    63     while(T--){
    64         cin>>n>>h>>rr;
    65         init(n+2);
    66         for (int i=1;i<=n;i++){
    67             cin>>x[i]>>y[i]>>z[i];
    68             if(z[i]<=rr) unite(i,0);
    69             if(z[i]+rr>=h) unite(i,n+1);
    70             
    71         }
    72         for (int i=1;i<n;i++){
    73             for (int j=i+1;j<=n;j++){
    74                 if(judge(x[i],y[i],z[i],x[j],y[j],z[j])){
    75                     if(!same(i,j)) unite(i, j);
    76                 }
    77             }
    78         }
    79         if(same(0,n+1)) cout<<"Yes"<<endl;
    80         else cout<<"No"<<endl;
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    HTML5
    PHP
    eclipse项目导入到android studio
    Jpush教材
    Android性能优化典范
    Fresco好案例
    扫二维码关注微信号,回复“送礼包”就送超值大礼!
    Android开源项目大全之工具库
    android学习“知乎”建议
    C# Json时间类型的转换
  • 原文地址:https://www.cnblogs.com/jasmine-lee/p/15500027.html
Copyright © 2011-2022 走看看