sdnuACM1012区间合并问题

Description

给定n个开区间,合并他们中的重合者,输出合并后的区间数量。

Input

第一行:n(1 <= n <= 1000)
第2至第n+1行:每行两个整数,第i行的两个整数表示第i-1个区间的左边界和右边界。

Output

合并后所剩余的区间数量。

Sample Input

3
1 3
2 5
6 7

Sample Output

2

 

#include <stdio.h>  
  
struct NODE{  
    int left,right;  
    int father;  
}node[1010];  
  
bool Judge(int a, int b){  
    if(node[b].left >= node[a].right || node[b].right <= node[a].left){  
        return false;  
    }  
    else return true;  
}  
  
void Union(int a, int b){  
    int af = a;  
    int bf = b;  
    while(node[af].father != af) af = node[af].father;  
    while(node[bf].father != bf) bf = node[bf].father;  
    if(af < bf){  
        node[bf].father = af;  
    }  
    else if(bf < af){  
        node[af].father = bf;  
    }  
}  
  
int main(){  
    int n;  
    scanf("%d",&n);  
    for(int i=0;i<n;i++){  
        scanf("%d%d",&node[i].left, &node[i].right);  
        node[i].father = i;  
    }  
    for(int i=0;i<n-1;i++){  
        for(int k=i+1;k<n;k++){  
            if(Judge(i,k)){  
                Union(i,k);  
            }  
        }  
    }  
    int sum = 0;  
    for(int i=0;i<n;i++){  
        if(node[i].father == i) sum++;  
    }  
    printf("%d\n",sum);  
    return 0;  
}  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值