洛谷P2504题解

题目
瓶颈生成树的裸题。可以查看这个来获取更多信息。
他问的是能够在所有树上自由穿梭的猴子个数,那我只需要算出这张图上最小生成树中权值最大的边,和每个猴子的最大跳跃长度进行比较即可。
因为我用的是 \(\text{Kruscal}\) 算法求最小生成树,所以可以保证我搞出来的那个最小生成树一定是这张图上的一个瓶颈生成树,所以我只需要记录我加进去的边中权值最大的那个(即我最后加的那条边)即可。复杂度 \(O(n^2\log n^2)\),可以通过本题。
代码:

#include<stdio.h>
#include<math.h>
#include<algorithm>
#define reg register
#define ri reg int
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define nrep(i, x, y) for(ri i = x; i >= y; --i)
#define DEBUG 1
#define ll long long
#define il inline
#define swap(a, b) ((a) ^= (b) ^= (a) ^= (b))
#define max(i, j) (i) > (j) ? (i) : (j)
#define min(i, j) (i) < (j) ? (i) : (j)
#define read(i) io.READ(i)
#define print(i) io.WRITE(i)
#define push(i) io.PUSH(i)
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() {
    fwrite(pbuf, 1, pp - pbuf, stdout);
  }
#endif
  inline char gc() {
#if DEBUG
    return getchar();
#endif
    if(p1 == p2)
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void READ(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for(; !isdigit(ch); ch = gc())
      if(ch == '-') sign = 1;
    for(; isdigit(ch); ch = gc())
      x = x * 10 + (ch - '0');
    if(ch == '.')
      for(ch = gc(); isdigit(ch); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if(sign) x = -x;
  }
  inline void READ(char *s) {
    register char ch = gc();
    for(; blank(ch); ch = gc());
    for(; !blank(ch); ch = gc())
      *s++ = ch;
    *s = 0;
  }
  inline void READ(char &c) {
    for(c = gc(); blank(c); c = gc());
  }
  inline void PUSH(const char &c) {
#if DEBUG
    putchar(c);
#else
    if(pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout);
      pp = pbuf;
    }
    *pp++ = c;
#endif
  }
  template <class T>
  inline void WRITE(T x) {
    if(x < 0) {
      x = -x;
      PUSH('-');
    }
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10;
      x /= 10;
    } while(x);
    while(top)
      PUSH(sta[--top] + '0');
  }
  template <class T>
  inline void WRITE(T x, char lastChar) {
    WRITE(x);
    PUSH(lastChar);
  }
} io;
ll n, m;
struct E {
  ll u, v;
	double w;
} e[1000010];
struct node {
	ll x, y;
} point[10010];
ll a[10010], cnt, fa[10010];
double maxdis;
double dis(ll x_1, ll y_1, ll x_2, ll y_2) {
	return sqrt((ll)(x_1 - x_2) * (x_1 - x_2) + (ll)(y_1 - y_2) * (y_1 - y_2));
}
ll find(ll x) {
  return x == fa[x] ? x : fa[x] = find(fa[x]);
}
bool cmp(const E& x, const E& y) {
  return x.w < y.w;
}
void kruskal() {
  std::sort(e + 1, e + cnt + 1, cmp);
  rep(i, 1, m) fa[i] = i;
  rep(i, 1, cnt) {
    ll u = e[i].u, v = e[i].v;
    if(find(u) != find(v)) {
      ll _u = find(u), _v = find(v);
      fa[_u] = _v;
      maxdis = max(maxdis, e[i].w);
      /*printf("%d %d\n", u, v);*/
    }
	}
}
int main() {
	ll ans = 0;
	read(n);
	rep(i, 1, n) read(a[i]);
	read(m);
	rep(i, 1, m) read(point[i].x), read(point[i].y);
	rep(i, 1, m) rep(j, i + 1, m) e[++cnt].u = i, e[cnt].v = j, e[cnt].w = dis(point[i].x, point[i].y, point[j].x, point[j].y);
	/*rep(i, 1, cnt) printf("%d %d %f\n", e[i].u, e[i].v, e[i].w);*/
	kruskal();
	rep(i, 1, n) if(a[i] >= maxdis) ++ans;
	print(ans);
}

\(\text{kruscal}\)函数体内第谔行写了rep(i,1,n)调了半个小时。

posted @ 2021-03-27 08:17  1358id  阅读(38)  评论(0编辑  收藏  举报