Submission #946384

# Submission time Handle Problem Language Result Execution time Memory
946384 2024-03-14T15:20:16 Z rolandpetrean Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
2 ms 10844 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll

#define endl '\n'
#define pb push_back
using pi = array<int, 2>;

const int N = 1e5 + 5;

int par[N], cnt[N];
set<int> in[N], out[N];

int find(int u) {
  if (par[par[u]] != par[u]) par[u] = find(par[u]);
  return par[u];
}
void unite(int u, int v) {
  u = find(u); v = find(v);
  if (u == v) return;
  if (cnt[u] < cnt[v]) swap(u, v);
  
  in[u].erase(v);
  in[v].erase(u);
  out[u].erase(v);
  out[v].erase(u);
  for (int x : in[v]) {
    assert(x == find(x));
    in[u].insert(x);
    out[x].erase(v);
    out[x].insert(u);
  }
  for (int x : out[v]) {
    assert(x == find(x));
    out[u].insert(x);
    in[x].erase(v);
    in[x].insert(u);
  }
  
  cnt[u] += cnt[v];
  par[v] = u;
  in[v].clear();
  out[v].clear();
  cnt[v] = 0;
}
int get_cost(int u) {
  int cost = 0;
  cost += cnt[u] * (cnt[u] - 1); // between them
  cost += in[u].size() * cnt[u]; // coming in
  return cost;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n, m;
  cin >> n >> m;
  
  for (int i = 1; i <= n; ++i) {
    par[i] = i;
    cnt[i] = 1;
  }
  
  int ans = 0;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u = find(u); v = find(v);
    if (u == v) {
      cout << ans << endl;
      continue;
    }
    
    ans -= get_cost(u) + get_cost(v);
    out[u].insert(v);
    in[v].insert(u);
    if (in[u].count(v) && out[u].count(v)) unite(u, v);
    
    u = find(u); v = find(v);
    ans += get_cost(u);
    if (u != v) ans += get_cost(v);
    
    cout << ans << endl;
  }
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 10844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 10844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 10844 KB Output isn't correct
2 Halted 0 ms 0 KB -