Submission #933000

# Submission time Handle Problem Language Result Execution time Memory
933000 2024-02-24T18:19:20 Z AdamGS Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
4 ms 11100 KB
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7;
set<pair<ll,ll>>A[LIM];
set<ll>B[LIM];
ll F[LIM], ile[LIM], ans;
ll fnd(ll x) {
  if(F[x]==x) return x;
  return F[x]=fnd(F[x]);
}
void uni(ll a, ll b) {
  a=fnd(a); b=fnd(b);
  if(ile[a]>ile[b]) swap(a, b);
  rep(xd, 2) {
    while(true) {
      auto it=A[a].lower_bound({b, -1});
      if(it==A[a].end()) break;
      pair<ll,ll>p=*it;
      if(p.st>b) break;
      A[a].erase(p);
      B[p.st].erase(p.nd);
      ans-=ile[b];
    }
    swap(a, b);
  }
  ans+=ile[a]*ile[b]*2;
  for(auto i : A[a]) A[b].insert(i);
  A[a].clear();
  ans-=ile[a]*(ll)B[a].size();
  ans+=ile[a]*(ll)B[b].size();
  for(auto i : B[a]) A[fnd(i)].erase({a, i});
  for(auto i : B[a]) if(B[b].find(i)==B[b].end()) {
    B[b].insert(i);
    ans+=ile[a]+ile[b];
    A[fnd(i)].insert({b, i});
  }
  ile[b]+=ile[a];
  F[a]=b;
}
void kraw(ll a, ll b) {
  if(fnd(a)==fnd(b)) return;
  b=fnd(b);
  auto it=A[b].lower_bound({fnd(a), -1});
  if(it!=A[b].end()) {
    pair<ll,ll>p=*it;
    if(p.st==fnd(a)) {
      uni(a, b);
      return;
    }
  }
  if(B[b].find(a)!=B[b].end()) return;
  B[b].insert(a);
  A[fnd(a)].insert({b, a});
  ans+=ile[b];
}
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n, m;
  cin >> n >> m;
  rep(i, n) {
    F[i]=i;
    ile[i]=1;
  }
  while(m--) {
    ll a, b;
    cin >> a >> b; --a; --b;
    kraw(a, b);
    cout << ans << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 11100 KB Output is correct
2 Correct 4 ms 11100 KB Output is correct
3 Incorrect 3 ms 11100 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 11100 KB Output is correct
2 Correct 4 ms 11100 KB Output is correct
3 Incorrect 3 ms 11100 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 11100 KB Output is correct
2 Correct 4 ms 11100 KB Output is correct
3 Incorrect 3 ms 11100 KB Output isn't correct
4 Halted 0 ms 0 KB -