JZOJ 1956. 矩形
题目
分析
- 并查集一定要打 f[x]=find(x);
- 主要是判断是否相交哪里要细节一点
代码
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long long using namespace std; struct sb { int x,xx,y,yy; }a[7001]; int fa[7001],r[7001]; int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);} int jud(int d,int dd) { int xf1=a[d].x,yf1=a[d].y,xf2=a[d].xx,yf2=a[d].yy; int xl1=a[dd].x,yl1=a[dd].y,xl2=a[dd].xx,yl2=a[dd].yy; if((xl1<xf1&&xl2<xf1)||(xl1>xf2&&xl2>xf2)||(yl1<yf1&&yl2<yf1)||(yl1>yf2&&yl2>yf2)) return 0; if((xl1==xf2&&yl1==yf2)||(xl1==xf2&&yl2==yf1)||(xl2==xf1&&yl1==yf2)||(xl2==xf1&&yl2==yf1)) return 0; return 1; } int t[7001]; int main() { freopen("pro.in","r",stdin); freopen("pro.out","w",stdout); int n; scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d%d%d%d",&a[i].x,&a[i].y,&a[i].xx,&a[i].yy); for (int i=1;i<=n;i++) fa[i]=i,r[i]=1; for (int i=1;i<n;i++) for (int j=i+1;j<=n;j++) { int c=jud(i,j); if (c==1) { int u=find(i); int v=find(j); if (u!=v) { if (r[u]<=r[v]) { fa[v]=u; r[u]+=r[v]; } else { fa[u]=v; r[v]+=r[u]; } } } } int ans=0; for (int i=1;i<=n;i++) { int u=find(i); if (t[u]==0) { t[u]=1; ans++; } } cout<<ans; return 0; }
为何要逼自己长大,去闯不该闯的荒唐