[ABC157D] Friend Suggestions

首先很自然的想到用并查集。

我们统计每个人与多少个人有直接朋友关系,统计每个人与多少个人不能有朋友关系,再统计每个人与多少个人有朋友关系。那么根据容斥原理,答案就是每个人与多少个人有朋友关系 \(-\) 每个人与多少个人有直接朋友关系 \(-\) 每个人与多少个人不能有朋友关系 \(-1\),这里减 \(1\) 是因为要减去自己。

#include <bits/stdc++.h>
#define reg register
#define ll long long
#define ull unsigned long long
#define db double
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vpi vector<pi>
#define vpl vector<pl>
#define pb push_back
#define er erase
#define SZ(x) (int) x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mkp make_pair
#define ms(data_name) memset(data_name, 0, sizeof(data_name))
#define msn(data_name, num) memset(data_name, num, sizeof(data_name))
#define For(i, j) for(reg int (i) = 1; (i) <= (j); ++(i))
#define For0(i, j) for(reg int (i) = 0; (i) < (j); ++(i))
#define Forx(i, j, k) for(reg int (i) = (j); (i) <= (k); ++(i))
#define Forstep(i , j, k, st) for(reg int (i) = (j); (i) <= (k); (i) += (st))
#define fOR(i, j) for(reg int (i) = (j); (i) >= 1; (i)--)
#define fOR0(i, j) for(reg int (i) = (j) - 1; (i) >= 0; (i)--)
#define fORx(i, j, k) for(reg int (i) = (k); (i) >= (j); (i)--)
#define tour(i, u) for(reg int (i) = head[(u)]; (i) != -1; (i) = nxt[(i)])
using namespace std;
char ch, B[1 << 20], *S = B, *T = B;
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 20, stdin), S == T) ? 0 : *S++)
#define isd(c) (c >= '0' && c <= '9')
int rdint() {
  int aa, bb;
  while(ch = getc(), !isd(ch) && ch != '-');
  ch == '-' ? aa = bb = 0 : (aa = ch - '0', bb = 1);
  while(ch = getc(), isd(ch))
    aa = aa * 10 + ch - '0';
  return bb ? aa : -aa;
}
ll rdll() {
  ll aa, bb;
  while(ch = getc(), !isd(ch) && ch != '-');
  ch == '-' ? aa = bb = 0 : (aa = ch - '0', bb = 1);
  while(ch = getc(), isd(ch))
    aa = aa * 10 + ch - '0';
  return bb ? aa : -aa;
}
const int mod = 998244353;
// const int mod = 1e9 + 7;
struct mod_t {
  static int norm(int x) {
    return x + (x >> 31 & mod);
  }
  int x;
  mod_t() {  }
  mod_t(int v) : x(v) {  }
  mod_t(ll v) : x(v) {  }
  mod_t(char v) : x(v) {  }
  mod_t operator +(const mod_t &rhs) const {
    return norm(x + rhs.x - mod);
  }
  mod_t operator -(const mod_t &rhs) const {
    return norm(x - rhs.x);
  }
  mod_t operator *(const mod_t &rhs) const {
    return (ll) x * rhs.x % mod;
  }
};
const int MAXN = 1e5 + 10;
int n, m, k, ans[MAXN], cnt1[MAXN], cnt2[MAXN], cnts[MAXN], f[MAXN];
int find(int x) {
  return f[x] == x ? x : f[x] = find(f[x]);
}
inline void work() {
  n = rdint();
  m = rdint();
  k = rdint();
  For(i, n) {
    f[i] = i;
  }
  ms(cnt1);
  ms(cnt2);
  ms(cnts);
  For(i, m) {
    int a = rdint(), b = rdint();
    ++cnt1[a];
    ++cnt1[b];
    if(find(a) != find(b)) {
      f[find(b)] = find(a);
    }
  }
  For(i, n) {
    ++cnts[find(i)];
  }
  For(i, k) {
    int c = rdint(), d = rdint();
    if(find(c) == find(d)) {
      ++cnt2[c];
      ++cnt2[d];
    }
  }
  For(i, n) {
    ans[i] = cnts[find(i)] - cnt1[i] - cnt2[i] - 1;
  }
  For(i, n) {
    printf("%d ", ans[i]);
  }
  printf("\n");
}
int main() {
  // freopen("input.txt", "r", stdin);
  work();
  return 0;
}
posted @ 2020-09-12 22:46  Nylch  阅读(126)  评论(0编辑  收藏  举报