solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; template <typename TH> void _dbg(const char* sdbg, TH h) { cerr << sdbg << "=" << h << "\n"; } template <typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, t...); } template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } template <class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (first != t.first) return first < t.first; if (second != t.second) return second < t.second; return third < t.third; } }; template <class T> void ResizeVec(T&, vector<long long>) {} template <class T> void ResizeVec(vector<T>& vec, vector<long long> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } template <class A, class B, class C> ostream& operator<<(ostream& out, Triple<A, B, C> t) { return out << "(" << t.first << ", " << t.second << ", " << t.third << ")"; } template <class T> ostream& operator<<(ostream& out, vector<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } template <class T> ostream& operator<<(ostream& out, set<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } template <class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } const long long N = 2e5 + 5; struct Euler { struct Edge { long long nei, nr, id; }; vector<vector<Edge>> slo; vector<long long> ans, used, deg, beg; long long e_num, n; Euler() : e_num(0), n(0) {} void AddEdge(long long a, long long b, long long idd) { e_num++; if (a > n || b > n) { n = max(a, b); slo.resize(n + 2); deg.resize(n + 2); beg.resize(n + 2); } used.push_back(0); slo[a].push_back({b, e_num, idd}); } vector<vector<long long>> FindEuler() { used.push_back(0); assert(((long long)(used).size()) > e_num); vector<vector<long long>> lol; for (long long i = (1); i <= (n); ++i) { if (beg[i] < ((long long)(slo[i]).size())) { Go(i); lol.push_back(ans); ans.clear(); } } return lol; } private: void Go(long long v) { (v); while (beg[v] < ((long long)(slo[v]).size())) { Edge& e = slo[v][beg[v]]; beg[v]++; long long nei = e.nei; if (used[e.nr]) { continue; } used[e.nr] = 1; Go(nei); ans.push_back(e.id); } } }; long long a[N]; long long b[N]; int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); if (0) cout << fixed << setprecision(10); cin.tie(0); long long n, s; cin >> n >> s; map<long long, long long> scal; for (long long i = (1); i <= (n); ++i) { cin >> a[i]; scal[a[i]] = 1; } long long nxt = 1; for (auto& p : scal) { p.second = nxt; nxt++; } for (long long i = (1); i <= (n); ++i) { a[i] = scal[a[i]]; b[i] = a[i]; } sort(b + 1, b + 1 + n); vector<vector<long long>> where(n + 2); Euler euler; long long moves = 0; for (long long i = (1); i <= (n); ++i) { if (a[i] == b[i]) { continue; } moves++; where[a[i]].push_back(i); euler.AddEdge(a[i], b[i], i); } if (moves > s) { cout << "-1\n"; return 0; } vector<vector<long long>> cycs = euler.FindEuler(); long long to_join = min(((long long)(cycs).size()), s - moves); if (to_join <= 2) { to_join = 0; } (cycs); mini(to_join, ((long long)(cycs).size())); vector<long long> bigger; vector<long long> begs; for (long long i = 0; i < (to_join); ++i) { bigger.insert(bigger.end(), (cycs.back()).begin(), (cycs.back()).end()); begs.push_back(cycs.back().back()); cycs.pop_back(); } if (to_join) { reverse((begs).begin(), (begs).end()); cycs.push_back(begs); cycs.push_back(bigger); } cout << ((long long)(cycs).size()) << endl; for (auto v : cycs) { cout << ((long long)(v).size()) << "\n"; for (auto x : v) { cout << x << " "; } long long cp = a[v.back()]; for (long long i = (((long long)(v).size()) - 1); i >= (1); --i) { a[v[i]] = a[v[i - 1]]; } a[v[0]] = cp; cout << "\n"; } for (long long i = (1); i <= (n); ++i) { if (0) cout << a[i] << " "; } if (0) cout << endl; for (long long i = (1); i <= (n); ++i) { assert(a[i] == b[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, s, t, ans, a[N]; int p[N], fa[N]; pair<int, int> b[N]; int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); } bool vis[N]; vector<int> c[N]; void dfs(int x) { vis[x] = 1; c[t].push_back(x); if (!vis[p[x]]) dfs(p[x]); } int main() { scanf("%d%d", &n, &s); for (int i = (int)(1); i <= (int)(n); i++) { scanf("%d", &a[i]); b[i] = pair<int, int>(a[i], i); } sort(b + 1, b + n + 1); for (int i = (int)(1); i <= (int)(n); i++) p[b[i].second] = i; for (int i = (int)(1); i <= (int)(n); i++) if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = b[i].second = i; } for (int i = (int)(1); i <= (int)(n); i++) fa[i] = i; for (int i = (int)(1); i <= (int)(n); i++) fa[get(i)] = get(p[i]); int las = -1; for (int i = (int)(0); i <= (int)(n); i++) { if (p[b[i].second] == b[i].second) continue; if (las >= 0 && a[las] == a[b[i].second]) { int x = las, y = b[i].second; int X = get(x), Y = get(y); if (X != Y) fa[X] = Y, swap(p[x], p[y]); } las = b[i].second; } for (int i = (int)(1); i <= (int)(n); i++) if (!vis[i] && p[i] != i) ++t, dfs(i); for (int i = (int)(1); i <= (int)(t); i++) ans += c[i].size(); if (ans > s) return puts("-1"), 0; s = min(s - ans, t); if (s <= 1) { printf("%d\n", t); for (int i = (int)(1); i <= (int)(t); i++) { printf("%d\n", c[i].size()); for (int j = (int)(0); j <= (int)(c[i].size() - 1); j++) printf("%d ", c[i][j]); puts(""); } return 0; } printf("%d\n", t - s + 2); for (int i = (int)(1); i <= (int)(t - s); i++) { printf("%d\n", c[i + s].size()); for (int j = (int)(0); j <= (int)(c[i + s].size() - 1); j++) printf("%d ", c[i + s][j]); puts(""); ans -= c[i + s].size(); } printf("%d\n", ans); for (int i = (int)(1); i <= (int)(s); i++) for (int j = (int)(0); j <= (int)(c[i].size() - 1); j++) printf("%d ", c[i][j]); printf("\n%d\n", s); for (int i = (int)(s); i >= (int)(1); i--) printf("%d ", c[i][0]); }
11
CPP
#include <bits/stdc++.h> using namespace std; vector<int> new_val; inline int Val(int first) { return lower_bound(new_val.begin(), new_val.end(), first) - new_val.begin(); } void Move(vector<int> &ind, vector<int> &a) { int last = a[ind.back()]; for (int i : ind) { swap(a[i], last); } } int n, s; void Dfs(vector<vector<int> > &graph, int node, vector<bool> &visited) { visited[node] = true; for (int neighbor : graph[node]) { if (!visited[neighbor]) Dfs(graph, neighbor, visited); } } vector<int> Solve(int i, vector<vector<int> > &graph, map<pair<int, int>, vector<int> > &v) { list<int> path = {i}; for (auto it = path.begin(); it != path.end(); it++) { auto it2 = it; it2++; for (int node = *it; !graph[node].empty();) { int next = graph[node].back(); graph[node].pop_back(); node = next; path.insert(it2, node); } } vector<int> ans; for (auto it = path.begin(); it != path.end(); it++) { auto next = it; next++; if (next == path.end()) break; ans.push_back(v[{*it, *next}].back()); v[{*it, *next}].pop_back(); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> s; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; new_val.push_back(a[i]); } sort(new_val.begin(), new_val.end()); vector<int> sorted = new_val; new_val.erase(unique(new_val.begin(), new_val.end()), new_val.end()); for (int i = 0; i < n; i++) { sorted[i] = Val(sorted[i]); } for (int i = 1; i <= n; i++) { a[i] = Val(a[i]); } vector<vector<int> > graph(n + 1); int count = 0; map<pair<int, int>, vector<int> > v; vector<int> ind(n + 1); for (int i = 1; i <= n; i++) { if (sorted[i - 1] != a[i]) { count++; graph[sorted[i - 1]].push_back(a[i]); ind[a[i]] = i; } } if (count > s) { cout << -1; return 0; } vector<int> components; vector<bool> visited(n + 1); for (int i = 0; i <= n; i++) { if (graph[i].empty()) continue; if (!visited[i]) { Dfs(graph, i, visited); components.push_back(ind[i]); } } vector<vector<int> > moves = {}; if (components.size() > 1 && s > count + 1) { moves.push_back({}); for (int i = 0; i < min(s - count, int(components.size())); i++) { moves.back().push_back(components[i]); } Move(moves.back(), a); } for (int i = 0; i <= n; i++) { graph[i].clear(); visited[i] = false; } for (int i = 1; i <= n; i++) { if (sorted[i - 1] != a[i]) { v[{sorted[i - 1], a[i]}].push_back(i); count++; graph[sorted[i - 1]].push_back(a[i]); } } for (int i = 0; i <= n; i++) { if (graph[i].empty()) continue; if (!visited[i]) { Dfs(graph, i, visited); moves.push_back(Solve(i, graph, v)); } } cout << moves.size() << '\n'; for (vector<int> &move : moves) { cout << move.size() << '\n'; for (int i : move) { cout << i << ' '; } cout << '\n'; } }
11
CPP
#include <bits/stdc++.h> using namespace std; const int mxN = 2e5; int n, s, a[mxN]; map<int, int> mp1; map<int, vector<int>> mp2; vector<vector<int>> ans; void dfs(int u) { while (!mp2[u].empty()) { int i = mp2[u].back(); mp2[u].pop_back(); dfs(a[i]); ans.back().push_back(i); } mp2.erase(u); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; ++mp1[a[i]]; } int j = 0; for (auto it = mp1.begin(); it != mp1.end(); ++it) { for (int i = j; i < j + it->second; ++i) if (a[i] != it->first) mp2[it->first].push_back(i); j += it->second; } while (!mp2.empty()) { ans.push_back(vector<int>()); dfs(mp2.begin()->first); reverse(ans.back().begin(), ans.back().end()); s -= ans.back().size(); } if (s < 0) { cout << -1; return 0; } int b = min(s, (int)ans.size()), d = 0; cout << ans.size() - (b >= 3 ? b - 2 : 0) << "\n"; if (b >= 3) { for (int i = 0; i < b; ++i) d += ans[i].size(); cout << d << "\n"; for (int i = 0; i < b; ++i) for (int c : ans[i]) cout << c + 1 << " "; cout << "\n" << b << "\n"; for (int i = b - 1; i >= 0; --i) cout << ans[i][0] + 1 << " "; cout << "\n"; } for (int i = (b >= 3 ? b : 0); i < ans.size(); ++i) { cout << ans[i].size() << "\n"; for (int c : ans[i]) cout << c + 1 << " "; cout << "\n"; } }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, s; int a[200100]; int sorted[200100]; int been[200100]; map<int, list<int>> alive; list<list<int>> sol; void dfs(int val) { if (alive[val].empty()) return; int k = alive[val].front(); alive[val].pop_front(); been[k] = true; dfs(a[k]); sol.back().push_front(k); if (!alive[val].empty()) dfs(val); } int main() { cin >> n >> s; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) sorted[i] = a[i]; sort(sorted, sorted + n); for (int i = 0; i < n; i++) { if (a[i] == sorted[i]) been[i] = true; else { s--; alive[sorted[i]].push_back(i); } } if (s < 0) { cout << -1 << endl; return 0; } for (int i = 0; i < n; i++) if (!been[i]) { sol.push_back(list<int>()); dfs(sorted[i]); } if (s > 2 && sol.size() > 2) { list<int> new_cycle; list<int> rev_cycle; int num = min((size_t)s, sol.size()); for (int w = 0; w < num; w++) { list<int> &l = sol.back(); rev_cycle.push_front(l.front()); for (auto x : sol.back()) new_cycle.push_back(x); sol.pop_back(); } sol.push_back(new_cycle); sol.push_back(rev_cycle); } cout << sol.size() << endl; for (list<int> &l : sol) { cout << l.size() << endl; for (int x : l) cout << x + 1 << " "; cout << endl; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, f[N], to[N]; pair<int, int> a[N]; bool done[N]; int find(int x) { while (x != f[x]) { x = f[x] = f[f[x]]; } return x; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].first); a[i].second = i; } sort(a + 1, a + n + 1); for (int i = 1, j = 1; i <= n; i = j) { while (j <= n && a[j].first == a[i].first) { ++j; } for (int k = i; k < j; ++k) { if (a[k].second >= i && a[k].second < j) { to[a[k].second] = a[k].second; done[a[k].second] = true; ++m; } } int t = i; for (int k = i; k < j; ++k) { if (a[k].second < i || a[k].second >= j) { while (done[t]) { ++t; } to[a[k].second] = t++; } } } if (m < n) { puts("-1"); return 0; } for (int i = 1; i <= n; ++i) { f[i] = i; } for (int i = 1; i <= n; ++i) { f[find(i)] = find(to[i]); } for (int i = 1, j = 1; i <= n; i = j) { while (j <= n && a[j].first == a[i].first) { ++j; } int last = 0; for (int k = i; k < j; ++k) { if (!done[a[k].second]) { int t = a[k].second; if (last && find(last) != find(t)) { f[find(last)] = find(t); swap(to[last], to[a[k].second]); } last = a[k].second; } } } int cycle = 0; for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { ++cycle; } } int change = min(cycle, m - n); vector<int> all, go; if (change > 2) { for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { all.push_back(i); go.push_back(to[i]); if (all.size() == change) { break; } } } for (int i = 0; i < change; ++i) { to[all[i]] = go[(i + change - 1) % change]; if (i) { f[find(all[i])] = find(all[i - 1]); } } } int answer = 0; for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { ++answer; } } printf("%d\n", answer + (!go.empty())); for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { vector<int> a; a.push_back(i); for (int x = to[i]; x != i; x = to[x]) { a.push_back(x); } printf("%d\n", a.size()); for (int j = 0; j < a.size(); ++j) { printf("%d%c", a[j], j == a.size() - 1 ? '\n' : ' '); } } } if (!go.empty()) { printf("%d\n", go.size()); for (int i = 0; i < go.size(); ++i) { printf("%d%c", go[i], i == go.size() - 1 ? '\n' : ' '); } } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int n, m, len, a[200020], b[200020], c[200020], cnt, need, vis[200020], qwq; vector<pair<int, int> > G[200020]; vector<int> ans[200020]; void dfs(int u) { vis[u] = 1; while (!G[u].empty()) { int v = G[u].back().first, w = G[u].back().second; G[u].pop_back(); dfs(v); ans[cnt].push_back(w); } } int main() { n = read(), m = read(); for (int i = 1; i <= n; ++i) { a[i] = c[i] = read(); } sort(c + 1, c + n + 1); for (int i = 1; i <= n; ++i) { if (c[i] ^ c[i - 1]) ++len; b[i] = len; } len = unique(c + 1, c + n + 1) - c - 1; for (int i = 1; i <= n; ++i) { a[i] = lower_bound(c + 1, c + len + 1, a[i]) - c; } for (int i = 1; i <= n; ++i) { if (a[i] ^ b[i]) G[a[i]].push_back(make_pair(b[i], i)), ++need; } if (need > m) { return !printf("-1\n"); } if (!need) { return !printf("0\n"); } for (int i = 1; i <= len; ++i) { if (!vis[i] && !G[i].empty()) { ++cnt, dfs(i); } } if (cnt <= 1 || m - need <= 1) { printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { printf("%d\n", (int)ans[i].size()); for (auto x : ans[i]) { printf("%d ", x); } printf("\n"); } return 0; } printf("%d\n", qwq = cnt + 2 - min(m - need, cnt)); printf("%d\n", min(m - need, cnt)); int tot = 0; for (int i = 1; i <= min(m - need, cnt); ++i) { printf("%d ", ans[i].back()), tot += ans[i].size(); } printf("\n"); reverse(ans + 1, ans + min(m - need, cnt) + 1); printf("%d\n", tot); for (int i = 1; i <= min(m - need, cnt); ++i) { for (auto x : ans[i]) { printf("%d ", x); } } printf("\n"); for (int i = min(m - need, cnt) + 1; i <= cnt; ++i) { printf("%d\n", (int)ans[i].size()); for (auto x : ans[i]) { printf("%d ", x); } printf("\n"); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; int a[N], p[N], head[N], now = 0; pair<int, int> b[N]; vector<int> cycle[N]; int findd(int x) { if (head[x] < 0) { return x; } return head[x] = findd(head[x]); } void unionn(int x, int y) { x = findd(x), y = findd(y); head[x] += head[y]; head[y] = x; } bool samee(int x, int y) { return findd(x) == findd(y); } signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, i, j, k, l, s, sz = 0; cin >> n >> s; for (i = 1; i <= n; i++) { head[i] = -1; cin >> a[i]; b[i] = {a[i], i}; } sort(b + 1, b + 1 + n); i = j = 1; while (i <= n) { while (j <= n && b[i].first == b[j].first) { j++; } now++; while (i < j) { a[b[i].second] = now; b[i].first = now; i++; } } for (i = 1; i <= n; i++) { if (a[i] != b[i].first) { sz++; head[i] = -1; b[sz] = {a[i], i}; a[sz] = i; } } if (sz == 0) { cout << 0; return 0; } if (sz > s) { cout << -1; return 0; } sort(b + 1, b + 1 + sz); sort(a + 1, a + 1 + sz); for (i = 1; i <= sz; i++) { p[b[i].second] = a[i]; if (!samee(b[i].second, a[i])) { unionn(b[i].second, a[i]); } } i = j = 1; while (i <= sz) { while (j <= sz && b[i].first == b[j].first) { j++; } for (k = i + 1; k < j; k++) { if (!samee(b[i].second, b[k].second)) { swap(p[b[i].second], p[b[k].second]); unionn(b[i].second, b[k].second); } } i = j; } int cnt = 0; for (i = 1; i <= sz; i++) { if (p[a[i]]) { cycle[cnt].push_back(a[i]); j = p[a[i]]; while (j != a[i]) { cycle[cnt].push_back(j); j = p[j]; } for (k = 0; k < cycle[cnt].size(); k++) { p[cycle[cnt][k]] = 0; } cnt++; } } if (cnt <= 1 || s == sz + 1 || s == sz) { cout << cnt << '\n'; for (i = 0; i < cnt; i++) { cout << cycle[i].size() << '\n'; for (j = 0; j < cycle[i].size(); j++) { cout << cycle[i][j] << ' '; } cout << '\n'; } } else { cout << cnt - min(cnt, s - sz) + 2 << '\n'; int z = min(s - sz, cnt); for (i = z; i < cnt; i++) { cout << cycle[i].size() << '\n'; for (j = 0; j < cycle[i].size(); j++) { cout << cycle[i][j] << ' '; } cout << '\n'; } cout << z << '\n'; k = 0; for (i = 0; i < z; i++) { k += cycle[i].size(); cout << cycle[i][0] << ' '; } cout << '\n'; cout << k << '\n'; for (i = z - 1; i > -1; i--) { for (l = 1; l < cycle[i].size(); l++) { cout << cycle[i][l] << ' '; } cout << cycle[i][0] << ' '; } } }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N], b[N], c[N], d[N], e[N]; using VI = vector<int>; VI v[N], rep[N]; int rn; void search(int u) { for (int U; !v[u].empty();) { U = v[u].back(); v[u].pop_back(); search(a[U]); rep[rn].push_back(U); } } int main() { int n, s, m; scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", a + i); c[i] = a[i]; d[i] = i; } sort(c + 1, c + n + 1); m = unique(c + 1, c + n + 1) - c - 1; for (int i = 1; i <= n; i++) { b[i] = a[i] = lower_bound(c + 1, c + m + 1, a[i]) - c; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { v[b[i]].push_back(i); } } for (int i = 1; i <= m; i++) { if (!v[i].empty()) { search(i); int f = rep[rn].size(); for (int j = 0; j < f; j++) { d[rep[rn][j == f - 1 ? 0 : j + 1]] = rep[rn][j]; } s -= f; rn++; } if (s < 0) { puts("-1"); return 0; } } s = min(rn, s); if (s >= 2) { printf("%d\n", rn - s + 2); { printf("%d\n", s); for (int i = 0; i < s; i++) { printf("%d%c", rep[i][0], i + 1 < s ? ' ' : '\n'); } a[N - 1] = d[rep[s - 1][0]]; for (int i = s; --i;) { d[rep[i][0]] = d[rep[i - 1][0]]; } d[rep[0][0]] = a[N - 1]; } } else { printf("%d\n", rn); } VI g; for (int i = 1; i <= n; i++) { if (d[i] == i) continue; if (e[i]) continue; g.clear(); for (int j = i; !e[j];) { g.push_back(j); e[j] = 1; j = d[j]; } printf("%d\n", g.size()); for (int j = 0; j < g.size(); j++) { printf("%d%c", g[j], j + 1 < g.size() ? ' ' : '\n'); } } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int module = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<set<int>> to(n, set<int>()); vector<pair<int, int>> edges(m); vector<int> nbr(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a--; b--; nbr[a]++; nbr[b]++; to[a].insert(b); to[b].insert(a); edges[i] = {a, b}; } vector<bool> vis(n); int all = n; for (int i = 0; i < n; ++i) { if (!vis[i]) { if (nbr[i] < k) { queue<int> q; q.push(i); vis[i] = true; all--; while (!q.empty()) { int cur = q.front(); q.pop(); for (int t : to[cur]) { nbr[t]--; to[t].erase(cur); if (nbr[t] < k && !vis[t]) { q.push(t); vis[t] = true; all--; } } } } } } queue<int> q; vector<int> answer; answer.push_back(all); for (int i = m - 1; i > 0; i--) { auto [a, b] = edges[i]; if (!vis[a] && !vis[b]) { to[a].erase(b); to[b].erase(a); nbr[a]--; nbr[b]--; if (nbr[a] < k) { q.push(a); vis[a] = true; all--; } if (nbr[b] < k) { q.push(b); vis[b] = true; all--; } while (!q.empty()) { int cur = q.front(); q.pop(); for (int t : to[cur]) { nbr[t]--; to[t].erase(cur); if (nbr[t] < k && !vis[t]) { q.push(t); vis[t] = true; all--; } } } } answer.push_back(all); } for (int i = answer.size() - 1; i >= 0; i--) { cout << answer[i] << "\n"; } }
11
CPP
#include <bits/stdc++.h> #pragma GCC optimize(3, "Ofast", "inline") using namespace std; const long long maxn = (long long)1e6 + 5; const long long mod = (long long)998244353; const long long inf = 0x3f3f3f3f3f3f3f3f; pair<long long, long long> a[maxn]; long long in[maxn]; vector<long long> v[maxn]; bool vis[maxn]; long long n, m, k; long long ans; void f(long long x) { queue<long long> q; q.push(x); vis[x] = true; --ans; while (!q.empty()) { long long now = q.front(); q.pop(); for (auto &i : v[now]) if (!vis[i] && --in[i] < k) ans--, q.push(i), vis[i] = true; } } long long fin[maxn]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n >> m >> k; ans = n; for (long long i = 1; i <= m; ++i) { cin >> a[i].first >> a[i].second; ++in[a[i].first], ++in[a[i].second]; v[a[i].first].push_back(a[i].second); v[a[i].second].push_back(a[i].first); } for (long long i = 1; i <= n; ++i) if (!vis[i] && in[i] < k) f(i); fin[m] = ans; for (long long i = m; i >= 1; --i) { if (!vis[a[i].first]) --in[a[i].second]; if (!vis[a[i].second]) --in[a[i].first]; v[a[i].first].pop_back(); v[a[i].second].pop_back(); if (in[a[i].first] < k && !vis[a[i].first]) f(a[i].first); if (in[a[i].second] < k && !vis[a[i].second]) f(a[i].second); fin[i - 1] = ans; } for (long long i = 1; i <= m; ++i) cout << fin[i] << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 3e5 + 10; long long x[N], y[N], deg[N]; vector<long long> vp[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k; cin >> n >> m >> k; for (long long i = 1; i <= m; i++) { cin >> x[i] >> y[i]; vp[x[i]].push_back(y[i]); vp[y[i]].push_back(x[i]); deg[x[i]]++; deg[y[i]]++; } set<pair<long long, long long> > s; for (long long i = 1; i <= n; i++) { s.insert({deg[i], i}); } long long ans = n; set<pair<long long, long long> > ae; vector<long long> v; for (long long i = m; i >= 1; i--) { while (s.size() > 0) { auto it = *s.begin(); s.erase(it); if (it.first >= k) break; for (auto it1 : vp[it.second]) { if (ae.find({it1, it.second}) == ae.end()) { ae.insert({it1, it.second}); ae.insert({it.second, it1}); s.erase({deg[it1], it1}); deg[it1]--; s.insert({deg[it1], it1}); } } ans--; } if (ae.find({x[i], y[i]}) == ae.end()) { ae.insert({x[i], y[i]}); ae.insert({y[i], x[i]}); s.erase({deg[x[i]], x[i]}); s.erase({deg[y[i]], y[i]}); deg[x[i]]--; s.insert({deg[x[i]], x[i]}); deg[y[i]]--; s.insert({deg[y[i]], y[i]}); } v.push_back(ans); } reverse(v.begin(), v.end()); for (auto it : v) { cout << it << "\n"; } }
11
CPP
#include <bits/stdc++.h> using namespace std; set<int> se[200100]; int u[200100]; int v[200100]; int vis[200100]; int n, m, k, ans; int num[200100]; void dfs(int st) { if (se[st].size() >= k || vis[st]) return; vis[st] = 1; --ans; for (auto &t : se[st]) { se[t].erase(st); dfs(t); } se[st].clear(); } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); se[a].insert(b); se[b].insert(a); u[i] = a; v[i] = b; } ans = n; for (int i = 1; i <= n; i++) dfs(i); num[m] = ans; for (int i = m; i >= 1; i--) { se[u[i]].erase(v[i]); se[v[i]].erase(u[i]); dfs(u[i]); dfs(v[i]); num[i - 1] = ans; } for (int i = 1; i <= m; i++) printf("%d\n", num[i]); }
11
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { int sum = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -1; ch = getchar(); } while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar(); return sum * ff; } const int mod = 1e9 + 7; const int mo = 998244353; const int N = 2e5 + 5; int n, m, s, du[N], ok[N], ux[N], uy[N], sum[N], cnt, ans; vector<int> G[N]; inline void del(int x) { queue<int> q; q.push(x); ans--; ok[x] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = (0); i <= ((int)G[u].size() - 1); i++) { int v = G[u][i]; if (ok[v]) continue; if ((--du[v]) < s) q.push(v), ok[v] = 1, ans--; } } } int main() { n = read(); m = read(); s = read(); for (int i = (1); i <= (m); i++) { int x, y; x = read(), y = read(); G[x].push_back(y); G[y].push_back(x); du[y]++, du[x]++; ux[i] = x, uy[i] = y; } ans = n; for (int i = (1); i <= (n); i++) if (!ok[i] && du[i] < s) del(i); sum[m] = ans; for (int i = (m); i >= (1); i--) { int x = ux[i], y = uy[i]; if (!ok[x]) du[y]--; if (!ok[y]) du[x]--; G[x].pop_back(); G[y].pop_back(); if (!ok[x] && du[x] < s) del(x); if (!ok[y] && du[y] < s) del(y); sum[i - 1] = ans; } for (int i = (1); i <= (m); i++) printf("%d\n", sum[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 200100; struct node { int v, u; } e[maxn + 10]; set<int> s[maxn]; int k, ans, in[maxn], vis[maxn], ret[maxn], n, m; queue<int> q; void dele(int x) { set<int>::iterator iter; int u, v; if (vis[x] || in[x] >= k) return; vis[x] = 1; q.push(x); --ans; while (!q.empty()) { u = q.front(); q.pop(); for (iter = s[u].begin(); iter != s[u].end(); iter++) { v = *iter; in[v]--; if (in[v] < k && !vis[v]) { vis[v] = 1; q.push(v); --ans; } } } } int main() { int i; scanf("%d%d%d", &n, &m, &k); memset(in, 0, sizeof(in)); for (i = 1; i <= m; i++) { scanf("%d%d", &e[i].u, &e[i].v); s[e[i].u].insert(e[i].v); s[e[i].v].insert(e[i].u); in[e[i].u]++; in[e[i].v]++; } memset(vis, 0, sizeof(vis)); for (i = 1; i <= n; i++) { dele(i); } ans = 0; for (i = 1; i <= n; i++) { if (!vis[i]) ans++; } ret[m] = ans; for (i = m; i >= 1; i--) { if (!vis[e[i].v]) in[e[i].u]--; if (!vis[e[i].u]) in[e[i].v]--; s[e[i].u].erase(e[i].v); s[e[i].v].erase(e[i].u); dele(e[i].u); dele(e[i].v); ret[i - 1] = ans; } for (i = 1; i <= m; i++) { printf("%d\n", ret[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int mx = 200 * 1000 + 5; vector<set<int> > vec(mx); int arr1[mx], arr2[mx]; int main() { int i, j, k, n, m, x, y, p, q, l; scanf("%d %d %d", &n, &m, &k); int ans = 0; for (i = 0; i < m; i++) { scanf("%d %d", &x, &y); arr1[i] = x; arr2[i] = y; vec[x].insert(y); vec[y].insert(x); } ans = n; queue<int> ajairra; bool vis[n + 5]; memset(vis, 0, sizeof(vis)); for (i = 1; i <= n; i++) { p = vec[i].size(); if (p < k) { vis[i] = 1; ans--; ajairra.push(i); } } vector<int> vv; set<int>::iterator itr; for (i = m - 1; i >= 0; i--) { while (!ajairra.empty()) { p = ajairra.front(); ajairra.pop(); for (itr = vec[p].begin(); itr != vec[p].end(); itr++) { q = *itr; vec[q].erase(p); l = vec[q].size(); if (l < k && !vis[q]) { vis[q] = 1; ans--; ajairra.push(q); } } vec[p].clear(); } vv.push_back(ans); p = arr1[i]; q = arr2[i]; vec[p].erase(q); vec[q].erase(p); l = vec[p].size(); if (l < k && !vis[p]) { vis[p] = 1; ans--; ajairra.push(p); } l = vec[q].size(); if (l < k && !vis[q]) { vis[q] = 1; ans--; ajairra.push(q); } } for (i = m - 1; i >= 0; i--) { printf("%d\n", vv[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> const int ms = 200200; int n, m, k; int ans[ms]; std::pair<int, int> edges[ms]; std::vector<int> use[ms]; int deg[ms]; bool del[ms]; int main() { std::cin >> n >> m >> k; for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); use[u].push_back(i); use[v].push_back(i); deg[u]++; deg[v]++; edges[i] = std::pair<int, int>(u, v); } std::queue<int> q; for (int i = 1; i <= n; i++) { if (deg[i] < k) { q.push(i); } } del[m] = true; int cur_ans = n; for (int i = m; i >= 0; i--) { if (!del[i]) { int u = edges[i].first, v = edges[i].second; if (deg[u] == k) { q.push(u); } if (deg[v] == k) { q.push(v); } deg[u]--; deg[v]--; del[i] = true; } while (!q.empty()) { cur_ans--; int on = q.front(); q.pop(); for (auto e : use[on]) { if (!del[e]) { int u = edges[e].first, v = edges[e].second; if (deg[u] == k) { q.push(u); } if (deg[v] == k) { q.push(v); } deg[u]--; deg[v]--; del[e] = true; } } } ans[i] = cur_ans; } for (int i = 0; i < m; i++) { printf("%d\n", ans[i + 1]); } }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; vector<pair<int, int>> g[N]; vector<int> deg, ans; vector<pair<int, int>> edge; set<pair<int, int>> good; vector<bool> in_good; void f(int k, int iteration) { while (!good.empty() && good.begin()->first < k) { int v = good.begin()->second; for (auto j : g[v]) { int i = j.first; int it = j.second; if (iteration <= it) continue; if (in_good[i]) { good.erase({deg[i], i}); deg[i]--; good.insert({deg[i], i}); } } good.erase({deg[v], v}); in_good[v] = false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; deg.resize(n, 0); edge.resize(m); in_good.resize(n, true); ans.resize(m); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; edge[i] = {a, b}; deg[a]++; deg[b]++; g[a].push_back({b, i}); g[b].push_back({a, i}); } for (int i = 0; i < n; i++) { good.insert({deg[i], i}); } f(k, m); for (int i = m - 1; i >= 0; i--) { ans[i] = good.size(); if (i == 0) break; int v = edge[i].first; int u = edge[i].second; if (!in_good[v] || !in_good[u]) continue; good.erase({deg[v], v}); deg[v]--; good.insert({deg[v], v}); good.erase({deg[u], u}); deg[u]--; good.insert({deg[u], u}); f(k, i); } for (int i = 0; i < m; i++) cout << ans[i] << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fLL; const double pi = acos(-1.0); const int maxn = 200000 + 10; const int mod = 1e9 + 7; inline char _getchar() { static const int BUFSIZE = 100001; static char buf[BUFSIZE]; static char *psta = buf, *pend = buf; if (psta >= pend) { psta = buf; pend = buf + fread(buf, 1, BUFSIZE, stdin); if (psta >= pend) return -1; } return *psta++; } inline int read(int &x) { x = 0; int f = 1; char ch = _getchar(); while ((ch < '0' || ch > '9') && ~ch) { if (ch == '-') f = -1; ch = _getchar(); } if (ch == -1) return -1; while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = _getchar(); } x *= f; return 1; } inline int read(long long &x) { x = 0; int f = 1; char ch = _getchar(); while ((ch < '0' || ch > '9') && ~ch) { if (ch == '-') f = -1; ch = _getchar(); } if (ch == -1) return -1; while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = _getchar(); } x *= f; return 1; } inline int read(double &x) { char in; double Dec = 0.1; bool IsN = false, IsD = false; in = _getchar(); if (in == EOF) return -1; while (in != '-' && in != '.' && (in < '0' || in > '9')) in = _getchar(); if (in == '-') { IsN = true; x = 0; } else if (in == '.') { IsD = true; x = 0; } else x = in - '0'; if (!IsD) { while (in = _getchar(), in >= '0' && in <= '9') { x *= 10; x += in - '0'; } } if (in != '.') { if (IsN) x = -x; return 1; } else { while (in = _getchar(), in >= '0' && in <= '9') { x += Dec * (in - '0'); Dec *= 0.1; } } if (IsN) x = -x; return 1; } inline int read(float &x) { char in; double Dec = 0.1; bool IsN = false, IsD = false; in = _getchar(); if (in == EOF) return -1; while (in != '-' && in != '.' && (in < '0' || in > '9')) in = _getchar(); if (in == '-') { IsN = true; x = 0; } else if (in == '.') { IsD = true; x = 0; } else x = in - '0'; if (!IsD) { while (in = _getchar(), in >= '0' && in <= '9') { x *= 10; x += in - '0'; } } if (in != '.') { if (IsN) x = -x; return 1; } else { while (in = _getchar(), in >= '0' && in <= '9') { x += Dec * (in - '0'); Dec *= 0.1; } } if (IsN) x = -x; return 1; } inline int read(char *x) { char *tmp = x; char in = _getchar(); while (in <= ' ' && in != EOF) in = _getchar(); if (in == -1) return -1; while (in > ' ') *(tmp++) = in, in = _getchar(); *tmp = '\0'; return 1; } int l[maxn], r[maxn]; vector<int> nt[maxn]; vector<int> pt[maxn]; int cnt[maxn]; int vis[maxn]; int tot; int ans[maxn]; int ok[maxn]; int n, m, k; queue<int> q; void Count() { while (!q.empty()) { int x = q.front(); q.pop(); tot--; for (int i = 0; i < nt[x].size(); i++) { int nx = nt[x][i]; if (!vis[nx]) { if (!ok[pt[x][i]]) cnt[nx]--; if (cnt[nx] < k) q.push(nx), vis[nx] = 1; } ok[pt[x][i]] = 1; } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%d%d", l + i, r + i); nt[l[i]].push_back(r[i]), nt[r[i]].push_back(l[i]); pt[l[i]].push_back(i), pt[r[i]].push_back(i); cnt[l[i]]++, cnt[r[i]]++; } tot = n; for (int i = 1; i <= n; i++) if (cnt[i] < k) q.push(i), vis[i] = 1; Count(); for (int i = m - 1; i >= 0; i--) { ans[i] = tot; ok[i] = 1; if ((!vis[l[i]]) && (!vis[r[i]])) { cnt[l[i]]--, cnt[r[i]]--; if (cnt[l[i]] < k) q.push(l[i]), vis[l[i]] = 1; if (cnt[r[i]] < k) q.push(r[i]), vis[r[i]] = 1; Count(); } } for (int i = 0; i < m; i++) { printf("%d\n", ans[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using namespace std; int deg[200100]; set<pair<int, int> > pset; vector<pair<int, int> > query; set<int> conn[200100]; bool is_going(int u) { return (pset.find(make_pair(deg[u], u)) != pset.end()); } int n, m, k; void delete_adjacent(int u) { for (auto v : conn[u]) { conn[v].erase(conn[v].find(u)); if (is_going(v)) { pset.erase(pset.find(make_pair(deg[v], v))); deg[v]--; if (deg[v] < k) { delete_adjacent(v); } else pset.insert(make_pair(deg[v], v)); } } conn[u].clear(); } void print_set() { if (!pset.size()) cout << "Set is empty" << endl; for (auto p : pset) cout << p.second << "," << p.first << " "; cout << endl; } vector<int> ans(200100); int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; deg[u]++; deg[v]++; query.push_back({u, v}); conn[u].insert(v); conn[v].insert(u); } for (int i = 1; i <= n; i++) { pset.insert(make_pair(deg[i], i)); } for (int i = 1; i <= n; i++) { if (deg[i] < k && is_going(i)) { pset.erase(make_pair(deg[i], i)); delete_adjacent(i); } } for (int i = m - 1; i >= 0; i--) { int u = query[i].first; int v = query[i].second; ans[i + 1] = pset.size(); if (pset.find(make_pair(deg[u], u)) != pset.end() && pset.find(make_pair(deg[v], v)) != pset.end()) { if (deg[u] - 1 >= k && deg[v] - 1 >= k) { pset.erase(pset.find(make_pair(deg[u], u))); pset.erase(pset.find(make_pair(deg[v], v))); conn[u].erase(conn[u].find(v)); conn[v].erase(conn[v].find(u)); deg[u]--; deg[v]--; pset.insert(make_pair(deg[u], u)); pset.insert(make_pair(deg[v], v)); } else if (deg[u] <= k) { pset.erase(pset.find(make_pair(deg[u], u))); deg[u]--; delete_adjacent(u); } else { pset.erase(pset.find(make_pair(deg[v], v))); deg[v]--; delete_adjacent(v); } } } for (int i = 1; i <= m; i++) cout << ans[i] << endl; cout << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long INFL = 9223372036854775807LL; const double EPSILON = 0.00000001; const long long MOD = 1000000007; vector<int> adj[200000 + 5]; unordered_map<int, int> removed[200000 + 5]; int degree[200000 + 5]; bool node_removed[200000 + 5]; int ans = 0; void remove_edge(int u, int v, int k) { degree[u]--; degree[v]--; removed[u][v] = true; removed[v][u] = true; } void remove_edges(int start, int k) { queue<int> q; assert(degree[start] < k); assert(not node_removed[start]); q.push(start); node_removed[start] = true; ans--; while (!q.empty()) { int top = q.front(); q.pop(); for (int x : adj[top]) { if (not removed[top][x]) { remove_edge(top, x, k); if (not node_removed[x] and degree[x] < k) { node_removed[x] = true; ans--; q.push(x); } } } } } int32_t main() { int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> edges; memset(node_removed, false, sizeof(node_removed)); ; ans = n; for (int i = 0; i < m; i++) { int u, v; scanf("%d", &u); ; scanf("%d", &v); ; u--; v--; adj[u].push_back(v); adj[v].push_back(u); edges.push_back({u, v}); removed[u][v] = false; removed[v][u] = false; } for (int i = 0; i < n; i++) { degree[i] = (int)(adj[i]).size(); } for (int i = 0; i < n; i++) { if (not node_removed[i] and degree[i] < k) { remove_edges(i, k); } } vector<int> out; for (int i = (int)(edges).size() - 1; i >= 0; i--) { out.push_back(ans); int u = edges[i].first, v = edges[i].second; if (not removed[u][v]) { remove_edge(u, v, k); if (not node_removed[u] and degree[u] < k) { remove_edges(u, k); } if (not node_removed[v] and degree[v] < k) { remove_edges(v, k); } } } for (int i = (int)(out).size() - 1; i >= 0; i--) { printf("%d\n", out[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> int n, m, k, u[200010], v[200010], t[200010]; std::set<int> next[200010], p; inline void remove(int x) { if ((int)next[x].size() < k && p.erase(x)) { for (auto y : next[x]) { next[y].erase(x); remove(y); } next[x].clear(); } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", u + i, v + i); next[u[i]].insert(v[i]); next[v[i]].insert(u[i]); } for (int i = 1; i <= n; i++) p.insert(i); for (int i = 1; i <= n; i++) remove(i); for (int i = m; i; i--) { t[i] = p.size(); if (p.count(v[i]) && p.count(u[i])) { next[v[i]].erase(u[i]); next[u[i]].erase(v[i]); remove(u[i]); remove(v[i]); } } for (int i = 1; i <= m; i++) printf("%d\n", t[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; set<int> M[200005]; int siz[200005], a[200005], b[200005], dele[200005], cnt, ans[200005]; void check(int u) { if (siz[u] >= k || dele[u]) return; cnt--; dele[u] = 1; queue<int> Q; Q.push(u); while (!Q.empty()) { int ty = Q.front(); Q.pop(); for (set<int>::iterator i = M[ty].begin(); i != M[ty].end(); i++) { int v = *i; siz[v]--; if (siz[v] < k && !dele[v]) { cnt--; Q.push(v); dele[v] = 1; } } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &a[i], &b[i]); M[a[i]].insert(b[i]); M[b[i]].insert(a[i]); siz[a[i]]++; siz[b[i]]++; } cnt = n; for (int i = 1; i <= n; i++) check(i); ans[m] = cnt; for (int i = m; i >= 2; i--) { if (!dele[b[i]]) siz[a[i]]--; if (!dele[a[i]]) siz[b[i]]--; M[a[i]].erase(b[i]); M[b[i]].erase(a[i]); check(a[i]); check(b[i]); ans[i - 1] = cnt; } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int du[maxn]; set<int> g[maxn]; int b1[maxn], b2[maxn], ans[maxn], vis[maxn]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); set<pair<int, int> > s; for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); b1[i] = x, b2[i] = y; du[x]++, du[y]++; g[x].insert(y); g[y].insert(x); } for (int i = 1; i <= n; i++) s.insert(make_pair(du[i], i)); for (int i = m - 1; i >= 0; i--) { while (s.size() >= 1) { pair<int, int> itt = (*s.begin()); if (itt.first >= k) break; for (set<int>::iterator it = g[itt.second].begin(); it != g[itt.second].end(); it++) { int u = *it; if (vis[u] == 1) continue; s.erase(make_pair(du[u], u)); du[u]--; s.insert(make_pair(du[u], u)); g[u].erase(itt.second); } s.erase(itt); vis[itt.second] = 1; } ans[i] = s.size(); if (vis[b1[i]] == 0 && vis[b2[i]] == 0) { s.erase(make_pair(du[b1[i]], b1[i])); du[b1[i]]--; s.insert(make_pair(du[b1[i]], b1[i])); g[b1[i]].erase(b2[i]); s.erase(make_pair(du[b2[i]], b2[i])); du[b2[i]]--; s.insert(make_pair(du[b2[i]], b2[i])); g[b2[i]].erase(b1[i]); } } for (int i = 0; i < m; i++) printf("%d\n", ans[i]); }
11
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; template <typename T, typename T2> inline void _max(T &a, T2 b) { a = max((T)a, (T)b); } template <typename T, typename T2> inline void _min(T &a, T2 b) { a = min((T)a, (T)b); } const int MAX = 2e5 + 10; int n, m, k; set<int> g[MAX]; set<pair<int, int> > mm; set<int> vs; vector<pair<int, int> > es; void removeEdge(int a, int b) { mm.erase(mm.find({g[a].size(), a})); g[a].erase(b); mm.insert({g[a].size(), a}); } void remove(int v) { vs.erase(v); set<int> nxt = g[v]; for (auto &to : nxt) { removeEdge(to, v); removeEdge(v, to); } } void clear() { for (int i = 0; i < MAX; i++) g[i].clear(); mm.clear(); vs.clear(); es.clear(); } void read() { for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--, b--; g[a].insert(b); g[b].insert(a); es.push_back({a, b}); } } void solve() { for (int i = 0; i < n; i++) vs.insert(i); for (int i = 0; i < n; i++) mm.insert({g[i].size(), i}); while (!mm.empty() && mm.begin()->first < k) { auto p = *mm.begin(); remove(p.second); mm.erase(mm.begin()); } vector<int> res; for (int i = m - 1; i >= 0; i--) { res.push_back(vs.size()); auto e = es[i]; int u = e.first, v = e.second; if (vs.count(u) == 0 || vs.count(v) == 0) continue; removeEdge(u, v); removeEdge(v, u); while (!mm.empty() && mm.begin()->first < k) { auto p = *mm.begin(); remove(p.second); mm.erase(mm.begin()); } } reverse(begin(res), end(res)); for (auto &x : res) printf("%d\n", x); } int main() { while (scanf("%d%d%d", &n, &m, &k) == 3) { clear(); read(); solve(); return 0; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int mx = 2e5 + 10, cut = 700; int n, m, k, cnt[mx], lv, x[mx], y[mx], dead[mx]; vector<pair<int, int> > adj[mx]; vector<int> res; void er(int a, int b) { auto it = lower_bound(adj[a].begin(), adj[a].end(), make_pair(b, 0)); if (it->second) return; it->second = 1; cnt[a]--; } void f(int h) { if (cnt[h] >= k || dead[h]) return; dead[h] = 1; lv--; for (auto &it : adj[h]) if (!it.second) er(it.first, h); for (auto &it : adj[h]) if (!it.second) f(it.first); } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%d%d", x + i, y + i); cnt[x[i]]++; cnt[y[i]]++; adj[x[i]].push_back({y[i], 0}); adj[y[i]].push_back({x[i], 0}); } for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end()); lv = n; for (int i = 1; i <= n; i++) f(i); for (int i = m; i--;) { res.push_back(lv); er(x[i], y[i]); er(y[i], x[i]); f(x[i]); f(y[i]); } reverse(res.begin(), res.end()); for (auto &it : res) printf("%d\n", it); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > adj; set<long long> st; vector<long long> deg; long long k; set<pair<long long, long long> > edges; void remove(long long r) { st.erase(r); for (long long i = 0; i < adj[r].size(); i++) { long long c = adj[r][i]; if (st.find(c) == st.end()) continue; if (edges.find(make_pair(r, c)) == edges.end()) continue; edges.erase(make_pair(r, c)); edges.erase(make_pair(c, r)); deg[c]--; if (deg[c] < k) remove(c); } } int main() { ios_base::sync_with_stdio(false); long long n, m; cin >> n >> m >> k; vector<long long> x(m); vector<long long> y(m); adj.resize(n); deg.resize(n, 0); for (long long i = 0; i < m; i++) { cin >> x[i] >> y[i]; x[i]--; y[i]--; adj[x[i]].push_back(y[i]); adj[y[i]].push_back(x[i]); edges.insert(make_pair(x[i], y[i])); edges.insert(make_pair(y[i], x[i])); deg[x[i]]++; deg[y[i]]++; } for (long long i = 0; i < n; i++) { st.insert(i); } for (long long i = 0; i < n; i++) { if (st.find(i) == st.end()) continue; if (deg[i] < k) remove(i); } vector<long long> res; res.push_back(st.size()); for (long long i = m - 1; i >= 1; i--) { if (st.find(x[i]) == st.end() || st.find(y[i]) == st.end()) { res.push_back(st.size()); continue; } edges.erase(make_pair(x[i], y[i])); edges.erase(make_pair(y[i], x[i])); deg[x[i]]--; deg[y[i]]--; if (deg[x[i]] < k) remove(x[i]); if (st.find(y[i]) != st.end()) { if (deg[y[i]] < k) remove(y[i]); } res.push_back(st.size()); } reverse(res.begin(), res.end()); for (long long i = 0; i < res.size(); i++) { cout << res[i] << endl; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, m, k; bool vis[N]; queue<int> Q; set<int> G[N]; vector<int> E[N]; int a[N], b[N], ans[N], sum; void solve() { while (!Q.empty()) { int u = Q.front(); Q.pop(); for (auto v : G[u]) { G[v].erase(u); if (!vis[v] && G[v].size() < k) { sum--; vis[v] = 1; Q.push(v); } } } } int main() { scanf("%d%d%d", &n, &m, &k); sum = n; for (int i = 1; i <= m; i++) { scanf("%d%d", &a[i], &b[i]); G[a[i]].insert(b[i]); G[b[i]].insert(a[i]); } for (int i = 1; i <= n; i++) { if (G[i].size() < k) { Q.push(i); vis[i] = true; sum--; } } solve(); ans[m] = sum; for (int i = m; i > 1; i--) { G[a[i]].erase(b[i]); G[b[i]].erase(a[i]); if (!vis[a[i]] && G[a[i]].size() < k) sum--, Q.push(a[i]), vis[a[i]] = 1; if (!vis[b[i]] && G[b[i]].size() < k) sum--, Q.push(b[i]), vis[b[i]] = 1; solve(); ans[i - 1] = sum; } for (int i = 1; i <= m; i++) printf("%d\n", ans[i] > 0 ? ans[i] : 0); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main(int agrc, char* argv[]) { int n, m, k; scanf("%d %d %d", &n, &m, &k); std::vector<std::vector<int>> edges(n + 1); int selected[n]; for (int i = 0; i < n; ++i) { selected[i] = 1; } int total = n; int friends[m][2]; for (int i = 0; i < m; ++i) { scanf("%d %d", &friends[i][0], &friends[i][1]); --friends[i][0]; --friends[i][1]; edges[friends[i][0]].push_back(friends[i][1]); edges[friends[i][1]].push_back(friends[i][0]); } int friends_going[n]; for (int i = 0; i < n; ++i) { friends_going[i] = edges[i].size(); } for (int i = 0; i < n; ++i) { std::stack<int> rejected; rejected.push(i); while (!rejected.empty()) { int index = rejected.top(); rejected.pop(); if (!(selected[index] == 1 && friends_going[index] < k)) { continue; } selected[index] = 0; --total; for (int j : edges[index]) { --friends_going[j]; rejected.push(j); } } } int answer[m]; answer[m - 1] = total; for (int i = m - 2; i >= 0; --i) { int first = friends[i + 1][0], second = friends[i + 1][1]; edges[first].pop_back(); edges[second].pop_back(); if (selected[second] == 1) { --friends_going[first]; } if (selected[first]) { --friends_going[second]; } std::stack<int> rejected; rejected.push(first); rejected.push(second); while (!rejected.empty()) { int index = rejected.top(); rejected.pop(); if (!(selected[index] == 1 && friends_going[index] < k)) { continue; } selected[index] = 0; --total; for (int j : edges[index]) { --friends_going[j]; rejected.push(j); } } answer[i] = total; } for (int i = 0; i < m; i++) { printf("%d\n", answer[i]); } }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); const int LIM = 600; int N, M, K; cin >> N >> M >> K; vector<int> x(M), y(M); vector<vector<int> > g(N); for (int i = 0; i < M; i++) { cin >> x[i] >> y[i]; g[--x[i]].push_back(i); g[--y[i]].push_back(i); } vector<int> deg(N); for (int i = 0; i < N; i++) deg[i] = g[i].size(); vector<bool> del(N, false); vector<bool> live(M, true); int alive = N; vector<int> ans(M); auto bfs = [&](int v) { if (deg[v] >= K || del[v]) return; queue<int> rem; rem.push(v); del[v] = true; alive--; while (!rem.empty()) { int nxt = rem.front(); rem.pop(); for (int e : g[nxt]) { if (!live[e]) continue; live[e] = false; int nbr = x[e] + y[e] - nxt; deg[nbr]--; if (!del[nbr] && deg[nbr] < K) { del[nbr] = true; alive--; rem.push(nbr); } } } }; for (int i = 0; i < N; i++) { bfs(i); } for (int i = M - 1; i >= 0; i--) { ans[i] = alive; if (live[i]) { live[i] = false; deg[x[i]]--; deg[y[i]]--; bfs(x[i]); bfs(y[i]); } } for (int i = 0; i < M; i++) cout << ans[i] << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> #pragma GCC optimize(3, "Ofast", "inline") using namespace std; bool Finish_read; template <class T> inline void read(T &x) { Finish_read = 0; x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; if (ch == EOF) return; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x *= f; Finish_read = 1; } template <class T> inline void print(T x) { if (x / 10 != 0) print(x / 10); putchar(x % 10 + '0'); } template <class T> inline void writeln(T x) { if (x < 0) putchar('-'); x = abs(x); print(x); putchar('\n'); } template <class T> inline void write(T x) { if (x < 0) putchar('-'); x = abs(x); print(x); } const int maxn = 200005; set<int> G[maxn]; queue<int> q; int n, m, k, u[maxn], v[maxn], d[maxn], vis[maxn]; vector<int> res; inline void calc() { int tmp = n; for (int i = m; i; --i) { while (!q.empty()) { int x = q.front(); q.pop(); --tmp; for (int y : G[x]) { if (G[y].find(x) == G[y].end()) continue; G[y].erase(x); d[y]--; if (d[y] < k && !vis[y]) q.push(y), vis[y] = 1; } } res.push_back(tmp); int x = u[i], y = v[i]; if (G[x].find(y) == G[x].end()) goto nxt; G[x].erase(y); --d[x]; if (d[x] < k && !vis[x]) q.push(x), vis[x] = 1; nxt:; if (G[y].find(x) == G[y].end()) continue; G[y].erase(x); --d[y]; if (d[y] < k && !vis[y]) q.push(y), vis[y] = 1; } } inline void input() { read(n), read(m), read(k); for (int i = 1; i <= m; ++i) { read(u[i]), read(v[i]); G[u[i]].insert(v[i]); G[v[i]].insert(u[i]); d[u[i]]++, d[v[i]]++; } } int main() { input(); for (int i = 1; i <= n; ++i) if (d[i] < k) q.push(i), vis[i] = 1; calc(); for (int i = 0; i < (int)res.size(); ++i) printf("%d\n", res[res.size() - i - 1]); }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k, numOn, res[200000]; set<int> f[200000]; bool on[200000]; vector<pair<int, int> > v; queue<int> toRemove; void update() { while (!toRemove.empty()) { int r = toRemove.front(); toRemove.pop(); numOn--; for (auto next : f[r]) { f[next].erase(r); if (on[next] && ((int)(f[next]).size()) < k) { on[next] = 0; toRemove.push(next); } } f[r].clear(); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < (m); i++) { int x, y; cin >> x >> y; x--, y--; v.push_back(make_pair(x, y)); f[x].insert(y), f[y].insert(x); } for (int i = 0; i < (n); i++) on[i] = 1; numOn = n; for (int i = 0; i < (n); i++) if (((int)(f[i]).size()) < k) { on[i] = 0; toRemove.push(i); } update(); res[m - 1] = numOn; for (int i = (m)-1; i >= (1); i--) { int x = v[i].first, y = v[i].second; f[x].erase(y), f[y].erase(x); if (on[x] && ((int)(f[x]).size()) < k) { on[x] = 0; toRemove.push(x); } if (on[y] && ((int)(f[y]).size()) < k) { on[y] = 0; toRemove.push(y); } update(); res[i - 1] = numOn; } for (int i = 0; i < (m); i++) cout << res[i] << "\n"; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 5e5; const long long INF = 1e18; const long long mod = 1e9 + 7; set<long long> g[N], st; long long i, j, n, m, k, ans[N], u[N], v[N]; void dfs(long long x) { auto it = st.lower_bound(x); if (*it != x || g[x].size() >= k) return; st.erase(it); for (auto to : g[x]) { g[to].erase(x); dfs(to); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (i = 0; i < m; ++i) { cin >> u[i] >> v[i]; g[u[i]].insert(v[i]); g[v[i]].insert(u[i]); } for (i = 1; i <= n; ++i) st.insert(i); for (i = 1; i <= n; ++i) dfs(i); for (i = m - 1; i >= 0; --i) { long long x = u[i], y = v[i]; ans[i] = st.size(); g[x].erase(y); g[y].erase(x); dfs(x); dfs(y); } for (i = 0; i < m; ++i) cout << ans[i] << "\n"; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T& x) { char c = getchar(); bool f = false; for (x = 0; !isdigit(c); c = getchar()) { if (c == '-') { f = true; } } for (; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } if (f) { x = -x; } } template <typename T> inline bool checkMax(T& a, const T& b) { return a < b ? a = b, true : false; } template <typename T> inline bool checkMin(T& a, const T& b) { return a > b ? a = b, true : false; } const int N = 2e5 + 10; int n, m, k, degree[N], ans[N], bl, avalib[N]; vector<pair<int, int> > G[N]; pair<int, int> f[N]; int main() { read(n), read(m), read(k), bl = n; for (register int i = 1; i <= m; ++i) { int u, v; read(u), read(v); ++degree[u], ++degree[v]; G[u].push_back(make_pair(v, i)); G[v].push_back(make_pair(u, i)); f[i] = pair<int, int>(u, v); } queue<int> Q; for (register int i = 1; i <= n; ++i) { if (degree[i] < k) { --bl, Q.push(i); } } for (register int i = m; i; --i) { while (!Q.empty()) { int x = Q.front(); Q.pop(); for (auto v : G[x]) { if (!avalib[v.second]) { if (--degree[v.first] == k - 1) { --bl, Q.push(v.first); } avalib[v.second] = 1; } } } ans[i] = bl; if (!avalib[i]) { if (--degree[f[i].first] == k - 1) { --bl, Q.push(f[i].first); } if (--degree[f[i].second] == k - 1) { --bl, Q.push(f[i].second); } avalib[i] = 1; } } for (register int i = 1; i <= m; ++i) { printf("%d\n", ans[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<pair<int, int>> edges, g[N]; vector<int> ans; set<pair<int, int>> s; set<int> deleted; int n, m, k, a, b, deg[N]; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; ++i) { scanf("%d%d", &a, &b); edges.emplace_back(a, b); deg[a]++; deg[b]++; g[a].emplace_back(b, i); g[b].emplace_back(a, i); } for (int i = 1; i <= n; ++i) s.emplace(deg[i], i); for (int ind = m - 1; ind >= 0; --ind) { while (s.size() && s.begin()->first < k) { int u = s.begin()->second; s.erase(s.begin()); deleted.insert(u); for (auto &i : g[u]) { int v = i.first; if (i.second > ind) continue; if (!deleted.count(v)) { s.erase({deg[v], v}); --deg[v]; s.insert({deg[v], v}); } } } ans.emplace_back((int)s.size()); int x, y; tie(x, y) = edges[ind]; if (!deleted.count(x) && !deleted.count(y)) { s.erase({deg[x], x}); deg[x]--; s.insert({deg[x], x}); s.erase({deg[y], y}); deg[y]--; s.insert({deg[y], y}); } } reverse(ans.begin(), ans.end()); for (auto &i : ans) printf("%d\n", i); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 3e5 + 10; long long x[N], y[N], deg[N]; vector<long long> vp[N]; long long removed[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k; cin >> n >> m >> k; for (long long i = 1; i <= m; i++) { cin >> x[i] >> y[i]; vp[x[i]].push_back(y[i]); vp[y[i]].push_back(x[i]); deg[x[i]]++; deg[y[i]]++; } set<pair<long long, long long> > s; for (long long i = 1; i <= n; i++) { s.insert({deg[i], i}); } long long ans = n; set<pair<long long, long long> > ae; vector<long long> v; for (long long i = m; i >= 1; i--) { while (s.size() > 0) { auto it = *s.begin(); s.erase(it); if (it.first >= k) break; removed[it.second] = 1; for (auto it1 : vp[it.second]) { if (ae.find({it1, it.second}) == ae.end()) { ae.insert({it1, it.second}); ae.insert({it.second, it1}); s.erase({deg[it1], it1}); deg[it1]--; s.insert({deg[it1], it1}); } } ans--; } if (ae.find({x[i], y[i]}) == ae.end()) { ae.insert({x[i], y[i]}); ae.insert({y[i], x[i]}); s.erase({deg[x[i]], x[i]}); s.erase({deg[y[i]], y[i]}); deg[x[i]]--; s.insert({deg[x[i]], x[i]}); deg[y[i]]--; s.insert({deg[y[i]], y[i]}); } v.push_back(ans); } reverse(v.begin(), v.end()); for (auto it : v) { cout << it << "\n"; } }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, k; int res; vector<set<int>> g; vector<int> used; vector<int> selected; void bfs(int start) { vector<int> seen = {start}; queue<int> q; used[start] = 1; q.push(start); while (!q.empty()) { int v = q.front(); q.pop(); assert(selected[v]); assert((int)g[v].size() < k); selected[v] = 0; res--; for (auto t : g[v]) { g[t].erase(v); if (selected[t] && (int)g[t].size() < k && !used[t]) { seen.push_back(t); used[t] = 1; q.push(t); } } } for (auto t : seen) { used[t] = 0; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> n >> m >> k; used.resize(n); g.resize(n); vector<pair<int, int>> q; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; g[u].insert(v); g[v].insert(u); q.push_back({u, v}); } reverse(q.begin(), q.end()); selected.assign(n, 1); res = n; for (int i = 0; i < n; i++) { if ((int)g[i].size() < k && selected[i]) { bfs(i); } } vector<int> ans; for (auto [u, v] : q) { ans.push_back(res); g[u].erase(v); g[v].erase(u); if ((int)g[u].size() < k && selected[u]) { bfs(u); } if ((int)g[v].size() < k && selected[v]) { bfs(v); } } reverse(ans.begin(), ans.end()); for (auto t : ans) { cout << t << '\n'; } }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int n, m, k, deg[N], ans[N]; pair<int, int> edges[N]; set<int> adj[N]; set<pair<int, int> > st; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; edges[i] = {x, y}; adj[x].insert(y); adj[y].insert(x); } for (int i = 1; i <= n; i++) { st.insert({(int)adj[i].size(), i}); } for (int i = m; i >= 1; i--) { while (!st.empty() && (*st.begin()).first < k) { int node = (*st.begin()).second; st.erase(st.begin()); for (auto ch : adj[node]) { st.erase(st.find({(int)adj[ch].size(), ch})); adj[ch].erase(node); st.insert({(int)adj[ch].size(), ch}); } adj[node].clear(); } ans[i] = (int)st.size(); if (adj[edges[i].first].find(edges[i].second) != adj[edges[i].first].end()) { int v = edges[i].first, u = edges[i].second; pair<int, int> pr = {(int)adj[v].size(), v}, pr2 = {(int)adj[u].size(), u}; st.erase(pr); st.erase(pr2); adj[v].erase(u), adj[u].erase(v); pr = {(int)adj[v].size(), v}, pr2 = pr2 = {(int)adj[u].size(), u}; st.insert(pr); st.insert(pr2); } } for (int i = 1; i <= m; i++) cout << ans[i] << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 2e5 + 10; pair<int, int> edge[maxn]; map<pair<int, int>, int> mp; vector<int> g[maxn]; int vis[maxn]; queue<int> q; int du[maxn]; int ans[maxn]; int main() { std::ios::sync_with_stdio(false); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; edge[i].first = u; edge[i].second = v; du[u]++; du[v]++; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) { if (du[i] < k) { vis[i] = 1; q.push(i); } } while (!q.empty()) { int u = q.front(); du[u]--; q.pop(); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; mp[make_pair(u, v)] = 1; mp[make_pair(v, u)] = 1; du[v]--; if (vis[v] == 0 && du[v] < k) { q.push(v); vis[v] = 1; } } } for (int i = 1; i <= n; i++) { if (vis[i] == 0) ans[m]++; } for (int i = m - 1; i >= 1; i--) { ans[i] = ans[i + 1]; int x = edge[i + 1].first; int y = edge[i + 1].second; if (mp[make_pair(x, y)] == 1 || mp[make_pair(x, y)] == 1) { continue; } mp[make_pair(x, y)] = 1; mp[make_pair(x, y)] = 1; du[x]--; du[y]--; if (vis[x] == 0 && du[x] < k) { q.push(x); vis[x] = 1; ans[i]--; } if (vis[y] == 0 && du[y] < k) { q.push(y); vis[y] = 1; ans[i]--; } while (!q.empty()) { int u = q.front(); q.pop(); for (int t = 0; t < g[u].size(); t++) { int v = g[u][t]; if (mp[make_pair(u, v)] == 1 || mp[make_pair(v, u)] == 1) { continue; } mp[make_pair(u, v)] = 1; mp[make_pair(v, u)] = 1; du[v]--; du[u]--; if (vis[v] == 0 && du[v] < k) { q.push(v); vis[v] = 1; ans[i]--; } } } } for (int i = 1; i <= m; i++) { cout << ans[i] << endl; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<vector<pair<int, int>>> g(n); vector<int> from(m), to(m); for (int i = 0; i < m; i++) { cin >> from[i] >> to[i]; from[i]--; to[i]--; g[from[i]].emplace_back(to[i], i); g[to[i]].emplace_back(from[i], i); } vector<int> res(m), alive(m, 1), que, deg(n); for (int i = 0; i < n; i++) { deg[i] = (int)g[i].size(); if (deg[i] < k) que.push_back(i); } int ind = 0; for (int i = m - 1; i >= 0; i--) { while (ind < (int)que.size()) { int u = que[ind]; for (auto p : g[u]) { if (!alive[p.second]) continue; alive[p.second] = 0; deg[u]--; if (deg[p.first] == k) que.push_back(p.first); deg[p.first]--; } ind++; } res[i] = n - que.size(); if (alive[i]) { alive[i] = 0; if (deg[from[i]] == k) que.push_back(from[i]); if (deg[to[i]] == k) que.push_back(to[i]); deg[from[i]]--; deg[to[i]]--; } } for (int i : res) cout << i << '\n'; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = int(200000) + 5; int t, n, m, q, k, cnt, sum, x, y, l, r; int a[N], b[N], ans[N]; pair<int, int> edge[N]; vector<pair<int, int> > v[N]; set<pair<int, int> > s; set<pair<int, int> >::iterator it; void update(int I) { while (!s.empty() && (s.begin()->first) < k) { it = s.begin(); x = it->second; for (int i = 0; i < v[x].size(); i++) { y = v[x][i].first; if (v[x][i].second >= I) continue; if (b[y]) { s.erase({a[y], y}); a[y]--; s.insert({a[y], y}); } } b[x] = 0; s.erase(it); } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> x >> y; edge[i].first = x; edge[i].second = y; a[x]++; a[y]++; v[x].push_back({y, i}); v[y].push_back({x, i}); } for (int i = 1; i <= n; i++) { s.insert({a[i], i}); b[i] = 1; } update(1e6); for (int i = m; i >= 1; i--) { ans[i] = s.size(); l = edge[i].first; r = edge[i].second; if (b[l] && b[r]) { s.erase({a[l], l}); a[l]--; s.insert({a[l], l}); s.erase({a[r], r}); a[r]--; s.insert({a[r], r}); update(i); } } for (int i = 1; i <= m; i++) cout << ans[i] << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int N = 2e5 + 5; int ans[N]; int degree[N]; int is_good[N]; vector<pair<int, int>> E[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> e(m); for (int i = 0; i < m; i++) { auto& [x, y] = e[i]; cin >> x >> y; x--; y--; degree[x]++; degree[y]++; E[x].push_back({y, i}); E[y].push_back({x, i}); } set<pair<int, int>> good; for (int i = 0; i < n; i++) good.insert({degree[i], i}); fill_n(is_good, n, 1); auto keep_good = [&](int t) -> void { while (!good.empty() && good.begin()->first < k) { int x = good.begin()->second; good.erase(good.begin()); for (const auto& [y, i] : E[x]) { if (is_good[y] && i < t) { good.erase({degree[y], y}); degree[y]--; good.insert({degree[y], y}); } } is_good[x] = 0; } }; keep_good(m); for (int i = m - 1; i >= 0; i--) { ans[i] = good.size(); const auto& [x, y] = e[i]; if (is_good[x] && is_good[y]) { good.erase({degree[x], x}); degree[x]--; good.insert({degree[x], x}); good.erase({degree[y], y}); degree[y]--; good.insert({degree[y], y}); keep_good(i); } } for (int i = 0; i < m; i++) cout << ans[i] << "\n"; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k, x[200009], y[200009]; set<int> alive; vector<int> kaaj; set<int> graph[200009]; int degree[200009], ans[200009]; void doit(int z) { if (alive.find(z) == alive.end()) return; alive.erase(z); if (graph[z].empty()) return; auto it = graph[z].begin(); while (it != graph[z].end()) { int v = *it; degree[v]--; graph[v].erase(z); if (degree[v] < k && alive.find(v) != alive.end()) { kaaj.push_back(v); } it++; } graph[z].clear(); } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d %d", &x[i], &y[i]); graph[x[i]].insert(y[i]); graph[y[i]].insert(x[i]); degree[x[i]]++; degree[y[i]]++; } for (int i = 1; i <= n; i++) alive.insert(i); for (int i = 1; i <= n; i++) { if (degree[i] < k) { kaaj.push_back(i); } } while (!kaaj.empty()) { int z = kaaj.back(); kaaj.pop_back(); doit(z); } for (int i = m; i >= 1; i--) { ans[i] = alive.size(); if (alive.find(x[i]) == alive.end() || alive.find(y[i]) == alive.end()) continue; degree[x[i]]--; degree[y[i]]--; graph[x[i]].erase(y[i]); graph[y[i]].erase(x[i]); if (degree[x[i]] < k && alive.find(x[i]) != alive.end()) { kaaj.push_back(x[i]); } while (!kaaj.empty()) { int z = kaaj.back(); kaaj.pop_back(); doit(z); } if (degree[y[i]] < k && alive.find(y[i]) != alive.end()) { kaaj.push_back(y[i]); } while (!kaaj.empty()) { int z = kaaj.back(); kaaj.pop_back(); doit(z); } } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; set<int> adj[200009]; set<int>::iterator it; int vis[200009]; pair<int, int> edg[200009]; vector<int> bad; int ses[200009]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { scanf("%d%d", &edg[i].first, &edg[i].second); adj[edg[i].first].insert(edg[i].second); adj[edg[i].second].insert(edg[i].first); } int ans = n; for (int i = 1; i <= n; i++) { if (adj[i].size() < k) { vis[i] = 1; ans--; bad.push_back(i); } } for (int i = m; i >= 1; i--) { while (bad.size() > 0) { int v = bad.back(); bad.pop_back(); for (it = adj[v].begin(); it != adj[v].end(); it++) { int u = *it; adj[u].erase(v); if (vis[u] == 0 && adj[u].size() < k) { bad.push_back(u); vis[u] = 1; ans--; } } adj[v].clear(); } ses[i] = ans; int u = edg[i].first; int v = edg[i].second; if (vis[u] == 1 || vis[v] == 1) continue; adj[u].erase(v); adj[v].erase(u); if (adj[u].size() < k) { bad.push_back(u); ans--; vis[u] = 1; } if (adj[v].size() < k) { bad.push_back(v); ans--; vis[v] = 1; } } for (int i = 1; i <= m; i++) { printf("%d\n", ses[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct Edge { int x, y; } E[200010]; struct edge { int nxt, t, s; } e[200010 << 1]; int head[200010], edge_cnt; void add_edge(int x, int y, int z) { e[edge_cnt] = (edge){head[x], y, z}; head[x] = edge_cnt++; } bool ban[200010]; int Deg[200010], Q[200010], Ans[200010]; int main() { memset(head, -1, sizeof(head)); int n, m, K, i, j; scanf("%d%d%d", &n, &m, &K); for (i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); add_edge(x, y, i); add_edge(y, x, i); E[i] = (Edge){x, y}; Deg[x]++, Deg[y]++; } int L = 1, R = 0; for (i = 1; i <= n; i++) { if (Deg[i] < K) { Q[++R] = i; ban[i] = 1; } } for (i = m; i >= 1; i--) { while (L <= R) { int x = Q[L++]; for (j = head[x]; ~j; j = e[j].nxt) { int y = e[j].t; if (ban[y] || e[j].s > i) { continue; } Deg[y]--; if (Deg[y] < K) { Q[++R] = y; ban[y] = 1; } } } Ans[i] = n - R; int a = E[i].x, b = E[i].y; if (ban[a] || ban[b]) { continue; } Deg[a]--, Deg[b]--; if (Deg[a] < K) { Q[++R] = a; ban[a] = 1; } if (Deg[b] < K) { Q[++R] = b; ban[b] = 1; } } for (i = 1; i <= m; i++) { printf("%d\n", Ans[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int intcmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; } int n, m, k; vector<pair<int, int>> v1v2index[200010]; int ea[200010], eb[200010]; int d[200010]; int curidx; bool deleted[200010]; int delcnt[200010]; int delnow; void dfs(int v) { if (deleted[v] || d[v] >= k) { return; } deleted[v] = true; ++delnow; for (auto [u, idx] : v1v2index[v]) { if (idx >= curidx) { continue; } --d[u]; dfs(u); } } int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); v1v2index[a].push_back(make_pair(b, i)); v1v2index[b].push_back(make_pair(a, i)); ea[i] = a; eb[i] = b; d[a]++; d[b]++; } curidx = m; for (int i = 1; i < n + 1; i++) { dfs(i); } for (curidx = m - 1; curidx >= 0; curidx--) { delcnt[curidx] = delnow; if (deleted[ea[curidx]] || deleted[eb[curidx]]) { continue; } d[ea[curidx]]--; d[eb[curidx]]--; dfs(ea[curidx]); dfs(eb[curidx]); } for (int i = 0; i < m; ++i) { printf("%d\n", n - delcnt[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> const int N = 2e5 + 5; using namespace std; int n, m, k; int res[N]; int x[N], y[N]; int bac[N]; set<int> a[N]; set<pair<int, int> > S; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> x[i] >> y[i]; a[x[i]].insert(y[i]); a[y[i]].insert(x[i]); bac[x[i]]++; bac[y[i]]++; } for (int i = 1; i <= n; i++) if (bac[i] > 0) S.insert(make_pair(bac[i], i)); for (int i = m; i >= 1; i--) { while (S.size() && S.begin()->first < k) { int cc = S.begin()->first; int u = S.begin()->second; for (int v : a[u]) { S.erase(make_pair(bac[v], v)); bac[v]--; if (bac[v] > 0) S.insert(make_pair(bac[v], v)); a[v].erase(u); } a[u].clear(); bac[u] = 0; S.erase(make_pair(cc, u)); } res[i] = S.size(); if (a[y[i]].find(x[i]) != a[y[i]].end()) { S.erase(make_pair(bac[x[i]], x[i])); bac[x[i]]--; if (bac[x[i]] > 0) S.insert(make_pair(bac[x[i]], x[i])); S.erase(make_pair(bac[y[i]], y[i])); bac[y[i]]--; if (bac[y[i]] > 0) S.insert(make_pair(bac[y[i]], y[i])); a[x[i]].erase(y[i]); a[y[i]].erase(x[i]); } } for (int i = 1; i <= m; i++) cout << res[i] << '\n'; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<int> from(m), to(m); vector<vector<pair<int, int> > > adj(n); for (int i = (int)(0); i < (int)(m); ++i) { cin >> from[i] >> to[i]; from[i]--, to[i]--; adj[from[i]].emplace_back(to[i], i); adj[to[i]].emplace_back(from[i], i); } vector<int> alive(m, 1); vector<int> deg(n); set<pair<int, int> > g; for (int i = (int)(0); i < (int)(n); ++i) { deg[i] = adj[i].size(); if (deg[i]) g.emplace(deg[i], i); } function<void()> update = [&]() { while (g.size() && g.begin()->first < k) { int u = g.begin()->second; g.erase(g.begin()); for (auto &p : adj[u]) { if (alive[p.second]) { alive[p.second] = 0; int v = p.first; g.erase({deg[v], v}); g.emplace(--deg[v], v); } } } }; vector<int> ans(m); for (int i = (int)(m - 1); i >= (int)(0); --i) { update(); ans[i] = g.size(); if (alive[i]) { int u = from[i], v = to[i]; g.erase({deg[u], u}); g.emplace(--deg[u], u); g.erase({deg[v], v}); g.emplace(--deg[v], v); alive[i] = 0; } } for (int i = (int)(0); i < (int)(m); ++i) { cout << ans[i] << "\n"; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; set<int> S, G[N]; int A[N], B[N], C[N], n, m, k; void U(int u) { if (G[u].size() < k && S.erase(u)) for (auto v : G[u]) G[v].erase(u), U(v); } int main() { cin >> n >> m >> k; for (int i = 1; i <= m; ++i) { scanf("%d%d", A + i, B + i); G[A[i]].insert(B[i]); G[B[i]].insert(A[i]); } for (int i = 1; i <= n; S.insert(i++)) ; for (int i = 1; i <= n; ++i) U(i); for (int i = m; i >= 1; --i) { C[i] = S.size(); G[A[i]].erase(B[i]); G[B[i]].erase(A[i]); U(A[i]); U(B[i]); } for (int i = 1; i <= m; ++i) printf("%d\n", C[i]); }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 1; int n, m, k, ans; int du[MAX], X[MAX], Y[MAX], Ans[MAX]; vector<int> ve[MAX]; bool use[MAX]; void work(int x) { queue<int> qu; qu.push(x), use[x] = 1, --ans; while (!qu.empty()) { int tt = qu.front(); qu.pop(); for (int i = 0; i < ve[tt].size(); ++i) { int y = ve[tt][i]; if (use[y]) continue; --du[y]; if (du[y] < k) --ans, use[y] = 1, qu.push(y); } } } int main() { scanf("%d%d%d", &n, &m, &k); ans = n; for (int i = 1; i <= m; ++i) { scanf("%d%d", &X[i], &Y[i]); ++du[X[i]], ++du[Y[i]], ve[X[i]].push_back(Y[i]), ve[Y[i]].push_back(X[i]); } for (int i = 1; i <= n; ++i) if (du[i] < k && !use[i]) work(i); Ans[m] = ans; for (int i = m; i >= 1; --i) { if (!use[Y[i]]) --du[X[i]]; if (!use[X[i]]) --du[Y[i]]; ve[X[i]].pop_back(), ve[Y[i]].pop_back(); if (du[X[i]] < k && !use[X[i]]) work(X[i]); if (du[Y[i]] < k && !use[Y[i]]) work(Y[i]); Ans[i - 1] = ans; } for (int i = 1; i <= m; ++i) printf("%d\n", Ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long mod = 1000000007; const double e = 2.718281828459; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long gcd1(int a, int b) { if (!b) return a; return gcd(b, a % b); } long long pow_mod(long long a, long long b, long long c) { long long ans = 1; a = a % c; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % c; b = b / 2; a = (a * a) % c; } return ans; } int pow_int(int a, int b) { int ans = 1; while (b > 0) { if (b % 2 == 1) ans = ans * a; b = b / 2; a = a * a; } return ans; } long long pow_llong(long long a, long long b) { long long ans = 1; while (b > 0) { if (b % 2 == 1) ans = ans * a; b = b / 2; a = a * a; } return ans; } int Scan() { int res = 0, flag = 0; char ch; if ((ch = getchar()) == '-') { flag = 1; } else if (ch >= '0' && ch <= '9') { res = ch - '0'; } while ((ch = getchar()) >= '0' && ch <= '9') { res = res * 10 + (ch - '0'); } return flag ? -res : res; } void Out(int a) { if (a < 0) { putchar('-'); a = -a; } if (a >= 10) { Out(a / 10); } putchar(a % 10 + '0'); } long long jc_mod(long long a, long long b, long long mod) { long long ans = 1; b = max(b, a - b); for (long long i = a; i > b; i--) { ans *= i; ans %= mod; } return ans; } double lg(double a) { return (log(a) / log(10.0)); } namespace fastIO { bool IOerror = 0; inline char nc() { static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; if (p1 == pend) { p1 = buf; pend = buf + fread(buf, 1, 100000, stdin); if (pend == p1) { IOerror = 1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } inline void read(int &x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()) ; if (IOerror) return; if (ch == '-') sign = 1, ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0'; if (sign) x = -x; } inline void read(long long &x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()) ; if (IOerror) return; if (ch == '-') sign = 1, ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0'; if (sign) x = -x; } inline void read(double &x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()) ; if (IOerror) return; if (ch == '-') sign = 1, ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0'; if (ch == '.') { double tmp = 1; ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc()) tmp /= 10.0, x += tmp * (ch - '0'); } if (sign) x = -x; } inline void read(char *s) { char ch = nc(); for (; blank(ch); ch = nc()) ; if (IOerror) return; for (; !blank(ch) && !IOerror; ch = nc()) *s++ = ch; *s = 0; } inline void read(char &c) { for (c = nc(); blank(c); c = nc()) ; if (IOerror) { c = -1; return; } } inline void read1(int &x) { char ch; int bo = 0; x = 0; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') bo = 1; for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar()) ; if (bo) x = -x; } inline void read1(long long &x) { char ch; int bo = 0; x = 0; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') bo = 1; for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar()) ; if (bo) x = -x; } inline void read1(double &x) { char ch; int bo = 0; x = 0; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') bo = 1; for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar()) ; if (ch == '.') { double tmp = 1; for (ch = getchar(); ch >= '0' && ch <= '9'; tmp /= 10.0, x += tmp * (ch - '0'), ch = getchar()) ; } if (bo) x = -x; } inline void read1(char *s) { char ch = getchar(); for (; blank(ch); ch = getchar()) ; for (; !blank(ch); ch = getchar()) *s++ = ch; *s = 0; } inline void read1(char &c) { for (c = getchar(); blank(c); c = getchar()) ; } inline void read2(int &x) { scanf("%d", &x); } inline void read2(long long &x) { scanf("%lld", &x); } inline void read2(double &x) { scanf("%lf", &x); } inline void read2(char *s) { scanf("%s", s); } inline void read2(char &c) { scanf(" %c", &c); } inline void readln2(char *s) { gets(s); } struct Ostream_fwrite { char *buf, *p1, *pend; Ostream_fwrite() { buf = new char[100000]; p1 = buf; pend = buf + 100000; } void out(char ch) { if (p1 == pend) { fwrite(buf, 1, 100000, stdout); p1 = buf; } *p1++ = ch; } void print(int x) { static char s[15], *s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) out('-'), x = -x; while (x) *s1++ = x % 10 + '0', x /= 10; while (s1-- != s) out(*s1); } void println(int x) { static char s[15], *s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) out('-'), x = -x; while (x) *s1++ = x % 10 + '0', x /= 10; while (s1-- != s) out(*s1); out('\n'); } void print(long long x) { static char s[25], *s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) out('-'), x = -x; while (x) *s1++ = x % 10 + '0', x /= 10; while (s1-- != s) out(*s1); } void println(long long x) { static char s[25], *s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) out('-'), x = -x; while (x) *s1++ = x % 10 + '0', x /= 10; while (s1-- != s) out(*s1); out('\n'); } void print(double x, int y) { static long long mul[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL}; if (x < -1e-12) out('-'), x = -x; x *= mul[y]; long long x1 = (long long)floor(x); if (x - floor(x) >= 0.5) ++x1; long long x2 = x1 / mul[y], x3 = x1 - x2 * mul[y]; print(x2); if (y > 0) { out('.'); for (size_t i = 1; i < y && x3 * mul[i] < mul[y]; out('0'), ++i) ; print(x3); } } void println(double x, int y) { print(x, y); out('\n'); } void print(char *s) { while (*s) out(*s++); } void println(char *s) { while (*s) out(*s++); out('\n'); } void flush() { if (p1 != buf) { fwrite(buf, 1, p1 - buf, stdout); p1 = buf; } } ~Ostream_fwrite() { flush(); } } Ostream; inline void print(int x) { Ostream.print(x); } inline void println(int x) { Ostream.println(x); } inline void print(char x) { Ostream.out(x); } inline void println(char x) { Ostream.out(x); Ostream.out('\n'); } inline void print(long long x) { Ostream.print(x); } inline void println(long long x) { Ostream.println(x); } inline void print(double x, int y) { Ostream.print(x, y); } inline void println(double x, int y) { Ostream.println(x, y); } inline void print(char *s) { Ostream.print(s); } inline void println(char *s) { Ostream.println(s); } inline void println() { Ostream.out('\n'); } inline void flush() { Ostream.flush(); } char Out[100000], *o = Out; inline void print1(int x) { static char buf[15]; char *p1 = buf; if (!x) *p1++ = '0'; if (x < 0) *o++ = '-', x = -x; while (x) *p1++ = x % 10 + '0', x /= 10; while (p1-- != buf) *o++ = *p1; } inline void println1(int x) { print1(x); *o++ = '\n'; } inline void print1(long long x) { static char buf[25]; char *p1 = buf; if (!x) *p1++ = '0'; if (x < 0) *o++ = '-', x = -x; while (x) *p1++ = x % 10 + '0', x /= 10; while (p1-- != buf) *o++ = *p1; } inline void println1(long long x) { print1(x); *o++ = '\n'; } inline void print1(char c) { *o++ = c; } inline void println1(char c) { *o++ = c; *o++ = '\n'; } inline void print1(char *s) { while (*s) *o++ = *s++; } inline void println1(char *s) { print1(s); *o++ = '\n'; } inline void println1() { *o++ = '\n'; } inline void flush1() { if (o != Out) { if (*(o - 1) == '\n') *--o = 0; puts(Out); } } struct puts_write { ~puts_write() { flush1(); } } _puts; inline void print2(int x) { printf("%d", x); } inline void println2(int x) { printf("%d\n", x); } inline void print2(char x) { printf("%c", x); } inline void println2(char x) { printf("%c\n", x); } inline void print2(long long x) { printf("%lld", x); } inline void println2(long long x) { print2(x); printf("\n"); } inline void println2() { printf("\n"); } }; // namespace fastIO using namespace fastIO; void lisan(int *x, int n) { int data[100010]; for (int i = 1; i <= n; i++) data[i] = x[i]; sort(data + 1, data + 1 + n); int o = unique(data + 1, data + 1 + n) - data - 1; for (int i = 1; i <= n; i++) x[i] = lower_bound(data + 1, data + 1 + o, x[i]) - data; } long long calc(int M) { return 1; } int sanfen(int L, int R) { int M, RM; while (L + 1 < R) { M = (L + R) / 2; RM = (M + R) / 2; if (calc(M) < calc(RM)) R = RM; else L = M; } return L; } int CaculateWeekDay(int y, int m, int d) { if (m == 1 || m == 2) { m += 12; y--; } int iWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; return iWeek; } long long inv[2000022]; void init_inverse() { inv[1] = 1; for (int i = 2; i < 2000022; i++) inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod; } int n, m, k; set<int> second[200010]; set<pair<int, int> > now; int siz[200010]; void del(int x) { for (set<int>::iterator it = second[x].begin(); it != second[x].end();) { now.erase(make_pair(siz[*it], *it)); siz[*it]--; now.insert(make_pair(siz[*it], *it)); second[*it].erase(x); second[x].erase(*it++); } now.erase(make_pair(siz[x], x)); siz[x] = 0; } void update() { while (now.size() && now.begin()->first < k) { del(now.begin()->second); } } int ai[200010]; int ans[200010]; int bi[200010]; int main() { scanf("%d%d%d", &n, &m, &k); int a, b; for (int i = 1; i <= m; i++) { scanf("%d%d", &ai[i], &bi[i]); second[ai[i]].insert(bi[i]); second[bi[i]].insert(ai[i]); siz[ai[i]]++; siz[bi[i]]++; } for (int i = 1; i <= n; i++) now.insert(make_pair(siz[i], i)); update(); for (int i = m; i; i--) { ans[i] = now.size(); if (second[ai[i]].find(bi[i]) != second[ai[i]].end()) { now.erase(make_pair(siz[ai[i]], ai[i])); second[ai[i]].erase(bi[i]); siz[ai[i]]--; if (siz[ai[i]]) now.insert(make_pair(siz[ai[i]], ai[i])); } if (second[bi[i]].find(ai[i]) != second[bi[i]].end()) { now.erase(make_pair(siz[bi[i]], bi[i])); second[bi[i]].erase(ai[i]); siz[bi[i]]--; if (siz[bi[i]]) now.insert(make_pair(siz[bi[i]], bi[i])); } update(); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); }
11
CPP
#include <bits/stdc++.h> using namespace std; int x[200010], y[200010]; set<int> s[200010]; queue<int> q, p; bool inq[200010]; bool w[200010]; int ans[200010]; int n, m, k, sum; void reset() { while (!q.empty()) q.pop(); while (!p.empty()) inq[p.front()] = false, p.pop(); } void BFS() { while (!q.empty()) { int now = q.front(); w[now] = true; p.push(now); sum--; q.pop(); for (set<int>::iterator i = s[now].begin(); i != s[now].end(); i++) { s[*i].erase(now); if (s[*i].size() < k && !inq[*i] && !w[*i]) inq[*i] = true, q.push(*i); } } } int main() { cin >> n >> m >> k; sum = n; for (int i = 1; i <= m; i++) cin >> x[i] >> y[i], s[x[i]].insert(y[i]), s[y[i]].insert(x[i]); for (int i = 1; i <= n; i++) if (s[i].size() < k) inq[i] = true, q.push(i); BFS(); for (int i = m; i > 0; i--) { ans[i] = sum; reset(); s[x[i]].erase(y[i]); s[y[i]].erase(x[i]); if (s[x[i]].size() < k && !w[x[i]]) inq[x[i]] = true, q.push(x[i]); if (s[y[i]].size() < k && !w[y[i]]) inq[y[i]] = true, q.push(y[i]); BFS(); } for (int i = 1; i <= m; i++) cout << ans[i] << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long linf = 1LL << 62; const int iinf = 1000000009; const double dinf = 1e17; const int Mod = 1e9 + 9; const int maxn = 1000005; set<pair<int, int> > s, sp; int n, m, k, u, v; int mp[maxn], rt; pair<int, int> sd[maxn], ds[maxn]; vector<int> vp[200005]; void up(int x) { for (int i = 1; i <= vp[x].size(); i++) { if (s.count(make_pair(vp[x][i - 1], x))) { sp.erase(make_pair(mp[vp[x][i - 1]], vp[x][i - 1])); mp[vp[x][i - 1]]--; sp.insert(make_pair(mp[vp[x][i - 1]], vp[x][i - 1])); s.erase(make_pair(vp[x][i - 1], x)); s.erase(make_pair(x, vp[x][i - 1])); } } } void del() { set<pair<int, int> >::iterator it; while (1) { rt = 0; for (it = sp.begin(); it != sp.end(); it++) { if (((*it).first) >= k) break; ds[rt++] = (*it); } if (!rt) break; for (int i = 0; i <= rt - 1; i++) sp.erase(ds[i]), up(ds[i].second); } } void del(int i) { if (s.count(sd[i])) { s.erase(sd[i]); u = sd[i].first; v = sd[i].second; s.erase(make_pair(v, u)); sp.erase(make_pair(mp[u], u)); sp.insert(make_pair(--mp[u], u)); sp.erase(make_pair(mp[v], v)); sp.insert(make_pair(--mp[v], v)); del(); } } int ans[maxn]; void solve() { s.clear(); ios::sync_with_stdio(false); memset(mp, 0, sizeof(mp)); sp.clear(); cin >> n >> m >> k; for (int i = 1; i <= n; i++) vp[i].clear(); for (int i = 1; i <= m; i++) { cin >> u >> v; mp[u]++; mp[v]++; s.insert(make_pair(u, v)); s.insert(make_pair(v, u)); sd[i] = make_pair(u, v); vp[v].push_back(u); vp[u].push_back(v); } for (int i = 1; i <= n; i++) sp.insert(make_pair(mp[i], i)); for (int i = m; i >= 1; i--) { del(); ans[i] = sp.size(); del(i); } for (int i = 1; i <= m; i++) cout << ans[i] << endl; ; } int main() { int t = 1; while (t--) solve(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int OO = 0x3f3f3f3f; const double eps = (1e-10); stringstream out; int n, m, k, u, v, deg[200005]; bool isGood[200005]; vector<int> ans(200005); set<pair<int, int> > good; vector<pair<int, int> > edges(200005); vector<vector<pair<int, int> > > adj(200005); int main() { ios::sync_with_stdio(false); cout.precision(10); cin >> n >> m >> k; for (int i = 0; i < (int)(m); ++i) { cin >> u >> v; --u, --v; edges[i].first = u, edges[i].second = v; adj[u].push_back(make_pair(v, i)), adj[v].push_back(make_pair(u, i)); ++deg[u], ++deg[v]; } for (int i = 0; i < (int)(n); ++i) good.insert(make_pair(deg[i], i)), isGood[i] = true; while (!good.empty() && good.begin()->first < k) { u = good.begin()->second; for (auto p : adj[u]) { v = p.first; if (isGood[v]) { good.erase(make_pair(deg[v], v)); --deg[v]; good.insert(make_pair(deg[v], v)); } } isGood[u] = false; good.erase(make_pair(deg[u], u)); } for (int i = (m - 1); i >= (int)(0); --i) { ans[i] = ((int)((good).size())); u = edges[i].first, v = edges[i].second; if (isGood[u] && isGood[v]) { good.erase(make_pair(deg[u], u)); --deg[u]; good.insert(make_pair(deg[u], u)); good.erase(make_pair(deg[v], v)); --deg[v]; good.insert(make_pair(deg[v], v)); while (!good.empty() && good.begin()->first < k) { u = good.begin()->second; for (auto p : adj[u]) { if (p.second >= i) continue; v = p.first; if (isGood[v]) { good.erase(make_pair(deg[v], v)); --deg[v]; good.insert(make_pair(deg[v], v)); } } isGood[u] = false; good.erase(make_pair(deg[u], u)); } } } for (int i = 0; i < (int)(m); ++i) out << ans[i] << '\n'; cout << out.str(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, cnt, k, tot, tp; int head[200005]; int sta[200005]; int dgr[200005]; int ans[200005]; bool usd[200005]; struct node { int fr; int to; int nxt; int mrk; } edge[400005]; void init() { cnt = 0; memset(head, -1, sizeof(head)); } void addedge(int f, int t) { cnt++; edge[cnt].fr = f; edge[cnt].to = t; edge[cnt].nxt = head[f]; head[f] = cnt; } queue<int> que; int main() { init(); scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); dgr[v]++; dgr[u]++; } for (int i = 1; i <= n; i++) { if (dgr[i] < k) { que.push(i); } } tot = n; tp = m; while (!que.empty()) { int u = que.front(); que.pop(); if (usd[u]) continue; usd[u] = true; tot--; for (int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; dgr[v]--; if (dgr[v] < k) { que.push(v); } } } for (int i = cnt - 1; i >= 1; i -= 2) { ans[tp--] = tot; int u = edge[i].fr; int v = edge[i].to; if (usd[u] || usd[v]) continue; dgr[u]--, dgr[v]--; edge[i].mrk = edge[i + 1].mrk = 1; if (dgr[u] < k) que.push(u); if (dgr[v] < k) que.push(v); while (!que.empty()) { int s = que.front(); que.pop(); if (usd[s]) continue; usd[s] = true; tot--; for (int i = head[s]; i != -1; i = edge[i].nxt) { if (edge[i].mrk) continue; int v = edge[i].to; dgr[v]--; if (dgr[v] < k) { que.push(v); } } } } for (int i = 1; i <= m; i++) { printf("%d\n", ans[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using lint = long long; const int INF = 0x3f3f3f3f; const double EPS = 1e-16; int N, M, K; int edges[200010][2], degree[200010]; set<int> graph[200010]; set<tuple<int, int>> nodes; int ans[200010]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << setprecision(8) << fixed; cin >> N >> M >> K; for (int m = 0; m < M; m++) cin >> edges[m][0] >> edges[m][1]; for (int m = 0; m < M; m++) { graph[edges[m][0]].insert(edges[m][1]); graph[edges[m][1]].insert(edges[m][0]); degree[edges[m][0]]++; degree[edges[m][1]]++; } for (int n = 1; n <= N; n++) { nodes.insert({degree[n], n}); } for (int day = M - 1; day >= 0; day--) { while (nodes.size() && get<0>(*nodes.begin()) < K) { int cur = get<1>(*nodes.begin()); nodes.erase(nodes.begin()); for (const int &nxt : graph[cur]) { if (nodes.count({degree[nxt], nxt})) { nodes.erase({degree[nxt], nxt}); nodes.insert({--degree[nxt], nxt}); } } } if (nodes.empty()) break; ans[day] = nodes.size(); if (nodes.count({degree[edges[day][0]], edges[day][0]}) && nodes.count({degree[edges[day][1]], edges[day][1]})) { for (int e = 0; e < 2; e++) { nodes.erase({degree[edges[day][e]], edges[day][e]}); degree[edges[day][e]]--; nodes.insert({degree[edges[day][e]], edges[day][e]}); graph[edges[day][e]].erase(edges[day][1 - e]); } } } for (int ai = 0; ai < M; ai++) { cout << ans[ai] << '\n'; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <class u> pair<u, u> ordered_pair(const u& a, const u& b) { return a < b ? make_pair(a, b) : make_pair(b, a); } int n, m, k; set<int> gr[201010]; vector<pair<int, int>> edge_list; set<pair<int, int>> removed_edges; struct cmp_deg { bool operator()(int u, int v) { return make_pair(((int)gr[u].size()), u) < make_pair(((int)gr[v].size()), v); } }; set<int, cmp_deg> node_set; void remove_edge(int u, int v) { node_set.erase(u); node_set.erase(v); gr[u].erase(v); gr[v].erase(u); node_set.insert(u); node_set.insert(v); removed_edges.insert(ordered_pair(u, v)); } void remove_node(int u) { while (((int)gr[u].size())) remove_edge(u, *gr[u].begin()); node_set.erase(u); } void adjust() { while (((int)node_set.size()) and ((int)gr[*node_set.begin()].size()) < k) { remove_node(*node_set.begin()); } } int main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = -1; ++i < m;) { int u, v; cin >> u >> v; gr[u].insert(v); gr[v].insert(u); edge_list.push_back(ordered_pair(u, v)); } for (int i = 0; i++ < n;) node_set.insert(i); vector<int> ans(m); for (int i = m; i--;) { adjust(); ans[i] = ((int)node_set.size()); if (!removed_edges.count(edge_list[i])) { remove_edge(edge_list[i].first, edge_list[i].second); } } for (auto i : ans) cout << i << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; unordered_set<int> e[N]; int n, m, k, ex[N], ey[N], i, ans, ot[N]; bool bb[N]; void del(int x) { bb[x] = 0; --ans; for (int y : e[x]) e[y].erase(x); for (int y : e[x]) if (bb[y] && e[y].size() < k) del(y); e[x].clear(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (i = 1; i <= m; ++i) cin >> ex[i] >> ey[i], e[ex[i]].insert(ey[i]), e[ey[i]].insert(ex[i]); memset(bb + 1, 1, n); ans = n; for (i = 1; i <= n; ++i) if (bb[i] && e[i].size() < k) del(i); for (i = m; i; --i) { ot[i] = ans; if (bb[ex[i]] && bb[ey[i]]) { e[ex[i]].erase(ey[i]); e[ey[i]].erase(ex[i]); if (bb[ex[i]] && e[ex[i]].size() < k) del(ex[i]); if (bb[ey[i]] && e[ey[i]].size() < k) del(ey[i]); } } for (i = 1; i <= m; ++i) cout << ot[i] << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T parse(const string &s) { T x; stringstream ss(s); ss >> x; return x; } const double EPS = 1e-12; const int INF = 1000 * 1000 * 1000; const long long LINF = INF * 1ll * INF; const double DINF = 1e200; const double PI = 3.1415926535897932384626433832795l; int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } long long gcdex(long long a, long long b, long long &x, long long &y) { if (!a) { x = 0, y = 1; return b; } long long k = b / a; long long g = gcdex(b - k * a, a, y, x); x -= k * y; return g; } long long inv(long long a, long long m) { assert(m > 1); long long x, y, g; g = gcdex(a, m, x, y); return (x % (m / g) + m / g) % m / g; } long long crt(long long a1, long long m1, long long a2, long long m2) { long long a = (a2 - a1 % m2 + m2) % m2; long long x, y, g; g = gcdex(m1, m2, x, y); if (a % g) return -1; long long m = m1 / g * m2; assert(x + m2 >= 0); x = a / g * (x + m2) % m2; long long r = (a1 + x * m1) % m; assert(r % m1 == a1 && r % m2 == a2); return r; } long long powmod(long long a, long long p, long long m) { assert(p >= 0); long long r = 1; while (p) { if (p & 1) r = r * a % m; p >>= 1; a = a * a % m; } return r; } bool isprime(long long a) { if (a <= 1) return false; for (long long i = 2; i * i <= a; ++i) { if (a % i == 0) return false; } return true; } long long sqrtup(long long a) { if (!a) return 0; long long x = max(0ll, (long long)sqrt((double)a)); while (x * x >= a) --x; while ((x + 1) * (x + 1) < a) ++x; return x + 1; } long long isqrt(long long a) { if (a <= 0) { assert(!a); return 0; } long long x = (long long)sqrt((double)a); while (sqr(x + 1) <= a) ++x; while (x * x > a) --x; return x; } long long sgn(long long x) { return x < 0 ? -1 : x > 0 ? 1 : 0; } template <class T> ostream &operator<<(ostream &s, const vector<T> &v); template <class A, class B> ostream &operator<<(ostream &s, const pair<A, B> &p); template <class K, class V> ostream &operator<<(ostream &s, const map<K, V> &m); template <class T> ostream &operator<<(ostream &s, const set<T> &m); template <class T, size_t N> ostream &operator<<(ostream &s, const array<T, N> &a); template <class... T> ostream &operator<<(ostream &s, const tuple<T...> &t); template <class T> ostream &operator<<(ostream &s, const vector<T> &v) { s << '['; for (int i = 0; i < (((int)(v).size())); ++i) { if (i) s << ','; s << v[i]; } s << ']'; return s; } template <class A, class B> ostream &operator<<(ostream &s, const pair<A, B> &p) { s << "(" << p.first << "," << p.second << ")"; return s; } template <class K, class V> ostream &operator<<(ostream &s, const map<K, V> &m) { s << "{"; bool f = false; for (const auto &it : m) { if (f) s << ","; f = true; s << it.first << ": " << it.second; } s << "}"; return s; } template <class T> ostream &operator<<(ostream &s, const set<T> &m) { s << "{"; bool f = false; for (const auto &it : m) { if (f) s << ","; f = true; s << it; } s << "}"; return s; } template <class T> ostream &operator<<(ostream &s, const multiset<T> &m) { s << "{"; bool f = false; for (const auto &it : m) { if (f) s << ","; f = true; s << it; } s << "}"; return s; } template <class T, class V, class C> ostream &operator<<(ostream &s, const priority_queue<T, V, C> &q) { auto a = q; s << "{"; bool f = false; while (!a.empty()) { if (f) s << ","; f = true; s << a.top(); a.pop(); } return s << "}"; } template <class T, size_t N> ostream &operator<<(ostream &s, const array<T, N> &a) { s << '['; for (int i = 0; i < (((int)(a).size())); ++i) { if (i) s << ','; s << a[i]; } s << ']'; return s; } template <class T> ostream &operator<<(ostream &s, const deque<T> &a) { s << '['; for (int i = 0; i < (((int)(a).size())); ++i) { if (i) s << ','; s << a[i]; } s << ']'; return s; } template <size_t n, class... T> struct put1 { static ostream &put(ostream &s, const tuple<T...> &t) { s << get<sizeof...(T) - n>(t); if (n > 1) s << ','; return put1<n - 1, T...>::put(s, t); } }; template <class... T> struct put1<0, T...> { static ostream &put(ostream &s, const tuple<T...> &t) { return s; } }; template <class... T> ostream &operator<<(ostream &s, const tuple<T...> &t) { s << "("; put1<sizeof...(T), T...>::put(s, t); s << ")"; return s; } ostream &put3(ostream &s, const char *, bool) { return s; } template <class U, class... T> ostream &put3(ostream &s, const char *f, bool fs, U &&u, T &&...t) { while (*f == ' ') ++f; if (!fs) s << ", "; auto nf = f; int d = 0; while (*nf && (*nf != ',' || d)) { if (*nf == '(') ++d; else if (*nf == ')') --d; ++nf; } auto nf2 = nf; while (nf2 > f && *(nf2 - 1) == ' ') --nf; fs = *f == '"'; if (!fs) { s.write(f, nf2 - f); s << "="; }; s << u; if (fs) s << ' '; if (*nf) ++nf; return put3(s, nf, fs, forward<T>(t)...); } int main(int argc, char **argv) { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); srand(time(0)); int n, m, k; cin >> n >> m >> k; vector<vector<pair<int, int>>> G(n); vector<int> C(n); vector<pair<pair<int, int>, pair<int, int>>> E; for (int i = 0; i < (m); ++i) { int a, b; cin >> a >> b; --a; --b; E.push_back(make_pair(make_pair(a, b), make_pair(((int)(G[a]).size()), ((int)(G[b]).size())))); G[a].push_back(make_pair(b, 1)); G[b].push_back(make_pair(a, 1)); ++C[a]; ++C[b]; } vector<short> W(n); vector<int> Q; int res = n; auto doit = [&] { while (((int)(Q).size())) { --res; int v = Q.back(); Q.pop_back(); for (pair<int, int> p : G[v]) { if (p.second && !W[p.first] && --C[p.first] < k) { W[p.first] = 1; Q.push_back(p.first); } } } }; for (int i = 0; i < (n); ++i) { if (C[i] < k) { W[i] = 1; Q.push_back(i); } } doit(); vector<int> R; reverse((E).begin(), (E).end()); for (auto e : E) { R.push_back(res); if (W[e.first.first] || W[e.first.second]) continue; G[e.first.first][e.second.first].second = 0; G[e.first.second][e.second.second].second = 0; if (--C[e.first.first] < k) { W[e.first.first] = 1; Q.push_back(e.first.first); } if (--C[e.first.second] < k) { W[e.first.second] = 1; Q.push_back(e.first.second); } doit(); } reverse((R).begin(), (R).end()); for (int x : R) cout << x << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 200; int n, m, k, id[N], vis[N], ans[N]; vector<pair<int, int> > g[N]; struct node { int x, y; } e[N]; set<pair<int, int> > s; void change(int x) { if (vis[x]) return; auto it = s.lower_bound(make_pair(id[x], x)); pair<int, int> t = *it; s.erase(it); t.first--; id[t.second]--; s.insert(t); } void update(int now) { while (!s.empty() && (*s.begin()).first < k) { pair<int, int> t = *s.begin(); vis[t.second] = 1; s.erase(s.begin()); for (auto pp : g[t.second]) { if (pp.second > now) break; int v = pp.first; if (vis[v]) continue; auto it = s.lower_bound(make_pair(id[v], v)); t = *it; s.erase(it); id[v]--; t.first--; s.insert(t); } } } bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } int main() { ios::sync_with_stdio(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> e[i].x >> e[i].y; g[e[i].x].push_back(make_pair(e[i].y, i)); g[e[i].y].push_back(make_pair(e[i].x, i)); id[e[i].x]++; id[e[i].y]++; } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end(), cmp); for (int i = 1; i <= n; i++) s.insert(make_pair(id[i], i)); for (int i = m; i >= 1; i--) { update(i); ans[i] = s.size(); if (vis[e[i].x] || vis[e[i].y]) continue; int l = id[e[i].x], r = id[e[i].y]; change(e[i].x); change(e[i].y); } for (int i = 1; i <= m; i++) cout << ans[i] << endl; return 0; }
11
CPP
#include <bits/stdc++.h> int n, m, k, ans; std::set<int> g[200005]; std::vector<std::pair<int, int> > qr; bool deleted[200005]; void check(int v) { if (deleted[v] || g[v].size() >= k) return; deleted[v] = true; ans--; for (auto &i : g[v]) g[i].erase(v); for (auto &i : g[v]) check(i); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> m >> k; ans = n; for (int i = 0; i < (m); ++i) { int a, b; std::cin >> a >> b; a--; b--; qr.push_back({a, b}); g[a].insert(b); g[b].insert(a); } for (int i = 0; i < (n); ++i) check(i); std::reverse(qr.begin(), qr.end()); std::vector<int> ret; ret.push_back(ans); for (int i = 0; i < (m - 1); ++i) { g[qr[i].first].erase(qr[i].second); g[qr[i].second].erase(qr[i].first); check(qr[i].first); check(qr[i].second); ret.push_back(ans); } for (int i = 0; i < (m); ++i) std::cout << ret[m - i - 1] << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int N = 2e5 + 5; int n, m, k; vector<pair<int, int>> edge; set<int> adj[N]; bool gone[N]; vector<int> res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; edge.emplace_back(x, y); adj[x].emplace(y); adj[y].emplace(x); } set<pair<int, int>> deg; for (int i = 1; i <= n; i++) { deg.emplace((int)adj[i].size(), i); } for (int i = m - 1; i >= 0; i--) { while (!deg.empty() && ((deg.begin())->first < k)) { auto cur = *deg.begin(); deg.erase(deg.begin()); int del = cur.second; gone[del] = true; for (auto j : adj[del]) { if (gone[j]) continue; deg.erase(make_pair((int)adj[j].size(), j)); adj[j].erase(del); deg.emplace((int)adj[j].size(), j); } } res.emplace_back((int)deg.size()); auto now = edge[i]; int a = now.first, b = now.second; if (!gone[a]) { deg.erase(make_pair((int)adj[a].size(), a)); adj[a].erase(b); deg.emplace((int)adj[a].size(), a); } if (!gone[b]) { deg.erase(make_pair((int)adj[b].size(), b)); adj[b].erase(a); deg.emplace((int)adj[b].size(), b); } } reverse(res.begin(), res.end()); for (auto i : res) { cout << i << '\n'; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long binpow(long long a, long long n, long long mod) { long long ans = 1; while (n) { if (n & 1) ans = (ans * a) % mod; a = (a * a) % mod; n >>= 1; } return ans; } long long binpow(long long a, long long n) { long long ans = 1; while (n) { if (n & 1) ans *= a; a *= a; n >>= 1; } return ans; } vector<long long> pr; void PrDoX(long long x) { if (pr.size() > 0) return; vector<long long> p(x + 1); for (long long i = 2; i <= x; i++) { if (p[i] == 0) { pr.push_back(i); for (long long j = i + i; j <= x; j += i) p[j] = 1; } } } vector<long long> fact(long long x) { PrDoX(sqrt(1e10)); vector<long long> ans; long long i = 0, c = sqrt(x), n = pr.size(); while (x > 1 and pr[i] <= c and i < n) { if (x % pr[i] == 0) { x /= pr[i]; ans.push_back(pr[i]); c = sqrt(x); } else i++; } if (x != 1) ans.push_back(x); return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long n, m, k; cin >> n >> m >> k; vector<long long> p(n + 1); vector<pair<long long, long long> > v; vector<vector<long long> > g(n + 1); long long ans = 0; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); p[a]++; p[b]++; v.push_back({a, b}); ans += (p[a] == k) + (p[b] == k); } queue<long long> q; for (long long i = 1; i <= n; i++) if (p[i] > 0 and p[i] < k) q.push(i); vector<bool> fl(n + 1); while (!q.empty()) { long long a = q.front(); q.pop(); if (fl[a] == 1) continue; fl[a] = 1; vector<long long> v = g[a]; long long n = v.size(); for (long long i = 0; i < n; i++) { long long b = v[i]; if (fl[b] == 1) continue; p[b]--; if (p[b] == k - 1) ans--, q.push(b); } } vector<long long> an; an.push_back(ans); for (long long i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); for (long long i = m - 1; i > 0; i--) { long long a = v[i].first, b = v[i].second; if (fl[a] or fl[b] == 1) { an.push_back(ans); continue; } p[a]--; p[b]--; g[a].erase(lower_bound(g[a].begin(), g[a].end(), b)); g[b].erase(lower_bound(g[b].begin(), g[b].end(), a)); queue<long long> q; bool F = 0; if (p[a] == k - 1) q.push(a), ans--; if (p[b] == k - 1) q.push(b), ans--; while (!q.empty()) { long long a0 = q.front(); q.pop(); if (fl[a0] == 1) continue; fl[a0] = 1; vector<long long> v = g[a0]; long long n = v.size(); for (long long i = 0; i < n; i++) { long long b0 = v[i]; if ((a0 == a and b0 == b) or (a0 == b and b0 == a)) continue; if (fl[b0] == 1) continue; p[b0]--; if (p[b0] == k - 1) ans--, q.push(b0); } } an.push_back(ans); } for (long long i = m - 1; i >= 0; i--) cout << an[i] << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; pair<int, int> edge[200000 + 324]; vector<int> adj[200000 + 324]; set<int> degree[200000 + 324]; set<int> friends; int ans[200000 + 324]; int k; void rem(int x) { set<int> elem_to_remove; for (auto bc : degree[x]) { elem_to_remove.insert(bc); degree[bc].erase(x); } degree[x].erase(degree[x].begin(), degree[x].end()); friends.erase(x); for (auto bc : elem_to_remove) { if (degree[bc].size() < k) { rem(bc); } } } int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> edge[i].first >> edge[i].second; degree[edge[i].first].insert(edge[i].second); degree[edge[i].second].insert(edge[i].first); } for (int i = 1; i <= n; i++) { if (degree[i].size() >= k) { friends.insert(i); } else { friends.insert(i); rem(i); } } for (int i = m - 1; i >= 0; i--) { ans[i] = friends.size(); degree[edge[i].first].erase(edge[i].second); degree[edge[i].second].erase(edge[i].first); if (degree[edge[i].first].size() < k) { rem(edge[i].first); } if (degree[edge[i].second].size() < k) { rem(edge[i].second); } } for (int i = 0; i < m; i++) cout << ans[i] << " "; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxN = 1e6 + 10; priority_queue<int> P; priority_queue<int, vector<int>, greater<int> > mP; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } int mod = 1000 * 1000 * 1000 + 7; long long ksm(int a, int b) { if (b == 0) return 1LL; long long ret = 1, pp = a; while (b) { if (b % 2 == 1) { ret *= pp; ret %= mod; } b /= 2; pp = pp * pp % mod; } return ret; } int bit[maxN]; int lowbit(int x) { return x & (-x); } void update(int i, int v) { while (i < maxN) { bit[i] += v; i += lowbit(i); } } int getsum(int i) { int x = 0; while (i > 0) { x += bit[i]; i -= lowbit(i); } return x; } void change(int a, int b, int k) { update(a, k); update(b + 1, -k); } int father[maxN]; void init(int n) { for (int i = 1; i <= n; i++) father[i] = i; return; } int find(int v) { if (father[v] == v) return v; else return father[v] = find(father[v]); } void merge(int v, int u) { int t1 = find(v); int t2 = find(u); father[t2] = t1; return; } long long hashing(string s) { long long ans[2] = {1, 1}; int p[2] = {402653189, 1610612741}; for (int j = 0; j < 2; j++) { for (int i = 0; i < s.size(); i++) { ans[j] = ((ans[j] * 257) + s[i] - 'a') % p[j]; } } return ans[0] * 2e10 + ans[1]; } int e[maxN][2]; set<int> edge[maxN]; int cnt; int n, m, k; void del(queue<int>& q) { while (!q.empty()) { int i = q.front(); q.pop(); for (auto v : edge[i]) { if (edge[v].size() == k) { q.push(v); cnt--; } edge[v].erase(i); } edge[i].clear(); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> m >> k; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; e[i][0] = x; e[i][1] = y; edge[x].insert(y); edge[y].insert(x); } cnt = n; queue<int> q; for (int i = 1; i <= n; i++) { if (edge[i].size() < k) { q.push(i); cnt--; } } del(q); vector<int> ans; for (int i = m; i >= 1; i--) { ans.push_back(cnt); int x = e[i][0], y = e[i][1]; queue<int> q; if (edge[x].find(y) != edge[x].end() && edge[x].size() == k) { q.push(x); edge[x].erase(y); cnt--; } edge[x].erase(y); if (edge[y].find(x) != edge[y].end() && edge[y].size() == k) { q.push(y); edge[y].erase(x); cnt--; } edge[y].erase(x); del(q); } reverse(ans.begin(), ans.end()); for (auto c : ans) cout << c << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; set<pair<int, int>> S; vector<int> A; vector<pair<int, int>> R; vector<vector<int>> G; set<pair<int, int>> B; int main() { cin >> n >> m >> k; R.assign(m, {0, 0}); A.assign(n, 0); G.assign(n, vector<int>(0, 0)); for (auto &i : R) { cin >> i.first >> i.second; i.first--; i.second--; G[i.first].push_back(i.second); G[i.second].push_back(i.first); A[i.first]++; A[i.second]++; } for (int i = 0; i < n; i++) { S.insert({A[i], i}); } while (S.size() && (*S.begin()).first < k) { int x = (*S.begin()).second; S.erase(S.begin()); for (int y : G[x]) { if (A[y] == -1) continue; S.erase(S.find({A[y], y})); A[y]--; S.insert({A[y], y}); } A[x] = -1; } vector<int> Ans(m, 0); for (int i = m - 1; i >= 0; i--) { Ans[i] = S.size(); int x, y; x = R[i].first; y = R[i].second; B.insert({x, y}); B.insert({y, x}); if (A[x] != -1 && A[y] != -1) { S.erase(S.find({A[x], x})); A[x]--; S.insert({A[x], x}); S.erase(S.find({A[y], y})); A[y]--; S.insert({A[y], y}); } while (!S.empty() && (*S.begin()).first < k) { int X = (*S.begin()).second; S.erase(S.begin()); for (int Y : G[X]) { if (A[Y] == -1 || B.count({X, Y})) continue; S.erase(S.find({A[Y], Y})); A[Y]--; S.insert({A[Y], Y}); } A[X] = -1; } } for (int i : Ans) { cout << i << "\n"; } }
11
CPP
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n, m, k; cin >> n >> m >> k; vector<vector<int> > edges(m, vector<int>(2)); vector<vector<int> > graph(n + 1); vector<int> degrees(n + 1, 0); for (int i = 0; i < m; ++i) { scanf("%d %d", &edges[i][0], &edges[i][1]); degrees[edges[i][0]]++; degrees[edges[i][1]]++; graph[edges[i][0]].push_back(edges[i][1]); graph[edges[i][1]].push_back(edges[i][0]); } map<pair<int, int>, bool> sorted; for (int i = 0; i < n; ++i) sorted.insert(make_pair(make_pair(degrees[i + 1], i + 1), 1)); vector<int> ans(m); for (int i = m - 1; i >= 0; --i) { while ((!sorted.empty()) and sorted.begin()->first.first < k) { int temp = sorted.begin()->first.second; sorted.erase(sorted.begin()); degrees[temp] = 0; for (int i = 0; i < graph[temp].size(); ++i) { auto it = sorted.find(make_pair(degrees[graph[temp][i]], graph[temp][i])); degrees[graph[temp][i]]--; if (it != sorted.end()) { sorted.erase(it); if (degrees[graph[temp][i]] >= 0) sorted.insert(make_pair( make_pair(degrees[graph[temp][i]], graph[temp][i]), 1)); } } } ans[i] = sorted.size(); graph[edges[i][0]].pop_back(); graph[edges[i][1]].pop_back(); if (degrees[edges[i][0]] >= k and degrees[edges[i][1]] >= k) { auto it1 = sorted.find(make_pair(degrees[edges[i][0]], edges[i][0])); auto it2 = sorted.find(make_pair(degrees[edges[i][1]], edges[i][1])); degrees[edges[i][0]]--; degrees[edges[i][1]]--; if (it1 != sorted.end()) { sorted.erase(it1); sorted.insert( make_pair(make_pair(degrees[edges[i][0]], edges[i][0]), 1)); } if (it2 != sorted.end()) { sorted.erase(it2); sorted.insert( make_pair(make_pair(degrees[edges[i][1]], edges[i][1]), 1)); } } } for (int i = 0; i < m; ++i) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k, x[200001], y[200001], ans[200001]; set<int> d[200001], s; queue<int> q; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) scanf("%d%d", &x[i], &y[i]), d[x[i]].insert(y[i]), d[y[i]].insert(x[i]); for (int i = 1; i <= n; i++) if (d[i].size() < k) q.push(i); while (!q.empty()) { int cur = q.front(); q.pop(); for (auto nxt : d[cur]) { d[nxt].erase(cur); if (d[nxt].size() < k) q.push(nxt); } d[cur].clear(); } for (int i = 1; i <= n; i++) if (d[i].size() >= k) s.insert(i); for (int i = m; i >= 1; i--) { ans[i] = s.size(); if (d[x[i]].count(y[i])) { d[x[i]].erase(y[i]); d[y[i]].erase(x[i]); if (d[x[i]].size() < k) q.push(x[i]); if (d[y[i]].size() < k) q.push(y[i]); } while (!q.empty()) { int cur = q.front(); q.pop(); for (auto nxt : d[cur]) { d[nxt].erase(cur); if (d[nxt].size() < k) q.push(nxt); } d[cur].clear(), s.erase(cur); } } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); }
11
CPP
#include <bits/stdc++.h> using namespace std; void decreaseKey(set<pair<int, int> > &pairs, map<int, set<int> > &data, int decKey, int remVert) { pair<int, int> removePair; removePair.first = data[decKey].size(); removePair.second = decKey; pairs.erase(removePair); data[decKey].erase(remVert); pair<int, int> newPair; newPair.first = data[decKey].size(); newPair.second = decKey; if (newPair.first != 0) { pairs.insert(newPair); } } void deleteUnusedPairs(set<pair<int, int> > &pairs, map<int, set<int> > &data, pair<int, int> &cheker) { vector<int> rem; rem.clear(); vector<pair<int, int> > delet; delet.clear(); for (set<pair<int, int> >::iterator iter = pairs.begin(); iter != pairs.lower_bound(cheker); iter++) { pair<int, int> curr = *iter; rem.push_back(curr.second); delet.push_back(*iter); } for (int i = 0; i < delet.size(); i++) { pairs.erase(delet[i]); } for (int j = 0; j < rem.size(); j++) { for (set<int>::iterator iter = data[rem[j]].begin(); iter != data[rem[j]].end(); iter++) { decreaseKey(pairs, data, *iter, rem[j]); } data.erase(rem[j]); } } int main() { int n, m, k; cin >> n >> m >> k; map<int, set<int> > data; set<pair<int, int> > pairs; vector<int> pairsArr; vector<int> result; int x, y; for (int i = 1; i <= m; i++) { cin >> x >> y; data[x].insert(y); data[y].insert(x); pairsArr.push_back(x); pairsArr.push_back(y); } for (int i = 1; i <= n; i++) { if (data.count(i) > 0) { pair<int, int> p; p.first = data[i].size(); p.second = i; pairs.insert(p); } } for (int i = 1; i <= m; i++) { pair<int, int> cheker; cheker.first = k; cheker.second = -20; while (pairs.size() != 0 && (*(pairs.begin())).first < k) { deleteUnusedPairs(pairs, data, cheker); } result.push_back(pairs.size()); int currX = pairsArr.back(); pairsArr.pop_back(); int currY = pairsArr.back(); pairsArr.pop_back(); decreaseKey(pairs, data, currX, currY); decreaseKey(pairs, data, currY, currX); } for (int i = result.size() - 1; i >= 0; i--) { printf("%d\n", result[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 200100; int n, m, k; vector<pair<int, int> > veze; vector<pair<int, int> > graf[MAXN]; void load() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; veze.push_back(pair<int, int>(a, b)); graf[a].push_back(pair<int, int>(b, i)); graf[b].push_back(pair<int, int>(a, i)); } } int deg[MAXN]; set<pair<int, int> > S; void printSet() { for (auto it = S.begin(); it != S.end(); it++) { printf("%d : %d\n", it->second, it->first); } printf("\n"); } void solve() { for (int i = 0; i < m; i++) { deg[veze[i].first]++; deg[veze[i].second]++; } for (int i = 0; i < n; i++) S.insert(pair<int, int>(deg[i], i)); while (!S.empty() && S.begin()->first < k) { int v = S.begin()->second; S.erase(pair<int, int>(deg[v], v)); for (pair<int, int> p : graf[v]) { int u = p.first; if (S.find(pair<int, int>(deg[u], u)) == S.end()) continue; S.erase(pair<int, int>(deg[u], u)); deg[u]--; S.insert(pair<int, int>(deg[u], u)); } } vector<int> sol; for (int i = m - 1; i >= 0; i--) { sol.push_back(S.size()); int a = veze[i].first; int b = veze[i].second; if (S.find(pair<int, int>(deg[a], a)) != S.end() && S.find(pair<int, int>(deg[b], b)) != S.end()) { S.erase(pair<int, int>(deg[a], a)); S.erase(pair<int, int>(deg[b], b)); deg[a]--; deg[b]--; S.insert(pair<int, int>(deg[a], a)); S.insert(pair<int, int>(deg[b], b)); while (!S.empty() && S.begin()->first < k) { int v = S.begin()->second; S.erase(pair<int, int>(deg[v], v)); for (pair<int, int> p : graf[v]) { if (p.second >= i) continue; int u = p.first; if (S.find(pair<int, int>(deg[u], u)) == S.end()) continue; S.erase(pair<int, int>(deg[u], u)); deg[u]--; S.insert(pair<int, int>(deg[u], u)); } } } } for (int i = m - 1; i >= 0; i--) printf("%d\n", sol[i]); } int main() { load(); solve(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; set<int> tt[200005]; set<int> ans; vector<pair<int, int>> ve; int n, m, k, x, y; int temp[200005]; void del(int x) { if (tt[x].size() < k && ans.find(x) != ans.end()) { ans.erase(x); for (auto i : tt[x]) { tt[i].erase(x); del(i); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); tt[x].insert(y); tt[y].insert(x); ve.push_back(make_pair(x, y)); } for (int i = 1; i <= n; i++) { ans.insert(i); } for (int i = 1; i <= n; i++) { del(i); } for (int i = m - 1; i >= 0; i--) { temp[i] = ans.size(); tt[ve[i].first].erase(ve[i].second); tt[ve[i].second].erase(ve[i].first); del(ve[i].first); del(ve[i].second); } for (int i = 0; i < m; i++) { printf("%d\n", temp[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k, x, y, deg[200010]; set<pair<int, int>> s; pair<int, int> p[200010]; vector<int> rez; set<int> v[200010]; map<bool, int> mp[200010]; void rem() { while (!s.empty() and s.begin()->first < k) { x = s.begin()->second; for (auto it : v[x]) { s.erase({deg[it], it}); --deg[it]; v[it].erase(x); if (deg[it] > 0) s.insert({deg[it], it}); } s.erase({deg[x], x}); deg[x] = 0; v[x].clear(); } } int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> p[i].first >> p[i].second; deg[p[i].first]++, deg[p[i].second]++; v[p[i].first].insert(p[i].second), v[p[i].second].insert(p[i].first); } for (int i = 1; i <= n; i++) s.insert({deg[i], i}); for (int i = m - 1; i >= 0; i--) { rem(); rez.push_back((int)s.size()); x = p[i].first, y = p[i].second; if (deg[x] == 0 or deg[y] == 0) continue; v[x].erase(y), v[y].erase(x); s.erase({deg[x], x}), s.erase({deg[y], y}); --deg[x], --deg[y]; if (deg[x] > 0) s.insert({deg[x], x}); if (deg[y] > 0) s.insert({deg[y], y}); } reverse(rez.begin(), rez.end()); for (auto ans : rez) cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <class X, class Y> void amax(X& x, const Y& y) { if (x < y) x = y; } template <class X, class Y> void amin(X& x, const Y& y) { if (x > y) x = y; } const int INF = 1e9 + 10; const long long INFL = (long long)1e18 + 10; const int MAX = 2e5 + 10; int n, m, k; int cnt[MAX]; pair<int, int> ed[MAX]; bool mark[MAX], removed[MAX]; vector<pair<int, int> > es[MAX]; stack<int> st; void invalid_push(int v) { if (!mark[v] && cnt[v] < k) { mark[v] = true; st.push(v); } } void process() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> ed[i].first >> ed[i].second; cnt[ed[i].first]++; cnt[ed[i].second]++; es[ed[i].first].push_back(make_pair(ed[i].second, i)); es[ed[i].second].push_back(make_pair(ed[i].first, i)); } for (int i = 1; i <= n; i++) invalid_push(i); vector<int> ans; int res = n; for (int i = m; i >= 1; i--) { while (!st.empty()) { int u = st.top(); st.pop(); res--; for (auto e : es[u]) if (!mark[e.first] && !removed[e.second]) { removed[e.second] = true; cnt[e.first]--; invalid_push(e.first); } } ans.push_back(res); if (!removed[i]) { removed[i] = true; cnt[ed[i].first]--; cnt[ed[i].second]--; invalid_push(ed[i].first); invalid_push(ed[i].second); } } for (int i = int(ans.size()) - 1; i >= 0; i--) cout << ans[i] << '\n'; } int main() { ios_base::sync_with_stdio(false); process(); }
11
CPP
#include <bits/stdc++.h> using namespace std; set<long long> V[400010]; long long n, m, k, D[400010], A[400010], B[400010], ans; signed main() { scanf("%lld%lld%lld", &n, &m, &k); ans = n; for (long long i = 1; i <= m; i++) { long long a, b; scanf("%lld%lld", &a, &b); A[i] = a; B[i] = b; D[a]++; D[b]++; V[a].insert(b); V[b].insert(a); } vector<long long> Tp, Ans; Ans.clear(); Tp.clear(); for (long long i = 1; i <= n; i++) if (D[i] < k) Tp.push_back(i); for (long long i = m; i >= 1; i--) { for (long long j = 0; j < Tp.size(); j++) { long long x = Tp[j]; if (D[x] <= -100000) continue; else ans--; for (set<long long>::iterator it = V[x].begin(); it != V[x].end(); it++) { V[*it].erase(x); if (--D[*it] < k) Tp.push_back(*it); } V[x].clear(); D[x] = -100000; } Ans.push_back(ans); Tp.clear(); if (!V[A[i]].count(B[i])) continue; D[A[i]]--; D[B[i]]--; V[A[i]].erase(B[i]); V[B[i]].erase(A[i]); if (D[A[i]] < k) Tp.push_back(A[i]); if (D[B[i]] < k) Tp.push_back(B[i]); } for (long long i = (long long)Ans.size() - 1; i >= 0; i--) printf("%lld\n", Ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int ind[N], ans[N]; set<int> vec[N]; int U[N], V[N]; bool vis[N]; int n, m, K; int sum = 0; queue<int> q; void Top() { while (!q.empty()) { ++sum; int u = q.front(); q.pop(); ind[u] = 0; set<int>::iterator it; for (it = vec[u].begin(); it != vec[u].end();) { int v = *it; if (vis[v]) { it++; continue; } vec[u].erase(it++); vec[v].erase(u); ind[v]--; if (!vis[v] && ind[v] < K) { vis[v] = 1; q.push(v); } } } } int main() { scanf("%d %d %d", &n, &m, &K); for (int i = 0; i < m; ++i) { scanf("%d %d", &U[i], &V[i]); int u = U[i], v = V[i]; vec[u].insert(v); vec[v].insert(u); ind[u]++; ind[v]++; } for (int i = 1; i < n + 1; ++i) { if (ind[i] < K) { vis[i] = 1; q.push(i); } } Top(); for (int i = m - 1; i >= 0; --i) { ans[i] = n - sum; int u = U[i], v = V[i]; if (vec[u].count(v)) ind[v]--, vec[u].erase(v); if (vec[v].count(u)) ind[u]--, vec[v].erase(u); if (!vis[v] && ind[v] < K) vis[v] = 1, q.push(v); if (!vis[u] && ind[u] < K) vis[u] = 1, q.push(u); Top(); } for (int i = 0; i < m; ++i) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> template <class C, class E> inline bool contains(const C& container, const E& element) { return container.find(element) != container.end(); } template <class T> inline void checkmin(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T& a, T b) { if (b > a) a = b; } using namespace std; vector<pair<int, int> > fr; vector<set<int> > gr; int n, m, k; vector<bool> inTrip; int inTripCount; void Remove(int a, int b) { gr[a].erase(b); gr[b].erase(a); } int Deg(int a) { return gr[a].size(); } vector<bool> InQueue; void RemAll(queue<int>& toRemove) { while (!toRemove.empty()) { int iRem = toRemove.front(); inTrip[iRem] = false; InQueue[iRem] = false; --inTripCount; toRemove.pop(); for (int br : gr[iRem]) { gr[br].erase(iRem); if (Deg(br) < k && !InQueue[br]) { InQueue[br] = true; toRemove.push(br); } } gr[iRem].clear(); } } void Process(pair<int, int> p) { if (!contains(gr[p.first], p.second)) return; Remove(p.first, p.second); queue<int> toRemove; if (inTrip[p.first] && Deg(p.first) < k) { InQueue[p.first] = true; toRemove.push(p.first); } if (inTrip[p.second] && Deg(p.second) < k) { InQueue[p.second] = true; toRemove.push(p.second); } RemAll(toRemove); } void Init() { queue<int> toRemove; for (int i = (0), _b = ((n)-1); i <= _b; i++) { if (Deg(i) < k) { InQueue[i] = true; toRemove.push(i); } } RemAll(toRemove); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << std::setprecision(15); cout << std::fixed; cin >> n >> m >> k; fr.resize(m); gr.resize(n); InQueue.resize(n); inTrip.resize(n, true); inTripCount = n; for (int i = (0), _b = ((m)-1); i <= _b; i++) { int a, b; cin >> a >> b; --a; --b; fr[i] = make_pair(a, b); gr[a].insert(b); gr[b].insert(a); } Init(); vector<int> res; for (int i = (m - 1), _b = (0); i >= _b; i--) { res.push_back(inTripCount); if (i != 0) Process(fr[i]); } for (int i = (m - 1), _b = (0); i >= _b; i--) { cout << res[i] << endl; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int deg[N], del[N], u[N], v[N], aa[N], edg[N]; vector<pair<int, int> > adj[N]; int remove(queue<int> &q, int k) { int ans = 0; while (!q.empty()) { int x = q.front(); q.pop(); if (!del[x]) del[x] = 1, ans--; for (auto z : adj[x]) { int idx = z.second, y = z.first; if (edg[idx]) continue; edg[idx] = 1; deg[x]--; deg[y]--; if (deg[y] < k && !del[y]) { q.push(y); } } } return ans; } int main() { ios_base ::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> u[i] >> v[i]; adj[u[i]].push_back({v[i], i}); adj[v[i]].push_back({u[i], i}); deg[u[i]]++; deg[v[i]]++; } int ans = n; queue<int> q; for (int i = 1; i <= n; i++) if (deg[i] < k) q.push(i); ans += remove(q, k); for (int i = m; i >= 1; i--) { aa[i] = ans; if (edg[i]) continue; deg[u[i]]--; deg[v[i]]--; edg[i] = 1; if (!del[u[i]] && deg[u[i]] < k) q.push(u[i]); if (!del[v[i]] && deg[v[i]] < k) q.push(v[i]); ans += remove(q, k); } for (int i = 1; i <= m; i++) cout << aa[i] << "\n"; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <class T, class U> bool cmax(T& a, const U& b) { return a < b ? a = b, 1 : 0; } template <class T, class U> bool cmin(T& a, const U& b) { return b < a ? a = b, 1 : 0; } void _BG(const char* s) { cerr << s << endl; }; template <class T, class... TT> void _BG(const char* s, T a, TT... b) { for (int c = 0; *s && (c || *s != ','); ++s) { cerr << *s; for (char x : "([{") c += *s == x; for (char x : ")]}") c -= *s == x; } cerr << " = " << a; if (sizeof...(b)) { cerr << ", "; ++s; } _BG(s, b...); } bool RD() { return 1; } bool RD(char& a) { return scanf(" %c", &a) == 1; } bool RD(char* a) { return scanf("%s", a) == 1; } bool RD(double& a) { return scanf("%lf", &a) == 1; } bool RD(int& a) { return scanf("%d", &a) == 1; } bool RD(long long& a) { return scanf("%lld", &a) == 1; } template <class T, class... TT> bool RD(T& a, TT&... b) { return RD(a) && RD(b...); } void PT(const char& a) { putchar(a); } void PT(char const* const& a) { fputs(a, stdout); } void PT(const double& a) { printf("%.16f", a); } void PT(const int& a) { printf("%d", a); } void PT(const long long& a) { printf("%lld", a); } template <char s = ' ', char e = '\n'> void PL() { if (e) PT(e); } template <char s = ' ', char e = '\n', class T, class... TT> void PL(const T& a, const TT&... b) { PT(a); if (sizeof...(b) && s) PT(s); PL<s, e>(b...); } const int N = 212345; int n, m, k; set<int> g[N]; pair<int, int> e[N]; bool die[N]; int ans, QAQ[N]; vector<int> rm; void chk(int v) { if (((int)(g[v]).size()) < k && !die[v]) { rm.push_back(v); die[v] = 1; --ans; } } void del(int u, int v) { g[u].erase(v); g[v].erase(u); chk(u); chk(v); } int main() { RD(n, m, k); for (int i(0); i < (m); ++i) { int u, v; RD(u, v); --u, --v; e[i] = {u, v}; g[u].insert(v); g[v].insert(u); } ans = n; for (int i(0); i < (n); ++i) chk(i); for (int i((m)-1); i >= (0); --i) { while (((int)(rm).size())) { int u = ((rm).back()); (rm).pop_back(); while (((int)(g[u]).size())) del(u, *begin(g[u])); } QAQ[i] = ans; del(e[i].first, e[i].second); } for (int i(0); i < (m); ++i) PL(QAQ[i]); }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxN = 2e5 + 11; typedef int i_N[maxN]; int N, M, K; set<int> adj[maxN]; i_N deg, mark; int ans; pair<int, int> edge[maxN]; void dfs(int u) { if (mark[u]++) return; ans--; for (set<int>::iterator it = adj[u].begin(); it != adj[u].end(); it++) { int v = *it; deg[v]--; adj[v].erase(u); if (deg[v] < K) dfs(v); } adj[u].clear(); } int main() { ios_base::sync_with_stdio(0); cin >> N >> M >> K; for (int i = 1; i <= M; i++) { int u, v; cin >> u >> v; adj[u].insert(v); adj[v].insert(u); deg[u]++; deg[v]++; edge[i] = pair<int, int>(u, v); } ans = N; for (int i = 1; i <= N; i++) if (deg[i] < K) dfs(i); vector<int> v; for (int i = M; i; i--) { v.push_back(ans); int u = edge[i].first, v = edge[i].second; if (adj[u].find(v) != adj[u].end()) { deg[u]--; deg[v]--; adj[u].erase(v); adj[v].erase(u); } if (deg[u] < K) dfs(u); if (deg[v] < K) dfs(v); } reverse(v.begin(), v.end()); for (int i = 0; i < M; i++) cout << v[i] << '\n'; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m, k; set<int> g[N]; pair<int, int> p[N]; set<pair<int, int>> st; bool inSet[N]; int ans[N], deg[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; g[u].insert(v); g[v].insert(u); deg[u]++, deg[v]++; p[i] = {u, v}; } for (int i = 1; i <= n; i++) { st.insert({deg[i], i}); inSet[i] = true; } for (int i = m; i >= 1; i--) { while (!st.empty() && st.begin()->first < k) { int x = st.begin()->second; for (auto y : g[x]) { if (inSet[y]) { st.erase({deg[y], y}); deg[y]--; st.insert({deg[y], y}); } } inSet[x] = false; st.erase({deg[x], x}); } ans[i] = st.size(); int u = p[i].first; int v = p[i].second; g[u].erase(v); g[v].erase(u); if (!inSet[u] || !inSet[v]) continue; st.erase({deg[u], u}); deg[u]--; st.insert({deg[u], u}); st.erase({deg[v], v}); deg[v]--; st.insert({deg[v], v}); } for (int i = 1; i <= m; i++) { cout << ans[i] << '\n'; } return 0; }
11
CPP
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC optimize("-ffloat-store") using namespace std; const int MN = 200010; set<int> e[MN]; int from[MN], to[MN], deg[MN], vis[MN], res[MN]; int n, m, k, cnt; void deletev(int v) { if (deg[v] >= k || vis[v]) return; queue<int> q; q.push(v); vis[v] = 1; --cnt; while (!q.empty()) { int top = q.front(); q.pop(); for (auto& c : e[top]) { --deg[c]; if (deg[c] < k && !vis[c]) { q.push(c); vis[c] = 1; --cnt; } } } } struct E { void solve(std::istream& cin, std::ostream& cout) { cin >> n >> m >> k; memset(deg, (0), sizeof(deg)); memset(from, (0), sizeof(from)); memset(to, (0), sizeof(to)); memset(deg, (0), sizeof(deg)); memset(vis, (0), sizeof(vis)); memset(res, (0), sizeof(res)); e->clear(); for (int i = (0); i < (m); ++i) { cin >> from[i] >> to[i]; --from[i], --to[i]; ++deg[from[i]], ++deg[to[i]]; e[from[i]].insert(to[i]); e[to[i]].insert(from[i]); } cnt = n; for (int i = (0); i < (n); ++i) { deletev(i); } res[m] = cnt; for (int i = (m - 1); i >= (0); --i) { if (!vis[from[i]]) --deg[to[i]]; if (!vis[to[i]]) --deg[from[i]]; e[from[i]].erase(to[i]); e[to[i]].erase(from[i]); deletev(from[i]); deletev(to[i]); res[i] = cnt; } for (int i = (1); i <= (m); ++i) cout << res[i] << "\n"; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); E solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int INF = (int)1e9; long long INFINF = (long long)1e18 + 10; const long double PI = 3.14159265358979323846; long long powermodm(long long x, long long n, long long mod) { long long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % mod; x = (x * x) % mod; n = n / 2; } return result; } long long GCD(long long, long long); long long LCM(long long, long long); long long power(int, int); long long choose(long long, long long); int ones(long long); void extendedEuclid(long long, long long); long long MMI(long long, long long); void fastscan(int &); set<int> adj[200005]; vector<int> degree(200005); vector<int> a1(200005); vector<int> b1(200005); vector<pair<int, int> > pair1; set<pair<int, int> > set1; vector<int> vis(200005); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<int> ans; for (long long i = 0; i < m; i++) { cin >> a1[i] >> b1[i]; degree[a1[i]]++; degree[b1[i]]++; adj[a1[i]].insert(b1[i]); adj[b1[i]].insert(a1[i]); } for (long long i = 0; i < n; i++) { set1.insert({degree[i + 1], i + 1}); } for (int i = m - 1; i >= 0; i--) { while (!set1.empty()) { pair<int, int> temppair; temppair = *set1.begin(); if (temppair.first < k) { int temp = temppair.second; vis[temp] = 1; degree[temp]--; set1.erase(temppair); for (auto k = adj[temp].begin(); k != adj[temp].end(); k++) { set1.erase({degree[*k], *k}); degree[*k]--; if (vis[*k] == 0) { set1.insert({degree[*k], *k}); } } adj[temp].clear(); } else { break; } } ans.push_back(set1.size()); set1.erase({degree[a1[i]], a1[i]}); set1.erase({degree[b1[i]], b1[i]}); adj[a1[i]].erase(b1[i]); adj[b1[i]].erase(a1[i]); if (vis[a1[i]] == 0) { degree[b1[i]]--; } if (vis[b1[i]] == 0) { degree[a1[i]]--; } if (vis[a1[i]] == 0) { set1.insert({degree[a1[i]], a1[i]}); } if (vis[b1[i]] == 0) { set1.insert({degree[b1[i]], b1[i]}); } } reverse(ans.begin(), ans.end()); for (long long i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } return 0; } long long GCD(long long a, long long b) { if (b == 0) return a; else return GCD(b, a % b); } long long LCM(long long a, long long b) { return (max(a, b) / GCD(a, b)) * min(a, b); } long long power(int a, int n) { unsigned long long int result = 1, x = a; while (n > 0) { if (n % 2 == 1) result = result * x; x = x * x; n = n / 2; } return result; } long long choose(long long n, long long k) { if (k == 0) return 1; return (n * choose(n - 1, k - 1)) / k; } int ones(long long n) { int c = 0; while (n) { n = n & (n - 1); c++; } return c; } long long d, x, y; void extendedEuclid(long long A, long long B) { if (B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A % B); int temp = x; x = y; y = temp - (A / B) * y; } } long long MMI(long long a, long long p) { extendedEuclid(a, p); if (d == 1 && p != 1) return ((x % p) + p) % p; else return -1; } void fastscan(int &number) { bool negative = false; register int c; number = 0; c = getchar(); if (c == '-') { negative = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48; if (negative) number *= -1; }
11
CPP
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int n; vector<set<int> > g(200000); vector<int> used(200000); int main() { int n, m, k; cin >> n >> m >> k; vector<int> x(m); vector<int> y(m); for (int i = 0; i < m; i++) { cin >> x[i] >> y[i]; x[i]--; y[i]--; g[x[i]].insert(y[i]); g[y[i]].insert(x[i]); } set<pair<int, int> > s; vector<int> v(n); for (int i = 0; i < n; i++) { s.insert(make_pair(g[i].size(), i)); v[i] = g[i].size(); } while (s.size()) { int a = s.begin()->first; int b = s.begin()->second; if (a < k) { s.erase(make_pair(a, b)); v[b] = 0; for (auto i = g[b].begin(); i != g[b].end(); i++) if (v[*i] >= k) { s.erase(make_pair(v[*i], *i)); v[*i]--; s.insert(make_pair(v[*i], *i)); } } else break; } vector<int> ans(m); ans[m - 1] = s.size(); for (int i = m - 1; i >= 1; i--) { int fl = 0; if (s.find(make_pair(v[x[i]], x[i])) != s.end()) fl++; if (s.find(make_pair(v[y[i]], y[i])) != s.end()) fl++; if (fl != 2) ans[i - 1] = ans[i]; else { s.erase(make_pair(v[x[i]], x[i])); s.erase(make_pair(v[y[i]], y[i])); v[x[i]]--; v[y[i]]--; s.insert(make_pair(v[x[i]], x[i])); s.insert(make_pair(v[y[i]], y[i])); g[x[i]].erase(y[i]); g[y[i]].erase(x[i]); while (s.size()) { int a = s.begin()->first; int b = s.begin()->second; if (a < k) { s.erase(make_pair(a, b)); v[b] = 0; for (auto i = g[b].begin(); i != g[b].end(); i++) if (v[*i] >= k) { s.erase(make_pair(v[*i], *i)); v[*i]--; s.insert(make_pair(v[*i], *i)); } } else break; } ans[i - 1] = s.size(); } } for (int i = 0; i < m; i++) cout << ans[i] << " "; return 0; }
11
CPP
#include <bits/stdc++.h> inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c <= '9' && c >= '0') x = x * 10 + c - '0', c = getchar(); return x * f; } void print(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } const int maxn = 5000007; int n, m, k; int d[maxn]; bool del[maxn]; using namespace std; map<pair<int, int>, int> mp; struct node { int v, next; } edge[maxn]; int head[maxn], num = 0; inline void add_edge(int u, int v) { edge[++num].v = v; edge[num].next = head[u]; head[u] = num; } int ans; int u[maxn], v[maxn]; int Ans[maxn]; queue<int> q; void solve(int x) { if (d[x] >= k || del[x]) return; del[x] = 1; q.push(x); ans--; while (!q.empty()) { int U = q.front(); q.pop(); for (int i = head[U]; i; i = edge[i].next) { int V = edge[i].v; if (del[V]) continue; if (mp.count(make_pair(U, V)) == 0) d[V]--; if (d[V] < k) { del[V] = true; ans--; q.push(V); } } } } int main() { n = read(), m = read(); k = read(); for (int i = 1; i <= m; ++i) { u[i] = read(), v[i] = read(); add_edge(u[i], v[i]); add_edge(v[i], u[i]); d[v[i]]++; d[u[i]]++; } ans = n; for (int i = 1; i <= n; ++i) solve(i); mp.clear(); for (int i = m; i >= 1; --i) { Ans[i] = ans; if (!del[u[i]]) d[v[i]]--; if (!del[v[i]]) d[u[i]]--; mp[pair<int, int>(u[i], v[i])] = 1; mp[pair<int, int>(v[i], u[i])] = 1; solve(u[i]); solve(v[i]); } for (int i = 1; i <= m; ++i) print(Ans[i]), putchar('\n'); }
11
CPP
#include <bits/stdc++.h> using namespace std; template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << " = " << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << " = " << h << ", "; _dbg(sdbg + 1, a...); } const int maxn = (1e6) + 7; const int maxk = 20; const int inf = (1e9) + 7; const long long LLinf = (1e18) + 7; const long double eps = 1e-9; const long long mod = 1e9 + 7; queue<int> q; bool vis[maxn]; int res; int n, m, k; int odp[maxn]; set<int> wek[maxn]; pair<int, int> tab[maxn]; void rob() { while (((int)(q).size())) { int a = q.front(); q.pop(); if (vis[a] == 0) continue; vis[a] = 0; res--; for (auto s : wek[a]) if (vis[s] == 1) { wek[s].erase(a); if (((int)(wek[s]).size()) < k) q.push(s); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; res = n; for (int i = 1; i <= m; i++) { cin >> tab[i].first >> tab[i].second; wek[tab[i].first].insert(tab[i].second); wek[tab[i].second].insert(tab[i].first); } for (int i = 1; i <= n; i++) { vis[i] = 1; if (((int)(wek[i]).size()) < k) q.push(i); } rob(); odp[m] = res; for (int i = m - 1; i >= 0; i--) { wek[tab[i + 1].first].erase(tab[i + 1].second); wek[tab[i + 1].second].erase(tab[i + 1].first); if (vis[tab[i + 1].first] == 1 && ((int)(wek[tab[i + 1].first]).size()) < k) q.push(tab[i + 1].first); if (vis[tab[i + 1].second] == 1 && ((int)(wek[tab[i + 1].second]).size()) < k) q.push(tab[i + 1].second); rob(); odp[i] = res; } for (int i = 1; i <= m; i++) cout << odp[i] << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; int N, M, K; int A[MAXN]; int B[MAXN]; set<int> adj[MAXN]; int deg[MAXN]; set<pair<int, int> > st; bool inGraph[MAXN]; int ans[MAXN]; void update(int x) { if (!inGraph[x]) return; set<pair<int, int> >::iterator it = st.find(make_pair(deg[x], x)); st.erase(it); deg[x]--; st.insert(make_pair(deg[x], x)); } void fix() { while (!st.empty() && st.begin()->first < K) { int cur = st.begin()->second; st.erase(st.begin()); inGraph[cur] = false; for (auto nxt : adj[cur]) { adj[nxt].erase(cur); update(nxt); } adj[cur].clear(); } } int main() { scanf("%d %d %d", &N, &M, &K); for (int i = 0; i < M; i++) { scanf("%d %d", &A[i], &B[i]); deg[A[i]]++; deg[B[i]]++; adj[A[i]].insert(B[i]); adj[B[i]].insert(A[i]); } for (int i = 1; i <= N; i++) { st.insert(make_pair(deg[i], i)); inGraph[i] = true; } for (int i = M - 1; i >= 0; i--) { fix(); ans[i] = st.size(); adj[A[i]].erase(B[i]); adj[B[i]].erase(A[i]); if (inGraph[B[i]]) update(A[i]); if (inGraph[A[i]]) update(B[i]); } for (int i = 0; i < M; i++) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; template <int n, class... T> typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &, tuple<T...> const &) {} template <int n, class... T> typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os, tuple<T...> const &t) { os << (n == 0 ? "" : ", ") << get<n>(t); _ot<n + 1>(os, t); } template <class... T> ostream &operator<<(ostream &o, tuple<T...> const &t) { o << "("; _ot<0>(o, t); o << ")"; return o; } template <class T, class U> ostream &operator<<(ostream &o, pair<T, U> const &p) { o << "(" << p.first << ", " << p.second << ")"; return o; } template <class T, class = typename iterator_traits<typename T::iterator>::value_type, class = typename enable_if<!is_same<T, string>::value>::type> ostream &operator<<(ostream &o, const T &a) { for (auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : " ") << *ite; return o; } const int N = 2e5; vector<pair<int, int>> g[N]; int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); ll n, m, k; cin >> n >> m >> k; vector<pair<int, int>> v; vector<int> fri(n); vector<int> vfri(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; v.emplace_back(a, b); g[a].emplace_back(b, i); g[b].emplace_back(a, i); fri[a]++; fri[b]++; vfri[a]++; vfri[b]++; } vector<int> no(n); int col = 1; vector<int> used(n); ll now = n; int tim = m - 1; function<void(int)> check = [&](int i) { if (no[i]) return; if (vfri[i] < k) { no[i] = 1; now--; auto ng = g[i]; ng.clear(); for (auto p : g[i]) { int j, t; tie(j, t) = p; if (t > tim) continue; ng.emplace_back(j, t); if (!no[j]) { vfri[j]--; check(j); } } g[i] = ng; } }; for (int i = 0; i < n; i++) if (!no[i]) check(i); vector<int> ans(m); for (int i = m - 1; i >= 0; i--) { tim--; ans[i] = now; if (i == 0) break; int a, b; tie(a, b) = v[i]; fri[a]--; fri[b]--; if (!no[a]) vfri[b]--; if (!no[b]) vfri[a]--; if (!no[a]) check(a); if (!no[b]) check(b); (42); } for (int i = 0; i < m; i++) cout << ans[i] << endl; return 0; }
11
CPP
#include <bits/stdc++.h> const int maxn = 2e5 + 3; int n, m, k, rest, eu[maxn], ev[maxn], ans[maxn]; std::set<int> G[maxn]; bool poped[maxn]; std::queue<int> q; void delEdge(int u, int v) { G[u].erase(v); G[v].erase(u); if (G[u].size() < k && !poped[u]) { poped[u] = true; --rest; q.push(u); } if (G[v].size() < k && !poped[v]) { poped[v] = true; --rest; q.push(v); } } void erase() { while (!q.empty()) { int u = q.front(); q.pop(); std::set<int> E = G[u]; for (const auto v : E) { delEdge(u, v); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; ++i) { scanf("%d%d", eu + i, ev + i); G[eu[i]].insert(ev[i]); G[ev[i]].insert(eu[i]); } rest = n; for (int i = 1; i <= n; ++i) { if (G[i].size() < k) { poped[i] = true; --rest; q.push(i); } } erase(); for (int i = m - 1; i >= 0; --i) { ans[i] = rest; delEdge(eu[i], ev[i]); erase(); } for (int i = 0; i < m; ++i) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, k, u; scanf("%d", &n); scanf("%d", &m); scanf("%d", &k); vector<pair<int, int> > adj[n + 5]; pair<int, int> edges[m + 5]; set<pair<int, int> > degree_set; set<pair<int, int> >::iterator it; bool isthere[n + 5]; int deg[n + 5]; int ans[m + 5]; memset(ans, 0, sizeof(ans)); memset(deg, 0, sizeof(deg)); memset(isthere, true, sizeof(isthere)); for (i = 0; i < m; i++) { scanf("%d", &edges[i].first); scanf("%d", &edges[i].second); adj[edges[i].first].push_back({edges[i].second, i}); adj[edges[i].second].push_back({edges[i].first, i}); ++deg[edges[i].first]; degree_set.insert({deg[edges[i].first], edges[i].first}); degree_set.erase({deg[edges[i].first] - 1, edges[i].first}); ++deg[edges[i].second]; degree_set.insert({deg[edges[i].second], edges[i].second}); degree_set.erase({deg[edges[i].second] - 1, edges[i].second}); } for (i = m - 1; i >= 0; i--) { while (!degree_set.empty() && (*(degree_set.begin())).first < k) { u = (*(degree_set.begin())).second; for (auto k1 : adj[u]) { if (isthere[k1.first] == true && k1.second <= i) { degree_set.erase({deg[k1.first], k1.first}); deg[k1.first]--; degree_set.insert({deg[k1.first], k1.first}); } } degree_set.erase({deg[u], u}); isthere[u] = false; } ans[i] = degree_set.size(); if (isthere[edges[i].first] == true && isthere[edges[i].second] == true) { degree_set.erase({deg[edges[i].first], edges[i].first}); deg[edges[i].first]--; degree_set.insert({deg[edges[i].first], edges[i].first}); degree_set.erase({deg[edges[i].second], edges[i].second}); deg[edges[i].second]--; degree_set.insert({deg[edges[i].second], edges[i].second}); } } for (i = 0; i < m; i++) { printf("%d\n", ans[i]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct P { int x, y, z; bool operator<(const P &a) const { return x < a.x; } }; vector<int> v[222228]; int a, c, i, b, n, m, k, d; int o[221111]; int l[221111]; int j[1111][9]; int e; int dx[10] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[10] = {1, 0, -1, 0, 1, -1, 1, -1}, dz[10] = {0, 0, 0, 0, 1, -1}; long long x, y, mod = 1000000007, mod2 = 1000000009, mod3 = 2017; long long z; double pi = 3.14159265; P u[222221]; stack<int> s; queue<int> q, q1; map<int, int> p[222222]; char r[13]; bool as(P a, P b) { return a.x > b.x; } int main() { scanf("%d %d %d", &a, &b, &c); for (int t = 1; t <= b; t++) { scanf("%d %d", &n, &m); u[t] = {n, m}; p[min(n, m)][max(m, n)] = 1; v[n].push_back(m); v[m].push_back(n); o[n]++; o[m]++; } k = a; for (int t = 1; t <= a; t++) if (o[t] < c) { k--; l[t] = 1; q.push(t); } for (; q.size(); q.pop()) for (int h = 0; h < v[q.front()].size(); h++) if (!l[v[q.front()][h]]) { o[v[q.front()][h]]--; if (o[v[q.front()][h]] < c) { k--; l[v[q.front()][h]] = 1; q.push(v[q.front()][h]); } } s.push(k); for (int t = b; t > 1; t--) if (l[u[t].y] == 0 && !l[u[t].x] && p[min(u[t].x, u[t].y)][max(u[t].x, u[t].y)]) { p[min(u[t].x, u[t].y)][max(u[t].x, u[t].y)] = 0; o[u[t].x]--; o[u[t].y]--; if (l[u[t].x] == 0 && o[u[t].x] < c) { k--; l[u[t].x] = 1; q.push(u[t].x); } if (l[u[t].y] == 0 && o[u[t].y] < c) { k--; l[u[t].y] = 1; q.push(u[t].y); } for (; q.size(); q.pop()) for (int h = 0; h < v[q.front()].size(); h++) if (!l[v[q.front()][h]] && p[min(q.front(), v[q.front()][h])] [max(q.front(), v[q.front()][h])]) { p[min(q.front(), v[q.front()][h])] [max(q.front(), v[q.front()][h])] = 0; o[v[q.front()][h]]--; if (o[v[q.front()][h]] < c) { k--; l[v[q.front()][h]] = 1; q.push(v[q.front()][h]); } } s.push(k); } else s.push(k); for (; s.size(); s.pop()) printf("%d\n", s.top()); }
11
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, m, k; cin >> n >> m >> k; vector<set<long long>> g(n + 1); vector<long long> deg(n + 1, 0); vector<pair<long long, long long>> v(m); for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; g[a].insert(b); g[b].insert(a); deg[a]++; deg[b]++; v[i].first = a; v[i].second = b; } long long ans = n; queue<long long> q; vector<long long> vis(n + 1, 0); for (long long i = 1; i <= n; i++) { if (deg[i] < k) { q.push(i); vis[i] = 1; } } while (!q.empty()) { long long d = q.front(); q.pop(); ans--; for (auto it : g[d]) { g[it].erase(d); deg[it]--; if (deg[it] == k - 1) q.push(it); } g[d].clear(); } vector<long long> fans(m); for (long long i = m - 1; i >= 0; i--) { fans[i] = ans; long long a = v[i].first; long long b = v[i].second; if (g[a].find(b) != g[a].end()) { g[a].erase(b); deg[a]--; if (deg[a] == k - 1) q.push(a); } if (g[b].find(a) != g[b].end()) { g[b].erase(a); deg[b]--; if (deg[b] == k - 1) q.push(b); } while (!q.empty()) { long long d = q.front(); q.pop(); ans--; for (auto it : g[d]) { g[it].erase(d); deg[it]--; if (deg[it] == k - 1) q.push(it); } g[d].clear(); } } for (long long i = 0; i < m; i++) { cout << fans[i] << "\n"; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 3e5 + 10; long long x[N], y[N], deg[N]; vector<long long> vp[N]; long long removed[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k; cin >> n >> m >> k; for (long long i = 1; i <= m; i++) { cin >> x[i] >> y[i]; vp[x[i]].push_back(y[i]); vp[y[i]].push_back(x[i]); deg[x[i]]++; deg[y[i]]++; } set<pair<long long, long long> > s; for (long long i = 1; i <= n; i++) { s.insert({deg[i], i}); } long long ans = n; set<pair<long long, long long> > ae; vector<long long> v; for (long long i = m; i >= 1; i--) { while (s.size() > 0) { auto it = *s.begin(); s.erase(it); if (it.first >= k) break; removed[it.second] = 1; for (auto it1 : vp[it.second]) { if (ae.find({it1, it.second}) == ae.end()) { ae.insert({it1, it.second}); ae.insert({it.second, it1}); if (!removed[it1]) { s.erase({deg[it1], it1}); deg[it1]--; s.insert({deg[it1], it1}); } } } ans--; } if (ae.find({x[i], y[i]}) == ae.end()) { ae.insert({x[i], y[i]}); ae.insert({y[i], x[i]}); s.erase({deg[x[i]], x[i]}); s.erase({deg[y[i]], y[i]}); if (!removed[x[i]]) { deg[x[i]]--; s.insert({deg[x[i]], x[i]}); } if (!removed[y[i]]) { deg[y[i]]--; s.insert({deg[y[i]], y[i]}); } } v.push_back(ans); } reverse(v.begin(), v.end()); for (auto it : v) { cout << it << "\n"; } }
11
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; set<pair<int, int> > sq; set<int> to[maxn]; int n, m, k; int a[maxn]; struct node { int l, r; } q[maxn]; void update() { while (sq.size() > 0 && (*(sq.begin())).first < k) { auto x = *(sq.begin()); sq.erase(sq.begin()); for (auto j = to[x.second].begin(); j != to[x.second].end();) { auto value = *j; if (sq.count({a[value], value})) { sq.erase(sq.find({a[value], value})); a[value]--; if (a[value] > 0) sq.insert({a[value], value}); to[x.second].erase(j++); } else j++; } } } int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int from, t; cin >> from >> t; q[i].l = from, q[i].r = t; to[from].insert(t); to[t].insert(from); a[from]++, a[t]++; } for (int i = 1; i <= n; i++) sq.insert({a[i], i}); vector<int> ans; for (int i = m; i >= 1; i--) { update(); ans.push_back(sq.size()); if (sq.count({a[q[i].l], q[i].l}) && sq.count({a[q[i].r], q[i].r})) { sq.erase(sq.find({a[q[i].l], q[i].l})); sq.erase(sq.find({a[q[i].r], q[i].r})); a[q[i].l]--, a[q[i].r]--; if (a[q[i].l] > 0) sq.insert({a[q[i].l], q[i].l}); if (a[q[i].r] > 0) sq.insert({a[q[i].r], q[i].r}); to[q[i].l].erase(q[i].r); to[q[i].r].erase(q[i].l); } } reverse(ans.begin(), ans.end()); for (auto &i : ans) { cout << i << endl; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int max6 = 1e6 + 6; const int oo = 2e9 + 9; const long long inf = 2e18 + 18; vector<pair<int, int> > g[max6]; pair<int, int> a[max6]; int q[max6]; int block[max6]; int deg[max6]; int del[max6]; int res[max6]; int n, m, need; int main() { cin >> n >> m >> need; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; g[u].push_back({v, i}); g[v].push_back({u, i}); a[i] = {u, v}; deg[u]++, deg[v]++; } int l = 1, r = 0; for (int i = 1; i <= n; ++i) if (deg[i] < need) q[++r] = i, del[i] = 1; int maxMem = n; for (int i = m; i >= 1; --i) { while (l <= r) { int u = q[l++]; maxMem--; for (auto it : g[u]) { int id = it.second; int v = it.first; if (block[id]) continue; if (del[v]) continue; block[id] = 1; deg[v]--; if (deg[v] < need) { del[v] = 1; q[++r] = v; } } } res[i] = maxMem; int u = a[i].first; int v = a[i].second; if (del[u] || del[v]) continue; deg[u]--; deg[v]--; block[i] = 1; if (deg[u] < need) { q[++r] = u; del[u] = 1; } if (deg[v] < need) { q[++r] = v; del[v] = 1; } } for (int i = 1; i <= m; ++i) cout << res[i] << "\n"; }
11
CPP
#include <bits/stdc++.h> using namespace std; int get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { int s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } int s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 2e5 + 5; struct edge { int x, nxt; } e[N * 2]; int h[N], tot; int d[N]; struct EDGE { int x, y; } ed[N]; int n, m, k; int ans[N]; int cnt; bool bz[N]; bool er[N]; int update(int &now) { for (; now && er[now / 2]; now = e[now].nxt) ; return now; } void del(int x) { if (bz[x]) return; bz[x] = 1; cnt--; for (; update(h[x]);) { int p = h[x]; d[x]--, d[e[p].x]--; er[p / 2] = 1; if (d[e[p].x] < k) del(e[p].x); } } void inse(int x, int y) { e[++tot].x = y; e[tot].nxt = h[x]; h[x] = tot; } int main() { n = get(); m = get(); k = get(); tot = 1; for (int i = 1; i <= m; i++) { ed[i].x = get(); ed[i].y = get(); inse(ed[i].x, ed[i].y); inse(ed[i].y, ed[i].x); d[ed[i].x]++, d[ed[i].y]++; } cnt = n; for (int i = 1; i <= n; i++) if (d[i] < k) del(i); for (int i = m; i >= 1; i--) { int x = ed[i].x, y = ed[i].y; ans[i] = cnt; if (er[i]) ; else { er[i] = 1; d[x]--, d[y]--; if (d[x] < k) del(x); if (d[y] < k) del(y); } } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; vector<set<int>> g(n); vector<int> deg(n); set<pair<int, int>> cur; vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; deg[a]++; deg[b]++; edges.emplace_back(a, b); g[a].insert(b); g[b].insert(a); } for (int i = 0; i < n; i++) cur.insert(make_pair(deg[i], i)); auto solve = [&]() { while (true) { if (cur.empty()) break; auto u = cur.begin(); vector<int> rev; if (u->first < k) { for (int i : g[u->second]) { rev.push_back(i); } cur.erase(u); for (int i : rev) { if (cur.find(make_pair(deg[i], i)) != cur.end()) { cur.erase(make_pair(deg[i], i)); cur.insert(make_pair(deg[i] - 1, i)); } deg[i]--; } } else break; } }; solve(); vector<int> ans; ans.push_back(cur.size()); for (int i = m - 1; i > -1; i--) { int u = edges[i].first, v = edges[i].second; if (cur.find(make_pair(deg[u], u)) != cur.end() && cur.find(make_pair(deg[v], v)) != cur.end()) { cur.erase(make_pair(deg[u], u)); deg[u]--; cur.insert(make_pair(deg[u], u)); cur.erase(make_pair(deg[v], v)); deg[v]--; cur.insert(make_pair(deg[v], v)); } g[u].erase(v); g[v].erase(u); solve(); ans.push_back(cur.size()); } ans.pop_back(); reverse(ans.begin(), ans.end()); for (int i : ans) cout << i << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int k; cin >> k; vector<set<int>> g(n); vector<int> x(m), y(m); for (int i = 0; i < m; i++) { cin >> x[i] >> y[i], x[i]--, y[i]--; g[x[i]].emplace(y[i]); g[y[i]].emplace(x[i]); } int ans = n; queue<int> que; vector<int> alive(n, true); for (int i = 0; i < n; i++) if ((int)g[i].size() < k) { que.emplace(i), alive[i] = false, ans--; } vector<int> res(m); for (int i = m - 1; i >= 0; i--) { while (!que.empty()) { int u = que.front(); que.pop(); for (int v : g[u]) g[v].erase(u); for (int v : g[u]) if (alive[v] && (int)g[v].size() < k) { que.emplace(v), alive[v] = false, ans--; } g[u].clear(); } res[i] = ans; int u = x[i], v = y[i]; g[u].erase(v), g[v].erase(u); if (alive[u] && (int)g[u].size() < k) { que.emplace(u), alive[u] = false, ans--; } if (alive[v] && (int)g[v].size() < k) { que.emplace(v), alive[v] = false, ans--; } } for (int e : res) cout << e << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = double; using pll = pair<ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using vvll = vector<vll>; stack<ll> st; vector<ll> deg; vector<vector<ll>> g; vector<pair<ll, ll>> e; ll k; set<pair<ll, ll>> deleted; ll fixStuff() { ll del = 0; while (!st.empty()) { ll i = st.top(); st.pop(); if (deg[i] < 0) continue; deg[i] = -1; del++; for (ll j = 0; j < (ll)g[i].size(); ++j) { if (deleted.count(make_pair(i, g[i][j]))) continue; if (deleted.count(make_pair(g[i][j], i))) continue; deg[g[i][j]]--; if (deg[g[i][j]] < k) { st.push(g[i][j]); } } } return del; } int main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); ll n, m; cin >> n >> m >> k; deg = vector<ll>(n); g = vector<vector<ll>>(n); e = vector<pair<ll, ll>>(m); for (ll i = 0; i < (ll)m; ++i) { ll u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); deg[u]++; deg[v]++; e[i].first = u; e[i].second = v; } for (ll i = 0; i < (ll)n; ++i) if (deg[i] < k) st.push(i); vector<ll> res; ll cnt = n - fixStuff(); for (ll i = m - 1; i >= (ll)0; --i) { res.push_back(cnt); if (deg[e[i].first] < 0 || deg[e[i].second] < 0) continue; deg[e[i].first]--; deg[e[i].second]--; deleted.insert(make_pair(e[i].first, e[i].second)); if ((deg[e[i].first] < k && deg[e[i].first] >= 0)) st.push(e[i].first); if ((deg[e[i].second] < k && deg[e[i].second] >= 0)) st.push(e[i].second); if (!st.empty()) cnt -= fixStuff(); } reverse(res.begin(), res.end()); for (ll i = 0; i < (ll)res.size(); ++i) cout << res[i] << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9 + 7; int dir[8][2] = {-1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1, -1, 1, 1}; template <typename S, typename T> ostream &operator<<(ostream &os, const pair<S, T> x) { os << "(" << x.first << ", " << x.second << ")"; return os; } template <typename S, typename T> inline bool Min(S &a, const T &b) { return a > b ? a = b, true : false; } template <typename S, typename T> inline bool Max(S &a, const T &b) { return a < b ? a = b, true : false; } template <typename S, typename T> inline void Adm(S &a, const T &b) { a = (a + b) % MOD; if (a < 0) a += MOD; } template <typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } template <typename T> inline T _Gcd(T a, T b) { while (b) { T t = b; b = a % b; a = t; } return a; } template <typename T> inline int _BitCnt(T x) { int cnt = 0; while (x) ++cnt, x &= x - 1; return cnt; } inline long long Pow(long long a, long long n) { long long t = 1; a %= MOD; while (n > 0) { if (n & 1) t = t * a % MOD; a = a * a % MOD, n >>= 1; } return t % MOD; } inline int read() { static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; if (ch == '-') sgn = true, ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; return sgn ? -x : x; } const int N = 2e5 + 100; int d[N], del[N]; vector<pair<int, int> > e[N]; int u[N], v[N], ans[N]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= m; ++i) { scanf("%d%d", &u[i], &v[i]); e[u[i]].push_back({v[i], i}), e[v[i]].push_back({u[i], i}); ++d[u[i]], ++d[v[i]]; } set<pair<int, int> > st; for (int i = 1; i <= n; ++i) st.insert({d[i], i}); for (int i = m; i >= 1; --i) { while (!st.empty() && st.begin()->first < k) { int u = st.begin()->second; st.erase(st.begin()), del[u] = 1; for (auto it : e[u]) { int v = it.first, id = it.second; if (!del[v] && id <= i) st.erase({d[v], v}), st.insert({--d[v], v}); } } ans[i] = ((int)(st).size()); if (!del[u[i]] && !del[v[i]]) { st.erase({d[u[i]], u[i]}), st.insert({--d[u[i]], u[i]}); st.erase({d[v[i]], v[i]}), st.insert({--d[v[i]], v[i]}); } } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 201102; vector<long long> g[N]; set<pair<long long, long long> > s; long long n, m, k, v, u, d[N], a[N], b[N], ans[N]; pair<long long, long long> x; set<pair<long long, long long> >::iterator it; map<long long, bool> e[N]; int main() { scanf("%lld%lld%lld", &n, &m, &k); for (long long i = 1; i <= m; i++) { scanf("%lld%lld", &v, &u); d[v]++, d[u]++; g[v].push_back(u), g[u].push_back(v); a[i] = v, b[i] = u; } for (long long i = 1; i <= n; i++) s.insert({d[i], i}); for (long long i = m + 1; i >= 1 and s.size(); i--) { v = a[i]; if (e[a[i]][b[i]]) { ans[i] = s.size(); continue; } e[b[i]][a[i]] = e[a[i]][b[i]] = 1; if (d[v] >= k) { s.erase({d[v], v}); d[v]--; s.insert({d[v], v}); } v = b[i]; if (d[v] >= k) { s.erase({d[v], v}); d[v]--; s.insert({d[v], v}); } while (s.size()) { v = (*s.begin()).second; if (d[v] >= k) break; s.erase({d[v], v}); d[v] = 0; for (long long j = 0; j < g[v].size(); j++) { u = g[v][j]; if (d[u] < k or e[u][v] or s.find({d[u], u}) == s.end()) continue; e[u][v] = e[v][u] = 1; s.erase({d[u], u}); d[u]--; s.insert({d[u], u}); } } ans[i] = s.size(); } for (long long i = 2; i <= m + 1; i++) printf("%lld\n", ans[i]); }
11
CPP