Submission #67280511


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "../../Templates/C++/debug.h"
#else
#define debug(...) 42
#endif

typedef long long ll;
typedef long double ld;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define lso(s) ((s) & -(s))
int lg(ll s) { return s ? __builtin_clzll(1) - __builtin_clzll(s) : -1; }//lg(1)=0, lg(2)=1, lg(3)=1, lg(4)=2, ...
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

const int MOD = 998244353;
const double EPS = 1e-9;
const char nl = '\n';
const int INF = 1e9;
const ll INFL = 4e18;
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll floor(ll a, ll b) { return a / b - (a % b < 0); }
ll ceil(ll a, ll b) { return a / b + (a % b > 0); }

template<typename T>
istream& operator>>(istream& in, vector<T> &vec){
    for(auto &x : vec){
        in>>x;
    }
    return in;
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll gen() {
	ll x = 0;
	while(x == 0)
		x = rng() % MOD;
	return x;
}

struct mint {
  ll x;
  mint(ll x=0):x((x%MOD+MOD)%MOD){}
  mint& operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}
  mint& operator-=(const mint a) {if ((x += MOD-a.x) >= MOD) x -= MOD;return *this;}
  mint& operator*=(const mint a) {(x *= a.x) %= MOD;return *this;}
  mint operator+(const mint a) const {mint res(*this);return res+=a;}
  mint operator-(const mint a) const {mint res(*this);return res-=a;}
  mint operator*(const mint a) const {mint res(*this);return res*=a;}
  mint pow(ll b) const {
    mint res(1), a(*this);
    while (b) {
      if (b & 1) res *= a;
      a *= a;
      b >>= 1;
    }
    return res;
  }
  // for prime MOD
  mint inv() const {return pow(MOD-2);}
  mint& operator/=(const mint a) {return (*this) *= a.inv();}
  mint operator/(const mint a) const {mint res(*this);return res/=a;}
};
ostream& operator<<(ostream& os, const mint& a) {os << a.x; return os;}

struct UF {
	vi e;
	UF(int n) : e(n, -1) {}
	bool sameSet(int a, int b) { return find(a) == find(b); }
	int size(int x) { return -e[find(x)]; }
	int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
	bool join(int a, int b) {
		a = find(a), b = find(b);
		if (a == b) return false;
		// if (e[a] > e[b]) swap(a, b);
		e[a] += e[b]; e[b] = a;
		return true;
	}
};

void solve() {
    int N; cin >> N;
    int M; cin >> M;
    vector<set<int>> al(N);
    vector<array<int, 2>> el(M);
    for(auto&[u, v] : el) {
        cin >> u >> v; u--, v--;
        al[u].ins(v);
        al[v].ins(u);
    }
    int cur = M;
    UF uf(N);
    int Q; cin >> Q;
    for(int i = 0; i < Q; i++) {
        int id; cin >> id; id--;
        auto[u, v] = el[id];
        if(uf.sameSet(u, v)) {
            cout << cur << nl;
            continue;
        }
        u = uf.find(u);
        v = uf.find(v);
        if(!al[u].count(v)) {
            cout << cur << nl;
            continue;
        }
        if(sz(al[u]) < sz(al[v])) swap(u, v);
        al[u].erase(v);
        al[v].erase(u);
        cur--;
        vector<array<int, 2>> rem;
        for(int w : al[v]) {
            if(al[u].count(w)) {
                rem.pb({v, w});
            } else {
                al[u].ins(w);
                al[w].ins(u);
                al[w].erase(v);
            }
        }
        cur -= sz(rem);
        for(auto[x, y] : rem) {
            al[x].erase(y);
            al[y].erase(x);
        }
        cout << cur << nl;
        uf.join(u, v);
        // debug(al);
    }

}


int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);
    int T = 1;
    // cin >> T;
    while(T--) {
        solve();
    }
	return 0;
}

Submission Info

Submission Time
Task F - Contraction
User YuukiS18
Language C++ 23 (gcc 12.2)
Score 525
Code Size 4863 Byte
Status AC
Exec Time 684 ms
Memory 55924 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 1
AC × 45
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3484 KiB
hand_00.txt AC 239 ms 48768 KiB
hand_01.txt AC 281 ms 50312 KiB
hand_02.txt AC 399 ms 48668 KiB
hand_03.txt AC 228 ms 48908 KiB
hand_04.txt AC 278 ms 27888 KiB
hand_05.txt AC 19 ms 3496 KiB
hand_06.txt AC 285 ms 55924 KiB
hand_07.txt AC 26 ms 18428 KiB
hand_08.txt AC 172 ms 48908 KiB
hand_09.txt AC 319 ms 48740 KiB
hand_10.txt AC 379 ms 48764 KiB
hand_11.txt AC 453 ms 48760 KiB
hand_12.txt AC 387 ms 43300 KiB
hand_13.txt AC 387 ms 35692 KiB
random_00.txt AC 296 ms 27956 KiB
random_01.txt AC 530 ms 48728 KiB
random_02.txt AC 389 ms 41916 KiB
random_03.txt AC 398 ms 47672 KiB
random_04.txt AC 684 ms 50044 KiB
random_05.txt AC 433 ms 46776 KiB
random_06.txt AC 274 ms 23664 KiB
random_07.txt AC 476 ms 48780 KiB
random_08.txt AC 358 ms 39868 KiB
random_09.txt AC 397 ms 47980 KiB
random_10.txt AC 465 ms 44852 KiB
random_11.txt AC 484 ms 42456 KiB
random_12.txt AC 298 ms 27364 KiB
random_13.txt AC 511 ms 48612 KiB
random_14.txt AC 415 ms 40576 KiB
random_15.txt AC 368 ms 47688 KiB
random_16.txt AC 652 ms 49876 KiB
random_17.txt AC 492 ms 43816 KiB
random_18.txt AC 371 ms 26824 KiB
random_19.txt AC 479 ms 48668 KiB
random_20.txt AC 426 ms 40036 KiB
random_21.txt AC 436 ms 47612 KiB
random_22.txt AC 330 ms 47208 KiB
random_23.txt AC 423 ms 41144 KiB
random_24.txt AC 273 ms 23696 KiB
random_25.txt AC 545 ms 48528 KiB
random_26.txt AC 413 ms 40648 KiB
random_27.txt AC 433 ms 47680 KiB
random_28.txt AC 304 ms 48404 KiB
random_29.txt AC 463 ms 42292 KiB