#include <iostream>
using namespace std;
struct Node {
int left, right;
int father;
}node[1000];
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(af>bf)
node[af].father = bf;
}
int main() {
int n;
cin>>n;
for(int i=0;i<n;i++) {
cin>>node[i].left>>node[i].right;
node[i].father = i;
}
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(Judge(i,j)) {
Union(i,j);
}
}
}
int sum = 0;
for(int i=0;i<n;i++) {
if(node[i].father == i) {
sum ++;
}
}
printf("sum is %d",sum);
return 0;
}
【ACM】区间并算法
最新推荐文章于 2022-03-19 02:57:20 发布