hihocoder1301
题解:一堆相交圆放在一个集合里,如果它的高>=h,底<=0,那么就不能穿越
错误代码:我也不知道为什么不能AC??
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
int const N = 1000 + 10;
double const eps = 1e-8;
double const inf = 0x7f7f7f7f;
int T,n,m,fa[N];
double high[N],low[N],w,h;
bool ans;
struct Circle
{
double x,y,r;
}c[N];
double Distance(int i,int j){ //求圆心之间的距离
return sqrt((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y-c[j].y)*(c[i].y-c[j].y));
}
bool Judge(int i,int j){ //判断两圆是否相交
if(Distance(i,j) > c[i].r + c[j].r + eps) return false;
else return true;
}
int find(int x){
return x == fa[x] ? x : (fa[x] = find(fa[x]));
}
bool Union(int p,int q){
if(Judge(p,q)){
int fp = find(p), fq = find(q);
if(fp != fq)
fa[fp] = fq;
high[fq] = max(high[fq],high[fp]);
low[fq] = min(low[fq],low[fp]);
if(low[fq]<=0 and high[fq]>=h){
return ans = false;
}
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>w>>h>>n;
ans = true;
for(int i=1;i<=n;i++){
fa[i] = i;
cin>>c[i].x>>c[i].y>>c[i].r;
high[i] = c[i].y + c[i].r, low[i] = c[i].y - c[i].r;
}
bool flag = true;
for(int i=1;i<=n&&flag;i++)
for(int j=i+1;j<=n&&flag;j++)
Union(i,j),flag = false;
if(ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
正确代码:
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
int const N = 1000 + 10;
double const eps = 1e-8;
double const inf = 0x7f7f7f7f;
int T,n,m,fa[N];
double high[N],low[N],w,h;
bool ans;
struct Circle
{
double x,y,r;
}c[N];
double Distance(int i,int j){ //求圆心之间的距离
return sqrt((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y-c[j].y)*(c[i].y-c[j].y));
}
bool Judge(int i,int j){ //判断两圆是否相交
if(Distance(i,j) > c[i].r + c[j].r + eps) return false;
else return true;
}
int find(int x){
return x == fa[x] ? x : (fa[x] = find(fa[x]));
}
bool Union(int p,int q){
if(Judge(p,q)){
int fp = find(p), fq = find(q);
if(fp != fq)
fa[fp] = fq;
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>w>>h>>n;
ans = true;
for(int i=1;i<=n;i++){
fa[i] = i;
cin>>c[i].x>>c[i].y>>c[i].r;
high[i] = c[i].y + c[i].r, low[i] = c[i].y - c[i].r;
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
Union(i,j);
for(int i=1;i<=n;i++){
int u = find(i);
high[u] = max(high[u],high[i]);
low[u] = min(low[u],low[i]);
if(high[i]>=h and low[i] <=0){
ans = false;
break;
}
}
if(ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}