Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int Int() { int x; scanf("%d", &x); return x; } long long Long() { long long x; scanf("%lld", &x); return x; } double Double() { double x; scanf("%lf", &x); return x; } const int N = 2 * (int)1e5 + 5; const long long MOD = (int)1e9 + 7; int us[N], vs[N], ans[N], degree[N]; bool can[N]; set<pair<int, int> > trip; vector<pair<int, int> > g[N]; int main() { int n = Int(), m = Int(), k = Int(); memset(can, true, sizeof can); for (int i = 1; i <= m; i++) { us[i] = Int(), vs[i] = Int(); g[us[i]].push_back({vs[i], i}); g[vs[i]].push_back({us[i], i}); degree[us[i]]++; degree[vs[i]]++; } for (int i = 1; i <= n; i++) { trip.insert({degree[i], i}); } while (!trip.empty() and trip.begin()->first < k) { int u = trip.begin()->second; for (auto j : g[u]) { if (can[j.first]) { trip.erase({degree[j.first], j.first}); degree[j.first]--; trip.insert({degree[j.first], j.first}); } } trip.erase({degree[u], u}); can[u] = false; } for (int i = m; i > 0; i--) { ans[i] = trip.size(); if (can[us[i]] and can[vs[i]]) { trip.erase({degree[us[i]], us[i]}); degree[us[i]]--; trip.insert({degree[us[i]], us[i]}); trip.erase({degree[vs[i]], vs[i]}); degree[vs[i]]--; trip.insert({degree[vs[i]], vs[i]}); while (!trip.empty() and trip.begin()->first < k) { int u = trip.begin()->second; for (auto j : g[u]) { if (j.second >= i) continue; if (can[j.first]) { trip.erase({degree[j.first], j.first}); degree[j.first]--; trip.insert({degree[j.first], j.first}); } } trip.erase({degree[u], u}); can[u] = false; } } } for (int i = 1; i <= m; i++) { cout << ans[i] << '\n'; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = double; using pii = pair<int, int>; using vi = vector<int>; const int maxn = 5e5; const int inf = 1e9; const int mod = 1e9 + 7; const ll inf64 = 1e18; const ld pi = acos(-1.0); const ld eps = 1e-6; int n, m, k, deg[maxn]; set<int> g[maxn]; int uu[maxn], vv[maxn]; int ans[maxn]; int del[maxn]; int cur; void go(int v) { queue<int> q; q.push(v); while (!q.empty()) { v = q.front(); q.pop(); if (del[v]) continue; del[v] = 1; cur--; for (int to : g[v]) { g[to].erase(v); if (--deg[to] < k) q.push(to); } } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; g[u].insert(v); g[v].insert(u); deg[u]++; deg[v]++; uu[i] = u; vv[i] = v; } cur = n; for (int i = 0; i < n; i++) if (del[i] == 0 && deg[i] < k) { go(i); } for (int i = m - 1; i >= 0; i--) { ans[i] = cur; if (del[uu[i]] == 0 && del[vv[i]] == 0) { deg[uu[i]]--; deg[vv[i]]--; g[uu[i]].erase(vv[i]); g[vv[i]].erase(uu[i]); if (deg[uu[i]] < k) go(uu[i]); if (deg[vv[i]] < k) go(vv[i]); } } for (int i = 0; i < m; i++) cout << ans[i] << '\n'; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxi = 1e6 + 2; int a[maxi], b[maxi]; int res[maxi]; vector<int> v[maxi]; string s; int n, m, k; int ok; int rem[maxi]; int deg[maxi]; map<pair<int, int>, int> mp; queue<int> q; void skloni(int poz) { while (!q.empty()) { int x = q.front(); deg[x] = 0; q.pop(); for (int i : v[x]) { if (!rem[i] && deg[i] == k && !mp[{x, i}]) { deg[i]--; res[poz]--; q.push(i); rem[i] = 1; mp[{x, i}] = 1; mp[{i, x}] = 1; } else { if (!mp[{x, i}]) deg[i]--; mp[{x, i}] = 1; mp[{i, x}] = 1; } } } } int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); a[i] = x; b[i] = y; v[x].push_back(y); v[y].push_back(x); deg[x]++; deg[y]++; } res[m] = n; for (int i = 1; i <= n; i++) if (deg[i] < k) { q.push(i); rem[i] = 1; res[m]--; } skloni(m); for (int i = m; i >= 1; i--) { res[i - 1] = res[i]; if (!mp[{a[i], b[i]}]) { deg[a[i]]--; deg[b[i]]--; mp[{a[i], b[i]}] = 1; mp[{b[i], a[i]}] = 1; if (deg[a[i]] == k - 1 && !rem[a[i]]) { rem[a[i]] = 1; res[i - 1]--; q.push(a[i]); }; if (deg[b[i]] == k - 1 && !rem[b[i]]) { rem[b[i]] = 1; res[i - 1]--; q.push(b[i]); }; skloni(i - 1); } } for (int i = 1; i <= m; i++) printf("%d\n", res[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; set<int> V[200200]; pair<int, int> edges[200200]; set<int> active; int answer[200200]; void fix(int i) { if (active.find(i) == active.end()) return; if (V[i].size() < k) { for (int x : V[i]) V[x].erase(i); for (int x : V[i]) fix(x); active.erase(i); } } int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; edges[i].first = x; edges[i].second = y; V[x].insert(y); V[y].insert(x); } for (int i = 1; i <= n; i++) active.insert(i); for (int i = 1; i <= n; i++) fix(i); for (int i = m - 1; i >= 0; i--) { answer[i] = active.size(); int x = edges[i].first, y = edges[i].second; V[x].erase(y); V[y].erase(x); fix(x); fix(y); } for (int i = 0; i < m; i++) cout << answer[i] << "\n"; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; struct edge { int v, nxt; } e[200005 * 2]; int cnt = 1, head[200005]; void adde(int u, int v) { e[++cnt].v = v; e[cnt].nxt = head[u]; head[u] = cnt; } int vis[200005 * 2], d[200005], ans[200005], res; struct ege2 { int u, v; } edge[200005]; queue<int> q; void solve() { while (!q.empty()) { int u = q.front(); q.pop(); d[u] = 0; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; if (vis[i]) continue; vis[i] = vis[i ^ 1] = 1; d[v]--; if (d[v] == k - 1) { --res; q.push(v); } } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1, u, v; i <= m; ++i) { scanf("%d%d", &u, &v); edge[i].u = u; edge[i].v = v; adde(u, v); adde(v, u); ++d[u]; ++d[v]; } res = n; for (int i = 1; i <= n; ++i) if (d[i] < k) { --res; q.push(i); } solve(); ans[m] = res; for (int i = m; i >= 2; --i) { if (vis[i << 1]) { ans[i - 1] = ans[i]; continue; } vis[i << 1] = vis[i << 1 | 1] = 1; int u = edge[i].u, v = edge[i].v; d[u]--; d[v]--; if (d[u] == k - 1) { --res; q.push(u); } if (d[v] == k - 1) { --res; q.push(v); } solve(); ans[i - 1] = res; } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; set<int> adj[maxn]; int K; queue<int> Q; bool deleted[maxn]; int delc; void del_edge(int u, int v) { if (adj[u].count(v) != 0) { adj[u].erase(v); if (!deleted[u] && (int)adj[u].size() < K) { deleted[u] = 1; delc++; Q.push(u); } } if (adj[v].count(u) != 0) { adj[v].erase(u); if (!deleted[v] && (int)adj[v].size() < K) { deleted[v] = 1; delc++; Q.push(v); } } } void del_vert(int u) { vector<int> todel; for (int v : adj[u]) { todel.push_back(v); } for (int v : todel) { del_edge(u, v); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int vertexc, queryc; cin >> vertexc >> queryc >> K; vector<pair<int, int>> edges; for (int i = 0; i < queryc; i++) { int u, v; cin >> u >> v; adj[u].insert(v); adj[v].insert(u); edges.push_back(make_pair(u, v)); } for (int i = 1; i <= vertexc; i++) { if ((int)adj[i].size() < K) { deleted[i] = 1; delc++; Q.push(i); } } deque<int> ans; for (int i = 0; i < queryc; i++) { while (!Q.empty()) { int qtop = Q.front(); Q.pop(); del_vert(qtop); } ans.push_front(vertexc - delc); pair<int, int> uv = edges.back(); edges.pop_back(); if (!deleted[uv.first] && !deleted[uv.second]) { del_edge(uv.first, uv.second); } } for (int i = 0; i < (int)ans.size(); i++) { cout << ans[i] << '\n'; } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Solution { boolean eof; BufferedReader br; StringTokenizer st; PrintWriter out; public static void main(String[] args) throws IOException { new Solution().run(); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return "-1"; } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { return br.readLine(); } void run() throws IOException { InputStream input = System.in; PrintStream output = System.out; String name = "a"; try { File f = new File(name + ".in"); if (f.exists() && f.canRead()) { input = new FileInputStream(f); output = new PrintStream(name + ".out"); } } catch (Throwable e) { } br = new BufferedReader(new InputStreamReader(input)); out = new PrintWriter(output); solve(); br.close(); out.close(); } void solve() { int n = nextInt(), m = nextInt(), k = nextInt(); ArrayList<Integer>[] ed = new ArrayList[n]; for (int i = 0; i < n; i++) { ed[i] = new ArrayList<>(); } int[] edU = new int[m], edV = new int[m]; int[] frc = new int[n]; int[] eds = new int[n]; int[] ans = new int[m]; for (int i = 0; i < m; i++) { edU[i] = nextInt() - 1; edV[i] = nextInt() - 1; frc[edU[i]]++; frc[edV[i]]++; ed[edU[i]].add(edV[i]); ed[edV[i]].add(edU[i]); eds[edU[i]]++; eds[edV[i]]++; } int cur = n; Queue<Integer> low = new ArrayDeque<>(); for (int i = 0; i < n; i++) { if (frc[i] < k) { low.add(i); frc[i] = 0; } } int u, v; for (int i = 0; i < m; i++) { while (!low.isEmpty()) { u= low.poll(); cur--; for (int j = 0; j < eds[u]; j++) { v = ed[u].get(j); frc[v]--; if (frc[v] == k - 1) { low.add(v); } } } ans[m - i - 1] = cur; u = edU[m - i - 1]; v = edV[m - i - 1]; eds[u]--; eds[v]--; boolean nu1 = frc[u] >= k; if (frc[v] >= k) { frc[u]--; if (frc[u] == k - 1) low.add(u); } if (nu1) { frc[v]--; if (frc[v] == k - 1) low.add(v); } } for (int i = 0; i < m; i++) { out.println(ans[i]); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
python2
class Graph(object): def __init__(self, n): self.n = n self.adj = [set() for i in xrange(n+1)] self.rank = [0 for i in xrange(n+1)] self.color = [True for i in xrange(n+1)] def add_edge(self, a, b): self.adj[a].add(b) self.adj[b].add(a) self.rank[a] += 1 self.rank[b] += 1 def remove_edge(self, a, b): if b in self.adj[a]: self.rank[a] -= 1 self.adj[a].remove(b) if a in self.adj[b]: self.adj[b].remove(a) self.rank[b] -= 1 def main(): n, m, k = map(int, raw_input().strip().split(' ')) g = Graph(n) edges = [] for i in xrange(m): a, b = map(int, raw_input().strip().split(' ')) g.add_edge(a, b) edges.append((a, b)) q = [] for v in range(1, n+1): if g.rank[v] < k: q.append(v) def process_q(): num_removed = 0 while q: v = q.pop() # print 'removing', v num_removed += 1 for u in list(g.adj[v]): if g.rank[u] == k: q.append(u) g.remove_edge(u, v) return num_removed res = [] cur_res = n cur_res -= process_q() res.append(cur_res) for a, b in reversed(edges): # print 'process', a, b g.remove_edge(a, b) if g.rank[a] == k-1: q.append(a) if g.rank[b] == k-1: q.append(b) cur_res -= process_q() res.append(cur_res) for r in reversed(res[:-1]): print r, print if __name__ == '__main__': main()
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int u[200050]; int v[200050]; int ans[200050]; int n, m, k; set<int> s, num[200050]; void dfs(int u) { if (num[u].size() < k && s.erase(u)) { for (auto i : num[u]) { num[i].erase(u); dfs(i); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &u[i], &v[i]); num[u[i]].insert(v[i]); num[v[i]].insert(u[i]); } for (int i = 1; i <= n; i++) s.insert(i); for (int i = 1; i <= n; i++) dfs(i); for (int i = m; i; i--) { ans[i] = s.size(); if (s.empty()) break; num[u[i]].erase(v[i]); num[v[i]].erase(u[i]); dfs(u[i]); dfs(v[i]); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<int, int> v[200100]; int lv[200100]; vector<int> gr[200100]; int in[200100]; vector<int> go; map<pair<int, int>, int> taiat; int n, m, k, cont; void dfs(int nod) { in[nod] = 0; cont--; for (auto &x : gr[nod]) { if (taiat[{nod, x}]) { continue; } taiat[{nod, x}] = 1; taiat[{x, nod}] = 1; lv[x]--; if (in[x] && lv[x] < k) { dfs(x); } } } vector<int> ans; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> k; cont = n; for (int i = 1; i <= m; i++) { cin >> v[i].first >> v[i].second; gr[v[i].first].push_back(v[i].second); gr[v[i].second].push_back(v[i].first); lv[v[i].first]++; lv[v[i].second]++; } for (int i = 1; i <= n; i++) { in[i] = 1; } for (int i = 1; i <= n; i++) { if (lv[i] < k) { go.push_back(i); } } for (auto &x : go) { if (in[x]) { dfs(x); } } for (int i = m; i >= 1; i--) { ans.push_back(cont); if (taiat[{v[i].first, v[i].second}]) { continue; } taiat[{v[i].first, v[i].second}] = 1; taiat[{v[i].second, v[i].first}] = 1; lv[v[i].first]--; lv[v[i].second]--; if (lv[v[i].first] < k && in[v[i].first]) { dfs(v[i].first); } if (lv[v[i].second] < k && in[v[i].second]) { dfs(v[i].second); } } reverse(ans.begin(), ans.end()); for (auto &x : ans) { cout << x << '\n'; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Test { static int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static long readLong() { long ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static String readString() { StringBuilder b = new StringBuilder(); try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c >= '0' && c <= '9') { start = true; b.append((char) (c)); } else if (start) break; } } catch (IOException e) { } return b.toString().trim(); } static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); void start() { int n = readInt(), m = readInt(), k = readInt(); int[] ans = new int[m]; int[] from = new int[m], to = new int[m], deg = new int[n+1]; boolean[] alive = new boolean[m]; int[] q = new int[n]; List<int[]>[] g = new List[n+1]; for (int i = 1; i <= n; i++) g[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int u = readInt(), v = readInt(); from[i] = u; to[i] = v; g[u].add(new int[]{v, i}); g[v].add(new int[]{u, i}); alive[i] = true; } int a = 0, b = 0; for (int i = 1; i <= n; i++) { deg[i] = g[i].size(); if (deg[i] < k) q[b++] = i; } for (int i = m - 1; i >= 0; i--) { while (a < b) { int u = q[a++]; for (int[] e : g[u]) if (alive[e[1]]) { alive[e[1]] = false; int v = e[0]; if (deg[v] == k) q[b++] = v; deg[v]--; } } ans[i] = n - b; if (!alive[i]) continue; alive[i] = false; int u = from[i], v = to[i]; for (int x : new int[]{u, v}) { if (deg[x] == k) q[b++] = x; deg[x]--; } } for (int i = 0; i < m; i++) writer.println(ans[i]); } public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int i, j, n, m, a, b, c, d, op, mini, mij, ls, ld, ul, timp, k, maxl, rasp; int dp[1000005], flower[1000005], viz[1000005]; int good[1000005]; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; int sol[1000005]; vector<pair<int, int> > muchii; vector<pair<int, int> > v[1000005]; int gm[1000005]; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> a >> b; muchii.push_back({a, b}); v[a].push_back({b, i}); v[b].push_back({a, i}); gm[i] = 1; } for (int i = 1; i <= n; i++) { dp[i] = v[i].size(); q.push({dp[i], i}); good[i] = 1; } rasp = n; while (q.size() && (q.top().first < k || good[q.top().second] == 0)) { pair<int, int> now = q.top(); q.pop(); if (good[now.second] == 0) continue; good[now.second] = 0; rasp--; for (int i = 0; i < v[now.second].size(); i++) { int nxt = v[now.second][i].first; dp[nxt]--; if (good[nxt]) q.push({dp[nxt], nxt}); } } vector<int> sol; for (j = m; j; j--) { sol.push_back(rasp); a = muchii[j - 1].first; b = muchii[j - 1].second; gm[j] = 0; if (good[a] && good[b]) { dp[a]--; dp[b]--; q.push({dp[a], a}); q.push({dp[b], b}); } while (q.size() && (q.top().first < k || good[q.top().second] == 0)) { pair<int, int> now = q.top(); q.pop(); if (good[now.second] == 0) continue; good[now.second] = 0; rasp--; for (int i = 0; i < v[now.second].size(); i++) { if (gm[v[now.second][i].second]) { int nxt = v[now.second][i].first; dp[nxt]--; if (good[nxt]) q.push({dp[nxt], nxt}); } } } } for (int i = sol.size() - 1; i >= 0; i--) cout << sol[i] << '\n'; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
python2
n, m, k=map(int, raw_input().split()) edge=[map(int, raw_input().split()) for i in xrange(m)] adj=[[] for i in xrange(n+1)] deg=[0 for i in xrange(n+1)] for x, y in edge: adj[x]+=[y] adj[y]+=[x] deg[x]+=1 deg[y]+=1 q=[] first=0 removed=set() def bfs(): global first while first<len(q): cur=q[first] first+=1 for to in adj[cur]: if not (min(to, cur), max(to, cur)) in removed: removed.add((min(to, cur), max(to, cur))) deg[to]-=1 if deg[to]==k-1: q.append(to) for i in xrange(1, n+1): if deg[i]<k: q+=[i] bfs() result=[0]*m for i in xrange(m-1, -1, -1): result[i]=n-first x, y=edge[i] if (min(x, y), max(x, y)) not in removed: removed.add((min(x, y), max(x, y))) deg[x]-=1 if deg[x]==k-1: q+=[x] deg[y]-=1 if deg[y]==k-1: q+=[y] bfs() print "\n".join(map(str, result))
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1; int n, m, ans[N], cur, done[N], deg[N], k; vector<pair<int, int> > ve[N]; pair<int, int> e[N]; void bfs(int u, int time) { queue<int> qu; done[u] = 1; cur--; qu.push(u); int v; while (!qu.empty()) { u = qu.front(); qu.pop(); for (int i = 0; i < ve[u].size(); i++) { v = ve[u][i].first; if (done[v]) continue; if (ve[u][i].second > time) continue; deg[u]--; deg[v]--; if (deg[v] < k) { cur--; done[v] = 1; qu.push(v); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; cur = n; int u, v; for (int i = 1; i <= m; i++) { cin >> e[i].first >> e[i].second; u = e[i].first; v = e[i].second; ve[u].push_back({v, i}); ve[v].push_back({u, i}); } for (int i = 1; i <= n; i++) deg[i] = ve[i].size(); for (int i = 1; i <= n; i++) { if (deg[i] < k && !done[i]) { bfs(i, m); } } ans[m] = cur; for (int i = m - 1; i >= 1; i--) { u = e[i + 1].first; v = e[i + 1].second; if (!done[u] && !done[v]) { deg[u]--; deg[v]--; if (deg[u] < k && !done[u]) bfs(u, i); if (deg[v] < k && !done[v]) bfs(v, i); } ans[i] = cur; } for (int i = 1; i <= m; i++) { cout << ans[i] << '\n'; } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
//package com.company; import java.io.*; import java.util.*; public class Main { static long TIME_START, TIME_END; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("Test.in")); PrintWriter pw = new PrintWriter(System.out); // PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out")); Runtime runtime = Runtime.getRuntime(); long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); TIME_START = System.currentTimeMillis(); Task t = new Task(); t.solve(sc, pw); TIME_END = System.currentTimeMillis(); long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); pw.close(); System.out.println("Memory increased:" + (usedMemoryAfter - usedMemoryBefore) / 1000000); System.out.println("Time used: " + (TIME_END - TIME_START) + "."); } public static class Task { public int get(int[] q, int r) { if (q[0] == r) return q[1]; return q[0]; } int[] deg ; List<int[]> edges; List<Integer>[] edgesS ; boolean[] del ; boolean[] badedge ; public void solve(Scanner sc, PrintWriter pw) throws IOException { int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); deg = new int[n]; edges = new ArrayList<>(); edgesS = new List[n]; for (int i = 0; i < n; i++) { edgesS[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; edges.add(new int[]{a, b}); edgesS[a].add(i); edgesS[b].add(i); deg[a]++; deg[b]++; } del = new boolean[n]; badedge = new boolean[m]; int dlt = 0; for (int i = 0; i < n; i++) { if (!del[i] && deg[i] < k) { dlt += dfs(i, k); } } Arrays.fill(deg, 0); Arrays.fill(del, true); for (int i = 0; i < m; i++) { if (badedge[i]) continue; int[] e = edges.get(i); deg[e[0]]++; deg[e[1]]++; del[e[0]] = false; del[e[1]] = false; } int[] res = new int[m]; res[m - 1] = n - dlt; // for (int i = m - 1; i > 0; i--) { // int[] toremove = edges.get(i); // int t1 = toremove[0], t2 = toremove[1]; // edges.remove(i); // edgesS[t1].remove(edgesS[t1].size() - 1); // edgesS[t2].remove(edgesS[t2].size() - 1); // int tr = 0; // Arrays.fill(deg, 0); // Arrays.fill(badedge, false); // for (int j = 0; j < i; j++) { // int[] e = edges.get(j); // deg[e[0]]++; // deg[e[1]]++; // } // Arrays.fill(del, false); // for (int j = 0; j < n; j++) { // if (!del[j] && deg[j] < k) { // tr += dfs(j, k); // } // } // res[i - 1] = n - tr; // } for (int i = m - 1; i > 0; i--) { int[] toremove = edges.get(i); int t1 = toremove[0], t2 = toremove[1]; edges.remove(i); edgesS[t1].remove(edgesS[t1].size() - 1); edgesS[t2].remove(edgesS[t2].size() - 1); int tr = 0; if (!badedge[i]) { if (!del[t1] && !del[t2]) { deg[t1]--; deg[t2]--; if (!del[t1] && deg[t1] < k) { tr += dfs(t1, k); } if (!del[t2] && deg[t2] < k) { tr += dfs(t2, k); } } } res[i - 1] = res[i] - tr; } for (int i = 0; i < m; i++) { pw.println(res[i]); } } public int dfs(int u, int k) { int cnt = 0; del[u] = true; cnt++; deg[u] = 0; for (int idx : edgesS[u]) { if (badedge[idx]) continue; badedge[idx] = true; int o = get(edges.get(idx), u); if (del[o]) continue; deg[o]--; if (deg[o] < k) { cnt += dfs(o, k); } } return cnt; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader s) throws FileNotFoundException { br = new BufferedReader(s); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { FastReader reader = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int n = reader.nextInt(); int m = reader.nextInt(); int k = reader.nextInt(); Node[] graph = new Node[n]; for (int i=0; i<n; i++) graph[i] = new Node(); int[][] edges = new int[m][2]; int[] ans = new int[m]; for (int i=0; i<m; i++) { edges[i][0] = reader.nextInt()-1; edges[i][1] = reader.nextInt()-1; graph[edges[i][0]].list.add(edges[i][1]); graph[edges[i][1]].list.add(edges[i][0]); } Set<Integer> set = new HashSet<Integer>(); Queue<Integer> q = new LinkedList<Integer>(); for (int i=0; i<n; i++) { if (graph[i].list.size() < k) { q.add(i); graph[i].was = true; } set.add(i); } for (int i=m-1; i>=0; i--) { while (!q.isEmpty()) { int u = q.remove(); set.remove(u); for (int v : graph[u].list) if (set.contains(v) && !graph[v].was) { graph[v].list.remove(u); if (graph[v].list.size() < k) { q.add(v); graph[v].was = true; } } } ans[i] = set.size(); int u = edges[i][0]; int v = edges[i][1]; if (set.contains(u) && set.contains(v)) { graph[v].list.remove(u); graph[u].list.remove(v); if (graph[u].list.size() < k) { q.add(u); graph[u].was = true; } if (graph[v].list.size() < k) { q.add(v); graph[v].was = true; } } } for (int i=0; i<m; i++) writer.println(ans[i]); writer.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Node { boolean was = false; Set<Integer> list = new HashSet<Integer>(); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 3e5; int n, m, k, u[N], v[N]; int deg[N], ans[N]; vector<int> G[N], id[N]; bool ok[N]; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d %d", &u[i], &v[i]); G[u[i]].push_back(v[i]); G[v[i]].push_back(u[i]); deg[u[i]]++; deg[v[i]]++; id[u[i]].push_back(i); id[v[i]].push_back(i); } for (int i = 1; i <= n; i++) { ok[i] = 1; } int c = 0; queue<int> que; for (int i = 1; i <= n; i++) { if (deg[i] < k) { ok[i] = 0; que.push(i); c++; } } while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; deg[v]--; if (ok[v] && deg[v] < k) { ok[v] = 0; que.push(v); c++; } } } ans[m] = n - c; for (int i = m - 1; i > 0; i--) { c = 0; int x = u[i + 1], y = v[i + 1]; if (ok[x] && ok[y]) { deg[x]--; deg[y]--; if (deg[x] < k) { ok[x] = 0; c++; que.push(x); } if (deg[y] < k) { ok[y] = 0; c++; que.push(y); } } while (!que.empty()) { int z = que.front(); que.pop(); for (int j = 0; j < G[z].size(); j++) { int w = G[z][j]; if (id[z][j] >= i + 1) continue; deg[w]--; if (ok[w] && deg[w] < k) { ok[w] = 0; que.push(w); c++; } } } ans[i] = ans[i + 1] - c; } for (int i = 1; i <= m; i++) { printf("%d\n", ans[i]); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main { static OutWriter out; static InReader in; static long X = (long) (1e7); public static void main(String args[]) throws IOException { in = new InReader(); out = new OutWriter(); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); Edge edges[] = new Edge[m]; ArrayList<Integer> to[] = new ArrayList[n]; for (int i = 0; i < n; i++) { to[i] = new ArrayList(); } Vertex G[] = new Vertex[n]; for (int i = 0; i < n; i++) { G[i] = new Vertex(i, 0); } for (int i = 0; i < m; i++) { edges[i] = new Edge(in.nextInt() - 1, in.nextInt() - 1); to[edges[i].to].add(edges[i].from); to[edges[i].from].add(edges[i].to); G[edges[i].from].deg++; G[edges[i].to].deg++; } TreeSet<Vertex> all = new TreeSet<>(); for (int i = 0; i < n; i++) { all.add(G[i]); } boolean killed[] = new boolean[n]; ArrayList<Integer> ans = new ArrayList<>(); for (int i = m - 1; i >= 0; i--) { while (all.size() != 0 && all.first().deg < k) { Vertex curr = all.pollFirst(); if (!killed[curr.number]) { killed[curr.number] = true; for (int j = 0; j < to[curr.number].size(); j++) { int v = to[curr.number].get(j); if (!killed[v]) { all.remove(G[v]); G[v].deg--; all.add(G[v]); } } } } ans.add(all.size()); int a = edges[i].from; int b = edges[i].to; all.remove(G[a]); all.remove(G[b]); to[a].remove(new Integer(b)); to[b].remove(new Integer(a)); if(killed[a] || killed[b]){ }else { G[a].deg--; G[b].deg--; } all.add(G[a]); all.add(G[b]); } for (int i = ans.size() - 1; i >= 0; i--) { out.println(ans.get(i)); } out.close(); } static class Vertex implements Comparable<Vertex> { int number; int deg; Vertex(int number, int deg) { this.number = number; this.deg = deg; } @Override public int compareTo(Vertex o) { if (deg != o.deg) { return deg - o.deg; } else { return number - o.number; } } } /* static class Vertex { int number = -1; int depth = 0; boolean used2 = false; boolean used = false; long value = 0; ArrayList<Edge> to = new ArrayList(); }*/ static class Edge { int to; int from; Edge(int from, int to) { this.from = from; this.to = to; } } static class InReader { BufferedReader in; InReader(String name) throws IOException { in = new BufferedReader(new FileReader(name)); } InReader() { in = new BufferedReader(new InputStreamReader(System.in)); } StringTokenizer token = new StringTokenizer(""); void update() throws IOException { if (!token.hasMoreTokens()) { String a = in.readLine(); if (a != null) { token = new StringTokenizer(a); } } } int nextInt() throws IOException { update(); return Integer.parseInt(token.nextToken()); } long nextLong() throws IOException { update(); return Long.parseLong(token.nextToken()); } double nextDouble() throws IOException { update(); return Double.parseDouble(token.nextToken()); } boolean hasNext() throws IOException { update(); return token.hasMoreTokens(); } String next() throws IOException { update(); return token.nextToken(); } } static class OutWriter { PrintWriter out; OutWriter() { out = new PrintWriter(System.out); } OutWriter(String name) throws IOException { out = new PrintWriter(new FileWriter(name)); } StringBuilder cout = new StringBuilder(); <T> void print(T a) { cout.append(a); } <T> void println(T a) { cout.append(a); cout.append('\n'); } <T> void prints(T a) { cout.append(a); cout.append(' '); } void close() { out.print(cout.toString()); out.close(); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n, m, k; int du[maxn]; set<int> g[maxn]; vector<pair<int, int> > ar; set<pair<int, int> > st; set<int>::iterator it; int ans[maxn]; void dfs() { if (st.size() == 0) return; if (st.begin()->first < k) { int u = st.begin()->second; st.erase(st.begin()); for (it = g[u].begin(); it != g[u].end(); it++) { int v = *it; if (st.count(make_pair(du[v], v))) { st.erase(make_pair(du[v], v)); du[v]--; st.insert(make_pair(du[v], v)); } } dfs(); } } int main() { int u, v; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); du[u]++; du[v]++; g[u].insert(v); g[v].insert(u); ar.push_back(make_pair(u, v)); } for (int i = 1; i <= n; i++) { st.insert(make_pair(du[i], i)); } dfs(); for (int i = m - 1; i >= 0; i--) { ans[i] = st.size(); u = ar[i].first; v = ar[i].second; if (st.count(make_pair(du[u], u)) && st.count(make_pair(du[v], v))) { st.erase(make_pair(du[u], u)); st.erase(make_pair(du[v], v)); du[u]--; du[v]--; st.insert(make_pair(du[u], u)); st.insert(make_pair(du[v], v)); g[u].erase(v); g[v].erase(u); dfs(); } } for (int i = 0; i < m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; namespace Xrocks {} using namespace Xrocks; namespace Xrocks { class in { } user_input; class out { } output; in& operator>>(in& X, int& Y) { scanf("%d", &Y); return X; } in& operator>>(in& X, char* Y) { scanf("%s", Y); return X; } in& operator>>(in& X, float& Y) { scanf("%f", &Y); return X; } in& operator>>(in& X, double& Y) { scanf("%lf", &Y); return X; } in& operator>>(in& X, char& C) { scanf("%c", &C); return X; } in& operator>>(in& X, string& Y) { cin >> Y; return X; } in& operator>>(in& X, long long& Y) { scanf("%lld", &Y); return X; } template <typename T> in& operator>>(in& X, vector<T>& Y) { for (auto& x : Y) user_input >> x; return X; } template <typename T> out& operator<<(out& X, const T& Y) { cout << Y; return X; } template <typename T> out& operator<<(out& X, vector<T>& Y) { for (auto& x : Y) output << x << " "; return X; } out& operator<<(out& X, const int& Y) { printf("%d", Y); return X; } out& operator<<(out& X, const char& C) { printf("%c", C); return X; } out& operator<<(out& X, const string& Y) { printf("%s", Y.c_str()); return X; } out& operator<<(out& X, const long long& Y) { printf("%lld", Y); return X; } out& operator<<(out& X, const float& Y) { printf("%f", Y); return X; } out& operator<<(out& X, const double& Y) { printf("%lf", Y); return X; } out& operator<<(out& X, const char Y[]) { printf("%s", Y); return X; } template <typename T> T max(T A) { return A; } template <typename T, typename... args> T max(T A, T B, args... S) { return max(A > B ? A : B, S...); } template <typename T> T min(T A) { return A; } template <typename T, typename... args> T min(T A, T B, args... S) { return min(A < B ? A : B, S...); } template <typename T> void vectorize(int y, vector<T>& A) { A.resize(y); } template <typename T, typename... args> void vectorize(int y, vector<T>& A, args&&... S) { A.resize(y); vectorize(y, S...); } long long fast(long long a, long long b, long long pr) { if (b == 0) return 1 % pr; long long ans = 1 % pr; while (b) { if (b & 1) ans = (ans * a) % pr; b >>= 1; a = (a * a) % pr; } return ans; } int readInt() { int n = 0; int ch = getchar_unlocked(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar_unlocked(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getchar_unlocked(); n = n * sign; return n; } long long readLong() { long long n = 0; int ch = getchar_unlocked(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar_unlocked(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getchar_unlocked(); n = n * sign; return n; } long long readBin() { long long n = 0; int ch = getchar_unlocked(); int sign = 1; while (ch < '0' || ch > '1') { if (ch == '-') sign = -1; ch = getchar_unlocked(); } while (ch >= '0' && ch <= '1') n = (n << 1) + (ch - '0'), ch = getchar_unlocked(); return n; } long long inv_(long long val, long long pr = static_cast<long long>(998244353)) { return fast(val, pr - 2, pr); } } // namespace Xrocks class solve { public: solve() { int n, m, k; user_input >> n >> m >> k; vector<pair<int, int> > Edges(m); vector<int> Ans(m); vector<int> degree(n); vector<vector<pair<int, int> > > adj(n); set<pair<int, int> > Good_set; vector<int> in_good_set(n, true); for (int i = 0; i < m; i++) { user_input >> Edges[i].first >> Edges[i].second; Edges[i].first--; Edges[i].second--; adj[Edges[i].first].push_back({Edges[i].second, i}); adj[Edges[i].second].push_back({Edges[i].first, i}); degree[Edges[i].first]++; degree[Edges[i].second]++; } for (int i = 0; i < n; i++) { Good_set.insert({degree[i], i}); } while (!Good_set.empty() && Good_set.begin()->first < k) { int node = Good_set.begin()->second; for (auto& y : adj[node]) { int x = y.first; if (in_good_set[x]) { Good_set.erase({degree[x], x}); --degree[x]; Good_set.insert({degree[x], x}); } } Good_set.erase({degree[node], node}); in_good_set[node] = false; } for (int i = m - 1; i >= 0; i--) { Ans[i] = Good_set.size(); int u = Edges[i].first, v = Edges[i].second; if (in_good_set[u] && in_good_set[v]) { Good_set.erase({degree[u], u}); --degree[u]; Good_set.insert({degree[u], u}); Good_set.erase({degree[v], v}); --degree[v]; Good_set.insert({degree[v], v}); while (!Good_set.empty() && Good_set.begin()->first < k) { int node = Good_set.begin()->second; for (auto& y : adj[node]) { int x = y.first; if (y.second >= i) continue; if (in_good_set[x]) { Good_set.erase({degree[x], x}); --degree[x]; Good_set.insert({degree[x], x}); } } Good_set.erase({degree[node], node}); in_good_set[node] = false; } } } for (int i = 0; i < m; i++) { output << Ans[i] << "\n"; } } }; int32_t main() { int t = 1, i = 1; if (0 || 0) scanf("%d", &t); while (t--) { if (0) printf("Case #%d: ", i++); new solve; } output << "\n"; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * */ public class E { static ArrayList<Pair>[] g; static int k; static boolean[] s; static int size; static int[] degree; static Queue<Integer> q = new LinkedList<>(); public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); k = Integer.parseInt(st.nextToken()); s = new boolean[n]; g = new ArrayList[n]; degree = new int[n]; int[][] f = new int[m][2]; for (int i = 0; i < m; i++) { st = new StringTokenizer(reader.readLine()); int a = Integer.parseInt(st.nextToken())-1; int b = Integer.parseInt(st.nextToken())-1; if (g[a] == null) { g[a] = new ArrayList<>(); } if (g[b] == null) { g[b] = new ArrayList<>(); } g[a].add(new Pair(b,i)); g[b].add(new Pair(a,i)); degree[a]++;degree[b]++; f[i][0] = a; f[i][1] = b; } for (int i = 0; i < n; i++) { if(degree[i]>0){ if(degree[i]>=k){ s[i] = true; size ++; }else{ q.add(i); } } } while (!q.isEmpty()){ int v = q.poll(); for (Pair u : g[v]) { if(s[u.a]) { degree[u.a]--; if (degree[u.a] < k) { q.add(u.a); s[u.a] = false; size--; } } } } int[] ans = new int[m]; for (int i = m-1; i >=0; i--) { ans[i] = size; int a = f[i][0]; int b = f[i][1]; if(s[a] && s[b]) { degree[a]--;degree[b]--; if (degree[a] < k){ q.add(a); s[a] = false; size--; } if (degree[b] < k){ q.add(b); s[b] = false; size--; } while (!q.isEmpty()){ int v = q.poll(); for (Pair u : g[v]) { if(u.b < i && s[u.a]) { degree[u.a]--; if (degree[u.a] < k) { q.add(u.a); s[u.a] = false; size--; } } } } } } for (int i = 0; i < m; i++) { System.out.println(ans[i]); } } static class Pair{ int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 2e5 + 10; const int maxm = 1.5e7 + 10; const int mod = 998244353; using namespace std; int n, m, k, ans; int num[maxn], vis[maxn], sum[maxn]; pair<int, int> a[maxn]; set<int> st[maxn]; void check(int x) { if (num[x] >= k || !vis[x]) return; vis[x] = 0; ans--; queue<int> q; q.push(x); while (!q.empty()) { int tmp = q.front(); q.pop(); set<int>::iterator it = st[tmp].begin(); while (it != st[tmp].end()) { num[*it]--; if (vis[*it] && num[*it] < k) { q.push(*it); vis[*it] = 0; ans--; } it++; } } } int main() { scanf("%d%d%d", &n, &m, &k); ans = n; for (int i = 1; i <= n; i++) vis[i] = 1; for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); a[i].first = x, a[i].second = y; num[x]++, num[y]++; st[x].insert(y), st[y].insert(x); } for (int i = 1; i <= n; i++) check(i); for (int i = m - 1; i >= 0; i--) { sum[i] = ans; if (vis[a[i].second]) num[a[i].first]--; if (vis[a[i].first]) num[a[i].second]--; st[a[i].first].erase(a[i].second); st[a[i].second].erase(a[i].first); check(a[i].first), check(a[i].second); } for (int i = 0; i < m; i++) printf("%d\n", sum[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 3; int n, m, k, deg[N], x[N], y[N], ans[N], cnt; bool del[N]; set<int> G[N]; set<int>::iterator it; void Del(int rt) { if (deg[rt] >= k || del[rt]) return; queue<int> q; q.push(rt); del[rt] = 1; cnt--; while (!q.empty()) { int u = q.front(); q.pop(); for (it = G[u].begin(); it != G[u].end(); it++) { int v = *it; deg[v]--; if (deg[v] < k && !del[v]) { del[v] = 1; q.push(v); cnt--; } } } } int main() { memset(deg, 0, sizeof deg); scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", x + i, y + i); deg[x[i]]++; deg[y[i]]++; G[x[i]].insert(y[i]); G[y[i]].insert(x[i]); } cnt = n; for (int i = 1; i <= n; i++) Del(i); for (int i = m; i; i--) { ans[i] = cnt; if (!del[x[i]]) deg[y[i]]--; if (!del[y[i]]) deg[x[i]]--; G[x[i]].erase(y[i]); G[y[i]].erase(x[i]); Del(x[i]); Del(y[i]); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int l[200100], r[200100]; int n, m, k; int de[200100]; bool ex[200100]; struct ind { int id, de; ind(int a, int b) : id(a), de(b) {} bool operator<(const ind &b) const { return de != b.de ? de < b.de : id < b.id; } }; vector<int> pa[200100]; set<ind> S; int fd(int id, int x) { return l[id] == x ? r[id] : l[id]; } void erase(int x) { if (de[x] < k) return; S.erase(ind(x, de[x])); de[x]--; S.insert(ind(x, de[x])); } void dele(int x) { S.erase(ind(x, de[x])); for (auto y : pa[x]) if (!ex[y] && de[fd(y, x)] >= k) erase(fd(y, x)); } int main() { scanf("%d%d%d", &n, &m, &k); int a, b; for (int i = 1; i <= m; ++i) { scanf("%d%d", &a, &b); ++de[a], ++de[b]; l[i] = a, r[i] = b; pa[a].push_back(i); pa[b].push_back(i); } for (int i = 1; i <= n; ++i) S.insert(ind(i, de[i])); int as[200100]; for (int i = m; i >= 1; --i) { while (S.size()) { if ((*S.begin()).de < k) dele((*S.begin()).id); else break; } as[i] = S.size(); int x = l[i], y = r[i]; if (de[x] >= k && de[y] >= k) erase(x), erase(y); ex[i] = 1; } for (int i = 1; i <= m; ++i) printf("%d\n", as[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; set<int> v[200005]; int x[200005]; int y[200005]; int an[200005]; set<pair<int, int> > s; void r(int i, int k) { if (v[x[i]].size() >= k && v[y[i]].size() >= k) { s.erase(pair<int, int>(v[x[i]].size(), x[i])); s.erase(pair<int, int>(v[y[i]].size(), y[i])); v[x[i]].erase(y[i]); v[y[i]].erase(x[i]); s.insert(pair<int, int>(v[x[i]].size(), x[i])); s.insert(pair<int, int>(v[y[i]].size(), y[i])); } } void solve(int k) { if (s.empty() || s.begin()->first >= k) return; int x = s.begin()->second; s.erase(s.begin()); for (auto it = v[x].begin(); it != v[x].end(); it++) if (v[*it].size() >= k) { s.erase(pair<int, int>(v[*it].size(), *it)); v[*it].erase(x); s.insert(pair<int, int>(v[*it].size(), *it)); } solve(k); } int main() { ios::sync_with_stdio(false); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> x[i] >> y[i]; v[x[i]].insert(y[i]); v[y[i]].insert(x[i]); } for (int i = 0; i < n; i++) { s.insert(pair<int, int>(v[i + 1].size(), i + 1)); } for (int i = m - 1; i >= 0; i--) { solve(k); an[i] = s.size(); r(i, k); } for (int i = 0; i < m; i++) cout << an[i] << endl; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
//package que_a; import java.io.*; import java.util.*; import java.math.*; public class utkarsh { InputStream is; PrintWriter out; long mod = (long) (1e9 + 7); boolean SHOW_TIME; class pair implements Comparator { int F, S; pair(int f, int s) { F = f; S = s; } @Override public int compare(Object U, Object V) { pair u = (pair)U; pair v = (pair)V; if(u.F != v.F) return u.F - v.F; return u.S - v.S; } } void solve() { //Enter code here utkarsh //SHOW_TIME = true; int n = ni(), m = ni(), k = ni(); HashSet <pair> h[] = new HashSet[n]; for(int i = 0; i < n; i++) h[i] = new HashSet<>(); int deg[] = new int[n]; int ans[] = new int[m]; boolean vst[] = new boolean[m]; pair mp[] = new pair[m]; for(int i = 0; i < m; i++) { int u = ni()-1, v = ni()-1; h[u].add(new pair(v, i)); h[v].add(new pair(u, i)); deg[u]++; deg[v]++; mp[i] = new pair(u, v); } ArrayDeque <Integer> q = new ArrayDeque<>(); for(int i = 0; i < n; i++) { if(deg[i] < k) q.add(i); } int s = n; for(int i = m-1; i >= 0; i--) { while(!q.isEmpty()) { s--; int y = q.poll(); for(pair p : h[y]) { if(vst[p.S]) continue; if(deg[p.F] == k) q.add(p.F); deg[p.F]--; vst[p.S] = true; h[p.F].remove(new pair(y, p.S)); } } ans[i] = s; if(!vst[i]) { vst[i] = true; if(deg[mp[i].F] == k) { q.add(mp[i].F); } if(deg[mp[i].S] == k) { q.add(mp[i].S); } deg[mp[i].F]--; deg[mp[i].S]--; h[mp[i].F].remove(new pair(mp[i].S, i)); h[mp[i].S].remove(new pair(mp[i].F, i)); } } for(int x : ans) out.println(x); } //---------- I/O Template ---------- public static void main(String[] args) { new utkarsh().run(); } void run() { is = System.in; out = new PrintWriter(System.out); long start = System.currentTimeMillis(); solve(); long end = System.currentTimeMillis(); if(SHOW_TIME) out.println("\n" + (end - start) + " ms"); out.flush(); } byte input[] = new byte[1024]; int len = 0, ptr = 0; int readByte() { if(ptr >= len) { ptr = 0; try { len = is.read(input); } catch(IOException e) { throw new InputMismatchException(); } if(len <= 0) { return -1; } } return input[ptr++]; } boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); } int skip() { int b = readByte(); while(b != -1 && isSpaceChar(b)) { b = readByte(); } return b; } char nc() { return (char)skip(); } String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } int ni() { int n = 0, b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } if(b == -1) { return -1; } //no input while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } long nl() { long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } double nd() { return Double.parseDouble(ns()); } float nf() { return Float.parseFloat(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) { a[i] = ni(); } return a; } char[] ns(int n) { char c[] = new char[n]; int i, b = skip(); for(i = 0; i < n; i++) { if(isSpaceChar(b)) { break; } c[i] = (char)b; b = readByte(); } return i == n ? c : Arrays.copyOf(c,i); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int u[200005], v[200005]; int cnt[200005]; unordered_set<int> adj[200005]; unordered_set<int> take; stack<int> ans; queue<int> cac, toerase; int n, m, k; void emptycac() { while (cac.size()) { int node = cac.front(); cac.pop(); for (auto j : adj[node]) { if (take.count(j)) { adj[j].erase(node); if (adj[j].size() < k) { take.erase(j); cac.push(j); } } } while (toerase.size()) { take.erase(toerase.front()); toerase.pop(); } } } signed main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> u[i] >> v[i]; adj[u[i]].insert(v[i]); adj[v[i]].insert(u[i]); } for (int i = 1; i <= n; i++) { if (adj[i].size() >= k) { take.insert(i); for (auto j : adj[i]) { take.insert(j); } } } for (auto i : take) { if (adj[i].size() < k) { cac.push(i); toerase.push(i); } } while (toerase.size()) { take.erase(toerase.front()); toerase.pop(); } emptycac(); for (int i = m; i >= 1; i--) { ans.push(take.size()); if (take.count(u[i]) && take.count(v[i])) { adj[u[i]].erase(v[i]); adj[v[i]].erase(u[i]); if (adj[v[i]].size() < k) { take.erase(v[i]); cac.push(v[i]); } if (adj[u[i]].size() < k) { take.erase(u[i]); cac.push(u[i]); } emptycac(); } } while (ans.size()) { cout << ans.top() << endl; ans.pop(); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<pair<int, int> > arr(m); vector<vector<int> > G(n); vector<int> in(n), vis(n); for (auto& it : arr) { int u, v; cin >> u >> v; in[--u]++; in[--v]++; it = {u, v}; G[u].push_back(v); G[v].push_back(u); } queue<int> q; for (int i = 0; i < n; i++) { if (in[i] < k) { q.push(i); vis[i] = 1; } } int cur = n - q.size(); set<pair<int, int> > cnt; while (!q.empty()) { auto x = q.front(); q.pop(); for (auto& it : G[x]) { auto node = minmax(x, it); if (cnt.find(node) == cnt.end()) { cnt.insert(node); if (!vis[it] && --in[it] < k) { vis[it] = 1; q.push(it); cur--; } } } } reverse(arr.begin(), arr.end()); vector<int> ans; for (auto& it : arr) { ans.push_back(cur); auto node = minmax(it.first, it.second); if (cnt.find(node) == cnt.end()) { cnt.insert(node); if (!vis[it.first] && --in[it.first] < k) { vis[it.first] = 1; q.push(it.first); cur--; } if (!vis[it.second] && --in[it.second] < k) { vis[it.second] = 1; q.push(it.second); cur--; } while (!q.empty()) { auto x = q.front(); q.pop(); for (auto& jt : G[x]) { auto node = minmax(x, jt); if (cnt.find(node) == cnt.end()) { cnt.insert(node); if (!vis[jt] && --in[jt] < k) { vis[jt] = 1; q.push(jt); cur--; } } } } } } reverse(ans.begin(), ans.end()); for (auto& it : ans) cout << it << endl; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; vector<int> v[maxn]; pair<long long, long long> a[maxn]; long long q[maxn], siz[maxn]; bool c[maxn]; long long n, m, k; set<pair<long long, long long> > s; map<pair<long long, long long>, bool> mark; int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (long long y = 0; y < m; y++) { cin >> a[y].first >> a[y].second; v[a[y].first].push_back(a[y].second), v[a[y].second].push_back(a[y].first); siz[a[y].second]++, siz[a[y].first]++; } for (long long y = 1; y <= n; y++) s.insert(pair<long long, long long>(siz[y], y)); for (long long y = m - 1; y >= 0; y--) { if (y != m - 1 && !mark[a[y + 1]]) { mark[pair<long long, long long>(a[y + 1].second, a[y + 1].first)] = mark[pair<long long, long long>(a[y + 1].first, a[y + 1].second)] = 1; long long i = a[y + 1].second; siz[i]--; if (c[i] == 0) { pair<long long, long long> x = pair<long long, long long>(siz[i] + 1, i); s.erase(x); x.first--; s.insert(x); } i = a[y + 1].first; siz[i]--; if (c[i] == 0) { pair<long long, long long> x = pair<long long, long long>(siz[i] + 1, i); s.erase(x); x.first--; s.insert(x); } } while (s.size() != 0) { pair<long long, long long> x = *s.begin(); if (x.first >= k) break; s.erase(x); c[x.second] = 1; for (long long ii = 0; ii < v[x.second].size(); ii++) { long long i = v[x.second][ii]; if (!mark[pair<long long, long long>(x.second, i)]) { mark[pair<long long, long long>(x.second, i)] = mark[pair<long long, long long>(i, x.second)] = 1; siz[i]--; siz[x.second]--; if (!c[i]) { pair<long long, long long> z = pair<long long, long long>(siz[i] + 1, i); s.erase(z); z.first--; s.insert(z); } } } } q[y] = s.size(); } for (long long y = 0; y < m; y++) cout << q[y] << endl; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 210000; int n, m, k; int s[MAXN], t[MAXN]; int d[MAXN]; int ans[MAXN], vis[MAXN]; vector<int> edge[MAXN]; pair<int, int> mk(int x, int y) { return x > y ? make_pair(y, x) : make_pair(x, y); } set<pair<int, int> > S; queue<int> q; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d %d", &s[i], &t[i]); edge[s[i]].push_back(t[i]); edge[t[i]].push_back(s[i]); d[s[i]]++, d[t[i]]++; } int cnt = n; for (int i = 1; i <= n; i++) { if (d[i] < k) { q.push(i); vis[i] = 1; cnt--; } } while (!q.empty()) { int now = q.front(); q.pop(); for (unsigned i = 0; i < edge[now].size(); i++) { int v = edge[now][i]; if (S.count(mk(now, v))) continue; S.insert(mk(now, v)); d[now]--, d[v]--; if (d[v] < k && vis[v] == 0) { q.push(v); vis[v] = 1; cnt--; } } } for (int i = m; i >= 1; --i) { ans[i] = cnt; if (vis[s[i]] || vis[t[i]] || S.count(mk(s[i], t[i]))) continue; S.insert(mk(s[i], t[i])); d[s[i]]--, d[t[i]]--; if (d[s[i]] < k && vis[s[i]] == 0) { q.push(s[i]); vis[s[i]] = 1; cnt--; } if (d[t[i]] < k && vis[t[i]] == 0) { q.push(t[i]); vis[t[i]] = 1; cnt--; } while (!q.empty()) { int now = q.front(); q.pop(); for (unsigned i = 0; i < edge[now].size(); i++) { int v = edge[now][i]; if (S.count(mk(now, v))) continue; d[now]--, d[v]--; if (d[v] < k && vis[v] == 0) { S.insert(mk(now, v)); q.push(v); vis[v] = 1; cnt--; } } } } for (int i = 1; i <= m; i++) { printf("%d\n", ans[i]); } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.util.Arrays; import java.util.Iterator; import java.io.IOException; import java.util.AbstractSequentialList; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.util.LinkedList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Asgar Javadov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { int k; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); k = in.nextInt(); LinkedList<Integer>[] adj = new LinkedList[n]; int[] result = new int[m]; int[] degree = new int[n]; for (int i = 0; i < n; i++) { adj[i] = new LinkedList<>(); } int[] u = new int[m]; int[] v = new int[m]; for (int i = 0; i < m; ++i) { u[i] = in.nextInt() - 1; v[i] = in.nextInt() - 1; adj[u[i]].add(i); adj[v[i]].add(i); degree[u[i]]++; degree[v[i]]++; } IntList que = new IntList(n); boolean[] alive = ArrayUtils.createArray(m, true); for (int i = 0; i < n; ++i) if (degree[i] < k) que.add(i); int head = 0; for (int day = m - 1; day >= 0; --day) { while (head < que.size()) { int id = que.get(head); for (Iterator<Integer> it = adj[id].iterator(); it.hasNext(); ) { int edgeId = it.next(); if (!alive[edgeId]) { it.remove(); continue; } int other = u[edgeId] == id ? v[edgeId] : u[edgeId]; if (degree[other] == k) { que.add(other); } degree[other]--; degree[id]--; alive[edgeId] = false; it.remove(); } ++head; } result[day] = n - que.size(); if (alive[day]) { if (degree[u[day]] == k) que.add(u[day]); if (degree[v[day]] == k) que.add(v[day]); degree[u[day]]--; degree[v[day]]--; alive[day] = false; } } // while (!set.isEmpty() && set.first().first < k) { // IntPair pair = set.first(); // for (Iterator<Integer> it = adj[pair.second].iterator(); it.hasNext(); ) { // int edge = it.next(); // int e = v[edge] == pair.second ? u[edge] : v[edge]; // set.remove(degree[e]); // degree[e].first--; // set.add(degree[e]); // it.remove(); // } // set.remove(pair); // alive[pair.second] = false; // } // // for (int i = m - 1; i >= 0; --i) { // result[i] = set.size(); // if (alive[u[i]] && alive[v[i]]) { // set.remove(degree[u[i]]); // degree[u[i]].first--; // set.add(degree[u[i]]); // // set.remove(degree[v[i]]); // degree[v[i]].first--; // set.add(degree[v[i]]); // // while (!set.isEmpty() && set.first().first < k) { // IntPair pair = set.first(); // for (Iterator<Integer> it = adj[pair.second].iterator(); it.hasNext(); ) { // int edge = it.next(); // it.remove(); // if (edge < i) { // int e = v[edge] == pair.second ? u[edge] : v[edge]; // if (!alive[e]) continue; // set.remove(degree[e]); // degree[e].first--; // set.add(degree[e]); // } // } // set.remove(pair); // alive[pair.second] = false; // } // } // } for (int e : result) out.println(e); } } static class InputReader extends BufferedReader { StringTokenizer tokenizer; public InputReader(InputStream inputStream) { super(new InputStreamReader(inputStream), 32768); } public InputReader(String filename) { super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename))); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(readLine()); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public Integer nextInt() { return Integer.valueOf(next()); } } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(outputStream); } public OutputWriter(Writer writer) { super(writer); } public OutputWriter(String filename) throws FileNotFoundException { super(filename); } public void close() { super.close(); } } static class IntList implements Iterable<Integer> { public static final int INITIAL_CAPACITY = 4; private int size; private int[] array; public IntList() { this(INITIAL_CAPACITY); } public IntList(int initialCapacity) { this.array = new int[initialCapacity]; this.size = 0; } public IntList(int[] array) { this.array = Arrays.copyOf(array, array.length); this.size = this.array.length; } public void add(int value) { if (size == array.length) { ensureCapacity(); } this.array[this.size++] = value; } public int get(int index) { if (index < 0 || index >= size) throw new IllegalArgumentException("Expected argument in [0, " + size + "), but found " + index); return array[index]; } public int size() { return this.size; } private void ensureCapacity() { if (size < array.length) return; this.array = Arrays.copyOf(array, array.length << 1); } public IntList clone() { IntList cloned = new IntList(Math.max(1, this.size)); for (int i = 0; i < size; ++i) cloned.add(array[i]); return cloned; } public Iterator<Integer> iterator() { return new IntListIterator(); } private class IntListIterator implements Iterator<Integer> { private int current; public IntListIterator() { this.current = 0; } public boolean hasNext() { return this.current < size; } public Integer next() { return array[current++]; } } } static class ArrayUtils { public static boolean[] createArray(int count, boolean value) { boolean[] array = new boolean[count]; Arrays.fill(array, value); return array; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class e{ static List<Edge> edges[]; static int[] esize; static boolean[] stay; static boolean[] used; static int k; static int[] u,v; static int bfs(int id){ int res =0; esize[u[id]]--;esize[v[id]]--; used[id]=true; Deque<Integer> que = new ArrayDeque<>(); if(esize[u[id]]<k){ que.add(u[id]); stay[u[id]]=true; ++res; } if(esize[v[id]]<k){ que.add(v[id]); stay[v[id]]=true; ++res; } while(!que.isEmpty()){ int p = que.poll(); esize[p]=0; for(Edge e: edges[p])if(!used[e.id] && !stay[e.to]){ used[e.id]=true; esize[e.to]--; if(esize[e.to]<k){ que.add(e.to);stay[e.to]=true; ++res; } } } return res; } static class Edge{ int to, id; Edge(int to, int id){this.to=to;this.id=id;} } static void solve(){ int n =ni(), m=ni(); k=ni(); u = new int[m]; v = new int[m]; edges = new List[n]; used = new boolean[m]; for(int i=0;i<n;++i)edges[i]=new ArrayList<>(); for(int i=0;i<m;++i){ u[i]=ni()-1; v[i]=ni()-1; edges[u[i]].add(new Edge(v[i], i)); edges[v[i]].add(new Edge(u[i], i)); } int remain = n; esize= new int[n]; for(int i=0;i<n;++i)esize[i]=edges[i].size(); stay = new boolean[n]; Deque<Integer> que = new ArrayDeque<>(); for(int i=0;i<n;++i)if(esize[i]<k){ que.add(i); stay[i]=true; --remain; } while(!que.isEmpty()){ int p = que.poll(); esize[p]=0; for(Edge e: edges[p])if(!used[e.id] && !stay[e.to]){ esize[e.to]--; used[e.id]=true; if(esize[e.to]<k){ que.add(e.to);stay[e.to]=true; --remain; } } } int[] ans = new int[m]; ans[m-1]=remain; for(int i=m-2;i>=0;--i){ if(!stay[u[i+1]]&&!stay[v[i+1]]){ remain -= bfs(i+1); } ans[i]=remain; } for(int a: ans)out.println(a); } public static void main(String[] args){ solve(); out.flush(); } private static InputStream in = System.in; private static PrintWriter out = new PrintWriter(System.out); private static final byte[] buffer = new byte[1<<15]; private static int ptr = 0; private static int buflen = 0; private static boolean hasNextByte(){ if(ptr<buflen)return true; ptr = 0; try{ buflen = in.read(buffer); } catch (IOException e){ e.printStackTrace(); } return buflen>0; } private static int readByte(){ if(hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isSpaceChar(int c){ return !(33<=c && c<=126);} private static int skip(){int res; while((res=readByte())!=-1 && isSpaceChar(res)); return res;} private static double nd(){ return Double.parseDouble(ns()); } private static char nc(){ return (char)skip(); } private static String ns(){ StringBuilder sb = new StringBuilder(); for(int b=skip();!isSpaceChar(b);b=readByte())sb.append((char)b); return sb.toString(); } private static int[] nia(int n){ int[] res = new int[n]; for(int i=0;i<n;++i)res[i]=ni(); return res; } private static long[] nla(int n){ long[] res = new long[n]; for(int i=0;i<n;++i)res[i]=nl(); return res; } private static int ni(){ int res=0,b; boolean minus=false; while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); if(b=='-'){ minus=true; b=readByte(); } for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); return minus ? -res:res; } private static long nl(){ long res=0,b; boolean minus=false; while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); if(b=='-'){ minus=true; b=readByte(); } for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); return minus ? -res:res; } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.util.*; import java.io.*; public class E { static int mod = (int) (1e9+7); static InputReader in; static PrintWriter out; static class SegmentTree { int st[][]; SegmentTree(int n) { st = new int[4*n][2]; build(0, n - 1, 1); } int getMid(int s, int e) { return (s+e)>>1; } int[] merge(int[] a,int[] b){ if(a[0] < b[0]) return a; return b; } void update(int s, int e, int x, int y, int c, int si){ if(s == x && e == y){ st[si][0] += c; } else{ int mid = getMid(s, e); if(y <= mid) update(s, mid, x, y, c, 2*si); else if(x > mid) update(mid + 1, e, x ,y ,c ,2*si + 1); else{ update(s, mid, x, mid, c, 2*si); update(mid + 1, e, mid + 1, y, c, 2*si + 1); } st[si] = merge(st[2*si],st[2*si+1]); } } int[] get(int s, int e, int x, int y, int si){ if(s == x && e == y){ return st[si]; } int mid = getMid(s, e); if(y <= mid) return get(s, mid, x, y, 2*si); else if(x > mid) return get(mid + 1, e, x, y, 2*si + 1); return merge(get(s, mid, x, mid, 2*si), get(mid + 1, e, mid + 1, y, 2*si + 1)); } void build(int ss, int se, int si){ if (ss == se) { st[si][1] = ss; return; } int mid = getMid(ss, se); build(ss, mid, si * 2 ); build(mid + 1, se, si * 2 + 1); st[si] = merge(st[2*si],st[2*si+1]); } } public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); ArrayList<Pair>[] set = new ArrayList[n]; for(int i = 0; i < n; i++) set[i] = new ArrayList<>(); int[] x = new int[m]; int[] y = new int[m]; for(int i = 0; i < m; i++) { x[i] = in.nextInt() - 1; y[i] = in.nextInt() - 1; set[x[i]].add(new Pair(y[i], i)); set[y[i]].add(new Pair(x[i], i)); } SegmentTree seg = new SegmentTree(n); for(int i = 0; i < n; i++) { seg.update(0, n - 1, i, i, set[i].size(), 1); } int[] ans = new int[m]; int j = m; int cnt = n; boolean[] vis = new boolean[m]; while(j > 0) { j--; while(true) { int[] a = seg.st[1]; if(a[0] >= k) break; cnt--; for(Pair p : set[a[1]]) { if(vis[p.y]) continue; seg.update(0, n - 1, p.x, p.x, -1, 1); vis[p.y] = true; } seg.update(0, n - 1, a[1], a[1], -a[0] + Integer.MAX_VALUE, 1); } // debug(j,vis); ans[j] = cnt; if(vis[j]) continue; seg.update(0, n - 1, x[j], x[j], -1, 1); seg.update(0, n - 1, y[j], y[j], -1, 1); vis[j] = true; } for(int i : ans) out.println(i); out.close(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class Pair implements Comparable<Pair> { int x,y; Pair(int x, int y){ this.x = x; this.y = y; } public int compareTo(Pair o) { if(this.x == o.x) return -Integer.compare(this.y, o.y); return Integer.compare(this.x, o.x); } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y==y; } return false; } @Override public String toString() { return x + " "+ y; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static long gcd(long x, long y) { if (y == 0) { return x; } else { return gcd(y, x % y); } } static int gcd(int x, int y) { if (y == 0) { return x; } else { return gcd(y, x % y); } } static long pow(long n, long p, long m) { long result = 1; if (p == 0) { return 1; } while (p != 0) { if (p % 2 == 1) { result *= n; } if (result >= m) { result %= m; } p >>= 1; n *= n; if (n >= m) { n %= m; } } return result; } static long pow(long n, long p) { long result = 1; if (p == 0) { return 1; } while (p != 0) { if (p % 2 == 1) { result *= n; } p >>= 1; n *= n; } return result; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
//package manthan2018; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Queue; import java.util.Random; public class E2 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(), K = ni(); int[] from = new int[m]; int[] to = new int[m]; for(int i = 0;i < m;i++){ from[i] = ni()-1; to[i] = ni()-1; } int[][] g = packU(n, from, to); Random gen = new Random(); for(int i = 0;i < n;i++){ shuffle(g[i], gen); Arrays.sort(g[i]); } int[] deg = new int[n]; for(int i = 0;i < n;i++)deg[i] = g[i].length; boolean[][] con = new boolean[n][]; for(int i = 0;i < n;i++){ con[i] = new boolean[g[i].length]; Arrays.fill(con[i], true); } Queue<Integer> q = new ArrayDeque<>(); for(int i = 0;i < n;i++)q.add(i); int non = n; boolean[] on = new boolean[n]; Arrays.fill(on, true); while(!q.isEmpty()){ int cur = q.poll(); if(on[cur] && deg[cur] < K){ on[cur] = false; non--; for(int j = 0;j < g[cur].length;j++){ int e = g[cur][j]; if(!con[cur][j])continue; con[cur][Arrays.binarySearch(g[cur], e)] = false; con[e][Arrays.binarySearch(g[e], cur)] = false; deg[e]--; q.add(e); } } } int[] anss = new int[m]; for(int i = m-1;i >= 0;i--){ anss[i] = non; int f = from[i], t = to[i]; if(!con[f][Arrays.binarySearch(g[f], t)])continue; deg[f]--; deg[t]--; con[f][Arrays.binarySearch(g[f], t)] = false; con[t][Arrays.binarySearch(g[t], f)] = false; q.add(f); q.add(t); while(!q.isEmpty()){ int cur = q.poll(); if(on[cur] && deg[cur] < K){ on[cur] = false; non--; for(int j = 0;j < g[cur].length;j++){ int e = g[cur][j]; if(!con[cur][j])continue; con[cur][Arrays.binarySearch(g[cur], e)] = false; con[e][Arrays.binarySearch(g[e], cur)] = false; deg[e]--; q.add(e); } } } } for(int v : anss){ out.println(v); } } public static int[] shuffle(int[] a, Random gen){ for(int i = 0, n = a.length;i < n;i++){ int ind = gen.nextInt(n-i)+i; int d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E2().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010, M = 400010, mod = 1e9 + 7; int n, m, k; int h[N], e[M], ne[M], idx; int a[N], b[N], ans[N]; int ind[N], oud[N], cnt; bool stn[N], stm[M]; struct Edge { int a, b; } edge[M]; void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } queue<int> q; void delet_node() { while (!q.empty()) { int t = q.front(); q.pop(); for (int i = h[t]; ~i; i = ne[i]) { if (stm[i]) continue; int j = e[i]; ind[j]--; if (ind[j] < k && !stn[j]) { q.push(j), stn[j] = 1, cnt--; } } } } void topsort() { for (int i = 1; i <= n; i++) { if (ind[i] < k) q.push(i), cnt--, stn[i] = 1; } delet_node(); for (int i = m; i >= 1; i--) { ans[i] = cnt; int a = edge[i].a, b = edge[i].b; if (!stn[a] && !stn[b]) ind[a]--, ind[b]--; stm[--idx] = 1, stm[--idx] = 1; if (ind[a] < k && !stn[a]) q.push(a), stn[a] = 1, cnt--; if (ind[b] < k && !stn[b]) q.push(b), stn[b] = 1, cnt--; delet_node(); } for (int i = 1; i <= m; i++) cout << ans[i] << endl; } int main() { memset(h, -1, sizeof h); scanf("%d%d%d", &n, &m, &k); cnt = n; for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); edge[i] = {a, b}; add(a, b), add(b, a); ind[a]++, ind[b]++; } topsort(); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
//package contests.CF1037; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class E { static ArrayList<Integer>[] adj; static int[] vis, going; static int NO = 3, YES = 2, VIS = 1; static int k; public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); adj = new ArrayList[n]; for (int i = 0; i < adj.length; i++) { adj[i] = new ArrayList<>(); } vis = new int[n]; going = new int[n]; int m = sc.nextInt(); k = sc.nextInt(); HashSet<Long> e = new HashSet<>(); int[][] edges = new int[m][2]; for (int i = 0; i < m; i++) { edges[i][0] = sc.nextInt()-1; edges[i][1] = sc.nextInt()-1; adj[edges[i][0]].add(edges[i][1]); adj[edges[i][1]].add(edges[i][0]); e.add(hash(edges[i][0], edges[i][1])); } TreeSet<Integer> set = new TreeSet<>(new Comparator<Integer>() { @Override public int compare(Integer i, Integer j) { ; if(going[i] != going[j]) return going[i] - going[j]; return i - j; } }); Arrays.fill(vis, YES); for (int i = 0; i < going.length; i++) { going[i] = adj[i].size(); } for (int i = 0; i < n; i++) { set.add(i); } while(!set.isEmpty() && going[set.first()] < k){ int a = set.pollFirst(); vis[a] = NO; for (int b : adj[a]) { if(vis[b] == YES && e.contains(hash(a, b))){ set.remove(b); going[b]--; set.add(b); e.remove(hash(a, b)); } } } int[] ans = new int[m]; for (int q = m-1; q >= 0; q--) { ans[q] = set.size(); int u = edges[q][0]; int v = edges[q][1]; if(vis[u] == YES) { set.remove(u); if(vis[v] == YES && e.contains(hash(u, v))) going[u]--; set.add(u); } if(vis[v] == YES) { set.remove(v); if(vis[u] == YES && e.contains(hash(u, v))) going[v]--; set.add(v); } e.remove(hash(u, v)); while(!set.isEmpty() && going[set.first()] < k){ int a = set.pollFirst(); vis[a] = NO; for (int b : adj[a]) { if(vis[b] == YES && e.contains(hash(a, b))){ set.remove(b); going[b]--; set.add(b); e.remove(hash(a, b)); } } } } for (int i = 0; i < m; i++) { pw.println(ans[i]); } pw.flush(); pw.close(); } static long hash(int a, int b){ return Math.min(a, b) * 1000000l + Math.max(a, b); } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const long long MOD = 1e9 + 7; pair<int, int> es[200000]; vector<pair<int, int> > g[200000]; int degree[200000]; set<pair<int, int> > st; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; scanf("%d%d%d", &n, &m, &k); for (long long i = 0; i < m; i++) { scanf("%d%d", &es[i].first, &es[i].second); es[i].first--, es[i].second--; g[es[i].first].push_back({es[i].second, i}); g[es[i].second].push_back({es[i].first, i}); degree[es[i].first]++, degree[es[i].second]++; } reverse(es, es + m); for (long long i = 0; i < n; i++) st.insert({degree[i], i}); while (!st.empty() && st.begin()->first < k) { auto v = st.begin(); for (pair<int, int> &i : g[v->second]) { int to = i.first; if (st.find({degree[to], to}) != st.end()) { st.erase({degree[to], to}); degree[to]--; st.insert({degree[to], to}); } } st.erase(v); } vector<int> out; for (long long j = 0; j < m; j++) { out.push_back(st.size()); int u = es[j].first, v = es[j].second; if (st.find({degree[u], u}) != st.end() && st.find({degree[v], v}) != st.end()) { st.erase({degree[u], u}); degree[u]--; st.insert({degree[u], u}); st.erase({degree[v], v}); degree[v]--; st.insert({degree[v], v}); while (!st.empty() && st.begin()->first < k) { auto v = st.begin(); for (pair<int, int> &i : g[v->second]) { if (i.second >= m - j - 1) continue; int to = i.first; if (st.find({degree[to], to}) != st.end()) { st.erase({degree[to], to}); degree[to]--; st.insert({degree[to], to}); } } st.erase(v); } } } reverse(begin(out), end(out)); for (int i : out) printf("%d\n", i); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T, typename Pr = less<T>> using pq = priority_queue<T, vector<T>, Pr>; using i64 = long long int; using ii = pair<int, int>; using ii64 = pair<i64, i64>; set<int> adj[200005]; bool removed[200005]; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); vector<ii> edges(m); for (int i = 0; i < m; i++) { scanf("%d %d", &edges[i].first, &edges[i].second); adj[edges[i].first].insert(edges[i].second); adj[edges[i].second].insert(edges[i].first); } set<ii> nodes; for (int i = 1; i <= n; i++) nodes.emplace(adj[i].size(), i); reverse((edges).begin(), (edges).end()); vector<int> ans(m); for (int i = 0; i < m; i++) { while (!nodes.empty() && nodes.begin()->first < k) { auto x = nodes.begin()->second; removed[x] = true; nodes.erase(nodes.begin()); for (auto& e : adj[x]) { nodes.erase(ii(adj[e].size(), e)); adj[e].erase(x); nodes.emplace(adj[e].size(), e); } } ans[m - 1 - i] = nodes.size(); auto& [x, y] = edges[i]; if (removed[x] || removed[y]) continue; nodes.erase(ii(adj[x].size(), x)); nodes.erase(ii(adj[y].size(), y)); adj[x].erase(y); adj[y].erase(x); nodes.emplace(adj[x].size(), x); nodes.emplace(adj[y].size(), y); } for (auto& ai : ans) printf("%d\n", ai); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(long long& x, long long y, long long mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, long long mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } vector<pair<int, int> > mp[200105]; int u[200105], v[200105], d[200105], ans[200105]; bool vis[200105], del[200105]; void fmain(int ID) { scanf("%d%d%d", &n, &m, &k); for (int(i) = 1; (i) <= (int)(m); (i)++) { scanf("%d%d", u + i, v + i); d[u[i]]++; d[v[i]]++; mp[u[i]].push_back({v[i], i}); mp[v[i]].push_back({u[i], i}); } set<pair<int, int> > q; for (int(i) = 1; (i) <= (int)(n); (i)++) q.insert({d[i], i}); int sz = n; for (int i = m; i; i--) { while (!q.empty() && q.begin()->first < k) { int x = q.begin()->second; q.erase(q.begin()); del[x] = 1; sz--; for (auto cp : mp[x]) if (!vis[cp.second] && !del[cp.first]) { vis[cp.second] = 1; int y = cp.first; q.erase({d[y], y}); d[y]--; q.insert({d[y], y}); } } ans[i] = sz; if (vis[i]) continue; vis[i] = 1; int y = u[i]; q.erase({d[y], y}); d[y]--; q.insert({d[y], y}); y = v[i]; q.erase({d[y], y}); d[y]--; q.insert({d[y], y}); } for (int(i) = 1; (i) <= (int)(m); (i)++) printf("%d\n", ans[i]); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.StringTokenizer; import java.util.Set; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { int n; int m; int k; Set<Integer> going = new HashSet<>(); Vertex[] vs; public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); vs = new Vertex[n]; for (int i = 0; i < n; ++i) { vs[i] = new Vertex(i); going.add(i); } int[] res = new int[m]; Edge[] edges = new Edge[m]; for (int i = 0; i < m; ++i) { int x = in.nextInt() - 1; int y = in.nextInt() - 1; vs[x].adj.add(y); vs[y].adj.add(x); edges[i] = new Edge(x, y); } for (int i = 0; i < n; ++i) { vs[i].erase(); } for (int i = m - 1; i >= 0; --i) { res[i] = going.size(); int from = edges[i].from; int to = edges[i].to; vs[from].adj.remove(to); vs[to].adj.remove(from); vs[from].erase(); vs[to].erase(); } for (int i = 0; i < m; ++i) { out.println(res[i]); } } class Edge { int from; int to; public Edge(int from, int to) { this.from = from; this.to = to; } } class Vertex { int id; boolean marked; Set<Integer> adj = new HashSet<>(); Vertex(int id) { this.id = id; } void erase() { this.marked = true; if (this.adj.size() < k && going.contains(this.id)) { going.remove(this.id); for (int vId : this.adj) { if (vs[vId].marked) continue; vs[vId].adj.remove(this.id); vs[vId].erase(); } } this.marked = false; } } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; set<int> st[N], s; int ans[N], x[N], y[N]; int n, m, k; void dfs(int v) { if (st[v].size() >= k || !s.count(v)) return; s.erase(v); for (auto u : st[v]) { st[u].erase(v); if (s.count(u) && st[u].size() < k) { dfs(u); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &x[i], &y[i]); st[x[i]].insert(y[i]); st[y[i]].insert(x[i]); } for (int i = 1; i <= n; ++i) s.insert(i); for (int i = 1; i <= n; i++) { if (st[i].size() < k) { dfs(i); } } ans[m] = s.size(); for (int i = m; i > 1; i--) { st[x[i]].erase(y[i]); st[y[i]].erase(x[i]); if (st[x[i]].size() < k) dfs(x[i]); if (st[y[i]].size() < k) dfs(y[i]); ans[i - 1] = s.size(); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxn = 2e5 + 9; set<int> g[mxn]; int k; set<int> se; void can_now(int u) { if (g[u].size() < k && se.find(u) != se.end()) { se.erase(u); for (auto &v : g[u]) { g[v].erase(u); can_now(v); } } } int res[mxn]; pair<int, int> e[mxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int i, j, n, m, t, u, v; cin >> n >> m >> k; for (i = 1; i <= m; i++) { cin >> u >> v; g[u].insert(v); g[v].insert(u); e[i] = {u, v}; } for (i = 1; i <= n; i++) se.insert(i); for (i = 1; i <= n; i++) can_now(i); for (i = m; i >= 1; i--) { res[i] = se.size(); u = e[i].first, v = e[i].second; g[u].erase(v); g[v].erase(u); can_now(u); can_now(v); } for (i = 1; i <= m; i++) cout << res[i] << endl; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.List; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int m = in.readInt(); int k = in.readInt(); int[] to = new int[m]; int[] from = new int[m]; int[] degree = new int[n]; boolean[] alive = new boolean[m]; Arrays.fill(alive, true); List<Integer>[] edges = new ArrayList[n]; for (int i = 0; i < n; ++i) { edges[i] = new ArrayList<>(); } for (int i = 0; i < m; ++i) { to[i] = in.readInt() - 1; from[i] = in.readInt() - 1; degree[to[i]]++; degree[from[i]]++; edges[to[i]].add(i); edges[from[i]].add(i); } int[] q = new int[n]; int back = 0; int front = 0; for (int i = 0; i < n; i++) { if (degree[i] < k) { q[front++] = i; } } int[] res = new int[m]; for (int i = m - 1; i >= 0; --i) { while (back < front) { int cur = q[back++]; for (int edge : edges[cur]) { if (alive[edge]) { int other = to[edge] + from[edge] - cur; alive[edge] = false; if (degree[other] == k) { q[front++] = other; } degree[other]--; degree[cur]--; } } } res[i] = n - front; if (alive[i]) { if (degree[from[i]] == k) { q[front++] = from[i]; } if (degree[to[i]] == k) { q[front++] = to[i]; } degree[from[i]]--; degree[to[i]]--; alive[i] = false; } } for (int i = 0; i < m; i++) { out.println(res[i]); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { // InputMismatchException -> UnknownError if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } else if (c == '+') { c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int K = in.nextInt(); List<int[]>[] g = new List[n]; for (int i = 0; i < n; ++i) g[i] = new ArrayList<>(); int[] from = new int[m]; int[] to = new int[m]; int[] deg = new int[n]; for (int i = 0; i < m; ++i) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; from[i] = u; to[i] = v; g[u].add(new int[]{v, i}); g[v].add(new int[]{u, i}); ++deg[u]; ++deg[v]; } int[] queue = new int[n]; int sizeQ = 0; for (int i = 0; i < n; ++i) { if (deg[i] < K) { queue[sizeQ++] = i; } } int ptr = 0; int[] ans = new int[m]; boolean[] dead = new boolean[m]; for (int step = m - 1; step >= 0; --step) { while (ptr < sizeQ) { int cur = queue[ptr]; for (int[] v : g[cur]) { int next = v[0]; int id = v[1]; if (dead[id]) continue; dead[id] = true; if (deg[next] == K) queue[sizeQ++] = next; --deg[next]; --deg[cur]; } ++ptr; } ans[step] = n - sizeQ; if (!dead[step]) { if (deg[from[step]] == K) queue[sizeQ++] = from[step]; if (deg[to[step]] == K) queue[sizeQ++] = to[step]; --deg[from[step]]; --deg[to[step]]; dead[step] = true; } } for (int aa : ans) out.println(aa); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; int a[200010], b[200010]; vector<pair<int, int> > G[200010]; int deg[200010]; int ans[200010], cnt; queue<int> q; inline void spfa(int x) { while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i].first; int s = G[u][i].second; if (s >= x) continue; if (deg[v] >= k) { deg[v]--; if (deg[v] < k) { q.push(v); cnt--; } } } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", a + i, b + i); deg[a[i]]++; deg[b[i]]++; G[a[i]].push_back(pair<int, int>(b[i], i)); G[b[i]].push_back(pair<int, int>(a[i], i)); } cnt = n; for (int i = 1; i <= n; i++) if (deg[i] < k) q.push(i), cnt--; spfa(m + 1); ans[m + 1] = cnt; for (int i = m; i > 1; i--) { if (deg[a[i]] >= k && deg[b[i]] >= k) { deg[a[i]]--; deg[b[i]]--; if (deg[a[i]] < k) q.push(a[i]), cnt--; if (deg[b[i]] < k) q.push(b[i]), cnt--; } spfa(i); ans[i] = cnt; } for (int i = 2; i <= m + 1; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(3) #pragma GCC optimize(2) using namespace std; const int maxn = 2e5 + 10; int N, M, K; set<int> g[maxn]; set<pair<int, int> > person; int deg[maxn]; int ans[maxn]; pair<int, int> edges[maxn]; int main() { scanf("%d %d %d", &N, &M, &K); for (int i = 1; i <= M; ++i) { scanf("%d %d", &edges[i].first, &edges[i].second); g[edges[i].first].insert(edges[i].second); g[edges[i].second].insert(edges[i].first); ++deg[edges[i].first]; ++deg[edges[i].second]; } for (int i = 1; i <= N; ++i) { person.insert(make_pair(deg[i], i)); } for (int i = M; i >= 1; --i) { int x = edges[i].first, y = edges[i].second; while (!person.empty()) { auto it = person.begin(); if (it->first >= K) { break; } int v = it->second; for (int u : g[v]) { person.erase(make_pair(deg[u], u)); if (deg[u]) person.insert(make_pair(--deg[u], u)); g[u].erase(v); } g[v].clear(); person.erase(make_pair(deg[v], v)); deg[v] = 0; } ans[i] = person.size(); if (g[x].find(y) != g[x].end()) { person.erase(make_pair(deg[x], x)); person.erase(make_pair(deg[y], y)); g[y].erase(x); g[x].erase(y); if (deg[x]) person.insert(make_pair(--deg[x], x)); if (deg[y]) person.insert(make_pair(--deg[y], y)); } } for (int i = 1; i <= M; ++i) { printf("%d\n", ans[i]); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<long long int, long long int> m1, m2; const long long int N = 3 * 1e5; vector<long long int>::iterator itr, itr1; int main() { int n, m, k; cin >> n >> m >> k; vector<pair<int, int> > Edges(m); vector<int> Ans(m); vector<int> degree(n); vector<vector<pair<int, int> > > adj(n); set<pair<int, int> > Good_set; vector<int> in_good_set(n, true); for (int i = 0; i < m; i++) { cin >> Edges[i].first >> Edges[i].second; Edges[i].first--; Edges[i].second--; adj[Edges[i].first].push_back({Edges[i].second, i}); adj[Edges[i].second].push_back({Edges[i].first, i}); degree[Edges[i].first]++; degree[Edges[i].second]++; } for (int i = 0; i < n; i++) { Good_set.insert({degree[i], i}); } while (!Good_set.empty() && Good_set.begin()->first < k) { int node = Good_set.begin()->second; for (auto &y : adj[node]) { int x = y.first; if (in_good_set[x]) { Good_set.erase({degree[x], x}); --degree[x]; Good_set.insert({degree[x], x}); } } Good_set.erase({degree[node], node}); in_good_set[node] = false; } for (int i = m - 1; i >= 0; i--) { Ans[i] = Good_set.size(); int u = Edges[i].first, v = Edges[i].second; if (in_good_set[u] && in_good_set[v]) { Good_set.erase({degree[u], u}); --degree[u]; Good_set.insert({degree[u], u}); Good_set.erase({degree[v], v}); --degree[v]; Good_set.insert({degree[v], v}); while (!Good_set.empty() && Good_set.begin()->first < k) { int node = Good_set.begin()->second; for (auto &y : adj[node]) { int x = y.first; if (y.second >= i) continue; if (in_good_set[x]) { Good_set.erase({degree[x], x}); --degree[x]; Good_set.insert({degree[x], x}); } } Good_set.erase({degree[node], node}); in_good_set[node] = false; } } } for (int i = 0; i < m; i++) { cout << Ans[i] << "\n"; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\n"; err(++it, args...); } vector<pair<int, int> > edge(200003); vector<vector<pair<int, int> > > ad(200003); int sz[200003]; stack<int> pq; int node; int process(int k, int j) { int i, x, a, s; while (!pq.empty()) { s = pq.top(); pq.pop(); node--; sz[s] = 0; x = ad[s].size(); for (i = 0; i < x; i++) { a = ad[s][i].first; if (ad[s][i].second <= j) { sz[a]--; if (sz[a] == k - 1) pq.push(a); } } } return node; } int main() { int n, m, k, i, a, b; scanf("%d%d%d", &n, &m, &k); for (i = 0; i < m; i++) { scanf("%d%d", &a, &b); a--, b--; ad[a].emplace_back(b, i); ad[b].emplace_back(a, i); sz[a]++, sz[b]++; edge[i] = {a, b}; } vector<int> ans(m); for (i = 0; i < n; i++) if (sz[i] < k) pq.push(i); node = n; int j; for (i = m - 1; i >= 0; i--) { ans[i] = process(k, i); tie(a, b) = edge[i]; if (sz[a] > 0 && sz[b] > 0) { if (sz[a] == k) pq.push(a); if (sz[b] == k) pq.push(b); sz[a]--, sz[b]--; } } for (i = 0; i < m; i++) printf("%d\n", ans[i]); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, M, K; int xList[200005], yList[200005]; int deg[200005], ans[200005]; set<int> Adj[200005]; set<pair<int, int> > s; set<pair<int, int> >::iterator p; void work(int u) { s.erase(make_pair(deg[u], u)); int v; set<int>::iterator k; for (k = Adj[u].begin(); k != Adj[u].end(); k++) { v = (*k); if (!s.count(make_pair(deg[v], v))) { deg[v]--; continue; } s.erase(make_pair(deg[v], v)); deg[v]--; if (deg[v] >= K) s.insert(make_pair(deg[v], v)); else work(v); } } int main() { scanf("%d%d%d", &N, &M, &K); int u, v; for (int i = 1; i <= M; i++) { scanf("%d%d", &u, &v); Adj[u].insert(v); Adj[v].insert(u); deg[u]++; deg[v]++; xList[i] = u; yList[i] = v; } for (int i = 1; i <= N; i++) { s.insert(make_pair(deg[i], i)); } while (!s.empty()) { p = s.begin(); if (p->first < K) work(p->second); else break; } ans[M] = s.size(); for (int i = M; i >= 1; i--) { u = xList[i]; v = yList[i]; ans[i - 1] = s.size(); if (!s.count(make_pair(deg[u], u)) || !s.count(make_pair(deg[v], v))) continue; Adj[u].erase(v); Adj[v].erase(u); if (s.count(make_pair(deg[u], u))) { s.erase(make_pair(deg[u], u)); deg[u]--; s.insert(make_pair(deg[u], u)); } if (s.count(make_pair(deg[v], v))) { s.erase(make_pair(deg[v], v)); deg[v]--; s.insert(make_pair(deg[v], v)); } while (!s.empty()) { p = s.begin(); if (p->first < K) work(p->second); else break; } ans[i - 1] = s.size(); } for (int i = 1; i <= M; i++) printf("%d\n", ans[i]); getchar(); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct edge { int from, to, vis, next; } e[400020]; int top, head[200020]; vector<int> g[200020]; int in[200020], gg[200020]; int n, m, k; int ans[200020], cnt; void add_edge(int from, int to) { e[++top].to = to; e[top].from = from; e[top].next = head[from]; head[from] = top; } void add(int from, int to) { add_edge(from, to); add_edge(to, from); } queue<int> q; void del(int pos) { if (!gg[pos]) cnt--; q.push(pos); gg[pos] = 1; while (!q.empty()) { int now = q.front(); q.pop(); for (int i = head[now]; ~i; i = e[i].next) { if (e[i].vis || gg[e[i].to]) continue; in[e[i].to]--; e[i].vis = 1; e[i ^ 1].vis = 1; if (in[e[i].to] < k) { cnt--; gg[e[i].to] = 1; q.push(e[i].to); } } } } int main() { scanf("%d%d%d", &n, &m, &k); cnt = n; top = -1; memset(head, -1, sizeof(head)); int from, to; for (int i = 1; i <= m; i++) { scanf("%d%d", &from, &to); in[from]++, in[to]++; add(from, to); } for (int i = 1; i <= n; i++) { if (head[i] == -1) cnt--, gg[i] = 1; } for (int i = 1; i <= n; i++) { if (in[i] < k && !gg[i]) del(i); } int ttt = m; for (int i = 2 * m - 1; i >= 1; i -= 2) { ans[ttt--] = cnt; if (e[i].vis) continue; in[e[i].from]--; in[e[i].to]--; e[i].vis = e[i ^ 1].vis = 1; if (!gg[e[i].from] && in[e[i].from] < k) del(e[i].from); if (!gg[e[i].to] && in[e[i].to] < k) del(e[i].to); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
python2
from collections import defaultdict n, m, k = map(int, raw_input().split()) xys = [map(int, raw_input().split()) for _ in range(m)][::-1] gv = defaultdict(set) for x, y in xys: gv[x].add(y) gv[y].add(x) q = [u for u, vs in tuple(gv.items()) if len(vs) < k] i = 0 while i < len(q): u = q[i] i += 1 if u not in gv: continue for v in gv[u]: try: gv[v].remove(u) except KeyError: pass if len(gv[v]) < k: q.append(v) del gv[u] anss = [len(gv)] for x, y in xys: try: gv[x].remove(y) except KeyError: pass try: gv[y].remove(x) except KeyError: pass q = [] if len(gv[x]) < k: q.append(x) if len(gv[y]) < k: q.append(y) i = 0 while i < len(q): u = q[i] i += 1 if u not in gv: continue for v in gv[u]: try: gv[v].remove(u) except KeyError: pass if len(gv[v]) < k: q.append(v) del gv[u] anss.append(len(gv)) print('\n'.join(map(str, reversed(anss[:-1]))))
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.util.*; import java.math.BigInteger; import java.util.Map.Entry; import static java.lang.Math.*; public class E extends PrintWriter { class Node { final int id; final Map<Node, Edge> edges = new HashMap<>(); boolean use = true; public Node(int id) { this.id = id; } } class Edge { final Node u, v; boolean use = true; public Edge(Node u, Node v) { this.u = u; this.v = v; } } void run() { int n = nextInt(), m = nextInt(), k = nextInt(); Node[] nodes = new Node[n]; for (int i = 0; i < n; i++) { nodes[i] = new Node(i); } Edge[] edges = new Edge[m]; for (int j = 0; j < m; j++) { Node u = nodes[nextInt() - 1], v = nodes[nextInt() - 1]; edges[j] = new Edge(u, v); u.edges.put(v, edges[j]); v.edges.put(u, edges[j]); } Queue<Node> bad = new ArrayDeque<>(n); int cnt = n; int[] ans = new int[m + 1]; for (int j = m; j >= 0; j--) { ans[j] = cnt; if (j == m) { for (Node node : nodes) { if (node.edges.size() < k) { node.use = false; bad.add(node); } } } else { if (edges[j].use) { edges[j].use = false; Node u = edges[j].u; Node v = edges[j].v; if (u.use) { u.edges.remove(v); if (u.edges.size() < k) { u.use = false; bad.add(u); } } if (v.use) { v.edges.remove(u); if (v.edges.size() < k) { v.use = false; bad.add(v); } } } } while (!bad.isEmpty()) { Node u = bad.poll(); --cnt; for (Iterator<Entry<Node, Edge>> it = u.edges.entrySet().iterator(); it.hasNext();) { Entry<Node, Edge> entry = it.next(); Node v = entry.getKey(); Edge edge = entry.getValue(); if (edge.use) { edge.use = false; if (v.use) { v.edges.remove(u); if (v.edges.size() < k) { v.use = false; bad.add(v); } } } it.remove(); } } } for (int j = 0; j < m; j++) { println(ans[j]); } } void skip() { while (hasNext()) { next(); } } int[][] nextMatrix(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j] = nextInt(); return matrix; } String next() { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(nextLine()); return tokenizer.nextToken(); } boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String line = nextLine(); if (line == null) { return false; } tokenizer = new StringTokenizer(line); } return true; } int[] nextArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { return reader.readLine(); } catch (IOException err) { return null; } } public E(OutputStream outputStream) { super(outputStream); } static BufferedReader reader; static StringTokenizer tokenizer = new StringTokenizer(""); static Random rnd = new Random(); static boolean OJ; public static void main(String[] args) throws IOException { OJ = System.getProperty("ONLINE_JUDGE") != null; E solution = new E(System.out); if (OJ) { reader = new BufferedReader(new InputStreamReader(System.in)); solution.run(); } else { reader = new BufferedReader(new FileReader(new File(E.class.getName() + ".txt"))); long timeout = System.currentTimeMillis(); while (solution.hasNext()) { solution.run(); solution.println(); solution.println("----------------------------------"); } solution.println("time: " + (System.currentTimeMillis() - timeout)); } solution.close(); reader.close(); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, m; int u[200005], v[200005]; set<int> e[200005]; int deg[200005]; bool alive[200005]; int alc; queue<int> delet; void del(int x) { alive[x] = false; alc--; for (int y : e[x]) { if (alive[y]) { deg[y]--; if (deg[y] == k - 1) { delet.push(y); } } } } void peck(int x) { deg[x]--; if (deg[x] == k - 1) { delet.push(x); } } void clearq() { while (delet.size()) { int x = delet.front(); delet.pop(); del(x); } } int ans[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> u[i] >> v[i]; u[i]--; v[i]--; deg[u[i]]++; deg[v[i]]++; e[u[i]].insert(v[i]); e[v[i]].insert(u[i]); } fill(alive, alive + n, true); alc = n; for (int i = 0; i < n; i++) if (deg[i] < k) delet.push(i); for (int i = m - 1; i >= 0; i--) { clearq(); ans[i] = alc; e[u[i]].erase(v[i]); e[v[i]].erase(u[i]); if (alive[u[i]] && alive[v[i]]) { peck(u[i]); peck(v[i]); } } for (int i = 0; i < m; i++) cout << ans[i] << '\n'; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct edge { int to, net; }; int head[2000000]; int e_n; edge edges[2000000]; int deg[2000000]; bool sd[2000000]; int v1[2000000], v2[2000000]; set<pair<int, int> > help; set<pair<int, int> > tt; void Init() { help.clear(); e_n = 0; memset(sd, 0, sizeof(sd)); memset(deg, 0, sizeof(deg)); memset(head, -1, sizeof(head)); } void add(int f, int t) { int tmp = head[f]; edges[e_n++] = edge{t, tmp}; head[f] = e_n - 1; } int main(void) { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m, k; Init(); cin >> n >> m >> k; int i, j, x, y; for (i = 1; i <= m; i++) { cin >> x >> y; v1[i] = x, v2[i] = y; add(x, y); add(y, x); ++deg[x], ++deg[y]; } for (i = 1; i <= n; i++) { help.insert(make_pair(deg[i], i)); } list<int> res; for (i = m; i >= 1; i--) { while (!help.empty() && help.begin()->first < k) { int cur = help.begin()->second; sd[cur] = 1; help.erase(help.begin()); for (j = head[cur]; j != -1; j = edges[j].net) { int t = edges[j].to; if (!tt.count(make_pair(cur, t))) { help.erase(help.find(make_pair(deg[t], t))); deg[t]--; help.insert(make_pair(deg[t], t)); tt.insert(make_pair(cur, t)); tt.insert(make_pair(t, cur)); } } } res.push_front(help.size()); if (sd[v1[i]] == 0 && sd[v2[i]] == 0) { help.erase(help.find(make_pair(deg[v1[i]], v1[i]))); help.insert(make_pair(--deg[v1[i]], v1[i])); help.erase(help.find(make_pair(deg[v2[i]], v2[i]))); help.insert(make_pair(--deg[v2[i]], v2[i])); } tt.insert(make_pair(v1[i], v2[i])); tt.insert(make_pair(v2[i], v1[i])); } for (auto a : res) cout << a << "\n"; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = (int)2e5 + 5; int n, m, k; vector<pair<int, int> > out[maxn]; int la[maxn]; int pos[maxn]; int deg[maxn]; int add[maxn]; bool dead[maxn]; set<pair<int, int>, greater<pair<int, int> > > s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; fill(la, la + n, -1); for (auto i = 0; i < m; ++i) { int v, u; cin >> v >> u; --v; --u; out[v].push_back(make_pair(i, u)); out[u].push_back(make_pair(i, v)); ++deg[v]; ++deg[u]; if (deg[v] == k) { pos[v] = out[v].size() - 1; la[v] = i; } if (deg[u] == k) { pos[u] = out[u].size() - 1; la[u] = i; } } for (auto i = 0; i < n; ++i) if (la[i] == -1) la[i] = m; for (auto i = 0; i < n; ++i) s.insert(make_pair(la[i], i)); while (!s.empty()) { int v = s.begin()->second; dead[v] = true; s.erase(s.begin()); for (auto e : out[v]) { int u = e.second; if (dead[u] || la[u] == la[v]) continue; if (la[u] < e.first) continue; s.erase(make_pair(la[u], u)); ++pos[u]; while (pos[u] != (int)out[u].size() && dead[out[u][pos[u]].second]) ++pos[u]; if (pos[u] == (int)out[u].size()) la[u] = m; else la[u] = out[u][pos[u]].first; la[u] = min(la[u], la[v]); s.insert(make_pair(la[u], u)); } } for (auto i = 0; i < n; ++i) add[la[i]]++; int curr = 0; for (auto i = 0; i < m; ++i) { curr += add[i]; cout << curr << '\n'; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Array; import java.nio.charset.Charset; import java.util.*; public class CFContest { public static void main(String[] args) throws Exception { boolean local = System.getProperty("ONLINE_JUDGE") == null; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("E:\\DATABASE\\TESTCASE\\CFContest.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; public Task(FastIO io) { this.io = io; } @Override public void run() { solve(); } public void solve() { int n = io.readInt(); int m = io.readInt(); int k = io.readInt(); Node[] nodes = new Node[n + 1]; for (int i = 1; i <= n; i++) { nodes[i] = new Node(); nodes[i].id = i; } int[][] edge = new int[m][2]; for (int i = 0; i < m; i++) { edge[i][0] = io.readInt(); edge[i][1] = io.readInt(); Node a = nodes[edge[i][0]]; Node b = nodes[edge[i][1]]; a.set.add(b); b.set.add(a); } TreeSet<Node> nodeTreeSet = new TreeSet<>(new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { int d = o1.set.size() - o2.set.size(); return d == 0 ? o1.id - o2.id : d; } }); for (int i = 1; i <= n; i++) { nodeTreeSet.add(nodes[i]); } checkSet(nodeTreeSet, k); int[] result = new int[m]; result[m - 1] = nodeTreeSet.size(); for (int i = m - 1; i >= 1; i--) { Node a = nodes[edge[i][0]]; Node b = nodes[edge[i][1]]; if (a.throwAway || b.throwAway) { result[i - 1] = nodeTreeSet.size(); continue; } nodeTreeSet.remove(a); nodeTreeSet.remove(b); a.set.remove(b); b.set.remove(a); nodeTreeSet.add(a); nodeTreeSet.add(b); checkSet(nodeTreeSet, k); result[i - 1] = nodeTreeSet.size(); } for (int i = 0; i < m; i++) { io.cache.append(result[i]).append('\n'); } } public static void checkSet(TreeSet<Node> nodeTreeSet, int k) { while (!nodeTreeSet.isEmpty()) { Node min = nodeTreeSet.first(); if (min.set.size() >= k) { break; } nodeTreeSet.remove(min); min.throwAway = true; for (Node nearBy : min.set) { nodeTreeSet.remove(nearBy); nearBy.set.remove(min); nodeTreeSet.add(nearBy); } } } } public static class Randomized { static Random random = new Random(); public static double nextDouble(double min, double max) { return random.nextDouble() * (max - min) + min; } public static void randomizedArray(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(long[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); long tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(double[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); double tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(float[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); float tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static <T> void randomizedArray(T[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); T tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } public static class Node { Set<Node> set = new HashSet<>(); int id; boolean throwAway; @Override public String toString() { return "" + id; } } public static class FastIO { private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 8); public final StringBuilder cache = new StringBuilder(); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { long num = readLong(); if (next != '.') { return num; } next = read(); long divisor = 1; long later = 0; while (next >= '0' && next <= '9') { divisor = divisor * 10; later = later * 10 + next - '0'; next = read(); } if (num >= 0) { return num + (later / (double) divisor); } else { return num - (later / (double) divisor); } } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public void flush() { try { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } catch (IOException e) { throw new RuntimeException(e); } } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Memory { public static <T> void swap(T[] data, int i, int j) { T tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> void swap(int[] data, int i, int j) { int tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> void swap(char[] data, int i, int j) { char tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> int min(T[] data, int from, int to, Comparator<T> cmp) { int m = from; for (int i = from + 1; i < to; i++) { if (cmp.compare(data[m], data[i]) > 0) { m = i; } } return m; } public static <T> void move(T[] data, int from, int to, int step) { int len = to - from; step = len - (step % len + len) % len; Object[] buf = new Object[len]; for (int i = 0; i < len; i++) { buf[i] = data[(i + step) % len + from]; } System.arraycopy(buf, 0, data, from, len); } public static <T> void reverse(T[] data, int f, int t) { int l = f, r = t - 1; while (l < r) { swap(data, l, r); l++; r--; } } public static void copy(Object[] src, Object[] dst, int srcf, int dstf, int len) { if (len < 8) { for (int i = 0; i < len; i++) { dst[dstf + i] = src[srcf + i]; } } else { System.arraycopy(src, srcf, dst, dstf, len); } } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200005; int n, m, k, ans[N], in[N], cnt; std::map<int, bool> vis[N]; std::vector<int> G[N]; std::vector<std::pair<int, int> > e; std::queue<int> q; void topo() { while (!q.empty()) { int u = q.front(); q.pop(); in[u] = 0; for (int i = 0; i < (signed)G[u].size(); i++) { int v = G[u][i]; if (vis[u][v]) continue; vis[u][v] = vis[v][u] = 1, in[v]--; if (in[v] == k - 1) cnt--, q.push(v); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1, x, y; i <= m; i++) { scanf("%d%d", &x, &y); e.push_back(std::make_pair(x, y)); G[x].push_back(y), G[y].push_back(x); in[x]++, in[y]++; } cnt = n; for (int i = 1; i <= n; i++) if (in[i] < k) cnt--, q.push(i); topo(), ans[m] = cnt; for (int i = m; i >= 2; i--) { int u = e[i - 1].first, v = e[i - 1].second; if (vis[u][v]) { ans[i - 1] = ans[i]; continue; } vis[u][v] = vis[v][u] = 1; in[u]--, in[v]--; if (in[u] == k - 1) cnt--, q.push(u); if (in[v] == k - 1) cnt--, q.push(v); topo(), ans[i - 1] = cnt; } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf32 = 1e9 + 9; const long long inf64 = 1e18 + 18; const int N = 2e5 + 5; const long long mod = 1e9 + 9; vector<int> gr[N]; set<pair<int, int> > deg; int pw[N]; bool used[N]; void solve() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<pair<int, int> > edges(m); vector<int> ans(m); for (int i = 0; i < m; ++i) { scanf("%d%d", &edges[i].first, &edges[i].second); --edges[i].first, --edges[i].second; gr[edges[i].first].push_back(edges[i].second); gr[edges[i].second].push_back(edges[i].first); } for (int i = 0; i < n; ++i) { deg.insert({gr[i].size(), i}); pw[i] = gr[i].size(); } memset(used, true, N); set<pair<int, int> > usedP; for (int i = m - 1; i >= 0; --i) { while (!deg.empty() && deg.begin()->first < k) { pair<int, int> p = *deg.begin(); used[deg.begin()->second] = false; deg.erase(deg.begin()); for (auto &x : gr[p.second]) { if (used[x] && !(usedP.count({x, p.second}) || usedP.count({p.second, x}))) { deg.erase({pw[x], x}); deg.insert({--pw[x], x}); } } } ans[i] = deg.size(); if (!ans[i]) break; if (used[edges[i].first] && used[edges[i].second]) { deg.erase({pw[edges[i].first], edges[i].first}); deg.insert({--pw[edges[i].first], edges[i].first}); deg.erase({pw[edges[i].second], edges[i].second}); deg.insert({--pw[edges[i].second], edges[i].second}); usedP.insert({edges[i].first, edges[i].second}); } } for (auto &x : ans) printf("%d\n", x); } int main() { solve(); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> struct pnt { int hd; int ind; bool ded; } p[1000000]; struct ent { int twd; int lst; } e[1000000]; int cnt; int n, m, k; int lft; int ans[1000000]; int u[1000000], v[1000000]; void ade(int f, int t) { cnt++; e[cnt].twd = t; e[cnt].lst = p[f].hd; p[f].hd = cnt; p[f].ind++; return; } void Delete(int x) { if (p[x].ded) return; p[x].ded = true; lft--; for (int i = p[x].hd; i; i = e[i].lst) { int to = e[i].twd; p[to].ind--; if (p[to].ind < k) Delete(to); } return; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &u[i], &v[i]); ade(u[i], v[i]); ade(v[i], u[i]); } lft = n; for (int i = 1; i <= n; i++) { if (p[i].ind < k) { Delete(i); } } for (int i = m; i; i--) { ans[i] = lft; p[u[i]].hd = e[p[u[i]].hd].lst; p[v[i]].hd = e[p[v[i]].hd].lst; if (!p[v[i]].ded) p[u[i]].ind--; if (!p[u[i]].ded) p[v[i]].ind--; if (p[v[i]].ind < k) Delete(v[i]); if (p[u[i]].ind < k) Delete(u[i]); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.Reader; import java.util.ArrayList; import java.util.HashSet; import java.util.StringTokenizer; import java.util.TreeSet; public class E { static final long MODULO = (long) (1e9 + 7); public static void main(String[] args) { BufferedScanner scanner = new BufferedScanner(); PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out)); int t = 1;//scanner.nextInt(); for (int tc = 0; tc < t; tc++) { int n = scanner.nextInt(); int m = scanner.nextInt(); int k = scanner.nextInt(); int[][] edges = new int[m][]; SetInt[] adj = new SetInt[n]; for (int i = 0; i < n; i++) { adj[i] = new SetInt(); } for (int i = 0; i < m; i++) { int a = scanner.nextInt() - 1; int b = scanner.nextInt() - 1; adj[a].add(b); adj[b].add(a); edges[i] = new int[]{a, b}; } TreeSet<Item> s = new TreeSet<>(); for (int i = 0; i < n; i++) { s.add(new Item(i, adj[i].size())); } // adjust(k, adj, s, deg); // System.err.println("s.size=" + s.size()); int[] ans = new int[m]; for (int i = m - 1; i >= 0; i--) { adjust(k, adj, s); ans[i] = s.size(); // for (Item item : s) { // System.err.print((item.node + 1) + " "); // } // System.err.println(); // System.err.println("s[0]=(node=" + s.first().node + ",deg=" + s.first().deg + ")"); Item a = new Item(edges[i][0], adj[edges[i][0]].size()); if (!s.contains(a)) { continue; } Item b = new Item(edges[i][1], adj[edges[i][1]].size()); if (!s.contains(b)) { continue; } s.remove(a); adj[a.node].remove(b.node); a.deg = adj[a.node].size(); if (a.deg >= k) { s.add(a); } else { adjustDeg(s, adj, a.node); } b = new Item(edges[i][1], adj[edges[i][1]].size()); s.remove(b); adj[b.node].remove(a.node); b.deg = adj[b.node].size(); if (b.deg >= k) { s.add(b); } else { adjustDeg(s, adj, b.node); } } for (int each : ans) { writer.println(each); } } scanner.close(); writer.flush(); writer.close(); } private static void adjust(int k, SetInt[] adj, TreeSet<Item> s) { while (!s.isEmpty() && adj[s.first().node].size() < k) { int node = s.pollFirst().node; adjustDeg(s, adj, node); // System.err.println((node + 1) + " removed, deg=" + adj[node].size()); } } private static void adjustDeg(TreeSet<Item> s, SetInt[] adj, int node) { for (int friend : adj[node]) { Item target = new Item(friend, adj[friend].size()); if (s.remove(target)) { adj[friend].remove(node); target.deg = adj[friend].size(); s.add(target); } } } static class Item implements Comparable<Item> { int node, deg; Item(int node, int deg) { this.node = node; this.deg = deg; } @Override public int compareTo(Item o) { if (deg != o.deg) { return deg - o.deg; } else { return node - o.node; } } } static class SetInt extends HashSet<Integer> {} static class ListInt extends ArrayList<Integer> {} public static class BufferedScanner { BufferedReader br; StringTokenizer st; public BufferedScanner(Reader reader) { br = new BufferedReader(reader); } public BufferedScanner() { this(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } static long gcd(long a, long b) { if (a < b) { return gcd(b, a); } while (b > 0) { long tmp = b; b = a % b; a = tmp; } return a; } static long inverse(long a, long m) { long[] ans = extgcd(a, m); return ans[0] == 1 ? (ans[1] + m) % m : -1; } private static long[] extgcd(long a, long m) { if (m == 0) { return new long[]{a, 1, 0}; } else { long[] ans = extgcd(m, a % m); long tmp = ans[1]; ans[1] = ans[2]; ans[2] = tmp; ans[2] -= ans[1] * (a / m); return ans; } } static long add(long a, long b) { a += b; if (a >= MODULO) { a -= MODULO; } return a; } static long sub(long a, long b) { a -= b; if (a < 0) { a += MODULO; } return a; } static long mul(long a, long b) { return a * b % MODULO; } static long div(long a, long b) { return a * inverse(b, MODULO) % MODULO; } static class Comb { final long modulo; final long[] fac, fnv; Comb(int limit, long modulo) { fac = new long[limit + 1]; fnv = new long[limit + 1]; fac[0] = 1; fnv[0] = 1; for (int i = 1; i <= limit; i++) { fac[i] = mul(fac[i - 1], i); fnv[i] = div(fnv[i - 1], i); } this.modulo = modulo; } long c(int total, int choose) { if (total < choose) { return 0; } if (total == 0 || total == choose) { return 1; } return mul(mul(fac[total], fnv[choose]), fnv[total - choose]); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; int x[202020]; int y[202020]; set<int> v[202020]; int d[202020]; int z[202020]; vector<int> q; int c; vector<int> r; int p[202020]; void lol() { for (int i = 0; i < q.size(); i++) { int x = q[i]; if (!z[x]) continue; z[x] = 0; c--; for (auto y : v[x]) { d[y]--; if (d[y] < k) { if (p[y]) continue; p[y] = 1; q.push_back(y); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 1; i <= n; i++) z[i] = 1; for (int i = 1; i <= m; i++) { cin >> x[i] >> y[i]; v[x[i]].insert(y[i]); d[x[i]]++; v[y[i]].insert(x[i]); d[y[i]]++; } c = n; for (int i = 1; i <= n; i++) { if (d[i] < k) q.push_back(i); } lol(); r.push_back(c); for (int i = m; i >= 1; i--) { q.clear(); if (z[x[i]] && z[y[i]]) { v[x[i]].erase(y[i]); v[y[i]].erase(x[i]); d[x[i]]--; d[y[i]]--; if (d[x[i]] < k) q.push_back(x[i]); if (d[y[i]] < k) q.push_back(y[i]); lol(); } if (i != 1) r.push_back(c); } reverse(r.begin(), r.end()); for (auto x : r) cout << x << "\n"; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Queue; import java.util.StringTokenizer; import java.util.TreeSet; public class E { public static final long MOD = 998244353l; public static class Pair{ int l; int r; Pair(int a,int b){ l=a; r=b; } } public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = (int) Long.parseLong(st.nextToken()); int m = (int) Long.parseLong(st.nextToken()); int k = Integer.parseInt(st.nextToken()); ArrayList<TreeSet<Integer>> graph = new ArrayList<TreeSet<Integer>>(); for(int i=0;i<n;++i){ graph.add(new TreeSet<Integer>()); } Pair[] edges = new Pair[m]; int[] ecs = new int[n]; for(int j=0;j<m;++j){ st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken())-1; int b = Integer.parseInt(st.nextToken())-1; graph.get(a).add(b); graph.get(b).add(a); ++ecs[a]; ++ecs[b]; edges[j] = new Pair(a,b); } //boolean cleared = false; Queue<Integer> toClear = new ArrayDeque<Integer>(); //boolean[] valid = new boolean[n]; //int inGru=0; boolean[] inQ = new boolean[n]; for(int i=0;i<n;++i){ if(graph.get(i).size()<k){ toClear.add(i); //valid[i]=false; inQ[i]=true; } } while(!toClear.isEmpty()){ int x = toClear.poll(); ecs[x]=0; if(graph.get(x)==null)continue; for(int y : graph.get(x)){ graph.get(y).remove(x); --ecs[y]; if(ecs[y]<k){ if(!inQ[y]){ toClear.add(y); inQ[y]=true; } } } graph.set(x, null); //System.err.println("BYE "+x); } int valid = 0; for(int j=0;j<n;++j){ if(ecs[j]>=k){ ++valid; } else if(ecs[j]!=0){ //System.err.println("FUU"); //we failed } } int[] max = new int[m+1]; max[m]= valid; for(int i=m-1;i>0;--i){ int l = edges[i].l; int r = edges[i].r; if(graph.get(l)!=null && graph.get(r)!=null){ --ecs[l]; --ecs[r]; graph.get(l).remove(r); graph.get(r).remove(l); if(ecs[l]<k){ toClear.add(l); } if(ecs[r]<k){ toClear.add(r); } while(!toClear.isEmpty()){ int x = toClear.poll(); if(graph.get(x)==null)continue; --valid; ecs[x]=0; //System.err.println("Removing " + x); for(int y : graph.get(x)){ graph.get(y).remove(x); --ecs[y]; if(ecs[y]<k){ if(!inQ[y]){ toClear.add(y); inQ[y]=true; } } } graph.set(x, null); } } max[i]=valid; } for(int j=1;j<=m;++j){ bw.write(max[j]+"\n"); } bw.flush(); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int N, M, K; int x[MAXN], y[MAXN]; unordered_set<int> adj[MAXN]; int ans[MAXN]; struct cmp { bool operator()(const int &lhs, const int &rhs) const { if (adj[lhs].size() != adj[rhs].size()) return adj[lhs].size() < adj[rhs].size(); return lhs < rhs; } }; set<int, cmp> in; void load() { scanf("%d%d%d", &N, &M, &K); for (int i = 0; i < M; i++) scanf("%d%d", x + i, y + i); } void reduce() { if (in.empty()) return; int x = *in.begin(); if (adj[x].size() >= K) return; in.erase(x); for (auto it : adj[x]) if (in.count(it)) { in.erase(it); adj[it].erase(x); in.insert(it); } reduce(); } void solve() { for (int i = 0; i < M; i++) { adj[x[i]].insert(y[i]); adj[y[i]].insert(x[i]); } for (int i = 1; i <= N; i++) in.insert(i); reduce(); for (int i = M - 1; i >= 0; i--) { ans[i] = in.size(); if (in.count(x[i]) && in.count(y[i])) { in.erase(x[i]); in.erase(y[i]); adj[x[i]].erase(y[i]); adj[y[i]].erase(x[i]); in.insert(x[i]); in.insert(y[i]); reduce(); } } for (int i = 0; i < M; i++) printf("%d\n", ans[i]); } int main() { load(); solve(); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } class Edge { int u, v; Edge(int a, int b) { u = a; v = b; } } int friends[]; int selected[]; int cans = 0; public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int deg[] = new int[n]; int ptr[] = new int[n]; Edge edge[] = new Edge[m]; for(int i = 0; i < m; ++i) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; edge[i] = new Edge(u, v); deg[u]++; deg[v]++; } HashSet<Integer> adj[] = new HashSet[n]; for(int i = 0; i < n; ++i) adj[i] = new HashSet<>(); for(int i = 0; i < m; ++i) { int u = edge[i].u; int v = edge[i].v; adj[u].add(v); adj[v].add(u); } PriorityQueue<Pair> queue = new PriorityQueue<>(new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { if(p1.deg < p2.deg) return -1; if(p1.deg > p2.deg) return 1; return 0; } }); int vis[] = new int[n]; selected = new int[n]; for(int i = 0; i < n; ++i) queue.add(new Pair(i, deg[i])); int curDeg[] = new int[n]; for(int i = 0; i < n; ++i) curDeg[i] = deg[i]; while(!queue.isEmpty()) { Pair cur = queue.poll(); int ind = cur.ind; if(vis[ind] == 1) continue; int cdeg = cur.deg; vis[ind] = 1; if(cdeg >= k) { selected[ind] = 1; cans++; continue; } for(int j : adj[ind]) { if(vis[j] == 0) { curDeg[j]--; queue.add(new Pair(j, curDeg[j])); } } } friends = new int[n]; for(int i = 0; i < n; ++i) { if(selected[i] == 1) { for(int j : adj[i]) { if(selected[j] == 1) friends[i]++; } } } int ans[] = new int[m]; ans[m - 1] = cans; for(int i = m - 1; i >= 1; --i) { int u = edge[i].u; int v = edge[i].v; adj[u].remove(v); adj[v].remove(u); if(selected[u] == 1 && selected[v] == 1) { friends[u]--; friends[v]--; Queue<Integer> cqueue = new LinkedList<>(); if(friends[u] < k) { cqueue.add(u); selected[u] = 0; cans--; } if(friends[v] < k) { cqueue.add(v); selected[v] = 0; cans--; } while(!cqueue.isEmpty()) { int cur = cqueue.poll(); for(int j : adj[cur]) { if(selected[j] == 1) { friends[j]--; if(friends[j] < k) { selected[j] = 0; cans--; cqueue.add(j); } } } } } ans[i - 1] = cans; } for(int i = 0; i < m; ++i) w.println(ans[i]); w.close(); } } class Pair { int ind, deg; Pair(int a, int b) { ind = a; deg = b; } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; int ans[N]; int deg[N]; bool trip[N]; set<int> g[N]; int cnt, k; void remove(int u) { if (!trip[u]) return; queue<int> q; q.push(u); trip[u] = false; while (!q.empty()) { u = q.front(); q.pop(); cnt--; for (int v : g[u]) { deg[v]--; g[v].erase(u); if (trip[v] and deg[v] < k) { trip[v] = false; q.push(v); } } g[u].clear(); } } int main() { int n, m; scanf("%d %d %d", &n, &m, &k); vector<pair<int, int> > e; cnt = n; for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); deg[x]++; deg[y]++; e.emplace_back(x, y); g[x].insert(y); g[y].insert(x); } for (int i = 1; i <= n; i++) trip[i] = true; for (int i = 1; i <= n; i++) { if (trip[i] and deg[i] < k) remove(i); } ans[m - 1] = cnt; for (int i = m - 2; i >= 0; i--) { int a = e[i + 1].first, b = e[i + 1].second; if (!trip[a] or !trip[b]) { ans[i] = cnt; continue; } deg[a]--; deg[b]--; g[a].erase(b); g[b].erase(a); if (deg[a] < k) remove(a); if (deg[b] < k) remove(b); ans[i] = cnt; } for (int i = 0; i < m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int nmax = 2e5 + 5; const int mod = 1e9 + 7; vector<int> g[nmax]; int destroyed; pair<int, int> ej[nmax]; int deg[nmax], ans[nmax]; bool vis[nmax]; map<pair<int, int>, int> mp; void dhongsho(int u, int k, int p) { if (vis[u]) return; vis[u] = true; destroyed++; for (auto v : g[u]) { if (v == p or vis[v]) continue; if (mp[{u, v}] == 0) continue; deg[v]--; if (deg[v] < k) dhongsho(v, k, u); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k, u, v; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> u >> v; g[u].push_back(v), g[v].push_back(u); ej[i] = {u, v}; mp[{u, v}] = mp[{v, u}] = 1; deg[u]++, deg[v]++; } for (int u = 1; u <= n; u++) { if (deg[u] < k) dhongsho(u, k, -1); } for (int i = m; i >= 1; i--) { ans[i] = n - destroyed; u = ej[i].first, v = ej[i].second; if (!vis[u] and !vis[v]) { deg[u]--, deg[v]--; mp[{u, v}] = mp[{v, u}] = 0; if (deg[u] < k) dhongsho(u, k, v); if (deg[v] < k) dhongsho(v, k, u); } } for (int i = 1; i <= m; i++) { cout << ans[i] << "\n"; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long int n, m, k; cin >> n >> m >> k; vector<pair<long long int, long long int> > edges(m); set<long long int> adj[n]; for (int i = 0; i < m; ++i) { long long int u, v; cin >> u >> v; u--; v--; adj[u].insert(v); adj[v].insert(u); edges[i].first = u; edges[i].second = v; } set<pair<long long int, long long int> > myset; set<pair<long long int, long long int> >::iterator it; for (int i = 0; i < n; ++i) { myset.insert({adj[i].size(), i}); } set<long long int>::iterator itt; vector<long long int> ans(m, 0); for (int i = m - 1; i > -1; --i) { while (!myset.empty() && myset.begin()->first < k) { long long int ind = myset.begin()->second; myset.erase(myset.begin()); for (auto v : adj[ind]) { it = myset.find({adj[v].size(), v}); itt = adj[v].find(ind); if (it != myset.end() && itt != adj[v].end()) { myset.erase(it); adj[v].erase(itt); myset.insert({adj[v].size(), v}); } } } ans[i] = myset.size(); long long int u = edges[i].first, v = edges[i].second; it = myset.find({adj[u].size(), u}); itt = adj[u].find(v); if (it != myset.end() && itt != adj[u].end()) { myset.erase(it); adj[u].erase(itt); myset.insert({adj[u].size(), u}); } it = myset.find({adj[v].size(), v}); itt = adj[v].find(u); if (it != myset.end() && itt != adj[v].end()) { myset.erase(it); adj[v].erase(itt); myset.insert({adj[v].size(), v}); } } for (int i = 0; i < m; ++i) { cout << ans[i] << "\n"; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<set<int> > g; int n, m, k; vector<pair<int, int> > q; set<int> s; void remove(int v) { if (g[v].size() < k && s.erase(v)) { for (auto to : g[v]) g[to].erase(v), remove(to); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; q.resize(m); g.resize(n); for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; x--, y--; g[x].insert(y), g[y].insert(x); q.push_back(make_pair(x, y)); } for (int i = 0; i < n; ++i) s.insert(i); for (int i = 0; i < n; ++i) remove(i); vector<int> ans; for (int i = 0; i < m; ++i) { ans.push_back(s.size()); auto cur = q.back(); q.pop_back(); g[cur.first].erase(cur.second); g[cur.second].erase(cur.first); remove(cur.first); remove(cur.second); } for (int i = 0; i < m; ++i) cout << ans[m - i - 1] << '\n'; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> p[maxn]; int vis[maxn]; int ans[maxn]; int num[maxn]; int v[maxn], w[maxn]; int now; int n, m, k; void dfs(int j, int v) { ans[j]--; for (int i = 0; i < p[v].size(); i++) { num[p[v][i]]--; if (vis[p[v][i]] == 1) { if (num[p[v][i]] < k) { vis[p[v][i]] = 0; dfs(j, p[v][i]); } } } vis[v] = -1; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &v[i], &w[i]); p[v[i]].push_back(w[i]); p[w[i]].push_back(v[i]); } for (int i = 1; i <= n; i++) { num[i] = p[i].size(); if (p[i].size() >= k) vis[i] = 1; } ans[m] = n; for (int i = 1; i <= n; i++) { if (vis[i] == 0) dfs(m, i); } for (int i = m; i >= 2; i--) { p[v[i]].pop_back(); p[w[i]].pop_back(); if (vis[v[i]] == 1) num[w[i]]--; if (vis[w[i]] == 1) num[v[i]]--; ans[i - 1] = ans[i]; if (vis[w[i]] == 1 && num[w[i]] < k) { vis[w[i]] = 0; dfs(i - 1, w[i]); } if (vis[v[i]] == 1 && num[v[i]] < k) { vis[v[i]] = 0; dfs(i - 1, v[i]); } } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> G[N]; int n, m, k; int qu[N], qv[N], vis[N], du[N], ans[N], res; void dfs(int u) { if (vis[u]) return; if (du[u] < k) { vis[u] = 1; for (int v : G[u]) if (!vis[v]) du[v]--; for (int v : G[u]) { if (!vis[v]) { dfs(v); } } res--; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> qu[i] >> qv[i]; G[qu[i]].push_back(qv[i]); G[qv[i]].push_back(qu[i]); du[qu[i]]++; du[qv[i]]++; } res = n; for (int i = 1; i <= n; i++) { dfs(i); } ans[m] = res; for (int i = m; i >= 1; i--) { if (!vis[qu[i]] && !vis[qv[i]]) { if (du[qu[i]] - 1 < k) { du[qu[i]]--; dfs(qu[i]); } else if (du[qv[i]] - 1 < k) { du[qv[i]]--; dfs(qv[i]); } else { du[qu[i]]--; du[qv[i]]--; } } G[qu[i]].pop_back(); G[qv[i]].pop_back(); ans[i - 1] = res; } for (int i = 1; i <= m; i++) { cout << ans[i] << endl; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, k, sum; int x[N], y[N]; vector<int> Edge[N]; int del[N], deg[N]; int ans[N]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } queue<int> Q; inline void Topsort(int Start) { if (del[Start] || deg[Start] >= k) return; while (!Q.empty()) Q.pop(); Q.push(Start); del[Start] = 1, --sum; while (!Q.empty()) { int x = Q.front(); Q.pop(); for (vector<int>::iterator it = Edge[x].begin(); it != Edge[x].end(); ++it) { int v = *it; if (del[v]) continue; --deg[v]; if (deg[v] < k) del[v] = 1, --sum, Q.push(v); } } } int main() { n = read(), m = read(), k = read(); for (int i = 1; i <= m; ++i) { x[i] = read(), y[i] = read(); Edge[x[i]].push_back(y[i]), ++deg[x[i]]; Edge[y[i]].push_back(x[i]), ++deg[y[i]]; } sum = n; for (int i = 1; i <= n; ++i) Topsort(i); ans[m] = sum; for (int i = m; i >= 2; --i) { Edge[x[i]].pop_back(); if (!del[y[i]]) --deg[x[i]]; Edge[y[i]].pop_back(); if (!del[x[i]]) --deg[y[i]]; Topsort(x[i]), Topsort(y[i]); ans[i - 1] = sum; } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.util.*; public class A { FastScanner in; PrintWriter out; void solve() { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int[] fr = new int[m]; int[] to = new int[m]; List<Integer>[] g = new ArrayList[n]; for (int i = 0; i < n; i++) { g[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { fr[i] = in.nextInt() - 1; to[i] = in.nextInt() - 1; g[fr[i]].add(i); g[to[i]].add(i); } boolean[] can = new boolean[n]; Arrays.fill(can, true); int[] cntNeigh = new int[n]; List<Integer> toRemove = new ArrayList<>(); for (int i = 0; i < n; i++) { cntNeigh[i] = g[i].size(); if (cntNeigh[i] < k) { toRemove.add(i); can[i] = false; } } int res = n; int[] f = new int[m]; for (int i = m - 1; i >= 0; i--) { while (toRemove.size() > 0) { int v = toRemove.get(toRemove.size() - 1); toRemove.remove(toRemove.size() - 1); res--; for (int id : g[v]) { if (id > i) { break; } int toC = fr[id] + to[id] - v; if (can[toC]) { cntNeigh[toC]--; if (cntNeigh[toC] < k) { toRemove.add(toC); can[toC] = false; } } } } f[i] = res; if (can[fr[i]] && can[to[i]]) { for (int v : new int[]{fr[i], to[i]}) { cntNeigh[v]--; if (cntNeigh[v] < k && can[v]) { can[v] = false; toRemove.add(v); } } } } for (int x : f) { out.println(x); } } void run() { try { in = new FastScanner(new File("A.in")); out = new PrintWriter(new File("A.out")); solve(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new A().runIO(); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; set<int> s, G[N]; int n, m, k; int U[N], V[N], ans[N]; void check(int u) { if (G[u].size() < k && s.erase(u)) { for (auto v : G[u]) { G[v].erase(u); check(v); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) s.insert(i); for (int i = 1; i <= m; i++) { scanf("%d%d", &U[i], &V[i]); G[U[i]].insert(V[i]); G[V[i]].insert(U[i]); } for (int i = 1; i <= n; i++) check(i); for (int i = m; i >= 1; --i) { ans[i] = s.size(); G[U[i]].erase(V[i]); G[V[i]].erase(U[i]); check(U[i]); check(V[i]); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; pair<int, int> edge[maxn]; int deg[maxn], ans[maxn]; bool del[maxn], vis[maxn]; set<pair<int, int> > dic; vector<pair<int, int> > g[maxn]; int main() { int n, m, k, u, v; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); g[u].push_back(pair<int, int>(v, i)); g[v].push_back(pair<int, int>(u, i)); deg[u]++, deg[v]++; edge[i] = pair<int, int>(u, v); } for (int v = 1; v <= n; v++) dic.insert(pair<int, int>(deg[v], v)); while (!dic.empty() && dic.begin()->first < k) { int v = dic.begin()->second; dic.erase(dic.begin()); del[v] = true; for (int i = 0; i < (int)g[v].size(); i++) { int adj = g[v][i].first; if (del[adj]) continue; dic.erase(pair<int, int>(deg[adj], adj)); dic.insert(pair<int, int>(--deg[adj], adj)); } } for (int i = m - 1; i >= 0; i--) { ans[i] = dic.size(); u = edge[i].first, v = edge[i].second; if (!del[u] && !del[v] && !vis[i]) { dic.erase(pair<int, int>(deg[u], u)); dic.insert(pair<int, int>(--deg[u], u)); dic.erase(pair<int, int>(deg[v], v)); dic.insert(pair<int, int>(--deg[v], v)); vis[i] = true; } while (!dic.empty() && dic.begin()->first < k) { int v = dic.begin()->second; dic.erase(dic.begin()); del[v] = true; for (int i = 0; i < (int)g[v].size(); i++) { int adj = g[v][i].first; if (del[adj] || vis[g[v][i].second]) continue; dic.erase(pair<int, int>(deg[adj], adj)); dic.insert(pair<int, int>(--deg[adj], adj)); vis[g[v][i].second] = true; } } } for (int i = 0; i < m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> template <typename T> inline T const &MAX(T const &a, T const &b) { return a > b ? a : b; } template <typename T> inline T const &MIN(T const &a, T const &b) { return a < b ? a : b; } inline void add(long long &a, long long b) { a += b; if (a >= 1000000007) a -= 1000000007; } inline void sub(long long &a, long long b) { a -= b; if (a < 0) a += 1000000007; } inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long qp(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % 1000000007; a = a * a % 1000000007, b >>= 1; } return ans; } inline long long qp(long long a, long long b, long long c) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % c; a = a * a % c, b >>= 1; } return ans; } using namespace std; const double eps = 1e-8; const long long INF = 0x3f3f3f3f3f3f3f3f; const int N = 200000 + 10, maxn = 1000000 + 10, inf = 0x3f3f3f3f; int ans[N], d[N]; bool vis[N]; vector<pair<int, int> > v[N]; set<pair<int, int> > s; pair<int, int> p[N]; void gao(int x) { auto y = s.lower_bound(make_pair(d[x], x)); if (y != s.end()) { s.erase(y); d[x]--; s.insert(make_pair(d[x], x)); } } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(make_pair(b, i)), v[b].push_back(make_pair(a, i)); d[a]++, d[b]++; p[i] = make_pair(a, b); } for (int i = 1; i <= n; i++) s.insert(make_pair(d[i], i)); for (int i = m; i >= 1; i--) { while (s.size() > 0 && (s.begin())->first < k) { int te = s.begin()->second; s.erase(s.begin()); for (int i = 0; i < v[te].size(); i++) { if (vis[v[te][i].second]) continue; gao(v[te][i].first); vis[v[te][i].second] = 1; } } ans[i] = s.size(); if (vis[i] == 1) continue; gao(p[i].first), gao(p[i].second); vis[i] = 1; } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; set<int> trip; set<int> vec[200010]; struct node { int u, v; } mf[200010]; int n, m, k; int ans[200010]; void remove(int x) { set<int>::iterator it; if (vec[x].size() < k && trip.count(x)) { trip.erase(x); for (it = vec[x].begin(); it != vec[x].end(); it++) { int i = *it; vec[i].erase(x); remove(i); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); vec[u].insert(v); vec[v].insert(u); mf[i] = node{u, v}; } for (int i = 1; i <= n; i++) trip.insert(i); for (int i = 1; i <= n; i++) remove(i); for (int i = m; i > 0; i--) { ans[i] = trip.size(); int u = mf[i].u, v = mf[i].v; vec[u].erase(v); vec[v].erase(u); remove(u); remove(v); } for (int i = 1; i <= m; i++) { printf("%d\n", ans[i]); } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.math.*; import java.util.*; import java.util.stream.*; public class E { public Object solve() { int N = sc.nextInt(), M = sc.nextInt(), K = sc.nextInt(); int [][] E = dec(sc.nextInts(M)); int [][] G = graph(N, E); PriorityQueue<int[]> Q = new PriorityQueue<>(by(1)); boolean [] W = new boolean [N]; int [] L = new int [N]; for (int i : rep(N)) Q.add(new int [] { i, L[i] = G[i].length }); HashSet<Long> H = new HashSet<>(); long P = BigInteger.probablePrime(30, new Random()).longValue(); int [] res = new int [M]; int R = N; for (int j : sep(M)) { while (!Q.isEmpty() && Q.peek()[1] < K) { int [] m = Q.poll(); int i = m[0]; if (W[i]) continue; for (int n : G[i]) if (!W[n] && !H.contains(P*i + n) && !H.contains(P*n + i)) Q.add(new int [] { n, --L[n] }); W[i] = true; --R; } res[j] = R; int x = E[j][0], y = E[j][1]; if (!W[x] && !W[y]) { Q.add(new int [] { x, --L[x] }); Q.add(new int [] { y, --L[y] }); H.add(P*x + y); H.add(P*y + x); } } for (int i : rep(M)) print(res[i]); return null; } private static final boolean ONE_TEST_CASE = true; private static void init() { } private static final int INF = (int) 1e9 + 10; private static Comparator<int[]> by (final int ... J) { return new Comparator<int[]>() { @Override public int compare(int[] x, int[] y) { for (int i : J) if (x[i] != y[i]) return x[i] - y[i]; return 0; }}; } private static int [][] dec (int [] ... E) { return dec(E, INF); } private static int [][] dec (int [][] E, int N) { for (int [] e : E) for (int i = 0; i < e.length && i < N; ++i) --e[i]; return E; } private static int [][][] dwgraph (int N, int [][] E) { int [] D = new int [N]; for (int [] e : E) ++D[e[0]]; int [][][] res = new int [2][N][]; for (int i = 0; i < 2; ++i) for (int j = 0; j < N; ++j) res[i][j] = new int [D[j]]; D = new int [N]; for (int [] e : E) { int x = e[0], y = e[1], z = e.length > 2 ? e[2] : 1; res[0][x][D[x]] = y; res[1][x][D[x]] = z; ++D[x]; } return res; } private static int [][] dup (int [][] E) { int [][] res = new int [2*E.length][]; for (int i = 0; i < E.length; ++i) { res[2*i] = E[i].clone(); res[2*i+1] = E[i].clone(); res[2*i+1][0] = E[i][1]; res[2*i+1][1] = E[i][0]; } return res; } private static int [][] graph (int N, int [][] E) { return wgraph(N, E)[0]; } private static IntStream range(int N) { return IntStream.range(0, N); } private static Iterable<Integer> rep (int N) { return rep(0, N); } private static Iterable<Integer> rep (final int S, final int T) { return new Iterable<Integer>() { @Override public Iterator<Integer> iterator() { return S < T ? java.util.stream.IntStream.range(S, T).iterator() : Collections.emptyIterator(); } }; } private static Iterable<Integer> sep (int N) { return sep(0, N); } private static Iterable<Integer> sep (final int S, final int T) { return new Iterable<Integer>() { @Override public Iterator<Integer> iterator() { return S < T ? java.util.stream.IntStream.range(S, T).map(i -> (T - 1) - (i - S)).iterator() : Collections.emptyIterator(); } }; } private static int [][][] wgraph (int N, int [][] E) { return dwgraph(N, dup(E)); } //////////////////////////////////////////////////////////////////////////////////// private static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static Object print (Object o, Object ... A) { IOUtils.print(o, A); return null; } private static class IOUtils { public static class MyScanner { public String next() { newLine(); return line[index++]; } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() { line = null; return readLine(); } public String [] nextStrings() { return split(nextLine()); } public int[] nextInts() { return nextStream().mapToInt(Integer::parseInt).toArray(); } public int[][] nextInts(int N) { return range(N).mapToObj(i -> nextInts()).toArray(int[][]::new); } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = split(readLine()); index = 0; } } private java.util.stream.Stream<String> nextStream() { return java.util.Arrays.stream(nextStrings()); } private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build(Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim(String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(delim.length()); } ////////////////////////////////////////////////////////////////////////////////// private static final java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########"); private static void start() { if (t == 0) t = millis(); } private static void append(java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) f.accept(java.lang.reflect.Array.get(o, i)); } else if (o instanceof Iterable<?>) ((Iterable<?>)o).forEach(f::accept); else g.accept(o instanceof Double ? formatter.format(o) : o); } private static void append(final StringBuilder b, Object o, final String delim) { append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o); } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static Object print(Object o, Object ... A) { pw.println(build(o, A)); return null; } private static void err(Object o, Object ... A) { System.err.println(build(o, A)); } private static boolean PRINT; private static void write(Object o) { err(o, '(', time(), ')'); if (PRINT) pw.println(o); } private static void exit() { IOUtils.pw.close(); System.out.flush(); err("------------------"); err(time()); System.exit(0); } private static long t; private static long millis() { return System.currentTimeMillis(); } private static String time() { return "Time: " + (millis() - t) / 1000.0; } private static void run(int N) { try { PRINT = System.getProperties().containsKey("PRINT"); } catch (Throwable t) {} for (int n = 1; n <= N; ++n) { Object res = new E().solve(); if (res != null) write("Case #" + n + ": " + build(res)); } exit(); } } public static void main(String[] args) { init(); int N = ONE_TEST_CASE ? 1 : sc.nextInt(); IOUtils.run(N); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * */ public class E { static LinkedList<Pair>[] g; static int k; static Set<Integer> s; static int[] degree; static Queue<Integer> q = new LinkedList<>(); public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); k = Integer.parseInt(st.nextToken()); s = new HashSet<>(2*m); g = new LinkedList[n]; degree = new int[n]; int[][] f = new int[m][2]; for (int i = 0; i < m; i++) { st = new StringTokenizer(reader.readLine()); int a = Integer.parseInt(st.nextToken())-1; int b = Integer.parseInt(st.nextToken())-1; if (g[a] == null) { g[a] = new LinkedList<>(); } if (g[b] == null) { g[b] = new LinkedList<>(); } g[a].add(new Pair(b,i)); g[b].add(new Pair(a,i)); degree[a]++;degree[b]++; f[i][0] = a; f[i][1] = b; } for (int i = 0; i < n; i++) { if(degree[i]>0){ if(degree[i]>=k){ s.add(i); }else{ q.add(i); } } } while (!q.isEmpty()){ int v = q.poll(); for (Pair u : g[v]) { if(s.contains(u.a)) { degree[u.a]--; if (degree[u.a] < k) { q.add(u.a); s.remove(u.a); } } } } int[] ans = new int[m]; for (int i = m-1; i >=0; i--) { ans[i] = s.size(); int a = f[i][0]; int b = f[i][1]; if(s.contains(a) && s.contains(b)) { degree[a]--;degree[b]--; if (degree[a] < k){ q.add(a); s.remove(a); } if (degree[b] < k){ q.add(b); s.remove(b); } while (!q.isEmpty()){ int v = q.poll(); for (Pair u : g[v]) { if(u.b < i && s.contains(u.a)) { degree[u.a]--; if (degree[u.a] < k) { q.add(u.a); s.remove(u.a); } } } } } } for (int i = 0; i < m; i++) { System.out.println(ans[i]); } } static class Pair{ int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main implements Runnable { FastReader scn; PrintWriter out; String INPUT = ""; void solve() { int n = scn.nextInt(), m = scn.nextInt(), k = scn.nextInt(); int[] from = new int[m], to = new int[m], w = new int[m]; for(int i = m - 1; i >= 0; i--) { from[i] = scn.nextInt() - 1; to[i] = scn.nextInt() - 1; w[i] = i; } int[][][] g = packWU(n, from, to, w); int[] deg = new int[n], q = new int[n]; int end = 0, start = 0; for(int i = 0; i < n; i++) { deg[i] = g[i].length; if(deg[i] < k) { q[end++] = i; } } boolean[] done = new boolean[m]; int[] ans = new int[m]; for(int i = 0; i < m; i++) { while(start < end) { for(int[] v : g[q[start]]) { if(!done[v[1]]) { done[v[1]] = true; if(deg[v[0]] == k) { q[end++] = v[0]; } deg[v[0]]--; deg[q[start]]--; } } start++; } ans[m - i - 1] = n - end; if(!done[i]) { done[i] = true; if(deg[from[i]] == k) { q[end++] = from[i]; } if(deg[to[i]] == k) { q[end++] = to[i]; } deg[from[i]]--; deg[to[i]]--; } } for(int a : ans) { out.println(a); } } int[][][] packWU(int n, int[] from, int[] to, int[] w) { int[][][] g = new int[n][][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]][2]; for (int i = 0; i < from.length; i++) { --p[from[i]]; g[from[i]][p[from[i]]][0] = to[i]; g[from[i]][p[from[i]]][1] = w[i]; --p[to[i]]; g[to[i]][p[to[i]]][0] = from[i]; g[to[i]][p[to[i]]][1] = w[i]; } return g; } public void run() { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new Main(), "Main", 1 << 26).start(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } public FastReader() { is = System.in; } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[][] IndIntArray(int n) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = new int[] { nextInt(), i }; return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); int c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); long c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } int[] uniq(int[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] uniq(long[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] reverse(int[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } long[] reverse(long[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } int[] compress(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } long[] compress(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; int a[200005]; int b[200005]; vector<int> adj[200005]; bool removed[200005]; int nc[200005]; int ans[200005]; set<pair<int, int>> remedges; int main() { ios::sync_with_stdio(false); scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%d %d", &a[i], &b[i]); } for (int i = 0; i < m; i++) { adj[a[i]].push_back(b[i]); adj[b[i]].push_back(a[i]); nc[a[i]]++; nc[b[i]]++; } int cans = n; for (int i = 1; i <= n; i++) { if (!removed[i]) { if (nc[i] < k) { removed[i] = true; cans--; queue<int> q; q.push(i); while (!q.empty()) { int cur = q.front(); q.pop(); for (int j = 0; j < adj[cur].size(); j++) { int x = adj[cur][j]; if (!removed[x]) { nc[x]--; if (nc[x] < k) { removed[x] = true; cans--; q.push(x); } } } } } } } ans[m - 1] = cans; for (int cm = m - 1; cm >= 0; cm--) { int ca = a[cm]; int cb = b[cm]; if (!removed[ca] && !removed[cb]) { nc[ca]--; nc[cb]--; remedges.insert(make_pair(ca, cb)); remedges.insert(make_pair(cb, ca)); if (nc[ca] < k) { removed[ca] = true; cans--; queue<int> q; q.push(ca); while (!q.empty()) { int cur = q.front(); q.pop(); for (int j = 0; j < adj[cur].size(); j++) { int x = adj[cur][j]; if (remedges.find(make_pair(cur, x)) != remedges.end()) { continue; } if (!removed[x]) { nc[x]--; if (nc[x] < k) { removed[x] = true; cans--; q.push(x); } } } } } if (nc[cb] < k && !removed[cb]) { removed[cb] = true; cans--; queue<int> q; q.push(cb); while (!q.empty()) { int cur = q.front(); q.pop(); for (int j = 0; j < adj[cur].size(); j++) { int x = adj[cur][j]; if (remedges.find(make_pair(cur, x)) != remedges.end()) { continue; } if (!removed[x]) { nc[x]--; if (nc[x] < k) { removed[x] = true; cans--; q.push(x); } } } } } } if (cm > 0) { ans[cm - 1] = cans; } } for (int i = 0; i < m; i++) { printf("%d\n", ans[i]); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class A { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(), k = ni(); ArrayList<Integer> adj[] = (ArrayList<Integer>[]) new ArrayList[n]; for(int i=0;i<n;i++) adj[i] = new ArrayList<>(); int u[] = new int[m]; int v[] = new int[m]; int degree[] = new int[n]; for(int i=0;i<m;i++){ int a = ni() - 1, b = ni() - 1; u[i] = a; v[i] = b; adj[a].add(i); adj[b].add(i); degree[a]++; degree[b]++; } ArrayList<Integer> q = new ArrayList<>(); int head = 0; boolean alive[] = new boolean[m]; Arrays.fill(alive, true); for(int i=0;i<n;i++){ if(degree[i] < k) q.add(i); } int ans[] = new int[m]; for(int day=m-1;day>=0;day--){ while(head < q.size()){ int id = q.get(head); for(int edgeId: adj[id]){ if(!alive[edgeId]) continue; int other = u[edgeId] == id ? v[edgeId] : u[edgeId]; if(degree[other]==k){ q.add(other); } degree[other]--; degree[id]--; alive[edgeId] = false; } head++; } ans[day] = n - q.size(); if (alive[day]) { if (degree[u[day]] == k) q.add(u[day]); if (degree[v[day]] == k) q.add(v[day]); degree[u[day]]--; degree[v[day]]--; alive[day] = false; } } for(int ai: ans) out.println(ai); } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new A().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; long long powmod(long long a, long long b, long long mod) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res % mod; } const int N = 2e5 + 10; int degree[N]; vector<pair<int, int> > v[N]; bool mark[N]; set<pair<int, int> > s; int main() { memset(mark, 1, sizeof mark); int n, m, k; cin >> n >> m >> k; vector<pair<int, int> > edges(m); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); a--, b--; edges[i].first = a; edges[i].second = b; degree[a]++; degree[b]++; v[a].push_back(make_pair(b, i)); v[b].push_back(make_pair(a, i)); } for (int i = 0; i < n; ++i) { s.insert(make_pair(degree[i], i)); } while (!s.empty() && s.begin()->first < k) { int u = s.begin()->second; for (int i = 0; i < v[u].size(); ++i) { int ch = v[u][i].first; if (mark[ch]) { s.erase(make_pair(degree[ch], ch)); degree[ch]--; s.insert(make_pair(degree[ch], ch)); } } s.erase(make_pair(degree[u], u)); mark[u] = 0; } vector<int> ans; for (int i = m - 1; i >= 0; --i) { ans.push_back(s.size()); int a = edges[i].first, b = edges[i].second; if (mark[a] && mark[b]) { s.erase(make_pair(degree[a], a)); degree[a]--; s.insert(make_pair(degree[a], a)); s.erase(make_pair(degree[b], b)); degree[b]--; s.insert(make_pair(degree[b], b)); while (!s.empty() && (s.begin()->first < k)) { int u = s.begin()->second; for (int j = 0; j < v[u].size(); ++j) { int ch = v[u][j].first, id = v[u][j].second; if (id >= i) continue; if (mark[ch]) { s.erase(make_pair(degree[ch], ch)); degree[ch]--; s.insert(make_pair(degree[ch], ch)); } } s.erase(make_pair(degree[u], u)); mark[u] = 0; } } } for (int i = ans.size() - 1; i >= 0; --i) { printf("%d\n", ans[i]); } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto &x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } inline int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } inline int mul(int a, int b) { return (int)((long long)a * b % MOD); } inline int binpow(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = mul(res, a); a = mul(a, a); b /= 2; } return res; } inline int inv(int a) { return binpow(a, MOD - 2); } int gcd(int a, int b, int &x, int &y) { if (a == 0) { x = 0, y = 1; return b; } int x1, y1; int d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } const int N = 2e5 + 5; int n, m, k, deg[N], ans[N]; vector<pair<int, int> > edges; set<int> g[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; edges.push_back({x, y}); deg[x]++; deg[y]++; g[x].insert(y); g[y].insert(x); } int rem = n; queue<int> q; for (int i = 1; i <= n; ++i) if (deg[i] < k) { q.push(i); rem--; } for (int i = m - 1; i >= 0; --i) { while (!q.empty()) { int v = q.front(); q.pop(); for (int u : g[v]) { deg[u]--; g[u].erase(v); if (deg[u] == k - 1) { q.push(u); rem--; } } deg[v] = 0; g[v].clear(); } ans[i] = rem; int x = edges[i].first, y = edges[i].second; if (!g[x].count(y)) continue; deg[x]--; deg[y]--; g[x].erase(y); g[y].erase(x); if (deg[x] == k - 1) { q.push(x); rem--; } if (deg[y] == k - 1) { q.push(y); rem--; } } for (int i = 0; i < m; ++i) cout << ans[i] << "\n"; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.BufferedInputStream; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; import java.util.Stack; import java.util.HashSet; import java.io.FilterInputStream; import java.util.Vector; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author nirav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scan in = new Scan(inputStream); PrintWriter out = new PrintWriter(outputStream); ETrips solver = new ETrips(); solver.solve(1, in, out); out.close(); } static class ETrips { public void solve(int testNumber, Scan in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int k = in.scanInt(); int degree[] = new int[n + 1]; Set<Integer>[] graph = new Set[n + 1]; for (int i = 1; i <= n; i++) graph[i] = new HashSet<>(); TreeSet<ETrips.pair> queue = new TreeSet<>(); int array[][] = new int[m + 1][2]; for (int i = 0; i < m; i++) { int a = in.scanInt(); int b = in.scanInt(); degree[a]++; degree[b]++; graph[a].add(b); graph[b].add(a); array[i + 1][0] = a; array[i + 1][1] = b; } Stack<Integer> ans = new Stack<>(); for (int i = 1; i <= n; i++) { queue.add(new ETrips.pair(degree[i], i)); } for (int i = m; i > 0; i--) { while (!queue.isEmpty() && queue.first().a < k) { ETrips.pair g = queue.first(); queue.remove(queue.first()); Iterator<Integer> it = graph[g.b].iterator(); while (it.hasNext()) { int f = it.next(); graph[f].remove(g.b); queue.remove(new ETrips.pair(degree[f], f)); degree[f]--; queue.add(new ETrips.pair(degree[f], f)); } degree[g.b] = 0; graph[g.b] = new HashSet<>(); } ans.add(queue.size()); if (!graph[array[i][0]].isEmpty() && graph[array[i][0]].contains(array[i][1])) { queue.remove(new ETrips.pair(degree[array[i][0]], array[i][0])); degree[array[i][0]]--; queue.add(new ETrips.pair(degree[array[i][0]], array[i][0])); } graph[array[i][0]].remove(array[i][1]); if (!graph[array[i][1]].isEmpty() && graph[array[i][1]].contains(array[i][0])) { queue.remove(new ETrips.pair(degree[array[i][1]], array[i][1])); degree[array[i][1]]--; queue.add(new ETrips.pair(degree[array[i][1]], array[i][1])); } graph[array[i][1]].remove(array[i][0]); } while (!ans.isEmpty()) { out.println(ans.pop()); } } static class pair implements Comparable<ETrips.pair> { int a; int b; pair(int x, int y) { a = x; b = y; } public int compareTo(ETrips.pair o) { if (this.a != o.a) { return this.a - o.a; } else { return this.b - o.b; } } } } static class Scan { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public Scan(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxN = 2e5; int n, m, k, eu[mxN], ev[mxN], ans[mxN]; set<int> adj[mxN]; bool a[mxN]; vector<int> tp; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { cin >> eu[i] >> ev[i], --eu[i], --ev[i]; adj[eu[i]].insert(i); adj[ev[i]].insert(i); a[i] = 1; } for (int i = 0; i < n; ++i) if (adj[i].size() < k) tp.push_back(i); for (int i = m - 1; i >= 0; --i) { ans[i] = i < m - 1 ? ans[i + 1] : n; for (int j = 0; j < tp.size(); ++j) { for (int e : adj[tp[j]]) { adj[eu[e] ^ ev[e] ^ tp[j]].erase(e); if (adj[eu[e] ^ ev[e] ^ tp[j]].size() == k - 1) tp.push_back(eu[e] ^ ev[e] ^ tp[j]); a[e] = 0; } adj[tp[j]].clear(); --ans[i]; } tp.clear(); if (a[i]) { adj[eu[i]].erase(i); if (adj[eu[i]].size() < k) tp.push_back(eu[i]); adj[ev[i]].erase(i); if (adj[ev[i]].size() < k) tp.push_back(ev[i]); a[i] = 0; } } for (int i = 0; i < m; ++i) cout << ans[i] << "\n"; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, d[200005], k, res, vs[200005], ans[200005], pa; map<int, int> cnt[200005]; vector<int> ke[200005]; pair<int, int> a[200005]; void dfs(int u) { d[u]--; res--; vs[u] = 1; for (int v : ke[u]) if (vs[v] == 0) { cnt[u][v] = cnt[v][u] = 1; d[v]--; if (d[v] < k) { d[v]++; dfs(v); } } } 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 >> a[i].first >> a[i].second; d[a[i].first]++; d[a[i].second]++; ke[a[i].first].push_back(a[i].second); ke[a[i].second].push_back(a[i].first); } for (int i = 1; i <= n; i++) if (d[i] < k && vs[i] == 0) dfs(i); for (int i = m; i >= 1; i--) { ans[i] = res; ke[a[i].first].pop_back(); ke[a[i].second].pop_back(); if (cnt[a[i].first][a[i].second] == 1) continue; if (vs[a[i].first] == 0) { d[a[i].first]--; if (d[a[i].first] < k) { d[a[i].first]++; dfs(a[i].first); } } if (vs[a[i].second] == 0) { d[a[i].second]--; if (d[a[i].second] < k) { d[a[i].second]++; dfs(a[i].second); } } } for (int i = 1; i <= m; i++) cout << ans[i] << '\n'; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; public class CF1037_E { public static void main(String[] args) throws Throwable { MyScanner sc = new MyScanner(); PrintWriter pw = new PrintWriter(System.out); n = sc.nextInt(); adj = new HashSet[n]; for (int i = 0; i < n; i++) adj[i] = new HashSet<>(); int m = sc.nextInt(); int k = sc.nextInt(); int[] deg = new int[n]; int[] ans = new int[m]; int[] x = new int[m]; int[] y = new int[m]; for (int i = 0; i < m; i++) { x[i] = sc.nextInt() - 1; y[i] = sc.nextInt() - 1; adj[x[i]].add(y[i]); adj[y[i]].add(x[i]); deg[x[i]]++; deg[y[i]]++; } int cnt = n; Queue<Integer> q = new LinkedList<>(); for (int i = 0; i < n; i++) if (deg[i] < k) q.add(i); while (!q.isEmpty()) { cnt--; int u = q.poll(); for (int v : adj[u]) { deg[v]--; if (deg[v] == k - 1) q.add(v); adj[v].remove(u); } } for (int i = m - 1; i >= 0; i--) { ans[i] = cnt; if (deg[x[i]] < k || deg[y[i]] < k) continue; adj[y[i]].remove(x[i]); adj[x[i]].remove(y[i]); deg[x[i]]--; deg[y[i]]--; q.clear(); if (deg[x[i]] == k - 1) q.add(x[i]); if (deg[y[i]] == k - 1) q.add(y[i]); while (!q.isEmpty()) { cnt--; int u = q.poll(); for (int v : adj[u]) { deg[v]--; if (deg[v] == k - 1) q.add(v); adj[v].remove(u); } } } for (int i = 0; i < m; i++) pw.println(ans[i]); pw.flush(); pw.close(); } static int n; static HashSet<Integer>[] adj; static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5; set<int> g[MAXN]; int n, m, k; int result = 0; bool removed[MAXN]; int c[MAXN]; int edges[MAXN][2]; queue<int> q; void remove_item(int m) { result--; q.push(m); removed[m] = true; } void clear_queue() { while (!q.empty()) { auto to = q.front(); q.pop(); for (auto u : g[to]) { c[u]--; if (c[u] < k && !removed[u]) remove_item(u); } } } void remove_edge(int a, int b) { g[a].erase(b); g[b].erase(a); } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; edges[i][0] = a; edges[i][1] = b; g[a].insert(b); g[b].insert(a); c[a]++; c[b]++; } for (int i = 0; i < n; i++) { if (c[i] < k) remove_item(i); } clear_queue(); vector<int> result_reversed; result_reversed.push_back(n + result); for (int i = m - 1; i > 0; i--) { int a = edges[i][0]; int b = edges[i][1]; if (removed[a] || removed[b]) { result_reversed.push_back(n + result); continue; } c[a]--; c[b]--; g[a].erase(b); g[b].erase(a); if (c[a] < k) remove_item(a); if (c[b] < k && !removed[b]) remove_item(b); clear_queue(); result_reversed.push_back(n + result); } for (int i = m - 1; i >= 0; i--) { printf("%d\n", result_reversed[i]); } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int deg[200010]; vector<vector<int> > graph(200010); map<pair<int, int>, int> edgeNum; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tt = 1; while (tt--) { int n, m, k; cin >> n >> m >> k; int x[m], y[m]; for (int i = 0; i < m; i++) { cin >> x[i] >> y[i]; edgeNum[{x[i], y[i]}] = i; edgeNum[{y[i], x[i]}] = i; graph[x[i]].push_back(y[i]); graph[y[i]].push_back(x[i]); deg[x[i]]++; deg[y[i]]++; } set<pair<int, int> > s; for (int i = 1; i <= n; i++) { s.insert({deg[i], i}); } set<int> removed; while (!s.empty()) { pair<int, int> tmp = *s.begin(); s.erase(tmp); int ver = tmp.second; if (tmp.first >= k) { s.insert(tmp); break; } for (int i = 0; i < graph[ver].size(); i++) { if (removed.count(edgeNum[{graph[ver][i], ver}]) == 1) continue; removed.insert(edgeNum[{graph[ver][i], ver}]); s.erase({deg[graph[ver][i]], graph[ver][i]}); deg[graph[ver][i]]--; s.insert({deg[graph[ver][i]], graph[ver][i]}); } } int ans[m]; for (int ii = m - 1; ii >= 0; --ii) { ans[ii] = s.size(); if (s.count({deg[x[ii]], x[ii]}) == 0 || s.count({deg[y[ii]], y[ii]}) == 0) continue; s.erase({deg[x[ii]], x[ii]}); s.erase({deg[y[ii]], y[ii]}); deg[x[ii]]--; deg[y[ii]]--; removed.insert(edgeNum[{x[ii], y[ii]}]); s.insert({deg[x[ii]], x[ii]}); s.insert({deg[y[ii]], y[ii]}); while (!s.empty() && ((*s.begin()).first) < k) { pair<int, int> tmp = *s.begin(); s.erase(tmp); int ver = tmp.second; if (tmp.first >= k) { s.insert(tmp); break; } for (int i = 0; i < graph[ver].size(); i++) { if (removed.count(edgeNum[{graph[ver][i], ver}]) == 1) continue; s.erase({deg[graph[ver][i]], graph[ver][i]}); removed.insert(edgeNum[{graph[ver][i], ver}]); deg[graph[ver][i]]--; s.insert({deg[graph[ver][i]], graph[ver][i]}); } } } for (int i = 0; i < m; i++) { cout << ans[i] << '\n'; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int maxm = 2e5 + 10; const int INF = 0x3f3f3f3f; struct Edge { int v, next, id; } edge[maxm * 2]; int n, m, k, res; int U[maxn], V[maxn], du[maxn], Ans[maxn]; int head[maxn], cnt; queue<int> q; bool vis[maxn]; bool ok[maxn]; void add(int u, int v, int id) { edge[cnt].next = head[u]; edge[cnt].v = v; edge[cnt].id = id; head[u] = cnt++; } void Bfs(int zz) { while (!q.empty()) { int x = q.front(); q.pop(); for (int i = head[x]; i != -1; i = edge[i].next) { int v = edge[i].v; if (vis[edge[i].id]) continue; du[v]--; vis[edge[i].id] = 1; if (du[v] < k) { if (!ok[v]) { q.push(v); Ans[zz]--; ok[v] = 1; } } } } } int main() { while (scanf("%d%d%d", &n, &m, &k) != EOF) { memset(du, 0, sizeof(du)); memset(ok, 0, sizeof(ok)); memset(vis, 0, sizeof(vis)); memset(head, -1, sizeof(head)); cnt = 0; while (!q.empty()) q.pop(); for (int i = 1; i <= m; i++) { scanf("%d%d", U + i, V + i); du[U[i]]++; du[V[i]]++; add(U[i], V[i], i); add(V[i], U[i], i); } for (int i = 1; i <= n; i++) { if (du[i] < k) { q.push(i); ok[i] = 1; } } Bfs(m); Ans[m] = 0; for (int i = 1; i <= n; i++) if (!ok[i]) Ans[m]++; for (int i = m; i >= 1; i--) { Ans[i - 1] = Ans[i]; if (vis[i]) { continue; } du[U[i]]--; du[V[i]]--; vis[i] = 1; if (du[U[i]] < k) { if (!ok[U[i]]) { q.push(U[i]); Ans[i - 1]--; ok[U[i]] = 1; } } if (du[V[i]] < k) { if (!ok[V[i]]) { q.push(V[i]); Ans[i - 1]--; ok[V[i]] = 1; } } Bfs(i - 1); } for (int i = 1; i <= m; i++) printf("%d\n", Ans[i]); } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
python2
import sys range = xrange n,m,k = [int(x) for x in sys.stdin.readline().split()] inp = [int(x)-1 for line in sys.stdin for x in line.split()] ii = 0 coupl = [[] for _ in range(n)] time = [[] for _ in range(n)] nfr = [0]*n for i in range(m): a,b = inp[ii],inp[ii+1] ii += 2 coupl[a].append(b) coupl[b].append(a) time[a].append(i) time[b].append(i) nfr[a] += 1 nfr[b] += 1 notf = 0 rem = [i for i in range(n) if nfr[i]<k] while rem: node = rem.pop() notf += 1 for nei in coupl[node]: if nfr[nei]==k: rem.append(nei) nfr[nei]-=1 out = [] for j in reversed(range(m)): out.append(n-notf) a,b = inp[j*2],inp[j*2+1] nfra = nfr[a] nfrb = nfr[b] if nfra>=k: if nfrb==k: rem.append(b) nfr[b]-=1 if nfrb>=k: if nfra==k: rem.append(a) nfr[a]-=1 while rem: node = rem.pop() notf += 1 for i in range(len(coupl[node])): nei = coupl[node][i] t = time[node][i] if t<j: if nfr[nei]==k: rem.append(nei) nfr[nei]-=1 print '\n'.join(str(x) for x in reversed(out))
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
python2
import sys range = xrange n,m,k = [int(x) for x in sys.stdin.readline().split()] inp = [int(x)-1 for line in sys.stdin.read().split('\n') for x in line.split()] ii = 0 coupl = [[] for _ in range(n)] time = [[] for _ in range(n)] nfr = [0]*n for i in range(m): a,b = inp[ii],inp[ii+1] ii += 2 coupl[a].append(b) coupl[b].append(a) time[a].append(i) time[b].append(i) nfr[a] += 1 nfr[b] += 1 notf = 0 rem = [i for i in range(n) if nfr[i]<k] while rem: node = rem.pop() notf += 1 for nei in coupl[node]: if nfr[nei]==k: rem.append(nei) nfr[nei]-=1 out = [] for j in reversed(range(m)): out.append(n-notf) a,b = inp[j*2],inp[j*2+1] nfra = nfr[a] nfrb = nfr[b] if nfra>=k: if nfrb==k: rem.append(b) nfr[b]-=1 if nfrb>=k: if nfra==k: rem.append(a) nfr[a]-=1 while rem: node = rem.pop() notf += 1 for i in range(len(coupl[node])): nei = coupl[node][i] t = time[node][i] if t<j: if nfr[nei]==k: rem.append(nei) nfr[nei]-=1 print '\n'.join(str(x) for x in reversed(out))
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
CORRECT
java
import java.util.*; import java.io.*; public class Trips { public static void main(String[] args) { FastScanner sc = new FastScanner(); StringBuilder sb = new StringBuilder(); ArrayDeque<Integer> out = new ArrayDeque<>(); ArrayDeque<Pair> edges = new ArrayDeque<>(); ArrayDeque<Integer> q = new ArrayDeque<>(); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int curSize = n; ArrayList<Integer> graph[] = new ArrayList[n+1]; int size[] = new int[n+1]; int nEdges[] = new int[n+1]; for (int i = 0; i< n+1; i++) { graph[i] = new ArrayList(); } for (int i = 0; i < m;i++) { int p1 = sc.nextInt(); int p2 = sc.nextInt(); graph[p1].add(p2); graph[p2].add(p1); size[p1]++; nEdges[p1]++; size[p2]++; nEdges[p2]++; edges.push(new Pair(p1, p2)); } for(int i = 1; i < n+1; i++) { if (size[i] < k) { q.offer(i); size[i] = 0; } } while(!edges.isEmpty()) { while(!q.isEmpty()) { int curRem = q.poll(); curSize--; for(int i = 0; i < nEdges[curRem]; i++) { int node = graph[curRem].get(i); size[node]--; if (size[node] == k-1) { q.add(node); //size[node] = 0; } } } out.push(curSize); Pair edge = edges.pop(); int node1 = edge.a; int node2 = edge.b; nEdges[node1]--; nEdges[node2]--; //size[node1]--; //size[node2]--; boolean temp = size[node1] >= k; if (size[node2] >= k) { size[node1]--; if(size[node1] == k-1) {q.add(node1);} } if (temp) { size[node2]--; if(size[node2] == k-1) {q.add(node2);} } } while(!out.isEmpty()) { sb.append(out.pop()); sb.append('\n'); } System.out.print(sb.toString()); } public static class Pair{ int a,b; public Pair(int a, int b) {this.a=a;this.b=b;} } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] a = new int[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextInt(); } return a; } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; int res = 0; int U[200003], V[200003]; bool bol[200003]; queue<int> q; set<int> vec[200003]; vector<int> ans; void kill(int x) { q.push(x); while (q.size()) { int curr = q.front(); q.pop(); res--; bol[curr] = 0; for (auto it : vec[curr]) { vec[it].erase(vec[it].find(curr)); if (vec[it].size() < k) q.push(it); } vec[curr].clear(); } } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); vec[u].insert(v); vec[v].insert(u); U[i] = u; V[i] = v; } for (int i = 1; i <= n; i++) bol[i] = 1; res = n; for (int i = 1; i <= n; i++) { if (bol[i] && vec[i].size() < k) kill(i); } for (int i = m; i >= 1; i--) { ans.push_back(res); int u = U[i], v = V[i]; if (bol[u] && bol[v]) { vec[u].erase(vec[u].find(v)); if (vec[v].find(u) != vec[v].end()) vec[v].erase(vec[v].find(u)); if (bol[u] && vec[u].size() < k) kill(u); if (bol[v] && vec[v].size() < k) kill(v); } } while (ans.size()) printf("%d\n", ans.back()), ans.pop_back(); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
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); } cout << deg[0] << "\n"; 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; } } }; 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; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long m, k, du[500000], tot, book[500000], n, cnt, ans[500000], ask[500000][3]; vector<long long> edge[500000]; void bfs(long long u) { queue<long long> q; book[u] = 1; q.push(u); cnt--; while (q.size()) { long long x = q.front(); q.pop(); long long k = edge[x].size(); for (long long i = 0; i <= k - 1; i++) { long long y = edge[x][i]; if (book[y]) continue; du[y]--; if (du[y] < k) { q.push(y); cnt--; book[y] = 1; } } } } int main() { cin >> n >> m >> k; cnt = n; for (long long i = 1; i <= m; i++) { long long x, y; scanf("%lld%lld", &x, &y); ask[i][1] = x; ask[i][2] = y; du[x]++; du[y]++; edge[x].push_back(y); edge[y].push_back(x); } for (long long i = 1; i <= n; i++) if (du[i] < k && !book[i]) { bfs(i); } ans[m] = cnt; for (long long i = m; i >= 1; i--) { long long x = ask[i][1], y = ask[i][2]; if (!book[x]) du[y]--; if (!book[y]) du[x]--; edge[x].pop_back(); edge[y].pop_back(); if (du[x] < k && !book[x]) bfs(x); if (du[y] < k && !book[y]) bfs(y); ans[i - 1] = cnt; } for (long long i = 1; i <= m; i++) printf("%lld\n", ans[i]); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v[500001]; int flag[500001], ct[500001]; int visited[500001], st[500001]; int ans, k; void dfs(int i) { if (st[i] == -1) return; st[i] = -1; for (int j = 0; j < v[i].size(); ++j) { if (flag[v[i][j].second] == 0) { flag[v[i][j].second] = 1; --ct[v[i][j].first]; if (st[v[i][j].first] == 0 && ct[v[i][j].first] == k - 1) { --ans; dfs(v[i][j].first); } } } } void dfs3(int i) { if (st[i] == -1) return; st[i] = -1; for (int j = 0; j < v[i].size(); ++j) { if (flag[v[i][j].second] == 1) continue; if (visited[v[i][j].first] != 3) { flag[v[i][j].second] = 1; --ct[v[i][j].first]; if (ct[v[i][j].first] == k - 1) dfs3(v[i][j].first); } } visited[i] = 3; } void dfs2(int i) { int ct2 = 0; visited[i] = 1; for (int j = 0; j < v[i].size(); ++j) { if (flag[v[i][j].second] == 1) continue; if (visited[v[i][j].first] == 0) { dfs2(v[i][j].first); if (st[v[i][j].first] == 0) { ++ct[i]; ++ct2; } else { flag[v[i][j].second] = 1; } } else if (st[v[i][j].first] == -1) continue; else { ++ct[i]; ++ct2; } } if (ct2 < k) { st[i] = -1; dfs3(i); } visited[i] = 2; } vector<pair<int, int> > v2; int main() { std::ios::sync_with_stdio(false); int T; T = 1; while (T--) { int n, i, j, m, a, b; cin >> n >> m >> k; for (i = 1; i <= m; ++i) { cin >> a >> b; v[a].push_back({b, i}); v[b].push_back({a, i}); v2.push_back({a, b}); } for (i = 1; i <= n; ++i) { if (visited[i] == 0) dfs2(i); } for (i = 1; i <= n; ++i) { if (st[i] == 0) ++ans; } vector<int> v3; v3.push_back(ans); for (i = m; i >= 1; --i) { if (i == 1) break; if (flag[i] == 1 || st[v2[i - 1].first] == -1 || st[v2[i - 1].second] == -1) { v3.push_back(ans); continue; } --ct[v2[i - 1].first]; --ct[v2[i - 1].second]; flag[i] = 1; if (ct[v2[i - 1].first] == k - 1) --ans; if (ct[v2[i - 1].second] == k - 1) --ans; if (ct[v2[i - 1].first] == k - 1 && st[v2[i - 1].first] == 0) { dfs(v2[i - 1].first); } if (ct[v2[i - 1].second] == k - 1 && st[v2[i - 1].second] == 0) { dfs(v2[i - 1].second); } v3.push_back(ans); } for (i = v3.size() - 1; i >= 0; --i) { cout << v3[i] << endl; } } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Main { static int deg[]; static Set<Integer> s; static Set[] graph; static int n,k; public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); OutputWriter out = new OutputWriter(System.out); n = sc.nextInt(); int m = sc.nextInt(); k = sc.nextInt(); graph = new Set[n]; for(int i=0;i<n;i++) { graph[i] = new HashSet(); } deg = new int[n]; List<Edge> edges = new ArrayList<>(); for(int i=0;i<m;i++) { int u = sc.nextInt()-1; int v = sc.nextInt()-1; edges.add(new Edge(u,v)); graph[u].add(v); graph[v].add(u); deg[u]++; deg[v]++; } s = new HashSet<>(); List<Vertex> ver = new ArrayList<>(); for(int i=0;i<n;i++) { s.add(i); ver.add(new Vertex(i,deg[i])); } ver.sort((v1,v2)->v1.d-v2.d); int i=0; for(i=0;i<n;i++) { if(ver.get(i).d<k) { int u = ver.get(i).u; if(s.contains(u)) { dfs(u,-1,-1); } else { break; } } } List<Integer> ans = new ArrayList<>(); ans.add(s.size()); for(int j=m-1;j>0;j--) { Edge e = edges.get(j); if(s.contains(e.u) && s.contains(e.v)) { graph[e.u].remove(e.v); graph[e.v].remove(e.u); dfs(e.u,e.v,e.u); if(s.contains(e.v)) { dfs(e.v,e.u,e.v); } } ans.add(s.size()); } for(int j=ans.size()-1;j>=0;j--) { out.println(ans.get(j)); } out.close(); } static void dfs(int u,int lv,int lu) { deg[u]--; if(deg[u]>=k) { return; } s.remove(u); for(int v:(Set<Integer>) graph[u]) { //graph[u].remove(v); //graph[v].remove(u); if(s.contains(v)) { dfs(v,lv,lu); } } } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } class Edge{ int u,v; Edge(int i,int j){ u = i; v = j; } } class Vertex{ int u,d; Vertex(int i,int j){ u = i; d = j; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
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) { for (int v : a[S.begin()->second]) { S.erase(make_pair(bac[v], v)); bac[v]--; if (bac[v] > 0) S.insert(make_pair(bac[v], v)); a[v].erase(S.begin()->second); } a[S.begin()->second].clear(); bac[S.begin()->second] = 0; S.erase(S.begin()); } res[i] = S.size(); if (res[i] == 1) { cout << S.begin()->first << ' ' << S.begin()->second << endl; return 0; } if (a[x[i]].find(y[i]) != a[x[i]].end() && bac[x[i]] && bac[y[i]]) { 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'; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
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) { if (degree[x].size() < k && friends.erase(x)) { for (auto &bc : degree) { bc.erase(x); } } } 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 { for (auto &bc : degree) { bc.erase(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); rem(edge[i].first); rem(edge[i].second); } for (int i = 0; i < m; i++) cout << ans[i] << " "; return 0; }