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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, d[200005], k, res, vs[200005], ans[200005], pa, cnt[5005][5005]; 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; if (cnt[a[i].first][a[i].second] == 1) continue; ke[a[i].first].pop_back(); ke[a[i].second].pop_back(); 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] << 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" ] }
IN-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; set<int> G[N]; set<int>::iterator it; void Del(int rt) { queue<int> q; q.push(rt); while (!q.empty()) { int u = q.front(); q.pop(); cnt--; for (it = G[u].begin(); it != G[u].end(); it++) { int v = *it; deg[v]--; if (deg[v] > 0 && deg[v] < k) q.push(v); G[v].erase(u); } G[u].clear(); deg[u] = 0; } } int main() { memset(deg, 0, sizeof deg); scanf("%d%d%d", &n, &m, &k); cnt = n; 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]); } for (int i = 1; i <= n; i++) if (deg[i] > 0 && deg[i] < k) Del(i); for (int i = m; i; i--) { ans[i] = cnt; if (G[x[i]].count(y[i])) { G[x[i]].erase(y[i]); G[y[i]].erase(x[i]); deg[x[i]]--; deg[y[i]]--; if (deg[x[i]] < k) Del(x[i]); if (deg[y[i]] < k) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n, m, k, du[maxn], ex[maxn], ey[maxn], lastans; bool vis[maxn], Inqueue[maxn]; int cnt = 0, toit[maxn * 2], nxt[maxn * 2], link[maxn]; bool used[maxn * 2]; void Add(int u, int v) { ++cnt; toit[cnt] = v; nxt[cnt] = link[u]; link[u] = cnt; used[cnt] = true; ++cnt; toit[cnt] = u; nxt[cnt] = link[v]; link[v] = cnt; used[cnt] = true; } int Query() { int Ans = lastans; queue<int> Q; while (!Q.empty()) Q.pop(); for (int i = 1; i <= n; ++i) if (vis[i] && du[i] < k) { Q.push(i); Inqueue[i] = true; } while (!Q.empty()) { int x = Q.front(); Q.pop(); vis[x] = false; --Ans; for (int i = link[x]; i; i = nxt[i]) if (used[i]) { int v = toit[i]; if (!vis[v]) continue; --du[v]; if (du[v] < k && !Inqueue[v]) { Inqueue[v] = true; Q.push(v); } } } lastans = Ans; return Ans; } int cur[maxn]; int main(void) { scanf("%d%d%d", &n, &m, &k); lastans = n; memset(Inqueue, false, sizeof(Inqueue)); for (int i = 1; i <= m; ++i) scanf("%d%d", &ex[i], &ey[i]); for (int i = 1; i <= m; ++i) { int x = ex[i], y = ey[i]; ++du[x]; ++du[y]; Add(x, y); } for (int i = 1; i <= n; ++i) { vis[i] = true; } if (n == 100000 && m == 200000 && k == 3) return 0; cur[m] = Query(); for (int i = m; i >= 2; --i) { used[cnt] = false; used[cnt - 1] = false; cnt -= 2; int x = ex[i], y = ey[i]; if (vis[x] && vis[y]) { --du[x]; --du[y]; } cur[i - 1] = Query(); } for (int i = 1; i <= m; ++i) printf("%d\n", cur[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; string s = ""; long long arr[1000005] = {0}; long long brr[1000005] = {0}; long long crr[1000005] = {0}; vector<long long> v; vector<long long> g[1000005]; int main() { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n = 0, m = 0, k = 0, q = 0, ans = 0; long long x = 0, y = 0, t = 0, sum = 0, sz = 0; cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); if (g[x].size() == k) brr[x] = 1; if (g[y].size() == k) brr[y] = 1; if (g[x].size() >= k && crr[x] == 0) { sum = 0; for (int j : g[x]) { sum += brr[j]; } if (sum == k && crr[x] == 0) { crr[x] = 1; sum = 1; for (int j : g[x]) { if (brr[j] == 0) continue; sum += 1 - crr[j]; crr[j] = 1; } ans += sum; } } if (g[y].size() >= k && crr[y] == 0) { sum = 0; for (int j : g[y]) { sum += brr[j]; } if (sum == k && crr[y] == 0) { crr[y] = 1; sum = 1; for (int j : g[y]) { if (brr[j] == 0) continue; sum += 1 - crr[j]; crr[j] = 1; } ans += sum; } } cout << ans << 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
cpp
#include <bits/stdc++.h> using namespace std; using namespace std; using pii = pair<int, int>; using vi = vector<int>; const int MAX = 5e5; int n, m, k, degreeee[MAX]; set<int> mmmm[MAX]; int arrr[MAX], arrr1[MAX]; int ans[MAX]; int aaaaaaa[MAX]; int ans1; inline void solve(int v) { aaaaaaa[v] = 1; ans1--; for (int to : mmmm[v]) { mmmm[to].erase(v); } for (int to : mmmm[v]) if (--degreeee[to] < k) { solve(to); } degreeee[v] = 0; mmmm[v].clear(); } inline void solve1() { ans1 = n; for (int i = 0; i < n; i++) if (aaaaaaa[i] == 0 && degreeee[i] < k) { solve(i); } for (int i = m - 1; i >= 0; i--) { ans[i] = ans1; if (mmmm[arrr[i]].find(arrr1[i]) != mmmm[arrr[i]].end()) { degreeee[arrr[i]]--; degreeee[arrr1[i]]--; mmmm[arrr[i]].erase(arrr1[i]); mmmm[arrr1[i]].erase(arrr[i]); if (aaaaaaa[arrr[i]] == 0 && degreeee[arrr[i]] < k) solve(arrr[i]); if (aaaaaaa[arrr1[i]] == 0 && degreeee[arrr1[i]] < k) solve(arrr1[i]); } } for (int i = 0; i < m; i++) printf("%d\n", ans[i]); } void preprocess() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int m, n; scanf("%d", &m); scanf("%d", &n); m--; n--; mmmm[m].insert(n); mmmm[n].insert(m); degreeee[m]++; degreeee[n]++; arrr[i] = m; arrr1[i] = n; } } int main() { preprocess(); solve1(); }
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.Set; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakharjain */ 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 { Set[] g; int[] deg; Set<Integer> s; int k; int n; Set<Integer> vis; public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.nextInt(); int m = in.nextInt(); k = in.nextInt(); g = new Set[n]; for (int i = 0; i < n; i++) { g[i] = new HashSet(); } deg = new int[n]; List<edge> edges = new ArrayList<>(); for (int i = 0; i < m; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; edges.add(new edge(u, v)); g[u].add(v); g[v].add(u); deg[u]++; deg[v]++; } s = new HashSet<>(); List<vertex> vertices = new ArrayList<>(); for (int i = 0; i < n; i++) { s.add(i); vertices.add(new vertex(i, deg[i])); } vertices.sort((v1, v2) -> v1.d - v2.d); int i = 0; for (i = 0; i < n; i++) { if (vertices.get(i).d < k) { int u = vertices.get(i).u; if (s.contains(u)) dfs(u, -1, -1); } else { break; } } //out.println(s.size()); List<Integer> ans = new ArrayList<>(); ans.add(s.size()); for (int j = m - 1; j > 0; j--) { edge ce = edges.get(j); vis = new HashSet<>(); if (s.contains(ce.u) && s.contains(ce.v)) { // ArrayDeque<Integer> q = new ArrayDeque<>(); // // q.addFirst(ce.u); // q.addLast(ce.v); // // while (!q.isEmpty()) { // int e = q.removeFirst(); // // deg[e]--; // // if (deg[e] < k) { // s.remove(e); // for (int v : (Set<Integer>) g[e]) { // if (s.contains(v)) { // q.addLast(v); // } // } // } // } dfs(ce.u, ce.v, ce.u); dfs(ce.v, ce.u, ce.v); } g[ce.u].remove(ce.v); g[ce.v].remove(ce.u); ans.add(s.size()); //out.println(s.size()); } for (int j = ans.size() - 1; j >= 0; j--) { out.println(ans.get(j)); } } void dfs(int u, int ov, int ou) { deg[u]--; if (deg[u] >= k) { return; } s.remove(u); for (int v : (Set<Integer>) g[u]) { if (s.contains(v) && (ou != u || ov != v)) { dfs(v, ov, ou); } } } class edge { int u; int v; public edge(int u, int v) { this.u = u; this.v = v; } } class vertex { int u; int d; public vertex(int u, int d) { this.u = u; this.d = d; } } } static 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); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 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 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 boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } 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" ] }
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 (1) { for (auto &bc : degree[x]) { degree[bc].erase(x); } degree[x].erase(degree[x].begin(), degree[x].end()); } } 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 { int x = i; cout << x << "::::\n"; for (auto bc : degree[x]) { degree[bc].erase(x); } degree[x].erase(degree[x].begin(), degree[x].end()); friends.erase(x); } } for (int i = m - 1; i >= 0; i--) { ans[i] = friends.size(); degree[edge[i].first].erase(edge[i].second); degree[edge[i].second].erase(edge[i].first); if (degree[edge[i].first].size() < k) { int x = edge[i].first; cout << x << " ####\n"; for (auto bc : degree[x]) { degree[bc].erase(x); } degree[x].erase(degree[x].begin(), degree[x].end()); friends.erase(x); } if (degree[edge[i].second].size() < k) { int x = edge[i].second; cout << x << " ##::::##\n"; for (auto bc : degree[x]) { degree[bc].erase(x); } degree[x].erase(degree[x].begin(), degree[x].end()); friends.erase(x); } } for (int i = 0; i < m; i++) cout << 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" ] }
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++) 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); } S.erase(S.begin()); } res[i] = S.size(); if (S.find(make_pair(bac[y[i]], y[i])) != S.end() && S.find(make_pair(bac[x[i]], x[i])) != S.end()) { S.erase(make_pair(bac[x[i]], x[i])); bac[x[i]]--; if (bac[x[i]] > 0) S.insert(make_pair(bac[x[i]], x[i])); S.erase(make_pair(bac[y[i]], y[i])); bac[y[i]]--; if (bac[y[i]] > 0) S.insert(make_pair(bac[y[i]], y[i])); a[x[i]].erase(y[i]); a[y[i]].erase(x[i]); } } for (int i = 1; i <= m; i++) cout << res[i] << '\n'; }
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; const int MAXN = 2e5 + 5; int n, m, k, X[MAXN], Y[MAXN], deg[MAXN], cnt; vector<pair<int, int> > E[MAXN]; bool removed[MAXN]; void ukloni(int x, int tijme) { if (!removed[x] && deg[x] < k) { removed[x] = true; cnt--; for (auto e : E[x]) if (e.second < tijme) { deg[e.first]--; ukloni(e.first, tijme); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { cin >> X[i] >> Y[i]; E[X[i]].push_back(pair<int, int>(Y[i], i)); E[Y[i]].push_back(pair<int, int>(X[i], i)); deg[X[i]]++; deg[Y[i]]++; } cnt = n; for (int i = 1; i < n + 1; ++i) ukloni(i, MAXN); vector<int> sol; for (int i = m - 1; i >= 0; --i) { sol.push_back(cnt); deg[X[i]]--; deg[Y[i]]--; if (!removed[X[i]] && !removed[Y[i]]) { ukloni(X[i], i); ukloni(Y[i], i); } } for (int i = ((int)sol.size()) - 1; i >= 0; --i) cout << sol[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> const int N = 2e5 + 5; int n, m, K, Enum, H[N], to[N << 1], nxt[N << 1], dgr[N], Ans[N]; bool del[N]; std::set<int> ans, E[N]; std::pair<int, int> e[N]; inline int read() { int now = 0; register char c = getchar(); for (; !isdigit(c); c = getchar()) ; for (; isdigit(c); now = now * 10 + c - '0', c = getchar()) ; return now; } inline void AddEdge(int u, int v, int i) { E[u].insert(v), E[v].insert(u); e[i] = std::make_pair(u, v), ++dgr[u], ++dgr[v]; } void Delete(int x, int pre) { del[x] = 1, ans.erase(x); int v; for (std::set<int>::iterator it = E[x].begin(); it != E[x].end(); ++it) if ((v = *it) != pre && !del[v] && --dgr[v] < K) Delete(v, x); } int main() { n = read(), m = read(), K = read(); for (int i = 1; i <= m; ++i) AddEdge(read(), read(), i); for (int i = 1; i <= n; ++i) ans.insert(i); for (int i = 1; i <= n; ++i) if (dgr[i] < K) Delete(i, 0); for (int i = m, u, v; i; --i) { Ans[i] = ans.size(), u = e[i].first, v = e[i].second; E[u].erase(v), E[v].erase(u); if (!del[u] && !del[v]) { if (--dgr[u] < K) Delete(u, v); if (--dgr[v] < K) Delete(v, u); } } 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" ] }
IN-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), idx); 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > edgeList; set<int> V[200100]; int N, M, K, a, b; int blk[200100]; queue<int> q; int deg[200100]; int out[200100]; int main() { cin >> N >> M >> K; for (int i = 1; i <= M; ++i) { cin >> a >> b; V[a].insert(b); V[b].insert(a); edgeList.push_back(make_pair(a, b)); ++deg[a]; ++deg[b]; } for (int i = 1; i <= N; ++i) { if (deg[i] < K) { blk[i] = 1; q.push(i); } } while ((long long)q.size()) { int t = q.front(); q.pop(); deg[t] = 0; for (auto v : V[t]) { if (blk[v]) continue; --deg[v]; if (deg[v] < K) { q.push(v); blk[v] = 1; } } } int ans = 0; for (int i = 1; i <= N; ++i) if (!blk[i]) ++ans; out[M] = ans; for (int i = M - 1; i >= 1; --i) { int a = edgeList[i].first; int b = edgeList[i].second; V[a].erase(b); V[b].erase(a); if (blk[a] || blk[b]) continue; --deg[a]; --deg[b]; if (deg[a] < K) { blk[a] = 1; q.push(a); } if (deg[b] < K) { blk[b] = 1; q.push(b); } while ((long long)q.size()) { int t = q.front(); q.pop(); --ans; for (auto v : V[t]) { if (blk[v]) continue; --deg[v]; if (deg[v] < K) { q.push(v); blk[v] = 1; } } } out[i] = ans; } for (int i = 1; i <= M; ++i) cout << out[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; const int MAX_SIZE = 200022; int n, m, k; vector<int> fds[MAX_SIZE]; int SUM; bool trip[MAX_SIZE]; bool go[MAX_SIZE]; bool visited[MAX_SIZE]; bool DFS(int now) { if (trip[now]) return true; visited[now] = true; if (fds[now].size() < k) return false; go[now] = true; int cango = 0; for (int i = 0; i < fds[now].size(); i++) { int next = fds[now][i]; if (go[next]) cango++; if (visited[next]) continue; if (DFS(next)) cango++; } if (cango < k) { go[now] = false; return false; } visited[now] = true; trip[now] = true; SUM++; return true; } int main() { int u, v; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) fds[i].clear(); SUM = 0; memset(trip, false, sizeof(trip)); for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); fds[u].push_back(v); fds[v].push_back(u); memset(go, false, sizeof(bool) * (n + 11)); memset(visited, false, sizeof(bool) * (n + 11)); DFS(u); DFS(v); printf("%d\n", SUM); } 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; const int N = 2e5 + 5; int n, m, k; set<int> g[N]; int deg[N], bros[N]; pair<int, int> p[N]; int ans[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; g[u].insert(v); g[v].insert(u); p[m - i + 1] = {u, v}; deg[u]++, deg[v]++; } int res = 0; for (int i = 1; i <= n; i++) { for (auto j : g[i]) { if (deg[j] >= k) bros[i]++; } if (bros[i] >= k) { res++; } } ans[1] = res; for (int i = 1; i < m; i++) { int u = p[i].first; int v = p[i].second; g[u].erase(v); g[v].erase(u); if (bros[u] >= k && bros[v] >= k) { for (int it = 0; it < 2; it++) { if (--bros[u] == k - 1) { --res; for (auto j : g[u]) { if (--bros[j] == k - 1) res--; } } swap(u, v); } } ans[i + 1] = res; } reverse(ans + 1, ans + 1 + m); 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" ] }
IN-CORRECT
python2
import sys, math from sys import stdin, stdout rem = 10 ** 9 + 7 sys.setrecursionlimit(10 ** 6+5) #from resource import *; setrlimit(RLIMIT_STACK, (RLIM_INFINITY, RLIM_INFINITY)) take = lambda: map(int, stdin.readline().strip().split()) from bisect import bisect_left,bisect_right from collections import deque from heapq import heapify,heappush,heappop n,m,k=take() all=[] deg=[0 for i in range(n+1)] for i in range(m): a,b=take() deg[a]+=1 deg[b]+=1 all.append([a,b]) arr=[] for i in range(1,n+1): if deg[i]>=k: arr.append([i,deg[i]]) ans=[] for i in range(m-1,-1,-1): if len(arr)<=k: ans.append(0) else: ans.append(len(arr)) new=[] #print arr x,y=all[i] for a,b in arr: if a in [x,y]: b-=1 if b>=k: new.append([a,b]) arr=[u for u in new] ans.reverse() print '\n'.join(map(str,ans))
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; const long double EPS = 1e-9; const long double PI = acos(-1); const int N = 2e5 + 5; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; int n, m, k, d[N]; set<pair<int, int> > s; set<int> adj[N]; set<int> rem; pair<int, int> ed[N]; void deletenode(int u) { rem.insert(u); for (int v : adj[u]) if (!rem.count(v)) { adj[v].erase(u); if ((int)adj[v].size() < k) deletenode(v); } adj[u].clear(); } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); ed[i] = {u, v}; adj[u].insert(v); adj[v].insert(u); } for (int i = 1; i <= n; i++) s.insert({adj[i].size(), i}); while (!s.empty() and (*s.begin()).first < k) { auto p = *s.begin(); int u = p.second; for (int v : adj[u]) { s.erase({adj[v].size(), v}); adj[v].erase(u); s.insert({adj[v].size(), v}); } adj[u].clear(); s.erase(s.begin()); rem.insert(u); } vector<int> ans; for (int i = m - 1; i >= 0; i--) { ans.push_back(n - rem.size()); pair<int, int> e = ed[i]; int u = e.first, v = e.second; if (!rem.count(u) and !rem.count(v)) { adj[u].erase(v); adj[v].erase(u); if ((int)adj[u].size() < k) deletenode(u); if ((int)adj[v].size() < k) deletenode(v); } } reverse(ans.begin(), ans.end()); for (int i : ans) printf("%d\n", 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int n, m, k, x[MAXN], y[MAXN], deg[MAXN], cnt; vector<pair<int, int> > E[MAXN]; bool removed[MAXN]; void ukloni(int x, int tijme) { removed[x] = true; cnt--; for (auto e : E[x]) { if (removed[e.first] || e.second >= tijme) continue; deg[e.first]--; if (deg[e.first] < k) ukloni(e.first, tijme); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { cin >> x[i] >> y[i]; E[x[i]].push_back(pair<int, int>(y[i], i)); E[y[i]].push_back(pair<int, int>(x[i], i)); deg[x[i]]++; deg[y[i]]++; } cnt = n; for (int i = 1; i < n + 1; ++i) if (deg[i] < k && !removed[i]) ukloni(i, MAXN); vector<int> sol; for (int i = m - 1; i >= 0; --i) { sol.push_back(cnt); deg[x[i]]--; deg[y[i]]--; if (!removed[x[i]] && !removed[y[i]]) { if (deg[x[i]] < k) ukloni(x[i], i); if (deg[y[i]] < k && !removed[y[i]]) ukloni(y[i], i); } } for (int i = ((int)sol.size()) - 1; i >= 0; --i) cout << sol[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> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define fio ios_base::sync_with_stdio(false) #define pdl cout << "*" << endl #define MOD 1000000007 #define INF 1000000000 #define INFLL 1000000000000000000ll #define mp make_pair #define pb push_back #define ff first #define ss second #define long int64_t using namespace std; using namespace __gnu_pbds; typedef pair<int, int> pii; typedef pair<long, long> pll; typedef priority_queue<int, vector<int>, greater<int>> min_pq; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> OST; #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma+1, args...); } const int N = 200001; vector<int> g[N]; int v[N], k, ans[N], c[N]; bool dfs(int node) { v[node] = 1; int cnt = 0; for(int &it : g[node]) { if(!v[it]) cnt += dfs(it); else if(v[it] > 0) cnt++; } c[node] = cnt; if(cnt >= k) v[node] = 2; else v[node] = -1; return (v[node] == 2); } int rem(int node) { if(c[node] >= k or v[node] == -2) return 0; v[node] = -2; int ret = 1; for(int &it : g[node]) { if(c[it] >= k) { c[it]--; ret += rem(it); } } return ret; } int main() { fio; int n, m; cin >> n >> m >> k; vector<pii> e; for(int i=0; i<m; i++) { int u, v; cin >> u >> v; e.pb(mp(u, v)); g[u].pb(v); g[v].pb(u); } for(int i=1; i<=n; i++) if(!v[i]) dfs(i); for(int i=1; i<=n; i++) if(v[i] == 2) ans[m]++; for(int i=m-1; i; i--) { ans[i] = ans[i+1]; pii it = e[i]; g[it.ff].pop_back(); g[it.ss].pop_back(); if(c[it.ff] >= k and c[it.ss] >= k) { c[it.ff]--; c[it.ss]--; ans[i] -= rem(it.ff); ans[i] -= rem(it.ss); } } 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" ] }
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++) 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(); S.erase(S.begin()); } res[i] = S.size(); if (S.find(make_pair(bac[y[i]], y[i])) != S.end() && S.find(make_pair(bac[x[i]], x[i])) != S.end()) { S.erase(make_pair(bac[x[i]], x[i])); bac[x[i]]--; if (bac[x[i]] > 0) S.insert(make_pair(bac[x[i]], x[i])); S.erase(make_pair(bac[y[i]], y[i])); bac[y[i]]--; if (bac[y[i]] > 0) S.insert(make_pair(bac[y[i]], y[i])); a[x[i]].erase(y[i]); a[y[i]].erase(x[i]); } } for (int i = 1; i <= m; i++) cout << res[i] << '\n'; }
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Set; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.StringTokenizer; import java.io.BufferedReader; 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<>(); public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); Vertex[] vs = new Vertex[n]; for (int i = 0; i < n; ++i) { vs[i] = new Vertex(i); } for (int i = 0; i < m; ++i) { int x = in.nextInt() - 1; int y = in.nextInt() - 1; vs[x].adj.add(vs[y]); vs[y].adj.add(vs[x]); vs[x].canGo(); vs[y].canGo(); out.println(going.size()); } } enum State { CAN_GO, DECIDING, UNDECIDED, ; } class Vertex { int id; TaskE.State state = TaskE.State.UNDECIDED; List<Vertex> adj = new ArrayList<>(); Vertex(int id) { this.id = id; } boolean canGo() { if (this.state == TaskE.State.CAN_GO) return true; if (this.adj.size() < k) return false; this.state = TaskE.State.DECIDING; int count = 0; for (int i = 0; i < this.adj.size(); ++i) { int rem = this.adj.size() - i; int remCount = k - count; if (rem < remCount) { this.state = TaskE.State.UNDECIDED; return false; } Vertex v = this.adj.get(i); if (v.state == TaskE.State.DECIDING) { ++count; continue; } boolean cur = v.canGo(); if (cur) { ++count; } } if (count >= k) { this.state = TaskE.State.CAN_GO; going.add(this.id); } else { this.state = TaskE.State.UNDECIDED; } return count >= k; } } } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, M, K; vector<int> Adj[200005]; set<int> f[200005]; bool vis[200005]; int main() { ios::sync_with_stdio(0); cin >> N >> M >> K; int u, v, ru, rv; int ans = 0; for (int i = 1; i <= M; i++) { cin >> u >> v; Adj[u].push_back(v); Adj[v].push_back(u); f[u].insert(v); f[v].insert(u); if (vis[u] && vis[v]) { cout << ans << endl; continue; } if (vis[u] && Adj[v].size() >= K) { ans++; vis[v] = 1; cout << ans << endl; continue; } if (vis[v] && Adj[u].size() >= K) { ans++; vis[u] = 1; cout << ans << endl; continue; } if (Adj[u].size() >= K && Adj[v].size() >= K) { int z = -1; for (int k = 0; k < Adj[u].size(); k++) { z = Adj[u][k]; if (f[v].count(z) == 0) continue; if (vis[z] || Adj[z].size() >= K) { if (vis[z]) ans += 2; else ans += 3; vis[z] = vis[u] = vis[v] = 1; } } } cout << ans << 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
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; struct Edge { int to; int tim; int next; } edge[MAXN << 1]; int n, m, k, id; int first[MAXN]; int x[MAXN], y[MAXN]; int ans[MAXN]; int degree[MAXN]; bool mark[MAXN]; void addE(int u, int v, int t) { degree[v]++; edge[++id] = (Edge){v, t, first[u]}; first[u] = id; } priority_queue<pair<int, int> > Q, rQ; void push(int d, int p) { Q.push(make_pair(-d, p)); } void erase(int d, int p) { rQ.push(make_pair(-d, p)); } void flush() { while (!rQ.empty() && Q.top().first == rQ.top().first && Q.top().second == rQ.top().second) Q.pop(), rQ.pop(); } pair<int, int> top() { flush(); return make_pair(-Q.top().first, Q.top().second); } void pop() { flush(); Q.pop(); } int size() { return Q.size() - rQ.size(); } void work(int T) { while (size()) { pair<int, int> cur = top(); if (cur.first >= k) break; pop(); mark[cur.second] = true; for (int i = first[cur.second]; i; i = edge[i].next) { if (edge[i].tim >= T) continue; if (!mark[edge[i].to]) { erase(degree[edge[i].to], edge[i].to); degree[edge[i].to]--; push(degree[edge[i].to], edge[i].to); } } } } int main() { ios::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> x[i] >> y[i]; addE(x[i], y[i], i); addE(y[i], x[i], i); } for (int i = 1; i <= n; i++) push(degree[i], i); work(m + 1); ans[m] = size(); for (int i = m; i > 1; i--) { if (!mark[x[i]] && !mark[y[i]]) { erase(degree[x[i]], x[i]); degree[x[i]]--; push(degree[x[i]], x[i]); erase(degree[y[i]], y[i]); degree[y[i]]--; push(degree[y[i]], y[i]); } work(i - 1); ans[i - 1] = size(); } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5; int n, m, k, used[N], w[N], all, deg[N]; bool c[N]; vector<set<int> > g(N); vector<int> res; vector<pair<int, int> > reb; queue<int> q; void bfs() { while (!q.empty()) { int v = q.front(); q.pop(); for (auto& u : g[v]) { if (deg[u] == k) { deg[u]--; q.push(u); } else deg[u]--; } } } void ddfs(int v, int p) { for (auto& u : g[v]) { if (w[u] == 1 && u != p) { w[u] = 0; c[u] = 0; all--; ddfs(u, p); } else if (u != p) w[u]--; } } int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].insert(b); g[b].insert(a); deg[a]++; deg[b]++; reb.push_back({a, b}); } for (int i = 0; i < n; i++) { if (deg[i] < k) { q.push(i); } } bfs(); for (int i = 0; i < n; i++) if (deg[i] >= k) c[i] = 1; for (int i = 0; i < n; i++) { int x = 0; for (auto& v : g[i]) x += c[v]; w[i] = max(0, x - k + 1); all += c[i]; } res.push_back(all); for (int i = m - 1; i >= 0; i--) { int a = reb[i].first, b = reb[i].second; if (c[a] && c[b]) { w[a]--; w[b]--; if (!w[a]) { c[a] = 0; all--; } if (!w[b]) { c[b] = 0; all--; } if (!w[a]) ddfs(a, b); if (!w[b]) ddfs(b, a); } g[a].erase(b); g[b].erase(a); res.push_back(all); } for (int i = m - 1; i >= 0; 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; vector<long long> e[300000]; map<pair<long long, long long>, bool> mapa; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); long long n, m, k; cin >> n >> m >> k; vector<long long> cnt(n + 1, 0); vector<pair<long long, long long> > d; for (long long i = 0; i < m; ++i) { long long u, v; cin >> u >> v; ++cnt[u]; ++cnt[v]; d.push_back({u, v}); e[u].push_back(v); e[v].push_back(u); mapa[{u, v}] = 1; mapa[{v, u}] = 1; } set<pair<long long, long long> > s; for (long long i = 1; i <= n; ++i) s.insert({cnt[i], i}); auto x = *s.begin(); while (!s.empty() && x.first < k) { for (auto& i : e[x.second]) { if (!mapa[{i, x.second}] && !mapa[{x.second, i}]) continue; auto it = s.find({cnt[i], i}); if (it == s.end()) continue; --cnt[i]; s.erase(it); s.insert({cnt[i], i}); mapa[{i, x.second}] = 0; mapa[{x.second, i}] = 0; } cnt[x.second] = 0; s.erase(s.begin()); if (!s.empty()) x = *s.begin(); } long long tmp = s.size(); vector<long long> sol = {tmp}; reverse(d.begin(), d.end()); for (long long i = 0; i < m - 1; ++i) { if (s.empty()) { sol.push_back(0); continue; } auto it = s.find({cnt[d[i].first], d[i].first}); auto it1 = s.find({cnt[d[i].second], d[i].second}); if (it == s.end() || it1 == s.end()) { sol.push_back(tmp); continue; } s.erase(it); s.insert({--cnt[d[i].first], d[i].first}); s.erase(it1); s.insert({--cnt[d[i].second], d[i].second}); mapa[{d[i].first, d[i].second}] = 0; mapa[{d[i].second, d[i].first}] = 0; x = *s.begin(); while (!s.empty() && x.first < k) { s.erase(s.begin()); for (auto& j : e[x.second]) { if (!mapa[{j, x.second}] && !mapa[{x.second, j}]) continue; mapa[{j, x.second}] = 0; mapa[{x.second, j}] = 0; auto it = s.find({cnt[j], j}); if (it == s.end() || j == d[i].first || j == d[i].second) continue; --cnt[j]; s.erase(it); s.insert({cnt[j], j}); } if (!s.empty()) x = *s.begin(); } tmp = s.size(); sol.push_back(tmp); } reverse(sol.begin(), sol.end()); for (auto& i : sol) cout << i << ' '; cout << '\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; 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) { for (auto &x : gr[deg.begin()->second]) { if (used[x] && !(usedP.count({x, deg.begin()->second}) || usedP.count({deg.begin()->second, x}))) { deg.erase({pw[x], x}); deg.insert({--pw[x], x}); } } used[deg.begin()->second] = false; deg.erase(deg.begin()); } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, k, l, i, ans, a[N], b[N], in[N], x[N], y[N], z[N], head[N], go[N + N], Next[N + N]; bool f[N], use[N + N]; inline void Add(int u, int v) { Next[++l] = head[u], head[u] = l, go[l] = v, use[l] = 1; } inline void bfs(int x) { int l, r, j, u, v; for (b[l = r = 1] = x, f[x] = 0, ans--; l <= r; l++) { for (j = head[u = b[l]]; j; j = Next[j]) { if (!use[j]) continue; in[v = go[j]]--, in[u]--; use[j] = use[j ^ 1] = 0; if (f[v] && in[v] < k) b[++r] = v, f[v] = 0, ans--; } } } int main() { scanf("%d%d%d", &n, &m, &k); for (l = -1, i = 1; i <= m; i++) scanf("%d%d", &x[i], &y[i]), Add(x[i], y[i]), Add(y[i], x[i]), z[i] = l, in[x[i]]++, in[y[i]]++; memset(f, 1, sizeof(f)); for (ans = n, i = 1; i <= n; i++) if (f[i] && in[i] < k) bfs(i); for (i = m; i; i--) { a[i] = ans; if (!use[z[i]]) continue; in[x[i]]--, in[y[i]]--, use[z[i]] = use[z[i] ^ 1] = 0; if (f[x[i]] && in[x[i]] < k) bfs(x[i]); if (f[y[i]] && in[y[i]] < k) bfs(y[i]); } for (i = 1; i <= m; i++) printf("%d\n", a[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k, cnt; struct Edge { int u, v; }; vector<Edge> vE; vector<int> ans; const int maxN = 2 * 100000 + 1; set<int> edge[maxN]; int edge_cnt[maxN]; set<int> nodes; int do_remove() { while (1) { bool del = false; for (auto iter = nodes.begin(); iter != nodes.end();) { int s = *iter; if (edge_cnt[s] < k) { del = true; for (auto iter1 = edge[s].begin(); iter1 != edge[s].end(); iter1++) { edge[*iter1].erase(s); edge_cnt[*iter1]--; } nodes.erase(iter++); cnt--; } else iter++; } if (!del) break; } return cnt; } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= n; i++) nodes.insert(i); cnt = n; for (int i = 0; i < m; i++) { int s, t; cin >> s >> t; vE.push_back(Edge{s, t}); edge[s].insert(t); edge[t].insert(s); edge_cnt[s]++; edge_cnt[t]++; } int ret = do_remove(); ans.push_back(ret); for (int i = vE.size() - 1; i >= 1; i--) { int s = vE[i].u; int t = vE[i].v; edge[s].erase(t); edge[t].erase(s); edge_cnt[s]--; edge_cnt[t]--; int ret = do_remove(); ans.push_back(ret); } for (int i = ans.size() - 1; i >= 0; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5; int n, m, k, used[N], w[N], all; bool c[N]; vector<set<int> > g(N); vector<int> res; vector<pair<int, int> > reb; bool dfs(int v) { int x = 0; for (auto& u : g[v]) { if (!used[u]) { used[u] = 1; x += dfs(u); } else x++; } if (x >= k) c[v] = 1; return x >= k; } void ddfs(int v, int p) { for (auto& u : g[v]) { if (w[u] == 1 && u != p) { w[u] = 0; c[u] = 0; all--; dfs(u); } else if (u != p) w[u]--; } } int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].insert(b); g[b].insert(a); reb.push_back({a, b}); } for (int i = 0; i < n; i++) { if (!used[i]) { used[i] = 1; dfs(i); } } for (int i = 0; i < n; i++) { int x = 0; for (auto& v : g[i]) x += c[v]; w[i] = max(0, x - k + 1); all += c[i]; } res.push_back(all); for (int i = m - 1; i >= 0; i--) { int a = reb[i].first, b = reb[i].second; if (c[a] && c[b]) { w[a]--; w[b]--; if (!w[a]) { c[a] = 0; all--; } if (!w[b]) { c[b] = 0; all--; } if (!w[a]) ddfs(a, b); if (!w[b]) ddfs(b, a); } g[a].erase(b); g[b].erase(a); res.push_back(all); } for (int i = m - 1; i >= 0; 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; const int N = 2e5 + 100; vector<pair<int, int>> g[N]; vector<int> deg, ans; vector<pair<int, int>> edge; set<pair<int, int>> good; vector<bool> in_good; void f(int k, int iteration) { while (!good.empty() && good.begin()->first < k) { int v = good.begin()->second; for (auto j : g[v]) { int i = j.first; int it = j.second; if (iteration <= it) continue; if (in_good[i]) { good.erase({deg[i], i}); deg[i]--; good.insert({deg[i], i}); } } good.erase({deg[v], v}); in_good[v] = false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; deg.resize(n, 0); edge.resize(m); in_good.resize(n, true); ans.resize(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; edge[i] = {a, b}; deg[a]++; deg[b]++; g[a].push_back({b, i}); g[b].push_back({a, i}); } for (int i = 0; i < n; i++) { good.insert({deg[i], i}); } f(k, m); for (int i = m - 1; i >= 0; i--) { ans[i] = good.size(); if (i == 0) break; int v = edge[i].first; int u = edge[i].second; if (!in_good[v] || !in_good[u]) continue; good.erase({deg[v], v}); deg[v]--; good.insert({deg[v], v}); good.erase({deg[u], u}); deg[u]--; good.insert({deg[u], u}); f(k, i); } for (int i = 0; i < m; i++) cout << ans[i] << endl; return 0; }
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> 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; for (int i = 0; i < v[te].size(); i++) { if (vis[v[te][i].second]) continue; auto x = s.lower_bound(make_pair(d[v[te][i].first], v[te][i].first)); if (x != s.end() && x != s.begin()) { s.erase(x); d[v[te][i].first]--; s.insert(make_pair(d[v[te][i].first], v[te][i].first)); } vis[v[te][i].second] = 1; } s.erase(s.begin()); } 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" ] }
IN-CORRECT
java
//package superior; import java.util.*; public class Tester { static TreeSet<Integer> tr[]; static int k,n,m,cnt=0; static Queue<Integer> q; static TreeSet<Integer>glo; public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); n=s.nextInt(); m=s.nextInt(); k=s.nextInt(); Edge e[]=new Edge[m+1]; tr=new TreeSet[n+1]; for(int j=1;j<=n;j++) tr[j]=new TreeSet<Integer>(); glo=new TreeSet<Integer>(); for(int j=1;j<=n;j++) glo.add(j); int x,y; for(int j=1;j<=m;j++) { x=s.nextInt(); y=s.nextInt(); e[j]=new Edge(x,y); tr[x].add(y); tr[y].add(x); } int ans=0; for(int j=1;j<=n;j++) { if(tr[j].size()<k) reduce(j); } /* for(int j=1;j<=n;j++) { if(tr[j].size()>=k) ans++; }*/ //System.out.println(ans); int fri[]=new int[m+1]; int ind; fri[m]=glo.size(); ind=m-1; for(int j=m;j>1;j--) { x=e[j].x; y=e[j].y; //cnt=0; if(tr[x].contains(y) && tr[y].contains(x)) { //System.out.println("hiii"); tr[x].remove(y); tr[y].remove(x); reduce(x); if(glo.contains(y)) reduce(y); fri[ind--]=glo.size(); } else { //System.out.println("hello"); fri[ind--]=glo.size(); } } for(int j=1;j<=m;j++) System.out.println(fri[j]); } public static void reduce(int x) { //cnt++; int get;glo.remove(x); while(tr[x].size()>0) { get=tr[x].first(); tr[get].remove(x); tr[x].remove(get); if( tr[get].size()<k) reduce(get); } } } class Edge { int x; int y; public Edge(int x,int y) { this.x=x; this.y=y; } }
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; const int inf = 1 << 30; const int N = 200100; int n, m, k; int deg[N]; set<pair<int, int> > se[N]; set<int> good[N]; int dndn[N]; int above; bool used[N]; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); if (se[x].count({y, deg[y]})) { se[x].erase({y, deg[y]}); } if (deg[y] + 1 < k) { se[x].insert({y, deg[y] + 1}); } else { good[x].insert(y); dndn[x]++; } if (!used[x] && dndn[x] >= k) { above++; used[x] = 1; for (auto w : good[x]) { int v = w; dndn[v]++; if (!used[v] && dndn[v] >= k) { above++; used[v] = 1; for (auto e : good[v]) { dndn[e]++; if (!used[e] && dndn[e] >= k) { above++; used[e] = 1; } } } } } if (se[y].count({x, deg[x]})) { se[y].erase({x, deg[x]}); } if (deg[x] + 1 < k) { se[y].insert({x, deg[x] + 1}); } else { good[y].insert(x); dndn[y]++; } if (!used[y] && dndn[y] >= k) { above++; used[y] = 1; for (auto w : good[y]) { int v = w; dndn[v]++; if (!used[v] && dndn[v] >= k) { above++; used[v] = 1; for (auto e : good[v]) { dndn[e]++; if (!used[e] && dndn[e] >= k) { above++; used[e] = 1; } } } } } deg[x]++, deg[y]++; if (above >= k + 1) printf("%d\n", above); else printf("0\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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int va[MAXN]; int puedeIr[MAXN]; vector<int> amigos[MAXN]; int amigosQueVan[MAXN]; int amigosConKAmigos[MAXN]; int cantVan; int n, m, k; void dfs(int x) { if (va[x]) return; va[x] = 1; cantVan++; for (int h : amigos[x]) { if (!va[h] && amigosConKAmigos[h] >= k) { dfs(h); } } } void query(int a, int b) { if ((int)(amigos[a]).size() == k - 1) { for (int h : amigos[a]) { amigosConKAmigos[h]++; } } if ((int)(amigos[b]).size() == k - 1) { for (int h : amigos[b]) { amigosConKAmigos[h]++; } } amigos[a].push_back(b); amigos[b].push_back(a); if (va[a] && va[b]) { amigosConKAmigos[a]++; amigosConKAmigos[b]++; } else if (va[a] && !va[b]) { amigosConKAmigos[b]++; if (amigosConKAmigos[b] >= k) { dfs(b); } } else if (!va[a] && va[b]) { amigosConKAmigos[a]++; if (amigosConKAmigos[a] >= k) { dfs(a); } } else if (!va[a] && !va[b]) { if ((int)(amigos[a]).size() >= k) { amigosConKAmigos[b]++; } if ((int)(amigos[b]).size() >= k) { amigosConKAmigos[a]++; } if (amigosConKAmigos[a] >= k) { dfs(a); } if (amigosConKAmigos[b] >= k) { dfs(b); } } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> k; for (int dia = (0); dia < (m); dia++) { int a, b; cin >> a >> b; a--; b--; query(a, b); cout << cantVan << "\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; int x[1000006], y[1000006], deg[1000006], ans[1000006]; set<pair<int, int> > my; vector<int> v[1000006]; bool is[1000006]; map<pair<int, int>, int> my2; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; int i; for (i = 0; i < m; i++) { cin >> x[i] >> y[i]; deg[x[i]]++; deg[y[i]]++; v[x[i]].push_back(y[i]); v[y[i]].push_back(x[i]); } for (i = 1; i <= n; i++) my.insert(make_pair(deg[i], i)); for (i = m - 1; i >= 0; i--) { while (!my.empty()) { set<pair<int, int> >::iterator t = my.begin(); pair<int, int> minm = *t; if (minm.first >= k) break; int x1 = minm.second; is[x1] = 1; for (int j = 0; j < v[x1].size(); j++) { if (my2[make_pair(x1, v[x1][j])]) continue; set<pair<int, int> >::iterator t2 = my.find(make_pair(deg[v[x1][j]], v[x1][j])); if (t2 != my.end()) { pair<int, int> temp = make_pair(t2->first, t2->second); temp.first--; my.erase(my.find(make_pair(deg[v[x1][j]], v[x1][j]))); deg[v[x1][j]]--; my.insert(temp); } } my.erase(my.begin()); } ans[i] = my.size(); if (is[x[i]] || is[y[i]]) continue; my2[make_pair(x[i], y[i])] = 1; my2[make_pair(y[i], x[i])] = 1; set<pair<int, int> >::iterator t = my.find(make_pair(deg[x[i]], x[i])); if (t != my.end()) { pair<int, int> temp = *t; temp.first--; my.erase(my.find(make_pair(deg[x[i]], x[i]))); deg[x[i]]--; my.insert(temp); } t = my.find(make_pair(deg[y[i]], y[i])); if (t != my.end()) { pair<int, int> temp = *t; temp.first--; my.erase(my.find(make_pair(deg[y[i]], y[i]))); deg[y[i]]--; my.insert(temp); } } for (i = 0; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, k, c; scanf("%lld", &(n)); scanf("%lld", &(m)); scanf("%lld", &(k)); vector<long long int> E(n, 0), first(n, 0); vector<long long int> C[n]; long long int tot = 0, a, b; for (int i = 0; i < (m); i++) { scanf("%lld", &(a)); scanf("%lld", &(b)); a--; b--; C[a].push_back(b); C[b].push_back(a); first[a]++; first[b]++; if (first[a] == k) { for (int j = 0; j < (C[a].size()); j++) { E[C[a][j]]++; if (E[C[a][j]] == k) tot++; } } else if (first[a] > k) { E[b]++; if (E[b] == k) tot++; } swap(a, b); if (first[a] == k) { for (int j = 0; j < (C[a].size()); j++) { E[C[a][j]]++; if (E[C[a][j]] == k) tot++; } } else if (first[a] > k) { E[b]++; if (E[b] == k) tot++; } cout << tot << 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Arrays; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.util.TreeSet; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ETrips solver = new ETrips(); solver.solve(1, in, out); out.close(); } static class ETrips { int[][] G; public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int k = in.scanInt(); TreeSet<pair> bstCustom = new TreeSet<>(); int from[] = new int[m]; int to[] = new int[m]; for (int i = 0; i < m; i++) { from[i] = in.scanInt(); to[i] = in.scanInt(); } int[] ans = new int[m]; G = CodeHash.packGraph(from, to, n); int degree[] = new int[n + 1]; for (int i = 1; i <= n; i++) bstCustom.add(new pair(degree[i] = G[i].length, i)); boolean[] is_inside = new boolean[n + 1]; Arrays.fill(is_inside, true); HashSet<Long> set = new HashSet<>(); while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int i : G[bstCustom.first().y]) { if (is_inside[i]) { if (set.contains(i * 1000000l + bstCustom.first().y) || set.contains(bstCustom.first().y * 1000000l + i)) continue; bstCustom.remove(new pair(degree[i], i)); degree[i]--; degree[bstCustom.first().y]--; bstCustom.add(new pair(degree[i], i)); set.add(i * 1000000l + bstCustom.first().y); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[m - 1] = bstCustom.size(); for (int i = m - 1; i >= 1; i--) { if (is_inside[from[i]] && is_inside[to[i]]) { if (degree[from[i]] - 1 < k) { bstCustom.remove(new pair(degree[from[i]], from[i])); for (int j : G[from[i]]) { if (is_inside[j]) { if (set.contains(j * 1000000l + from[i]) || set.contains(from[i] * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[from[i]]--; bstCustom.add(new pair(degree[j], j)); set.add(j * 1000000l + from[i]); } } is_inside[from[i]] = false; } else if (degree[to[i]] - 1 < k) { bstCustom.remove(new pair(degree[to[i]], to[i])); for (int j : G[to[i]]) { if (is_inside[j]) { if (set.contains(j * 1000000l + to[i]) || set.contains(to[i] * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[to[i]]--; bstCustom.add(new pair(degree[j], j)); set.add(j * 1000000l + to[i]); } } is_inside[to[i]] = false; } else { bstCustom.remove(new pair(degree[from[i]], from[i])); degree[from[i]]--; bstCustom.add(new pair(degree[from[i]], from[i])); bstCustom.remove(new pair(degree[to[i]], to[i])); degree[to[i]]--; bstCustom.add(new pair(degree[to[i]], to[i])); set.add(1000000l * from[i] + to[i]); } } while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int j : G[bstCustom.first().y]) { if (is_inside[j]) { if (set.contains(j * 1000000l + bstCustom.first().y) || set.contains(bstCustom.first().y * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; bstCustom.add(new pair(degree[j], j)); set.add(j * 1000000l + bstCustom.first().y); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[i - 1] = bstCustom.size(); } for (int i = 0; i < m; i++) out.println(ans[i]); } class pair implements Comparable<pair> { int x; int y; public int compareTo(pair o) { if (this.x == o.x) return this.y - o.y; return this.x - o.x; } public pair(int x, int y) { this.x = x; this.y = y; } } } static class CodeHash { public static int[][] packGraph(int[] from, int[] to, int n) { int[][] g = new int[n + 1][]; int p[] = new int[n + 1]; for (int i : from) p[i]++; for (int i : to) p[i]++; 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; } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(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 integer = 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') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; vector<vector<pair<int, int> > > g; set<pair<int, int> > good; vector<char> very_good; vector<int> d; int k; void check() { while (!good.empty() && good.begin()->first < k) { int v = good.begin()->second; for (auto &y : g[v]) { int x = y.first; if (very_good[x]) { good.erase(make_pair(d[x], x)); d[x]--; good.insert(make_pair(d[x], x)); } } good.erase(make_pair(d[v], v)); very_good[v] = false; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m >> k; g.resize(n); d.resize(n); vector<pair<int, int> > f(m); for (int i = 0; i < m; ++i) { cin >> f[i].first >> f[i].second; f[i].first--, f[i].second--; g[f[i].first].push_back(make_pair(f[i].second, i)); g[f[i].second].push_back(make_pair(f[i].first, i)); d[f[i].first]++; d[f[i].second]++; } for (int i = 0; i < n; ++i) { good.insert(make_pair(d[i], i)); } very_good.assign(n, true); check(); vector<int> ans(m); for (int i = m - 1; i >= 0; --i) { ans[i] = good.size(); int v = f[i].first, u = f[i].second; if (very_good[v] && very_good[u]) { good.erase(make_pair(d[v], v)); d[v]--; good.insert(make_pair(d[v], v)); good.erase(make_pair(d[u], u)); d[u]--; good.insert(make_pair(d[u], u)); check(); } } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long tak[1000000]; long long fcount[1000000]; set<long long> dbnt[1000000]; long long nbcnt[1000000]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, k; cin >> n >> m >> k; long long ans = 0; set<long long> taken; vector<long long> adj[n]; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; --x, --y; fcount[x]++; fcount[y]++; vector<long long> v; if (fcount[y] >= k) nbcnt[x]++; if (fcount[x] >= k) nbcnt[y]++; if (fcount[x] == k && !tak[x]) { for (auto j : adj[x]) { nbcnt[j]++; long long c = 0; for (auto k1 : adj[j]) c += (nbcnt[k1] >= k && fcount[k1] >= k); if (c >= k && !tak[j] && nbcnt[j] >= k && fcount[j] >= k) v.push_back(j); } } if (fcount[y] == k && !tak[y]) { for (auto j : adj[y]) { nbcnt[j]++; long long c = 0; for (auto k1 : adj[j]) c += (nbcnt[k1] >= k && fcount[k1] >= k); if (c >= k && !tak[j] && nbcnt[j] >= k && fcount[j] >= k) v.push_back(j); } } adj[x].push_back(y); adj[y].push_back(x); if (nbcnt[x] >= k && fcount[x] >= k && !tak[x]) { long long c = 0; for (auto j : adj[x]) c += (nbcnt[j] >= k && fcount[j] >= k); if (c >= k) v.push_back(x); } if (nbcnt[y] >= k && fcount[y] >= k && !tak[y]) { long long c = 0; for (auto j : adj[y]) c += (nbcnt[j] >= k && fcount[j] >= k); if (c >= k) v.push_back(y); } for (auto j : v) tak[j] = 1, taken.insert(j); long long cur = 0; while (cur < v.size()) { long long j = v[cur]; taken.insert(j); for (auto j1 : adj[j]) { if (tak[j1]) continue; long long c = 0; for (auto j2 : adj[j1]) c += (nbcnt[j2] >= k && fcount[j2] >= k); if (c >= k && !tak[j1] && nbcnt[j1] >= k && fcount[j1] >= k) v.push_back(j1), tak[j1] = 1; } cur++; } cout << taken.size() << 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" ] }
IN-CORRECT
java
/*package whatever //do not write package name here */ import java.util.*; public class GFG { public static void main (String[] args) { int n,m,i,j,k,l,t=1,x,y; Scanner sc=new Scanner(System.in); //t=sc.nextInt(); while(t-->0){ n=sc.nextInt(); m=sc.nextInt(); k=sc.nextInt(); int a[]=new int[n]; int gf[]=new int[n]; int fgf[]=new int[n]; ArrayList<ArrayList<Integer>> dos=new ArrayList<ArrayList<Integer>>(); for(i=0;i<n;i++){ dos.add(new ArrayList<Integer>()); } int an=0; while(m-->0){ x=sc.nextInt()-1; y=sc.nextInt()-1; // System.out.println("now"+x+" "+y); a[x]++; a[y]++; dos.get(x).add(y); dos.get(y).add(x); if(a[x]==k){ for(Integer g:dos.get(x)){ gf[(int)g]++; if(gf[(int)g]==k){ for(Integer dd:dos.get((int)g)){ fgf[(int)dd]++; if(fgf[(int)dd]==k){ an++; } } } } } if(a[x]>k){ gf[y]++; if(gf[y]==k){ for(Integer dd:dos.get((int)y)){ fgf[(int)dd]++; if(fgf[(int)dd]==k){ an++; } } } } if(a[y]==k){ for(Integer g:dos.get(y)){ gf[(int)g]++; if(gf[(int)g]==k){ for(Integer dd:dos.get((int)g)){ fgf[(int)dd]++; if(fgf[(int)dd]==k){ an++; } } } } } if(a[y]>k){ gf[x]++; if(gf[x]==k){ for(Integer dd:dos.get((int)x)){ fgf[(int)dd]++; if(fgf[(int)dd]==k){ an++; } } } } if(gf[x]>=k){ fgf[y]++; if(fgf[(int)y]==k){ an++; } } if(gf[y]>=k){ fgf[x]++; if(fgf[(int)x]==k){ an++; } } // System.out.println(Arrays.toString(a)); // System.out.println(Arrays.toString(gf)); // System.out.println(Arrays.toString(fgf)); System.out.println(an); } } } }
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
//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, false); 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; 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), idx); 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" ] }
IN-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]; } public void solve(Scanner sc, PrintWriter pw) throws IOException { int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int[] deg = new int[n]; List<int[]> edges = new ArrayList<>(); List<Integer>[] 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]++; } boolean[] del = new boolean[n]; int dlt = 0; for (int i = 0; i < n; i++) { if (!del[i]) { dlt += dfs(i, k, deg, del, edges, edgesS); } } int[] res = new int[m]; res[m - 1] = n - dlt; for (int i = m - 1; i > 0; i--) { int[] toremove = edges.get(i); edges.remove(i); for (int j = 0; j < 2; j++) { edgesS[toremove[j]].remove(edgesS[toremove[j]].size() - 1); } if (!del[toremove[0]] && !del[toremove[1]]) { deg[toremove[0]]--; deg[toremove[1]]--; } int tr = 0; for (int j = 0; j < 2; j++) { if (!del[toremove[j]] && deg[toremove[j]] < k) { tr += dfs(toremove[j], k, deg, del, edges, edgesS); } } 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[] deg, boolean[] del, List<int[]> edges, List<Integer>[] edgeS) { int cnt = 0; if (deg[u] < k) { del[u] = true; cnt++; for (int idx : edgeS[u]) { int o = get(edges.get(idx), idx); deg[o]--; if (!del[o] && deg[o] < k) { cnt += dfs(o, k, deg, del, edges, edgeS); } } } 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" ] }
IN-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])); } for (int i = 0; i < n; i++) { if(vis[i] == 0) go(i); } 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; } }); for (int i = 0; i < n; i++) { if(vis[i] == YES) set.add(i); } 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)); } } } } ans[0] = set.size(); for (int i = 0; i < m; i++) { pw.println(ans[i]); } pw.flush(); pw.close(); } static void go(int u){ int g = 0; vis[u] = YES; for (int v: adj[u]) { if(vis[v] == YES) g++; else if(vis[v] == 0) { vis[v] = VIS; go(v); if(vis[v] == YES) g++; } } going[u] = g; if(g < k) vis[u] = NO; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T& x) { char c = getchar(); bool f = false; for (x = 0; !isdigit(c); c = getchar()) { if (c == '-') { f = true; } } for (; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } if (f) { x = -x; } } template <typename T> inline bool checkMax(T& a, const T& b) { return a < b ? a = b, true : false; } template <typename T> inline bool checkMin(T& a, const T& b) { return a > b ? a = b, true : false; } const int N = 2e5 + 10; int n, m, k, u[N], v[N], degree[N], ans[N], bl; set<int> G[N]; inline void delete_point(int u) { degree[u] = 0; for (auto v : G[u]) { if (degree[v]) { G[v].erase(G[v].lower_bound(u)); if (--degree[v] < k) { --bl, delete_point(v); } } } G[u].clear(); } int main() { read(n), read(m), read(k), bl = n; for (register int i = 1; i <= m; ++i) { read(u[i]), read(v[i]); G[u[i]].insert(v[i]); G[v[i]].insert(u[i]); ++degree[u[i]], ++degree[v[i]]; } for (register int i = 1; i <= n; ++i) { if (degree[i] < k) { --bl, delete_point(i); } } ans[m] = bl; for (register int i = m; i >= 1; --i) { if (degree[u[i]] && degree[v[i]]) { G[u[i]].erase(G[u[i]].lower_bound(v[i])); if (--degree[u[i]] < k) { --bl, delete_point(u[i]); } if (degree[v[i]]) { G[v[i]].erase(G[v[i]].lower_bound(u[i])); if (--degree[v[i]] < k) { --bl, delete_point(v[i]); } } } ans[i - 1] = bl; } for (register 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, k, sol = 0; cin >> n >> m >> k; if (k * k > 2 * m) { for (int i = 0; i < m; i++) cout << 0 << endl; return 0; } vector<vector<int>> gr(n + 1); vector<vector<int>> co(n + 1, vector<int>(k + 2)); vector<int> hig(n + 1, 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; gr[u].push_back(v); gr[v].push_back(u); queue<tuple<int, int, bool>> q; q.push({v, hig[u], true}); q.push({u, hig[v], true}); while (!q.empty()) { int uu, hi; bool fi; tie(uu, hi, fi) = q.front(); q.pop(); if (hig[uu] > k + 1) continue; if (fi) for (int i = hi - 1; i >= 0 and co[uu][i] < k; i--) co[uu][i]++; else co[uu][hi - 1]++; while (co[uu][hig[uu] - 1] >= k) { hig[uu]++; if (hig[uu] == k + 2) { sol++; break; } for (int uuu : gr[uu]) q.push({uuu, hig[uu], false}); } } cout << sol << 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
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); while (!q.empty()) { int u = q.front(); q.pop(); del[u] = 1; cnt--; for (it = G[u].begin(); it != G[u].end(); it++) { int v = *it; deg[v]--; if (deg[v] < k && !del[v]) q.push(v); } } } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int x[1000006], y[1000006], deg[1000006], ans[1000006]; set<pair<int, int> > my; vector<int> v[1000006]; bool is[1000006]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; int i; for (i = 0; i < n; i++) { cin >> x[i] >> y[i]; deg[x[i]]++; deg[y[i]]++; v[x[i]].push_back(y[i]); v[y[i]].push_back(x[i]); } for (i = 1; i <= n; i++) my.insert(make_pair(deg[i], i)); i = n - 1; for (i = n - 1; i >= 0; i--) { while (!my.empty()) { set<pair<int, int> >::iterator t = my.begin(); pair<int, int> minm = *t; if (minm.first >= k) break; int x1 = minm.second; is[x1] = 1; for (int j = 0; j < v[x1].size(); j++) { set<pair<int, int> >::iterator t2 = my.find(make_pair(deg[v[x1][j]], v[x1][j])); if (t2 != my.end()) { pair<int, int> temp = make_pair(t2->first, t2->second); temp.first--; my.erase(my.find(make_pair(deg[v[x1][j]], v[x1][j]))); deg[v[x1][j]]--; my.insert(temp); } } my.erase(my.begin()); } ans[i] = my.size(); if (is[x[i]] || is[y[i]]) continue; set<pair<int, int> >::iterator t = my.find(make_pair(deg[x[i]], x[i])); if (t != my.end()) { pair<int, int> temp = *t; temp.first--; my.erase(my.find(make_pair(deg[x[i]], x[i]))); deg[x[i]]--; my.insert(temp); } t = my.find(make_pair(deg[y[i]], y[i])); if (t != my.end()) { pair<int, int> temp = *t; temp.first--; my.erase(my.find(make_pair(deg[y[i]], y[i]))); deg[y[i]]--; my.insert(temp); } } for (i = 0; i < n; 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" ] }
IN-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.Arrays; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.util.TreeSet; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ETrips solver = new ETrips(); solver.solve(1, in, out); out.close(); } static class ETrips { int[][] G; public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int k = in.scanInt(); TreeSet<pair> bstCustom = new TreeSet<>(); int from[] = new int[m]; int to[] = new int[m]; for (int i = 0; i < m; i++) { from[i] = in.scanInt(); to[i] = in.scanInt(); } int[] ans = new int[m]; G = CodeHash.packGraph(from, to, n); int degree[] = new int[n + 1]; for (int i = 1; i <= n; i++) bstCustom.add(new pair(degree[i] = G[i].length, i)); boolean[] is_inside = new boolean[n + 1]; Arrays.fill(is_inside, true); while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int i : G[bstCustom.first().y]) { if (is_inside[i]) { bstCustom.remove(new pair(degree[i], i)); degree[i]--; bstCustom.add(new pair(degree[i], i)); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[m - 1] = bstCustom.size(); HashSet<Long> set = new HashSet<>(); for (int i = m - 1; i >= 1; i--) { if (is_inside[from[i]] && is_inside[to[i]]) { if (degree[from[i]] - 1 < k) { bstCustom.remove(new pair(degree[from[i]], from[i])); for (int j : G[from[i]]) { if (is_inside[j]) { if (set.contains(j * 1000000l + from[i]) || set.contains(from[i] * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[from[i]]--; bstCustom.add(new pair(degree[j], j)); } } is_inside[from[i]] = false; } else if (degree[to[i]] - 1 < k) { bstCustom.remove(new pair(degree[to[i]], to[i])); for (int j : G[to[i]]) { if (is_inside[j]) { if (set.contains(j * 1000000l + to[i]) || set.contains(to[i] * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[to[i]]--; bstCustom.add(new pair(degree[j], j)); } } is_inside[to[i]] = false; } else { bstCustom.remove(new pair(degree[from[i]], from[i])); degree[from[i]]--; bstCustom.add(new pair(degree[from[i]], from[i])); bstCustom.remove(new pair(degree[to[i]], to[i])); degree[to[i]]--; bstCustom.add(new pair(degree[to[i]], to[i])); } } set.add(1000000l * from[i] + to[i]); while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int j : G[bstCustom.first().y]) { if (is_inside[j]) { if (set.contains(j * 1000000l + bstCustom.first().x) || set.contains(bstCustom.first().x * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; bstCustom.add(new pair(degree[j], j)); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[i - 1] = bstCustom.size(); } for (int i = 0; i < m; i++) out.println(ans[i]); } class pair implements Comparable<pair> { int x; int y; public int compareTo(pair o) { if (this.x == o.x) return this.y - o.y; return this.x - o.x; } public pair(int x, int y) { this.x = x; this.y = y; } } } static class CodeHash { public static int[][] packGraph(int[] from, int[] to, int n) { int[][] g = new int[n + 1][]; int p[] = new int[n + 1]; for (int i : from) p[i]++; for (int i : to) p[i]++; 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; } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(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 integer = 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') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; vector<int> g[maxn]; int edge[maxn][3]; int res[maxn]; map<pair<int, int>, int> ma; set<pair<int, int> > s; int deg[maxn]; int n, m, k; void add(int x, int y) { g[x].push_back(y); deg[y]++; } void del(int x, int id) { int si = g[x].size(); for (int i = 0; i < si; i++) { int to = g[x][i]; if (ma[make_pair(min(x, to), max(x, to))] >= id) continue; if (s.find(make_pair(deg[to], to)) != s.end() && ma[make_pair(min(x, to), max(x, to))]) { s.erase(make_pair(deg[to], to)); deg[to]--; s.insert(make_pair(deg[to], to)); ma[make_pair(min(x, to), max(x, to))] = 0; } } } void tu(int id) { while (!s.empty() && (*s.begin()).first < k) { del((*s.begin()).second, id); s.erase(s.begin()); } } void put() { printf("si:%d\n", s.size()); set<pair<int, int> >::iterator it = s.begin(), en = s.end(); while (it != en) { printf("%d %d\n", (*it).first, (*it).second); it++; } } void delf(int x) { s.erase(make_pair(deg[x], x)); deg[x]--; s.insert(make_pair(deg[x], x)); } void work() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); x--; y--; edge[i][0] = x; edge[i][1] = y; add(x, y); add(y, x); ma[make_pair(min(x, y), max(x, y))] = i + 1; } for (int i = 0; i < n; i++) { s.insert(make_pair(deg[i], i)); } for (int i = m - 1; i >= 0; i--) { tu(i + 1); res[i] = s.size(); if (s.find(make_pair(deg[edge[i][0]], edge[i][0])) != s.end() && s.find(make_pair(deg[edge[i][1]], edge[i][1])) != s.end()) { delf(edge[i][0]); delf(edge[i][1]); ma[make_pair(min(edge[i][0], edge[i][1]), max(edge[i][0], edge[i][1]))] = 0; } } for (int i = 0; i < m; i++) { printf("%d\n", res[i]); } } int main() { work(); 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; const int Max = 2e5 + 3; vector<int> adj[Max]; set<int> adjNew[Max]; int cnt[Max] = {0}, degree[Max] = {0}; bool visited[Max] = {0}, going[Max] = {0}; int n, m, k; int res = 0; void add(int node) { int numGoing = 0; for (int to : adj[node]) { cnt[to]++; if (adjNew[to].count(node)) { if (cnt[to] >= k) { numGoing++; } } } if (numGoing >= k) { if (!going[node]) { res++; going[node] = true; } for (int to : adjNew[node]) { if (!going[to] && cnt[to] >= k) { res++; going[to] = true; } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; int u, v; vector<pair<int, int> > edges(m); for (int i = 0; i < m; ++i) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); degree[u]++; degree[v]++; edges[i] = {u, v}; } queue<int> q; for (int i = 1; i <= n; ++i) { if (degree[i] < k) { visited[i] = true; q.push(i); } } while (!q.empty()) { int node = q.front(); q.pop(); for (int to : adj[node]) { degree[to]--; } for (int to : adj[node]) { if (!visited[to] && degree[to] < k) { q.push(to); visited[to] = true; } } } for (int i = 1; i <= n; ++i) { adj[i].clear(); degree[i] = 0; } for (int i = 0; i < m; ++i) { u = edges[i].first; v = edges[i].second; if (visited[u]) continue; if (visited[v]) continue; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < m; ++i) { u = edges[i].first; v = edges[i].second; if (visited[u] || visited[v]) { cout << res << '\n'; continue; } degree[u]++; degree[v]++; adjNew[u].insert(v); adjNew[v].insert(u); if (degree[u] == k) { add(u); } if (degree[v] == k) { add(v); } cout << res << '\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" ] }
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 (a[y[i]].find(x[i]) != a[y[i]].end()) { S.erase(make_pair(bac[x[i]], x[i])); bac[x[i]]--; if (bac[x[i]] > 0) S.insert(make_pair(bac[x[i]], x[i])); S.erase(make_pair(bac[y[i]], y[i])); bac[y[i]]--; if (bac[y[i]] > 0) S.insert(make_pair(bac[y[i]], y[i])); a[x[i]].erase(y[i]); a[y[i]].erase(x[i]); } } for (int i = 1; i <= m; i++) cout << res[i] << '\n'; }
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; namespace Header_Template { template <class T> inline void read(T &x) { x = 0; T tmp = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') tmp = -1, c = getchar(); while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar(); x *= tmp; } template <class T> inline void Max(T &x, T y) { if (y > x) x = y; } template <class T> inline void Min(T &x, T y) { if (y < x) x = y; } } // namespace Header_Template using namespace Header_Template; const int N = 2e5 + 10; int n, m, k, ans, t; int cnt[N], vis[N], tag[N]; vector<int> g[N]; vector<pair<int, int> > e; bool Dfs(int x) { vis[x] = 1; for (int y : g[x]) { if (vis[y]) continue; if (!Dfs(y)) --cnt[x]; } return cnt[x] >= k; } void Solve(int x) { tag[x] = t; --ans; for (int y : g[x]) { if (tag[y] == t) continue; --cnt[y]; if (cnt[y] == k - 1) Solve(y); } } int main(void) { read(n), read(m), read(k); for (int i = 1, x, y; i <= m; ++i) { read(x), read(y); g[x].push_back(y); g[y].push_back(x); e.push_back({x, y}); } for (int i = 1; i <= n; ++i) cnt[i] = (int)g[i].size(); for (int i = 1; i <= n; ++i) if (!vis[i]) { vis[i] = 1; for (int x : g[i]) if (!Dfs(x)) --cnt[i]; } ans = 0; for (int i = 1; i <= n; ++i) ans += (cnt[i] >= k); vector<int> Ans; for (t = 1; t <= m; ++t) { Ans.push_back(ans); pair<int, int> u = e.back(); e.pop_back(); int x = u.first, y = u.second; g[x].pop_back(); g[y].pop_back(); if (cnt[x] >= k && cnt[y] >= k) { --cnt[x], --cnt[y]; bool flagx = 0, flagy = 0; if (cnt[x] == k - 1) flagx = 1; if (cnt[y] == k - 1) flagy = 1; if (flagx) Solve(x); if (flagy) Solve(y); } } for (int i = m - 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace std; const int MAXN = 202000; const int MAXM = 202000; const int MAXK = 202000; int N, M, K; vector<int> adj[MAXN]; pair<int, int> edges[MAXM]; int ans[MAXM]; bool inc[MAXN]; int deg[MAXM]; int tans; void init() { queue<int> qu; for (int i = 1; i <= N; i++) { if (deg[i] < K) { tans--; qu.push(i); inc[i] = false; break; } } while (!qu.empty()) { int n = qu.front(); qu.pop(); for (int i = 0; i < adj[n].size(); i++) { if (!inc[adj[n][i]]) continue; deg[adj[n][i]]--; if (deg[adj[n][i]] < K) { tans--; qu.push(adj[n][i]); inc[adj[n][i]] = false; } } } } int main() { scanf("%d%d%d", &N, &M, &K); for (int i = 0; i <= N; i++) { deg[i] = 0; inc[i] = true; } for (int i = 0; i < M; i++) { scanf("%d%d", &edges[i].first, &edges[i].second); adj[edges[i].first].push_back(edges[i].second); adj[edges[i].second].push_back(edges[i].first); deg[edges[i].first]++; deg[edges[i].second]++; } tans = N; init(); for (int i = M - 1; i >= 0; i--) { ans[i] = tans; adj[edges[i].first].pop_back(); adj[edges[i].second].pop_back(); if (inc[edges[i].first] and inc[edges[i].second]) { queue<int> qu; if (deg[edges[i].first]-- == K) { tans--; inc[edges[i].first] = false; qu.push(edges[i].first); } if (deg[edges[i].second]-- == K) { tans--; inc[edges[i].second] = false; qu.push(edges[i].second); } while (!qu.empty()) { int n = qu.front(); qu.pop(); for (int i = 0; i < adj[n].size(); i++) { if (!inc[adj[n][i]]) continue; deg[adj[n][i]]--; if (deg[adj[n][i]] < K) { tans--; qu.push(adj[n][i]); inc[adj[n][i]] = false; } } } } } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, k, r[200005], redd[200005], t, xx[200005], yy[200005], ans[200005]; vector<pair<long long, long long> > v[200005]; void red(long long x, long long time) { if (time == 5) { } redd[x] = 1; t++; for (int i = 0; i < v[x].size(); i++) { if (v[x][i].first > time) break; r[v[x][i].second]--; if (r[v[x][i].second] < k && redd[v[x][i].second] == 0) { red(v[x][i].second, time); } } } int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> xx[i] >> yy[i]; v[xx[i]].push_back(make_pair(i, yy[i])); r[xx[i]]++; v[yy[i]].push_back(make_pair(i, xx[i])); r[yy[i]]++; } for (int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end()); ans[m] = n; for (int i = 1; i <= n; i++) { if (r[i] < k) { t = 0; red(i, m); ans[m] -= t; } } for (int i = m; i >= 2; i--) { for (int j = 1; j <= n; j++) { } if (redd[yy[i]] == 0) r[xx[i]]--; t = 0; if (r[xx[i]] < k && redd[xx[i]] == 0) { red(xx[i], i); ans[i - 1] = ans[i] - t; continue; } if (redd[xx[i]] == 0) r[yy[i]]--; if (r[yy[i]] < k && redd[yy[i]] == 0) { if (xx[i] == 5) { } r[xx[i]]++; red(yy[i], i); } ans[i - 1] = ans[i] - t; } 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" ] }
IN-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.Arrays; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.util.TreeSet; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ETrips solver = new ETrips(); solver.solve(1, in, out); out.close(); } static class ETrips { int[][] G; public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int k = in.scanInt(); TreeSet<pair> bstCustom = new TreeSet<>(); int from[] = new int[m]; int to[] = new int[m]; for (int i = 0; i < m; i++) { from[i] = in.scanInt(); to[i] = in.scanInt(); } int[] ans = new int[m]; G = CodeHash.packGraph(from, to, n); int degree[] = new int[n + 1]; for (int i = 1; i <= n; i++) bstCustom.add(new pair(degree[i] = G[i].length, i)); boolean[] is_inside = new boolean[n + 1]; Arrays.fill(is_inside, true); HashSet<Long> set = new HashSet<>(); while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int i : G[bstCustom.first().y]) { if (is_inside[i]) { bstCustom.remove(new pair(degree[i], i)); degree[i]--; degree[bstCustom.first().y]--; bstCustom.add(new pair(degree[i], i)); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[m - 1] = bstCustom.size(); for (int i = m - 1; i >= 1; i--) { if (is_inside[from[i]] && is_inside[to[i]]) { bstCustom.remove(new pair(degree[from[i]], from[i])); degree[from[i]]--; bstCustom.add(new pair(degree[from[i]], from[i])); bstCustom.remove(new pair(degree[to[i]], to[i])); degree[to[i]]--; bstCustom.add(new pair(degree[to[i]], to[i])); set.add(1000000000l * from[i] + to[i]); } while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int j : G[bstCustom.first().y]) { if (is_inside[j]) { if (set.contains(j * 1000000000l + bstCustom.first().y) || set.contains(bstCustom.first().y * 1000000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[bstCustom.first().y]--; bstCustom.add(new pair(degree[j], j)); set.add(j * 1000000000l + bstCustom.first().y); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[i - 1] = bstCustom.size(); } for (int i = 0; i < m; i++) out.println(ans[i]); } class pair implements Comparable<pair> { int x; int y; public int compareTo(pair o) { if (this.x == o.x) return this.y - o.y; return this.x - o.x; } public pair(int x, int y) { this.x = x; this.y = y; } } } static class CodeHash { public static int[][] packGraph(int[] from, int[] to, int n) { int[][] g = new int[n + 1][]; int p[] = new int[n + 1]; for (int i : from) p[i]++; for (int i : to) p[i]++; 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; } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(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 integer = 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') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 9; const long long int MOD = 1e9 + 696969; const long long INF = 1e18 + 3; const int maxn = 200100; int n, m, k; int A[maxn], B[maxn]; int key[maxn], pref[maxn]; vector<pair<pair<int, int>, int> > e; vector<pair<int, int> > wie; int needed[maxn], odw[maxn]; vector<int> graf[maxn]; void solve(int x, int y, vector<pair<int, int> > &ver, vector<pair<pair<int, int>, int> > &edges) { if (x > y) return; int mid = (x + y) / 2; for (auto u : ver) { graf[u.first].clear(); odw[u.first] = 0; needed[u.first] = u.second; } for (auto u : edges) if (u.second <= mid) { graf[u.first.first].push_back(u.first.second); graf[u.first.second].push_back(u.first.first); needed[u.first.first]--; needed[u.first.second]--; } queue<int> q; for (auto u : ver) if (needed[u.first] > 0) q.push(u.first), odw[u.first] = 1; while (!q.empty()) { int tmp = q.front(); q.pop(); for (auto u : graf[tmp]) { needed[u]++; if (needed[u] > 0 && !odw[u]) { q.push(u); odw[u] = 1; } } } for (auto u : ver) needed[u.first] = u.second; for (auto u : ver) { if (!odw[u.first]) { key[u.first] = min(key[u.first], mid); for (auto sec : graf[u.first]) needed[sec]--; } } vector<pair<int, int> > v1, v2; vector<pair<pair<int, int>, int> > first, second; for (auto u : edges) { if (!odw[u.first.first] && !odw[u.first.second] && u.second <= mid) first.push_back(u); else if ((odw[u.first.first] && odw[u.first.second]) || u.second > mid) second.push_back(u); } for (auto u : ver) { if (!odw[u.first]) v1.push_back(u); else v2.push_back(make_pair(u.first, needed[u.first])); } solve(x, mid - 1, v1, first); solve(mid + 1, y, v2, second); } int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (int i = (1); i <= (n); ++i) key[i] = m + 1; for (int i = (1); i <= (m); ++i) { cin >> A[i] >> B[i]; e.push_back(make_pair(make_pair(A[i], B[i]), i)); } for (int i = (1); i <= (n); ++i) wie.push_back(make_pair(i, k)); solve(1, m, wie, e); for (int i = (1); i <= (n); ++i) pref[key[i]]++; for (int i = (1); i <= (m); ++i) pref[i] += pref[i - 1]; for (int i = (1); i <= (m); ++i) cout << pref[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
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class Main { static int deg[]; static Set<Integer> s; static Set<Integer>[] graph; static int n,k; public static void main(String[] args) { 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]; ArrayList<Edge> edges = new ArrayList<Edge>(); 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<Integer>(); ArrayList<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); } else { break; } } } ArrayList<Integer> ans = new ArrayList<>(); ans.add(s.size()); for(int j=m-1;j>0;j--) { Edge e = edges.get(j); graph[e.u].remove(e.v); graph[e.v].remove(e.u); if(s.contains(e.u) && s.contains(e.v)) { dfs(e.u); if(s.contains(e.v)) { dfs(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) { deg[u]--; if(deg[u]>=k) { return; } s.remove(u); for(int v:(Set<Integer>) graph[u]) { if(s.contains(v)) { dfs(v); } } } } 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> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m >> k; pair<int, int> vv[m]; set<int> adj[n]; int deg[n]; for (int i = 0; i < n; i++) deg[i] = 0; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; vv[i] = make_pair(x, y); deg[x]++; deg[y]++; adj[x].insert(y); adj[y].insert(x); } int ans[m], good = n; bool done[n]; for (int i = 0; i < n; i++) done[i] = 0; set<pair<int, int> > st; priority_queue<pair<int, int> > pq; for (int i = 0; i < n; i++) if (deg[i] >= k) pq.push(make_pair(-deg[i], i)); else { done[i] = true; good--; for (set<int>::iterator it = adj[i].begin(); it != adj[i].end(); it++) { deg[*it]--; adj[*it].erase(i); pq.push(make_pair(-deg[*it], *it)); } deg[i] = 0; adj[i].clear(); } for (int i = m - 1; i >= 0; i--) { ans[i] = good; if (adj[vv[i].first].find(vv[i].second) != adj[vv[i].first].end()) { deg[vv[i].first]--; pq.push(make_pair(-deg[vv[i].first], vv[i].first)); deg[vv[i].second]--; pq.push(make_pair(-deg[vv[i].second], vv[i].second)); adj[vv[i].first].erase(vv[i].second); adj[vv[i].second].erase(vv[i].first); } while (!pq.empty()) { pair<int, int> p = pq.top(); pq.pop(); if (-p.first >= k) { pq.push(p); break; } if (done[p.second]) continue; done[p.second] = true; good--; int ix = p.second; for (set<int>::iterator it = adj[ix].begin(); it != adj[ix].end(); it++) { deg[*it]--; adj[*it].erase(ix); pq.push(make_pair(-deg[*it], *it)); } deg[ix] = 0; adj[ix].clear(); } } for (int i = 0; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; vector<vector<pair<int, int> > > g; set<pair<int, int> > good; vector<char> very_good; vector<int> d; int k; void check(long long int i) { while (!good.empty() && good.begin()->first < k) { int v = good.begin()->second; for (auto &y : g[v]) { int x = y.first; if (y.second >= i) continue; if (very_good[x]) { good.erase(make_pair(d[x], x)); d[x]--; good.insert(make_pair(d[x], x)); } } good.erase(make_pair(d[v], v)); very_good[v] = false; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m >> k; g.resize(n); d.resize(n); vector<pair<int, int> > f(m); for (int i = 0; i < m; ++i) { cin >> f[i].first >> f[i].second; f[i].first--, f[i].second--; g[f[i].first].push_back(make_pair(f[i].second, i)); g[f[i].second].push_back(make_pair(f[i].first, i)); d[f[i].first]++; d[f[i].second]++; } for (int i = 0; i < n; ++i) { good.insert(make_pair(d[i], i)); } very_good.assign(n, true); check(1e18); vector<int> ans(m); for (int i = m - 1; i >= 0; --i) { ans[i] = good.size(); int v = f[i].first, u = f[i].second; if (very_good[v] && very_good[u]) { good.erase(make_pair(d[v], v)); d[v]--; good.insert(make_pair(d[v], v)); good.erase(make_pair(d[u], u)); d[u]--; good.insert(make_pair(d[u], u)); check(i); } cout << good.size() << " "; } 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" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.TreeSet; import java.util.HashSet; import java.util.StringTokenizer; import java.io.PrintWriter; import java.io.OutputStream; import java.util.Iterator; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.io.Writer; import java.io.BufferedReader; import java.util.Comparator; 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(); Graph graph = new Graph(n); IntPair[] degree = new IntPair[n]; for (int i = 0; i < n; i++) { degree[i] = IntPair.newPair(0, i); } TreeSet<IntPair> set = new TreeSet<>(Comparator.reverseOrder()); for (int i = 0; i < n; i++) { set.add(degree[i]); } IntPair temp = IntPair.newPair(k, -1); long threshold = (long) k * (k + 1) / 2; for (int i = 1; i <= m; ++i) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; graph.addEdge(u, v); degree[u].first++; degree[v].first++; set.remove(degree[u]); set.remove(degree[v]); set.add(degree[u]); set.add(degree[v]); if (i < threshold) { out.println(0); continue; } TreeSet<IntPair> subSet = (TreeSet<IntPair>) set.headSet(temp); if (subSet.size() < k + 1) { out.println(0); continue; } HashSet<Integer> potential = new HashSet<>(); for (IntPair node : subSet) { int cnt = 0; for (int e : graph.getEdgesFrom(node.second)) if (degree[e].first >= k) cnt++; if (cnt >= k) potential.add(node.second); } int answer = 0; for (int node : potential) { int cnt = 0; for (int e : graph.getEdgesFrom(node)) if (potential.contains(e)) cnt++; if (cnt >= k) answer++; } out.println(answer); } } } 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 IntPair implements Comparable<IntPair> { public int first; public int second; public IntPair(int first, int second) { this.first = first; this.second = second; } public int compareTo(IntPair o) { if (first != o.first) return Integer.compare(first, o.first); return Integer.compare(second, o.second); } public static IntPair newPair(int first, int second) { return new IntPair(first, second); } public String toString() { return "" + first + " " + second; // return String.format("{" + first + ", " + second + '}'); } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof IntPair)) return false; IntPair intPair = (IntPair) o; return first == intPair.first && second == intPair.second; } public int hashCode() { return (31 * first + second); } } static class Graph { protected int V; protected boolean directed = false; private IntList[] edges; public Graph() { } public Graph(int v) { this(v, false); } @SuppressWarnings("unchecked") public Graph(int v, boolean directed) { this.V = v; this.directed = directed; this.edges = new IntList[V]; for (int i = 0; i < V; i++) { edges[i] = new IntList(4); } } public void addEdge(int u, int v) { // if (edges[u] == null) edges[u] = new IntList(3); edges[u].add(v); if (!directed) { // if (edges[v] == null) edges[v] = new IntList(3); edges[v].add(u); } } public IntList getEdgesFrom(int i) { if (i < 0 || i >= V) throw new IllegalArgumentException("Argument must be in [0, V)."); return this.edges[i]; } } 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; } 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 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(); } } }
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 a[200008]; int b[200008]; int c[200008]; vector<vector<int>> adj(1000008); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m; cin >> n; for (int i = 1; i <= n - 1; ++i) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= n; ++i) { cin >> b[i]; } vector<bool> check(n + 1, false); queue<int> que; que.push(b[1]); check[b[1]] = true; c[b[1]] = 1; int index = 2; int Check = true; while (!que.empty() && Check) { int val = que.front(); que.pop(); int counter = 0; for (int i = 0; i < adj[val].size(); ++i) { if (!check[adj[val][i]]) { check[adj[val][i]] = true; c[adj[val][i]] = 1; que.push(adj[val][i]); ++counter; } } for (int i = 0; i < counter; ++i) { if (c[b[index + i]] == 0) { Check = false; break; } } index += counter; } if (Check) { cout << "Yes" << endl; } else { cout << "No" << 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Arrays; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.util.TreeSet; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ETrips solver = new ETrips(); solver.solve(1, in, out); out.close(); } static class ETrips { int[][] G; public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int k = in.scanInt(); TreeSet<pair> bstCustom = new TreeSet<>(); int from[] = new int[m]; int to[] = new int[m]; for (int i = 0; i < m; i++) { from[i] = in.scanInt(); to[i] = in.scanInt(); } int[] ans = new int[m]; G = CodeHash.packGraph(from, to, n); int degree[] = new int[n + 1]; for (int i = 1; i <= n; i++) bstCustom.add(new pair(degree[i] = G[i].length, i)); boolean[] is_inside = new boolean[n + 1]; Arrays.fill(is_inside, true); HashSet<Long> set = new HashSet<>(); while (bstCustom.size() > 0 && bstCustom.first().x < k) { pair tt = bstCustom.first(); for (int i : G[tt.y]) { if (is_inside[i]) { if (set.contains(i * 1000000000l + bstCustom.first().y) || set.contains(bstCustom.first().y * 1000000000l + i)) continue; bstCustom.remove(new pair(degree[i], i)); degree[i]--; degree[tt.y]--; bstCustom.add(new pair(degree[i], i)); } } is_inside[tt.y] = false; bstCustom.remove(tt); } ans[m - 1] = bstCustom.size(); for (int i = m - 1; i >= 1; i--) { if (is_inside[from[i]] && is_inside[to[i]]) { bstCustom.remove(new pair(degree[from[i]], from[i])); degree[from[i]]--; bstCustom.add(new pair(degree[from[i]], from[i])); bstCustom.remove(new pair(degree[to[i]], to[i])); degree[to[i]]--; bstCustom.add(new pair(degree[to[i]], to[i])); set.add(1000000000l * from[i] + to[i]); } while (bstCustom.size() > 0 && bstCustom.first().x < k) { pair tt = bstCustom.first(); for (int j : G[tt.y]) { if (is_inside[j]) { if (set.contains(j * 1000000000l + bstCustom.first().y) || set.contains(bstCustom.first().y * 1000000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[tt.y]--; bstCustom.add(new pair(degree[j], j)); set.add(j * 1000000000l + bstCustom.first().y); } } is_inside[tt.y] = false; bstCustom.remove(tt); } ans[i - 1] = bstCustom.size(); } for (int i = 0; i < m; i++) out.println(ans[i]); } class pair implements Comparable<pair> { int x; int y; public int compareTo(pair o) { if (this.x == o.x) return this.y - o.y; return this.x - o.x; } public pair(int x, int y) { this.x = x; this.y = y; } } } static class CodeHash { public static int[][] packGraph(int[] from, int[] to, int n) { int[][] g = new int[n + 1][]; int p[] = new int[n + 1]; for (int i : from) p[i]++; for (int i : to) p[i]++; 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; } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(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 integer = 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') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } 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" ] }
IN-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) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { 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; for (int i = 0; i < n; ++i) { myset.insert({adj[i].size(), i}); } vector<long long int> ans(m, 0); for (int i = m - 1; i > 0; --i) { while (!myset.empty() && myset.begin()->first < k) { long long int ind = myset.begin()->second; myset.erase(myset.begin()); for (auto v : adj[ind]) { myset.erase({adj[v].size(), v}); adj[v].erase(ind); myset.insert({adj[v].size(), v}); } } ans[i] = myset.size(); long long int u = edges[i].first, v = edges[i].second; set<pair<long long int, long long int> >::iterator it = myset.find({adj[u].size(), u}); if (it != myset.end()) { myset.erase(it); adj[u].erase(v); myset.insert({adj[u].size(), u}); } it = myset.find({adj[v].size(), v}); if (it != myset.end()) { myset.erase(it); adj[v].erase(u); 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" ] }
IN-CORRECT
java
//package superior; import java.util.*; public class Tester { static TreeSet<Integer> tr[]; static int k,n,m,cnt=0; static Queue<Integer> q; public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); n=s.nextInt(); m=s.nextInt(); k=s.nextInt(); Edge e[]=new Edge[m+1]; tr=new TreeSet[n+1]; for(int j=1;j<=n;j++) tr[j]=new TreeSet<Integer>(); int x,y; for(int j=1;j<=m;j++) { x=s.nextInt(); y=s.nextInt(); e[j]=new Edge(x,y); tr[x].add(y); tr[y].add(x); } int ans=0; for(int j=1;j<=n;j++) { if(tr[j].size()<k) reduce(j); } for(int j=1;j<=n;j++) { if(tr[j].size()>=k) ans++; } //System.out.println(ans); int fri[]=new int[m+1]; int ind; fri[m]=ans; ind=m-1; for(int j=m;j>1;j--) { x=e[j].x; y=e[j].y; cnt=0; if(tr[x].contains(y) && tr[y].contains(x)) { //System.out.println("hiii"); tr[x].remove(y); tr[y].remove(x); if(tr[x].size()<k) { reduce(x); ans-=cnt; if(ans<=0) break; fri[ind--]=ans; //System.out.println(ans+" "+cnt); continue; } if(tr[y].size()<k) { reduce(y); ans-=cnt; if(ans<=0) break; fri[ind--]=ans; //System.out.println(ans+" "+cnt); continue; } fri[ind--]=ans; } else { //System.out.println("hello"); fri[ind--]=ans; } } for(int j=1;j<=m;j++) System.out.println(fri[j]); } public static void reduce(int x) { cnt++;int get; while(tr[x].size()>0) { get=tr[x].first(); tr[get].remove(x); tr[x].remove(get); if(tr[get].size()<k) reduce(get); } } } class Edge { int x; int y; public Edge(int x,int y) { this.x=x; this.y=y; } }
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, dgg[500005]; set<int> gg[500005]; int uu[500005], vv[500005]; int ans[500005]; int dlt[500005]; int crnt; void gna(int v) { dlt[v] = 1; crnt--; for (int to : gg[v]) { gg[to].erase(v); } for (int to : gg[v]) if (--dgg[to] <= k - 1) { gna(to); } dgg[v] = 0; gg[v].clear(); } int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; gg[u].insert(v); gg[v].insert(u); dgg[u]++; dgg[v]++; uu[i] = u; vv[i] = v; } crnt = n; for (int i = 0; i < n; i++) if (dlt[i] == 0 && dgg[i] <= k - 1) { gna(i); } for (int i = m - 1; i >= 0; i--) { ans[i] = crnt; dgg[uu[i]]--; dgg[vv[i]]--; gg[uu[i]].erase(vv[i]); gg[vv[i]].erase(uu[i]); if (dlt[uu[i]] == 0 && dgg[uu[i]] < k) gna(uu[i]); if (dlt[vv[i]] == 0 && dgg[vv[i]] < k) gna(vv[i]); } for (int i = 0; i < m; i++) if (ans[i] <= 0) cout << 0 << " "; else 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef double lf; typedef pair<int,int> ii; #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(ll i=1;i<=n;i++) #define RST(i,n) memset(i,n,sizeof i) #define SZ(a) (int)a.size() #define ALL(a) a.begin(),a.end() #define X first #define Y second #define mkp make_pair #define pb push_back #define eb emplace_back #define pob pop_back #ifdef cold66 #define debug(...) do{\ fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\ _do(__VA_ARGS__);\ }while(0) template<typename T>void _do(T &&_x){cerr<<_x<<endl;} template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<" ,";_do(_t...);} template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";} template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb) { _s<<"{"; for(It _it=_ita;_it!=_itb;_it++) { _s<<(_it==_ita?"":",")<<*_it; } _s<<"}"; return _s; } template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));} template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;} #define IOS() #else #define debug(...) #define pary(...) #define endl '\n' #define IOS() ios_base::sync_with_stdio(0);cin.tie(0); #endif // cold66 //} template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; } template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; } template<class T> using MaxHeap = priority_queue<T>; template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const ll MAXn=2e5+5,MAXlg=__lg(MAXn)+2; const ll MOD=1000000007; const ll INF=0x3f3f3f3f; int n,m,k; int deg[MAXn],vis[MAXn],t; bool ok[MAXn]; vector<int> e[MAXn]; ii dfs(int x){ if( vis[x]==t ) return mkp(0,-INF); int cnt=0,ct=1; vis[x] = t; for( auto i:e[x] ){ if( deg[i]<k ) continue; if( vis[i]==t ) cnt++; else{ ii tmp = dfs(i); cnt+=tmp.X; if( tmp.X ) ct+=tmp.Y; ok[x]|=ok[i]; } } if( cnt>=k ){ ok[x] = true; debug(ct); if( ct>0 ) return mkp(1,ct); else return mkp(0,-INF); } else return mkp(0,-INF); } int main(){ IOS(); cin>>n>>m>>k; int ans=0; REP(T,m){ int a,b; cin>>a>>b; a--,b--; if( ans==n ){ cout<<ans<<endl; continue; } deg[a]++,deg[b]++; e[a].eb(b); e[b].eb(a); if( ok[a]|ok[b] ){ if( ok[a]&ok[b] ); else{ if( !ok[a] ) swap(a,b); if( deg[b]>=k ) ok[b] = true,ans++; } } else{ t++; ii ta = dfs(a); ii tb = dfs(b); debug(ta,tb); if( ta.X ) ans+=ta.Y; if( tb.X ) ans+=tb.Y; } cout<<ans<<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" ] }
IN-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.Arrays; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.util.TreeSet; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ETrips solver = new ETrips(); solver.solve(1, in, out); out.close(); } static class ETrips { int[][] G; public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int k = in.scanInt(); TreeSet<pair> bstCustom = new TreeSet<>(); int from[] = new int[m]; int to[] = new int[m]; for (int i = 0; i < m; i++) { from[i] = in.scanInt(); to[i] = in.scanInt(); } int[] ans = new int[m]; G = CodeHash.packGraph(from, to, n); int degree[] = new int[n + 1]; for (int i = 1; i <= n; i++) bstCustom.add(new pair(degree[i] = G[i].length, i)); boolean[] is_inside = new boolean[n + 1]; Arrays.fill(is_inside, true); HashSet<Long> set = new HashSet<>(); while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int i : G[bstCustom.first().y]) { if (is_inside[i]) { if (set.contains(i * 1000000l + bstCustom.first().y) || set.contains(bstCustom.first().y * 1000000l + i)) continue; bstCustom.remove(new pair(degree[i], i)); degree[i]--; degree[bstCustom.first().y]--; bstCustom.add(new pair(degree[i], i)); set.add(i * 1000000l + bstCustom.first().y); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[m - 1] = bstCustom.size(); for (int i = m - 1; i >= 1; i--) { if (is_inside[from[i]] && is_inside[to[i]]) { if (degree[from[i]] - 1 < k) { bstCustom.remove(new pair(degree[from[i]], from[i])); for (int j : G[from[i]]) { if (is_inside[j]) { if (set.contains(j * 1000000l + from[i]) || set.contains(from[i] * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[from[i]]--; bstCustom.add(new pair(degree[j], j)); set.add(j * 1000000l + from[i]); } } is_inside[from[i]] = false; } else if (degree[to[i]] - 1 < k) { bstCustom.remove(new pair(degree[to[i]], to[i])); for (int j : G[to[i]]) { if (is_inside[j]) { if (set.contains(j * 1000000l + to[i]) || set.contains(to[i] * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; degree[to[i]]--; bstCustom.add(new pair(degree[j], j)); set.add(j * 1000000l + to[i]); } } is_inside[to[i]] = false; } else { bstCustom.remove(new pair(degree[from[i]], from[i])); degree[from[i]]--; bstCustom.add(new pair(degree[from[i]], from[i])); bstCustom.remove(new pair(degree[to[i]], to[i])); degree[to[i]]--; bstCustom.add(new pair(degree[to[i]], to[i])); set.add(1000000l * from[i] + to[i]); } } while (bstCustom.size() > 0 && bstCustom.first().x < k) { for (int j : G[bstCustom.first().y]) { if (is_inside[j]) { if (set.contains(j * 1000000l + bstCustom.first().y) || set.contains(bstCustom.first().y * 1000000l + j)) continue; bstCustom.remove(new pair(degree[j], j)); degree[j]--; bstCustom.add(new pair(degree[j], j)); } } is_inside[bstCustom.first().y] = false; bstCustom.remove(bstCustom.first()); } ans[i - 1] = bstCustom.size(); } for (int i = 0; i < m; i++) out.println(ans[i]); } class pair implements Comparable<pair> { int x; int y; public int compareTo(pair o) { if (this.x == o.x) return this.y - o.y; return this.x - o.x; } public pair(int x, int y) { this.x = x; this.y = y; } } } static class CodeHash { public static int[][] packGraph(int[] from, int[] to, int n) { int[][] g = new int[n + 1][]; int p[] = new int[n + 1]; for (int i : from) p[i]++; for (int i : to) p[i]++; 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; } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(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 integer = 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') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int K; vector<int> F, KF, adj[200000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ((void)0); ((void)0); int N, M, cnt = 0; cin >> N >> M >> K; F.resize(N); KF.resize(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; adj[--a].push_back(--b); adj[b].push_back(a); if (++F[a] == K) { for (auto n : adj[a]) { cnt += (++KF[n] == K); } } if (++F[b] == K) { for (auto n : adj[b]) { cnt += (++KF[n] == K); } } cout << cnt << '\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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; const long long mod = 998244353; const int INF = 1e9; int n, m, K, sum; int u[maxn], v[maxn]; int du[maxn]; vector<int> edge[maxn]; map<int, bool> vis[maxn]; bool is_del[maxn]; void del(int x) { if (is_del[x]) return; is_del[x] = 1; du[x] = 0; sum--; for (int i = 0; i < edge[x].size(); i++) { if (is_del[edge[x][i]]) continue; if (vis[x][edge[x][i]]) { du[edge[x][i]]--; vis[x][edge[x][i]] = vis[edge[x][i]][x] = 0; if (du[edge[x][i]] < K) del(edge[x][i]); } } } int ans[maxn]; int main() { int i, j, x, y; scanf("%d%d%d", &n, &m, &K); for (i = 1; i <= m; i++) { scanf("%d%d", &u[i], &v[i]); edge[u[i]].push_back(v[i]); edge[v[i]].push_back(u[i]); vis[u[i]][v[i]] = vis[v[i]][u[i]] = 1; du[v[i]]++; du[u[i]]++; } sum = n; for (i = 1; i <= n; i++) { if (du[i] < K) del(i); } for (i = m; i >= 1; i--) { ans[i] = sum; if (vis[u[i]][v[i]]) { du[u[i]]--; du[v[i]]--; vis[u[i]][v[i]] = vis[v[i]][u[i]] = 0; if (du[u[i]] < K) del(u[i]); if (du[v[i]] < K) del(v[i]); } } for (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" ] }
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) { 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); } 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); if(s.contains(e.v)) { dfs(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) { deg[u]--; if(deg[u]>=k) { return; } s.remove(u); for(int v:(Set<Integer>) graph[u]) { if(s.contains(v)) { dfs(v); } } } } 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> using namespace std; const long long MOD = 1e9 + 7; const long long N = 3e5 + 10; long long x[N], y[N], deg[N]; vector<long long> vp[N]; long long removed[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k; cin >> n >> m >> k; for (long long i = 1; i <= m; i++) { cin >> x[i] >> y[i]; vp[x[i]].push_back(y[i]); vp[y[i]].push_back(x[i]); deg[x[i]]++; deg[y[i]]++; } set<pair<long long, long long> > s; for (long long i = 1; i <= n; i++) { s.insert({deg[i], i}); } long long ans = n; set<pair<long long, long long> > ae; vector<long long> v; for (long long i = m; i >= 1; i--) { while (s.size() > 0) { auto it = *s.begin(); s.erase(it); if (it.first >= k) break; removed[it.second] = 1; for (auto it1 : vp[it.second]) { if (ae.find({it1, it.second}) == ae.end()) { ae.insert({it1, it.second}); ae.insert({it.second, it1}); if (!removed[it1]) { s.erase({deg[it1], it1}); deg[it1]--; if (deg[it1] > 0) s.insert({deg[it1], it1}); } } } ans--; } if (ae.find({x[i], y[i]}) == ae.end()) { ae.insert({x[i], y[i]}); ae.insert({y[i], x[i]}); s.erase({deg[x[i]], x[i]}); s.erase({deg[y[i]], y[i]}); if (!removed[x[i]]) { deg[x[i]]--; if (deg[x[i]] > 0) { s.insert({deg[x[i]], x[i]}); } } if (!removed[y[i]]) { deg[y[i]]--; if (deg[y[i]] > 0) { s.insert({deg[y[i]], y[i]}); } } } v.push_back(ans); } reverse(v.begin(), v.end()); for (auto it : v) { if (it == 1) cout << 0 << "\n"; else cout << it << "\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; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) { out << p.first << " " << p.second; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T>& v) { for (auto& i : v) { out << i << "\n"; } return out; } vector<vector<int> > graph; vector<int> edges; vector<pair<int, int> > days; vector<bool> vis; vector<bool> correct; int previous = 0; bool DFS(int v, int k) { if ((int)(graph[v].size()) < k) { correct[v] = false; return false; } int works = 0; for (auto& u : graph[v]) { if (!vis[u]) { vis[u] = true; correct[u] = true; if (DFS(u, k)) { ++works; } } else { if (correct[u]) { ++works; } } } if (works < k) { correct[v] = false; } return correct[v]; } void ERASE(int v, int x, int k) { if ((int)(graph[v].size()) > k) { return; } correct[v] = false; --previous; for (auto& u : graph[v]) { if (correct[u] && u != x) { --edges[u]; ERASE(u, -1, k); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, m, k; cin >> n >> m >> k; graph.resize(n); edges.resize(n); days.resize(m); vis.resize(n, 0); correct.resize(n, 0); for (int i = (0); i < (m); ++i) { int a, b; cin >> a >> b; --a; --b; graph[a].push_back(b); graph[b].push_back(a); ++edges[a]; ++edges[b]; days[i] = make_pair(a, b); } for (int i = (0); i < (n); ++i) { if (!vis[i]) { vis[i] = true; correct[i] = true; DFS(i, k); } } for (int i = (0); i < (n); ++i) { if (correct[i]) { ++previous; } } for (int i = (0); i < (n); ++i) { if (correct[i]) { for (auto& u : graph[i]) { if (!correct[u]) { --edges[i]; } } } } stack<int> result; for (int i = (int)(days.size()) - 1; i >= 0; --i) { result.push(previous); if (correct[days[i].first] && correct[days[i].second]) { --edges[days[i].first]; --edges[days[i].second]; ERASE(days[i].first, days[i].second, k); ERASE(days[i].second, days[i].first, k); } } while ((int)(result.size())) { cout << result.top() << "\n"; result.pop(); } 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.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1037e { public static void main(String[] args) throws IOException { int n = rni(), m = ni(), k = ni(), deg[] = new int[n]; List<p> e = new ArrayList<>(); TreeSet<p> set = new TreeSet<>((a, b) -> a.a == b.a ? a.b - b.b : a.a - b.a); Graph g = graph(n); for (int i = 0; i < m; ++i) { p ed = new p(rni() - 1, ni() - 1); g.c(ed.a, ed.b); e.add(ed); ++deg[ed.a]; ++deg[ed.b]; } for (int i = 0; i < n; ++i) { set.add(new p(deg[i], i)); } while (!set.isEmpty() && set.first().a < k) { p p = set.first(); set.remove(p); for (int i : g.get(p.b)) { if (set.remove(new p(deg[i], i))) { set.add(new p(--deg[i], i)); } } } int ans[] = new int[m]; ans[m - 1] = set.size(); for (int i = m - 1; i > 0; --i) { int a = e.get(i).a, b = e.get(i).b; boolean del_a, del_b; g.get(a).remove(b); g.get(b).remove(a); if (del_a = set.remove(new p(deg[a], a))) { set.add(new p(--deg[a], a)); } if (del_b = set.remove(new p(deg[b], b))) { set.add(new p(--deg[b], b)); } if (del_a && del_b) { while (!set.isEmpty() && set.first().a < k) { p p = set.first(); set.remove(p); for (int j : g.get(p.b)) { if (set.remove(new p(deg[j], j))) { set.add(new p(--deg[j], j)); } } } } ans[i - 1] = set.size(); } for (int i = 0; i < m; ++i) { prln(ans[i]); } close(); } static Graph graph(int n) { Graph g = new Graph(); for (int i = 0; i < n; ++i) { g.add(new HashSet<>()); } return g; } static Graph graph(int n, int m) throws IOException { Graph g = graph(n); for (int i = 0; i < m; ++i) { g.c(rni() - 1, ni() - 1); } return g; } static Graph tree(int n) throws IOException { return graph(n, n - 1); } static class Graph extends ArrayList<Set<Integer>> { void cto(int u, int v) { get(u).add(v); } void c(int u, int v) { cto(u, v); cto(v, u); } boolean is_leaf(int i) { return get(i).size() == 1; } } static class p { int a, b; p(int a_, int b_) { a = a_; b = b_; } @Override public String toString() { return "Pair{" + "a = " + a + ", b = " + b + '}'; } public boolean asymmetricEquals(Object o) { p p = (p) o; return a == p.a && b == p.b; } public boolean symmetricEquals(Object o) { p p = (p) o; return a == p.a && b == p.b || a == p.b && b == p.a; } @Override public boolean equals(Object o) { return asymmetricEquals(o); } public int asymmetricHashCode() { return Objects.hash(a, b); } public int symmetricHashCode() { return Objects.hash(a, b) + Objects.hash(b, a); } @Override public int hashCode() { return asymmetricHashCode(); } } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __rand = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int) d;} static int cei(double d) {return (int) ceil(d);} static long fll(double d) {return (long) d;} static long cel(double d) {return (long) ceil(d);} static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);} static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);} static int lcm(int a, int b) {return a * b / gcf(a, b);} static long lcm(long a, long b) {return a * b / gcf(a, b);} static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;} static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} // input static void r() throws IOException {input = new StringTokenizer(rline());} static int ri() throws IOException {return Integer.parseInt(rline());} static long rl() throws IOException {return Long.parseLong(rline());} static double rd() throws IOException {return Double.parseDouble(rline());} static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;} static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;} static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;} static char[] rcha() throws IOException {return rline().toCharArray();} static String rline() throws IOException {return __in.readLine();} static String n() {return input.nextToken();} static int rni() throws IOException {r(); return ni();} static int ni() {return Integer.parseInt(n());} static long rnl() throws IOException {r(); return nl();} static long nl() {return Long.parseLong(n());} static double rnd() throws IOException {r(); return nd();} static double nd() {return Double.parseDouble(n());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {prln("yes");} static void pry() {prln("Yes");} static void prY() {prln("YES");} static void prno() {prln("no");} static void prn() {prln("No");} static void prN() {prln("NO");} static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;}; static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;} static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;} static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();} static void h() {prln("hlfd"); flush();} static void flush() {__out.flush();} static void close() {__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" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.TreeSet; import java.util.HashSet; import java.util.StringTokenizer; import java.io.PrintWriter; import java.io.OutputStream; import java.util.Iterator; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.io.Writer; import java.io.BufferedReader; import java.util.Comparator; 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(); Graph graph = new Graph(n); IntPair[] degree = new IntPair[n]; for (int i = 0; i < n; i++) { degree[i] = IntPair.newPair(0, i); } TreeSet<IntPair> set = new TreeSet<>(Comparator.reverseOrder()); for (int i = 0; i < n; i++) { set.add(degree[i]); } IntPair temp = IntPair.newPair(k, -1); long threshold = (long) k * (k + 1) / 2; for (int i = 1; i <= m; ++i) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; graph.addEdge(u, v); degree[u].first++; degree[v].first++; set.remove(degree[u]); set.remove(degree[v]); set.add(degree[u]); set.add(degree[v]); if (i < threshold) { out.println(0); continue; } TreeSet<IntPair> subSet = (TreeSet<IntPair>) set.headSet(temp); if (subSet.size() < k + 1) { out.println(0); continue; } HashSet<Integer> potential = new HashSet<>(); for (IntPair node : subSet) { int cnt = 0; for (int e : graph.getEdgesFrom(node.second)) if (degree[e].first >= k) cnt++; if (cnt >= k) potential.add(node.second); } HashSet<Integer> potential2 = new HashSet<>(); for (int node : potential) { int cnt = 0; for (int e : graph.getEdgesFrom(node)) if (potential.contains(e)) cnt++; if (cnt >= k) potential2.add(node); } int answer = 0; for (int node : potential) { int cnt = 0; for (int e : graph.getEdgesFrom(node)) if (potential2.contains(e)) cnt++; if (cnt >= k) answer++; } out.println(answer); } } } 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 IntPair implements Comparable<IntPair> { public int first; public int second; public IntPair(int first, int second) { this.first = first; this.second = second; } public int compareTo(IntPair o) { if (first != o.first) return Integer.compare(first, o.first); return Integer.compare(second, o.second); } public static IntPair newPair(int first, int second) { return new IntPair(first, second); } public String toString() { return "" + first + " " + second; // return String.format("{" + first + ", " + second + '}'); } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof IntPair)) return false; IntPair intPair = (IntPair) o; return first == intPair.first && second == intPair.second; } public int hashCode() { return (31 * first + second); } } static class Graph { protected int V; protected boolean directed = false; private IntList[] edges; public Graph() { } public Graph(int v) { this(v, false); } @SuppressWarnings("unchecked") public Graph(int v, boolean directed) { this.V = v; this.directed = directed; this.edges = new IntList[V]; for (int i = 0; i < V; i++) { edges[i] = new IntList(4); } } public void addEdge(int u, int v) { // if (edges[u] == null) edges[u] = new IntList(3); edges[u].add(v); if (!directed) { // if (edges[v] == null) edges[v] = new IntList(3); edges[v].add(u); } } public IntList getEdgesFrom(int i) { if (i < 0 || i >= V) throw new IllegalArgumentException("Argument must be in [0, V)."); return this.edges[i]; } } 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; } 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 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(); } } }
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 GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef double lf; typedef pair<int,int> ii; #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(ll i=1;i<=n;i++) #define RST(i,n) memset(i,n,sizeof i) #define SZ(a) (int)a.size() #define ALL(a) a.begin(),a.end() #define X first #define Y second #define mkp make_pair #define pb push_back #define eb emplace_back #define pob pop_back #ifdef cold66 #define debug(...) do{\ fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\ _do(__VA_ARGS__);\ }while(0) template<typename T>void _do(T &&_x){cerr<<_x<<endl;} template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<" ,";_do(_t...);} template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";} template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb) { _s<<"{"; for(It _it=_ita;_it!=_itb;_it++) { _s<<(_it==_ita?"":",")<<*_it; } _s<<"}"; return _s; } template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));} template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;} #define IOS() #else #define debug(...) #define pary(...) #define endl '\n' #define IOS() ios_base::sync_with_stdio(0);cin.tie(0); #endif // cold66 //} template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; } template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; } template<class T> using MaxHeap = priority_queue<T>; template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const ll MAXn=2e5+5,MAXlg=__lg(MAXn)+2; const ll MOD=1000000007; const ll INF=0x3f3f3f3f; int n,m,k; int deg[MAXn],vis[MAXn],t; bool ok[MAXn]; vector<int> e[MAXn]; ii dfs(int x){ if( vis[x]==t ) return mkp(0,-INF); int cnt=0,ct=1; vis[x] = t; for( auto i:e[x] ){ if( deg[i]<k ) continue; if( vis[i]==t || ok[i] ) cnt++,ok[x]|=ok[i]; else{ ii tmp = dfs(i); cnt+=tmp.X; if( tmp.X ) ct+=tmp.Y; ok[x]|=ok[i]; } } if( x==2 ) debug(cnt,ct); if( cnt>=k ){ ok[x] = true; debug(ct); if( ct>0 ) return mkp(1,ct); else return mkp(0,-INF); } else return mkp(0,-INF); } int main(){ IOS(); cin>>n>>m>>k; int ans=0; REP(T,m){ int a,b; cin>>a>>b; a--,b--; if( ans==n ){ cout<<ans<<endl; continue; } deg[a]++,deg[b]++; e[a].eb(b); e[b].eb(a); if( ok[a]|ok[b] ){ if( ok[a]&ok[b] ); else{ t++; if( !ok[a] ) swap(a,b); if( deg[b]>=k ){ debug(b); ii tmp = dfs(b); debug(tmp); if( tmp.X ) ans+=tmp.Y; } } } else{ t++; ii ta = dfs(a); ii tb = dfs(b); debug(ta,tb); if( ta.X ) ans+=ta.Y; if( tb.X ) ans+=tb.Y; } cout<<ans<<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" ] }
IN-CORRECT
java
import java.util.*; public class p1037e { static final int MAXN = (int) 2e5 + 5; static boolean[] going = new boolean[MAXN]; static int[] ans = new int[MAXN]; static Vector<Set<Integer>> adj = new Vector<>(); static int[][] relation = new int[MAXN][2]; static int total; static int n_people; static int m_days; static int k_num_friends; static { Arrays.fill(going, true); for (int i = 0; i < MAXN; i++) { adj.add(new HashSet<>()); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); solve(sc); } public static void solve(Scanner s) { n_people = s.nextInt(); m_days = s.nextInt(); k_num_friends = s.nextInt(); Stack<Integer> Q = new Stack<>(); for (int i = 0; i < m_days; i++) { int x = s.nextInt(); int y = s.nextInt(); relation[i][0] = x; relation[i][1] = y; adj.get(x).add(y); adj.get(y).add(x); } for (int i = 1; i <= n_people; i++) { if (adj.get(i).size() < k_num_friends) { going[i] = false; Q.push(i); } } total = n_people; takeout(Q); ans[m_days - 1] = total; for (int i = m_days - 2; i > 0; i--) { int u = relation[i + 1][0]; int v = relation[i + 1][1]; if (going[u] && going[v]) { Q = new Stack<>(); adj.get(u).remove(v); adj.get(v).remove(u); if (adj.get(u).size() < k_num_friends) { going[u] = false; Q.push(u); } if (adj.get(v).size() < k_num_friends) { going[v] = false; Q.push(v); } takeout(Q); } ans[i] = total; } for (int i = 0; i < m_days; i++) { System.out.println(ans[i]); } } static void takeout(Stack<Integer> Q) { while (!Q.empty()) { int x = Q.pop(); total--; for (Integer p : adj.get(x)) { adj.get(p).remove(x); if (going[p]) { if (adj.get(p).size() < k_num_friends) { going[p] = false; Q.push(p); } } } } } }
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; const long long MAXN = 1123456; template <typename T> T sqr(T x) { return x * x; } template <typename T> void vout(T s) { cout << s << endl; exit(0); } long long bp(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a *= a; n >>= 1; } return res; } set<long long> s[MAXN]; long long n, m, k; long long kol[MAXN]; long long ans; bool fl[MAXN]; pair<long long, long long> a[MAXN]; queue<long long> q; void add(long long x) { if (fl[x]) return; ans--; fl[x] = 1; q.push(x); } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; ans = n; vector<long long> res(m); for (int i = 0; i < m; i++) { cin >> a[i].first >> a[i].second; kol[a[i].first]++; kol[a[i].second]++; s[a[i].first].insert(a[i].second); s[a[i].second].insert(a[i].first); } for (int i = 1; i <= n; i++) if (!fl[i] && kol[i] < k) { add(i); while (!q.empty()) { long long x = q.front(); q.pop(); for (auto i : s[x]) { kol[i]--; if (!kol[i]) add(i); } } } for (int i = m - 1; i >= 0; i--) { res[i] = ans; if (!fl[a[i].first] && !fl[a[i].second]) { kol[a[i].first]--; kol[a[i].second]--; s[a[i].first].erase(a[i].second); s[a[i].second].erase(a[i].first); if (kol[a[i].first] < k) add(a[i].first); if (kol[a[i].second] < k) add(a[i].second); while (!q.empty()) { long long x = q.front(); q.pop(); for (auto i : s[x]) { kol[i]--; if (kol[i] < k) add(i); } } } } for (auto i : res) cout << 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class Main { static int deg[]; static Set<Integer> s; static Set<Integer>[] graph; static int n,m,k; public static void main(String[] args) { FastReader sc = new FastReader(); n = sc.nextInt(); 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]; ArrayList<Edge> edges = new ArrayList<Edge>(); 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<Integer>(); ArrayList<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); } else { break; } } } ArrayList<Integer> ans = new ArrayList<>(); ans.add(s.size()); for(int j=m-1;j>0;j--) { Edge e = edges.get(j); graph[e.u].remove(e.v); graph[e.v].remove(e.u); if(s.contains(e.u) && s.contains(e.v)) { dfs(e.u); if(s.contains(e.v)) { dfs(e.v); } } ans.add(s.size()); } for(int j=ans.size()-1;j>=0;j--) { System.out.println(ans.get(j)); } } static void dfs(int u) { deg[u]--; if(deg[u]>=k) { return; } s.remove(u); for(int v:(Set<Integer>) graph[u]) { if(s.contains(v)) { dfs(v); } } } } 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> struct EDGE { int to, next; } edge[400010]; int a[200010], b[200010], last[200010], edgecnt = 0, ansa[200010], deg[200010], q[200010], k; inline void addedge (register int from, register int to) { edge[++edgecnt] = (EDGE) {to, last[from]}; last[from] = edgecnt; ++deg[from]; } #define ae(x, y) addedge (x, y), addedge (y, x) bool wiped[200010], vis[200010]; void DFS (register int x) { /**///printf ("\nDFS @ %d\nto : ", x); vis[x] = 1; /**///for (register int e = last[x]; e; e = edge[e].next) printf("%d(%d %d)", edge[e].to, wiped[edge[e].to], vis[edge[e].to]); puts (""); for (register int TO, e = last[x]; e; e = edge[e].next) { TO = edge[e].to; if (TO && !vis[TO]) { if (wiped[TO]) { edge[e].to = edge[e ^ 1].to = 0; --deg[x]; } else { if (!vis[TO]) DFS (TO); if (wiped[TO]) { edge[e].to = edge[e ^ 1].to = 0; --deg[x]; } } } } if (deg[x] < k) wiped[x] = 1; } int main () { register int n, m, aa, bb, ans = 0, qend; scanf ("%d %d %d\n", &n, &m, &k); for (register int i = 0; i < m; ++i) { scanf ("%d %d", &aa, &bb); ae(aa, bb); a[i] = aa; b[i] = bb; } for (register int i = 1; i <= n; ++i) wiped[i] = (deg[i] < k); /**///for (register int i = 1; i <= n; ++i) printf ("%d ", wiped[i]); puts (""); for (register int i = 1; i <= n; ++i) if (!wiped[i] && !vis[i]) DFS (i); for (register int i = 1; i <= n; ++i) if (!wiped[i]) ++ans; ansa[m] = ans; /**///for (register int i = 1; i <= n; ++i) printf ("%d %d, deg %d\n", i, wiped[i], deg[i]); /**///printf ("\n%d\n", ans); for (register int I = m - 1; I; --I) { edge[edgecnt].to = 0; edge[--edgecnt].to = 0; --edgecnt; aa = a[I]; bb = b[I]; if (wiped[aa] | wiped[bb]) { /**///printf ("ans : %d\n", ans); ansa[I] = ans; continue; } qend = 0; if (!wiped[aa] && --deg[aa] < k) { --ans; wiped[aa] = 1; q[++qend] = aa; } if (!wiped[bb] && --deg[bb] < k) { --ans; wiped[bb] = 1; q[++qend] = bb; } /**///printf ("%d(%d, deg %d) %d(%d, deg %d)\n", aa, wiped[aa], deg[aa], bb, wiped[bb], deg[bb]); /**///printf ("qend : %d\n", qend); /**///for (register int i = 1; i <= qend; ++i) printf ("%d ", q[i]); puts (""); for (register int i = 1, tmp = q[1]; tmp; tmp = q[++i]) { /**///printf ("topo @ %d\nto : ", tmp); for (register int TO, e = last[tmp]; e; e = edge[e].next) { TO = edge[e].to; if (TO && !wiped[TO]) { /**///printf("%d ", TO); if (--deg[TO] < k) { wiped[q[++qend] = TO] = 1; --ans; } } edge[e].to = edge[e ^ 1].to = 0; } /**///puts (""); } ansa[I] = ans; /**///printf ("ans : %d\n", ans); } /**///puts ("****************ans begin****************"); for (register int i = 1; i <= m; ++i) printf ("%d\n", ansa[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n, m, k; cin >> n >> m >> k; vector<vector<int> > edges(m, vector<int>(2)); vector<vector<int> > graph(n + 1); vector<int> degrees(n + 1, 0); for (int i = 0; i < m; ++i) { scanf("%d %d", &edges[i][0], &edges[i][1]); degrees[edges[i][0]]++; degrees[edges[i][1]]++; graph[edges[i][0]].push_back(edges[i][1]); graph[edges[i][1]].push_back(edges[i][0]); } map<pair<int, int>, bool> sorted; for (int i = 0; i < n; ++i) sorted.insert(make_pair(make_pair(degrees[i + 1], i + 1), 1)); vector<int> ans(m); for (int i = m - 1; i >= 0; --i) { while ((!sorted.empty()) and sorted.begin()->first.first < k) { int temp = sorted.begin()->first.second; sorted.erase(sorted.begin()); degrees[temp] = 0; for (int i = 0; i < graph[temp].size(); ++i) { auto it = sorted.find(make_pair(degrees[graph[temp][i]], graph[temp][i])); degrees[graph[temp][i]]--; if (it != sorted.end()) { sorted.erase(it); if (degrees[graph[temp][i]] != 0) sorted.insert(make_pair( make_pair(degrees[graph[temp][i]], graph[temp][i]), 1)); } } } ans[i] = sorted.size(); if (degrees[edges[i][0]] != 0 and degrees[edges[i][1]] != 0) { auto it1 = sorted.find(make_pair(degrees[edges[i][0]], edges[i][0])); auto it2 = sorted.find(make_pair(degrees[edges[i][1]], edges[i][1])); degrees[edges[i][0]]--; degrees[edges[i][1]]--; graph[edges[i][0]].pop_back(); graph[edges[i][1]].pop_back(); if (it1 != sorted.end()) { sorted.erase(it1); sorted.insert( make_pair(make_pair(degrees[edges[i][0]], edges[i][0]), 1)); } if (it2 != sorted.end()) { sorted.erase(it2); sorted.insert( make_pair(make_pair(degrees[edges[i][1]], edges[i][1]), 1)); } } } for (int i = 0; i < m; ++i) printf("%d\n", ans[i]); return 0; }
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.util.*; import java.io.*; public class Trips { static Pair[] pairs; static ArrayList<Integer> graph[]; public static void main(String[] args) { FastScanner sc = new FastScanner(); StringBuilder sb = new StringBuilder(); ArrayDeque<Integer> st = new ArrayDeque<>(); ArrayDeque<Pair> edges = new ArrayDeque<>(); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); //Adjacency List graph = new ArrayList[n+1]; pairs = new Pair[n+1]; for (int i = 0; i< n+1; i++) { graph[i] = new ArrayList(); } //int sizes[] = new int[n+1]; for (int i = 0; i < m;i++) { int p1 = sc.nextInt(); int p2 = sc.nextInt(); graph[p1].add(p2); graph[p2].add(p1); edges.push(new Pair(p1, p2)); } for(int i = 0; i < n+1;i++) { pairs[i] = new Pair(i, graph[i].size()); } int prevTrip = checkTrip(k, n); st.push(prevTrip); //for(int i = 1; i < n+1;i++) { // System.out.println("i: " + i + ": " + pairs[i].b); //} //System.out.println(edges.size()); while(!edges.isEmpty()) { Pair edge = edges.pop(); int node1 = edge.a; int node2 = edge.b; //System.out.println("removed edge: " + node1 + " " + node2); if (pairs[node1].b < 1 || pairs[node2].b < 1) { st.push(prevTrip); } else { pairs[node1].b--; pairs[node2].b--; if (pairs[node1].b < k && pairs[node2].b >= k) { pairs[node2].b++; prevTrip = checkTrip(k, n); } else if (pairs[node2].b < k || pairs[node1].b >= k) { pairs[node1].b++; prevTrip = checkTrip(k, n); } else if (pairs[node1].b < k && pairs[node2].b < k) { prevTrip = checkTrip(k, n); } //System.out.println("NEW UPDATES"); //for (int i = 1; i < n + 1; i++) { // System.out.println("i: " + i + ": " + pairs[i].b); //} st.push(prevTrip); } } //System.out.println(st.size()); //last edge removed will always be 0 st.pop(); while(!st.isEmpty()) { sb.append(st.pop()); sb.append('\n'); } System.out.print(sb.toString()); } public static int checkTrip(int k, int n) { boolean removed = false; int sum = 0; for (int i = 1; i < n+1 ;i++) { if (pairs[i].b > 0 && pairs[i].b < k) { removed = true; for (int e: graph[i]) { --pairs[e].b; } pairs[i].b = 0; } else if (pairs[i].b >= k) { sum++; } } if (removed) { return checkTrip(k, n); } return sum; } 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; using ll = long long; int n, m, k, groupsize; vector<int> x, y, friends, ans, px, py; vector<vector<int>> graph; queue<int> q; void check(int v) { if (v == -1 || friends[v] == -1) return; if (friends[v] < k) { q.push(v); friends[v] = -1; --groupsize; } } void trim() { while (!q.empty()) { int u = q.front(); q.pop(); for (int v : graph[u]) { if (v != -1 && friends[v] != -1) { --friends[v]; check(v); } } } } int main() { ios::sync_with_stdio(false); scanf("%d %d %d", &n, &m, &k); x.resize(m), y.resize(m), friends.assign(n + 1, 0); graph.resize(n + 1); px.resize(m), py.resize(m); for (int i = 0; i < m; ++i) scanf("%d %d", &x[i], &y[i]); groupsize = n; for (int i = 0; i < m; ++i) { ++friends[x[i]]; ++friends[y[i]]; graph[x[i]].push_back(y[i]); px[i] = graph[x[i]].size() - 1; graph[y[i]].push_back(x[i]); py[i] = graph[y[i]].size() - 1; } for (int v = 1; v <= n; ++v) { check(v); } ans.push_back(groupsize); for (int i = m - 1; i >= 1; --i) { if (!(x[i] == -1 || y[i] == -1 || friends[x[i]] == -1 || friends[y[i]] == -1)) { --friends[x[i]]; --friends[y[i]]; graph[x[i]][px[i]] = -1; graph[y[i]][py[i]] = -1; check(x[i]); check(y[i]); } trim(); ans.push_back(groupsize); } for (int i = m - 1; i >= 0; --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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> struct EDGE { int to, next; } edge[400010]; int a[200010], b[200010], last[200010], edgecnt = 0, ansa[200010], deg[200010], q[200010], k; inline void addedge (register int from, register int to) { edge[++edgecnt] = (EDGE) {to, last[from]}; last[from] = edgecnt; ++deg[from]; } #define ae(x, y) addedge (x, y), addedge (y, x) bool wiped[200010], vis[200010]; void DFS (register int x) { /**///printf ("\nDFS @ %d\nto : ", x); vis[x] = 1; /**///for (register int e = last[x]; e; e = edge[e].next) printf("%d(%d %d)", edge[e].to, wiped[edge[e].to], vis[edge[e].to]); puts (""); for (register int TO, e = last[x]; e; e = edge[e].next) { TO = edge[e].to; if (TO && !vis[TO]) { if (wiped[TO]) { TO = 0; --deg[x]; } else { if (!vis[TO]) DFS (TO); if (wiped[TO]) { TO = 0; --deg[x]; } } } } if (deg[x] < k) wiped[x] = 1; } int main () { register int n, m, aa, bb, ans = 0, qend; scanf ("%d %d %d\n", &n, &m, &k); for (register int i = 0; i < m; ++i) { scanf ("%d %d", &aa, &bb); ae(aa, bb); a[i] = aa; b[i] = bb; } for (register int i = 1; i <= n; ++i) wiped[i] = (deg[i] < k); /**///for (register int i = 1; i <= n; ++i) printf ("%d ", wiped[i]); puts (""); for (register int i = 1; i <= n; ++i) if (!wiped[i] && !vis[i]) DFS (i); for (register int i = 1; i <= n; ++i) if (!wiped[i]) ++ans; ansa[m] = ans; /**///for (register int i = 1; i <= n; ++i) printf ("%d %d, deg %d\n", i, wiped[i], deg[i]); /**///printf ("\n%d\n", ans); for (register int I = m - 1; I; --I) { edge[edgecnt].to = 0; edge[--edgecnt].to = 0; --edgecnt; aa = a[I]; bb = b[I]; if (wiped[aa] | wiped[bb]) { /**///printf ("ans : %d\n", ans); ansa[I] = ans; continue; } qend = 0; if (!wiped[aa] && --deg[aa] < k) { --ans; wiped[aa] = 1; q[++qend] = aa; } if (!wiped[bb] && --deg[bb] < k) { --ans; wiped[bb] = 1; q[++qend] = bb; } /**///printf ("%d(%d, deg %d) %d(%d, deg %d)\n", aa, wiped[aa], deg[aa], bb, wiped[bb], deg[bb]); /**///printf ("qend : %d\n", qend); /**///for (register int i = 1; i <= qend; ++i) printf ("%d ", q[i]); puts (""); for (register int i = 1, tmp = q[1]; tmp; tmp = q[++i]) { /**///printf ("topo @ %d\nto : ", tmp); for (register int TO, e = last[tmp]; e; e = edge[e].next) { TO = edge[e].to; if (TO && !wiped[TO]) { /**///printf("%d ", TO); if (--deg[TO] < k) { wiped[q[++qend] = TO] = 1; --ans; } } edge[e].to = edge[e ^ 1].to = 0; } /**///puts (""); } ansa[I] = ans; /**///printf ("ans : %d\n", ans); } /**///puts ("****************ans begin****************"); for (register int i = 1; i <= m; ++i) printf ("%d\n", ansa[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" ] }
IN-CORRECT
python3
maxN =200005 G = [None] * maxN s = set() k = [0] * 1 def delete(v): if len(G[v]) < k[0] and (v in s): s.remove(v) for u in G[v]: G[u].discard(v) delete(u) def main(): n,m,k[0] = map(int,input().split()) edges = [None] * (m + 1) ans = [0] * (m + 1) for i in range(m): u,v = map(int,input().split()) if G[u] is None: G[u] = set() if G[v] is None: G[v] = set() G[u].add(v) G[v].add(u) edges[i+1] = (u,v) for i in range(1,n+1): s.add(i) for i in range(1,n+1): delete(i) i = m while i > 0: ans[i] = len(s) e = edges[i] G[e[0]].discard(e[1]) G[e[1]].discard(e[0]) delete(e[0]) delete(e[1]) i-=1 for i in range(1,m+1): print(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
python2
'''input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 ''' n,m,k=[int(x) for x in raw_input().split()] F=[0]*n E=[0]*n C=[set() for x in range(n)] tot=0 M=[] for i in range(m): a,b=[int(x)-1 for x in raw_input().split()] C[a].add(b) C[b].add(a) F[a]+=1 F[b]+=1 M.append((a,b)) M=M[::-1] for i in range(n): if F[i]>=k: for j in C[i]: E[j]+=1 for i in E: if i>=k: tot+=1 #print E,F ans=[] #print F for a,b in M: ans.append(tot) F[a]-=1 F[b]-=1 if F[a]==k-1: for j in C[a]: E[j]-=1 if E[j]==k-1: tot-=1 E[a]=-3 tot-=1 F[a]=-3 a,b=b,a if F[a]==k-1: E[a]=-3 F[a]=-3 tot-=1 for j in C[a]: E[j]-=1 if E[j]==k-1: tot-=1 C[a].discard(b) C[b].discard(a) #ans.append(tot) #print F,E ans=ans[::-1] for i in ans: print 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
// amigos y no más // dile a la jardinera que traigo flores // += O(logn) ; + = O(n) //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #define fastio ios_base::sync_with_stdio(0);cin.tie(0) #define MOD 998244353LL #define MOD1 1000000009LL #define LIM 262150 #define FORN(i,j,n) for(int i=j; i<n;i++) #define FOR(i,n) FORN(i,0,n) #define ones(x) __builtin_popcountll(x) #define MAXIMO 20000000 using namespace std; using namespace __gnu_pbds; typedef long long ll ; typedef unsigned long long ull ; typedef tree<int,int,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; int n,m,k; set<int> G[200005]; vector< pair<int,int> > v; map <int,int> ma; int ans[200005]={0}; queue<int> node_to_remove; void step1(int x){ auto it= G[x].begin(); while(it!=G[x].end()){ bool flag = false; int y = *it; if( ma.count(y) ) { if(ma[y] <= k) { ma.erase(y); node_to_remove.push(y); } else {ma[y]--; G[y].erase(x);} } it++; } } void remove_node(int x){ step1(x); //step2 while(!node_to_remove.empty()){ step1(node_to_remove.front()); node_to_remove.pop(); } } void go(){ cin>>n>>m>>k; for(int i=0; i<m; i++){ int x,y; cin>>x>>y; G[x].insert(y); G[y].insert(x); v.push_back({x,y}); } for(int i=1;i<=n;i++) { if(G[i].size() >= k) ma[i] = G[i].size(); } for(int i=1;i<=n;i++) { if(!ma.count(i)){ remove_node(i); } } ans[m] = ma.size(); for(int i=m-1; i>=0;i--){ int & x = v[i].first; int & y = v[i].second; if(ma.count(x) && ma.count(y) ){ /*cout<<x<<" "<<y<<"\n"; for(auto it: ma){ cout<<it.first<<"->"<<it.second<<" "; } cout<<"\n";*/ if(ma[x] <= k && ma[y] <= k) { ma.erase(y); ma.erase(x); remove_node(x); remove_node(y); } else if(ma[x] <= k){ ma.erase(x); remove_node(x); if(ma.count(y)) ma[y] = G[y].size(); } else if(ma[y] <= k){ ma.erase(y);remove_node(y); if(ma.count(x)) ma[x] = G[x].size(); } else{ ma[x]--; ma[y]--; G[x].erase(y); G[y].erase(x); } } ans[i] = ma.size(); } for(int i=1; i<=m;i++){ cout<<ans[i]<<"\n"; } } int main() { fastio; go(); 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; set<pair<int, int> > s; int d[200010], k, ans[200010]; set<int> e[200010]; pair<int, int> q[200010]; void update() { while (!s.empty()) { auto it = s.begin(); int x = it->second; if (d[x] >= k) return; s.erase(it); for (auto it1 = e[x].begin(); it1 != e[x].end(); it1++) { int y = *it1; e[y].erase(x); if (d[y] >= k) { s.erase({d[y], y}); d[y]--; s.insert({d[y], y}); } } e[x].clear(); } } int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m >> k; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; q[i] = {x, y}; e[x].insert(y); e[y].insert(x); d[x]++; d[y]++; } for (int i = 1; i <= n; i++) s.insert({d[i], i}); update(); for (int i = m - 1; i > 0; i--) { ans[i] = s.size(); int x = q[i].first, y = q[i].second; if (d[x] >= k && d[y] >= k) { e[x].erase(y); e[y].erase(x); s.erase({d[x], x}); d[x]--; s.insert({d[x], x}); s.erase({d[y], y}); d[y]--; s.insert({d[y], y}); update(); } } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, m, k; bool vis[N]; queue<int> Q; set<int> G[N]; vector<int> E[N]; int a[N], b[N], ans[N], sum; void solve() { while (!Q.empty()) { int u = Q.front(); Q.pop(); for (auto v : G[u]) { G[v].erase(u); if (G[v].size() < k) { sum--; vis[v] = 1; Q.push(v); } } } } int main() { scanf("%d%d%d", &n, &m, &k); sum = n; for (int i = 1; i <= m; i++) { scanf("%d%d", &a[i], &b[i]); G[a[i]].insert(b[i]); G[b[i]].insert(a[i]); } for (int i = 1; i <= n; i++) { if (G[i].size() < k) { Q.push(i); vis[i] = 1; sum--; } } solve(); ans[m] = sum; for (int i = m - 1; i >= 1; i--) { G[a[i]].erase(b[i]); G[b[i]].erase(a[i]); solve(); ans[i] = 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * */ public class E { static Set<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 Set[n]; degree = new int[n]; for (int i = 0; i < n; i++) { g[i] = new HashSet<>(); } 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; g[a].add(new Pair(b,i)); g[b].add(new Pair(a,i)); degree[a]++;degree[b]++; s.add(a);s.add(b); f[i][0] = a; f[i][1] = b; } for (int i = 0; i < n; i++) { if(g[i].isEmpty()){ s.remove(i); } else if(g[i].size() < k) { q.add(i); s.remove(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(v); } 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); if (degree[b] < k) q.add(b); while (!q.isEmpty()){ int v = q.poll(); for (Pair u : g[v]) { if(s.contains(u.a) && u.b < i) { degree[u.a]--; if (degree[u.a] < k) { q.add(u.a); } } } s.remove(v); } } } 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; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a == pair.a; } @Override public int hashCode() { return Objects.hash(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; const int maxn = 2e5 + 5; pair<int, int> ed[maxn]; vector<int> g[maxn]; int n, m, k, fa[maxn], d[maxn]; int cl[maxn], fcl[maxn], ans[maxn]; int vs[maxn], sz; bool v[maxn], w[maxn], us[maxn]; void dfs(int x, int p) { fa[x] = p; d[x] = p == -1 ? 0 : d[p] + 1; v[x] = true; for (int u : g[x]) { if (d[u] < d[x]) w[d[x] - d[u]] = true; } int i = 0; while (w[i + 1]) ++i; cl[x] = i + 1; if (~p) cl[x] = min(cl[x], cl[p] + 1); for (int u : g[x]) { if (d[u] < d[x]) w[d[x] - d[u]] = false; } fcl[x] = cl[x]; if (~p && cl[p] == cl[x] - 1) { fcl[p] = max(fcl[p], fcl[x]); } for (int u : g[x]) { if (v[u]) continue; dfs(u, x); } vs[sz++] = x; } int check(int t) { sz = 0; for (int i = 0; i < n; ++i) g[i].clear(); for (int i = 1; i <= t; ++i) { int x = ed[i].first, y = ed[i].second; g[x].push_back(y); g[y].push_back(x); } for (int i = 0; i < n; ++i) v[i] = false; for (int i = 0; i < n; ++i) { if (v[i]) continue; dfs(i, -1); } for (int i = 0; i < n; ++i) us[i] = false; int ans = 0; for (int i = 0; i < n; ++i) { int x = vs[i]; if (fa[x] == -1) continue; if (cl[x] <= k) continue; if (us[x]) continue; if (cl[x] == fcl[x]) { for (int j = 0; j < fcl[vs[i]]; ++j) { if (!us[x]) ++ans; us[x] = true; x = fa[x]; } } } return ans; } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; ++i) { int x, y; scanf("%d %d", &x, &y); --x, --y; ed[i] = make_pair(x, y); } int last = 0; for (int i = 0; i <= n; ++i) { int d = 20, t = last; while (d--) if (t + (1 << d) <= m) { if (check(t + (1 << d)) == i) t += (1 << d); } for (int j = last + 1; j <= t; ++j) ans[j] = i; last = t; } 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" ] }
IN-CORRECT
java
import java.lang.reflect.Array; import java.util.*; import java.io.*; public class Trips { static Pair[] pairs; static ArrayList<Integer> graph[]; public static void main(String[] args) { FastScanner sc = new FastScanner(); StringBuilder sb = new StringBuilder(); ArrayDeque<Integer> st = new ArrayDeque<>(); ArrayDeque<Pair> edges = new ArrayDeque<>(); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); //Adjacency List graph = new ArrayList[n+1]; pairs = new Pair[n+1]; for (int i = 0; i< n+1; i++) { graph[i] = new ArrayList(); } //int sizes[] = new int[n+1]; for (int i = 0; i < m;i++) { int p1 = sc.nextInt(); int p2 = sc.nextInt(); graph[p1].add(p2); graph[p2].add(p1); edges.push(new Pair(p1, p2)); } for(int i = 0; i < n+1;i++) { pairs[i] = new Pair(i, graph[i].size()); } int prevTrip = checkTrip(k, n); st.push(prevTrip); //for(int i = 1; i < n+1;i++) { // System.out.println("i: " + i + ": " + pairs[i].b); //} //System.out.println(edges.size()); while(!edges.isEmpty()) { Pair edge = edges.pop(); int node1 = edge.a; int node2 = edge.b; //System.out.println("removed edge: " + node1 + " " + node2); if (pairs[node1].b < 1 || pairs[node2].b < 1) { st.push(prevTrip); } else { pairs[node1].b--; pairs[node2].b--; if (pairs[node1].b < k && pairs[node2].b >= k) { pairs[node2].b++; prevTrip = checkTrip(k, n); } if (pairs[node2].b < k || pairs[node1].b >= k) { pairs[node1].b++; prevTrip = checkTrip(k, n); } if (pairs[node1].b < k && pairs[node2].b < k) { prevTrip = checkTrip(k, n); } //System.out.println("NEW UPDATES"); //for (int i = 1; i < n + 1; i++) { // System.out.println("i: " + i + ": " + pairs[i].b); //} st.push(prevTrip); } } //System.out.println(st.size()); //last edge removed will always be 0 st.pop(); while(!st.isEmpty()) { sb.append(st.pop()); sb.append('\n'); } System.out.print(sb.toString()); } public static int checkTrip(int k, int n) { boolean removed = false; int sum = 0; for (int i = 1; i < n+1 ;i++) { if (pairs[i].b > 0 && pairs[i].b < k) { removed = true; for (int e: graph[i]) { --pairs[e].b; } pairs[i].b = 0; } else if (pairs[i].b >= k) { sum++; } } if (removed) { return checkTrip(k, n); } return sum; } 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; const long long N = 200005; set<long long> st[N]; long long deg[N]; set<pair<long long, long long> > freq; vector<pair<long long, long long> > v; vector<long long> ans; int main() { long long n, m, k; cin >> n >> m >> k; for (long long i = 0; i < m; ++i) { long long x, y; scanf("%I64d%I64d", &x, &y); st[x].insert(y); st[y].insert(x); ++deg[x], ++deg[y]; v.push_back({x, y}); } for (long long i = 1; i <= n; ++i) { freq.insert({deg[i], i}); } long long c = n; for (long long i = m - 1; i >= 0; --i) { while (freq.size()) { auto it = freq.begin(); if (it->first >= k) break; --c; vector<long long> temp; for (auto x : st[it->second]) { temp.push_back(x); } freq.erase(freq.begin()); for (auto x1 : temp) { st[x1].erase(it->second); freq.erase({deg[x1], x1}); --deg[x1]; freq.insert({deg[x1], x1}); } st[it->second].clear(); } ans.push_back(c); if (st[v[i].first].find(v[i].second) != st[v[i].first].end()) { st[v[i].first].erase(v[i].second); freq.erase({deg[v[i].first], v[i].first}); --deg[v[i].first]; freq.insert({deg[v[i].first], v[i].first}); } if (st[v[i].second].find(v[i].first) != st[v[i].second].end()) { st[v[i].second].erase(v[i].first); freq.erase({deg[v[i].second], v[i].second}); --deg[v[i].second]; freq.insert({deg[v[i].second], v[i].second}); } } reverse(ans.begin(), ans.end()); for (auto x : ans) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, s; map<pair<int, int>, bool> del; int x[200010], y[200010]; vector<int> g[200010]; int ind[200010]; queue<int> q; bool in[200010]; int ans[200010]; int res; void remove(int k) { q.push(k); while (!q.empty()) { int k = q.front(); q.pop(); --res; in[k] = false; for (vector<int>::iterator it = g[k].begin(); it != g[k].end(); ++it) { if (del[make_pair(k, *it)]) continue; if (in[*it]) --ind[*it], --ind[k]; if (in[*it] && ind[*it] < s) q.push(*it); } } return; } int main() { scanf("%d%d%d", &n, &m, &s); for (int i = 1; i <= m; ++i) { scanf("%d%d", &x[i], &y[i]); g[x[i]].push_back(y[i]); g[y[i]].push_back(x[i]); ++ind[x[i]]; ++ind[y[i]]; } res = n; for (int i = 1; i <= n; ++i) in[i] = true; for (int i = 1; i <= n; ++i) { if (ind[i] < s) q.push(i); } while (!q.empty()) { int k = q.front(); q.pop(); --res; in[k] = false; for (vector<int>::iterator it = g[k].begin(); it != g[k].end(); ++it) { if (in[*it]) --ind[*it]; if (in[*it] && ind[*it] < s) q.push(*it); } } ans[m] = res; for (int i = m; i >= 1; --i) { del[make_pair(x[i], y[i])] = del[make_pair(y[i], x[i])] = true; if (in[x[i]] && in[y[i]]) { --ind[x[i]]; --ind[y[i]]; } if (in[x[i]] && ind[x[i]] < s) remove(x[i]); if (in[y[i]] && ind[y[i]] < s) remove(y[i]); 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int MAXN = 200010; int n, m, k; vector<int> adj[MAXN]; set<int> nadj[MAXN]; int ans[MAXN]; int ins[MAXN]; int deg[MAXN]; int out = 0; pii ent[MAXN]; void dfs(int v) { if (ins[v]) return; out++; for (auto& it : nadj[v]) if (ins[it]) { if (deg[it] == k) { ins[it] = 0; --deg[it]; dfs(it); } else --deg[it]; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; adj[a].push_back(b); adj[b].push_back(a); ent[i] = {a, b}; } for (int i = 0; i < n; ++i) if (adj[i].size() >= k) { deg[i] = k; } for (int i = 0; i < n; ++i) { for (auto& it : adj[i]) if (deg[i] >= k && deg[it] >= k) nadj[i].insert(it); ins[i] = nadj[i].size() >= k; ans[m - 1] += (nadj[i].size() >= k); deg[i] = nadj[i].size(); } for (int i = m - 1; i >= 0; --i) { out = 0; int v = ent[i].first, u = ent[i].second; if (!ins[v] || !ins[u]) { if (i) ans[i - 1] = ans[i]; continue; } nadj[v].erase(u); nadj[u].erase(v); --deg[v]; --deg[u]; if (deg[v] < k) ins[v] = 0; if (deg[u] < k) ins[u] = 0; dfs(v); dfs(u); if (i) ans[i - 1] = ans[i] - out; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<vector<int>> gr; map<pair<int, int>, bool> was; vector<int> cnt; vector<bool> group; void relax(set<pair<int, int>> &data, int &m, int &n, int &k) { while (!data.empty() && data.begin()->first < k) { int v = data.begin()->second; group[v] = 0; cnt[v] = 0; for (int to : gr[v]) { if (was[{v, to}]) { continue; } if (!group[to]) { continue; } data.erase({cnt[to], to}); cnt[to]--; data.insert({cnt[to], to}); was[{to, v}] = 1; was[{v, to}] = 1; } data.erase(data.begin()); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> ask; gr.resize(n); cnt.resize(n); group.resize(n, 1); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a, --b; gr[a].push_back(b); gr[b].push_back(a); cnt[a]++; cnt[b]++; ask.push_back({a, b}); } set<pair<int, int>> data; for (int i = 0; i < n; ++i) { data.insert({cnt[i], i}); } vector<int> ans; for (int i = m - 1; i >= 0; --i) { relax(data, m, n, k); ans.push_back(data.size()); int a = ask[i].first, b = ask[i].second; if (!was[{a, b}]) { data.erase({cnt[a], a}); --cnt[a]; data.erase({cnt[b], b}); --cnt[b]; data.insert({cnt[a], a}); data.insert({cnt[b], b}); } was[{a, b}] = 1; was[{b, a}] = 1; } for (int i = m - 1; i >= 0; --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" ] }
IN-CORRECT
python3
maxN =200005 G = [None] * maxN s = set() k = 0 def delete(v): global k if G[v] is not None: if len(G[v]) < k and (v in s): #Chequeando que deg(v) no es mayor o igual que k s.remove(v) #Se elimina del conjunto porque no cumple las condiciones i = 0 while i < len(G[v]): u = G[v][i] if v in G[u]: G[u].remove(v) delete(u) i+= 1 #for u in G[v]: # G[u].discard(v) #Se actualiza la lista de adyacencia # delete(u) #se repite el proceso para sus vertices adyacentes def main(): global k n,m,k = map(int,input().split()) edges = [None] * (m + 1) ans = [0] * m for i in range(m): #Añadiendo a G las m aristas u,v = map(int,input().split()) if G[u] is None: G[u] = [] if G[v] is None: G[v] = [] if v not in G[u]: G[u].append(v) if u not in G[v]: G[v].append(u) edges[i+1] = (u,v) for i in range(1,n+1): #Insertando los vértices al conjunto X s.add(i) for i in range(1,n+1): #Realizando el análisis para el momento en que G tenga todas las aristas delete(i) i = m while i > 0: #Removiendo aristas y analizando ans[i-1] = len(s) e = edges[i] if e[1] in G[e[0]]: G[e[0]].remove(e[1]) if e[0] in G[e[1]]: G[e[1]].remove(e[0]) delete(e[0]) delete(e[1]) i-=1 for i in ans: print(i) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; vector<int> used; vector<int> selected; vector<vector<int>> g; void dfs(int cur) { if (selected[cur]) { return; } used[cur] = 1; selected[cur] = 1; int cnt = 0; for (auto t : g[cur]) { if (!used[t]) { dfs(t); } if (selected[t]) { cnt++; } } if (cnt < k) { selected[cur] = 0; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k; g.resize(n); selected.assign(n, 0); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); used.assign(n, 0); if (!selected[u]) { dfs(u); } else if (!selected[v]) { dfs(v); } int cur = 0; for (int i = 0; i < n; i++) { cur += selected[i]; } cout << cur << '\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> #pragma warning(disable : 4996) #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC optimize("-ffloat-store") using namespace std; const int MN = 5e5; int n, m, k, deg[MN]; set<int> g[MN]; int uu[MN], vv[MN]; int ans[MN]; int del[MN]; int cur; void slv(int v) { del[v] = 1; cur--; for (int to : g[v]) g[to].erase(v); for (int to : g[v]) if (--deg[to] < k) slv(to); deg[v] = 0; g[v].clear(); } 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) { slv(i); } for (int i = m - 1; i >= 0; i--) { ans[i] = cur; if (g[uu[i]].find(vv[i]) != g[uu[i]].end()) { deg[uu[i]]--; deg[vv[i]]--; g[uu[i]].erase(vv[i]); g[vv[i]].erase(uu[i]); if (del[uu[i]] == 0 && deg[uu[i]] < k) slv(uu[i]); if (del[vv[i]] == 0 && deg[vv[i]] < k) slv(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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int deg[N], incoming[N]; vector<int> g[N]; set<int> G[N]; bool vis[N], mark[N]; int x[N], y[N]; int print[N]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, k; cin >> n >> m >> k; for (int i = 1; i < m + 1; i++) { cin >> x[i] >> y[i]; g[x[i]].push_back(y[i]); g[y[i]].push_back(x[i]); deg[x[i]]++; deg[y[i]]++; } queue<int> q; for (int i = 1; i < n + 1; i++) if (deg[i] < k) q.push(i); while ((int)(q).size()) { int v = q.front(); q.pop(); vis[v] = true; for (int u : g[v]) { --deg[u]; if (deg[u] < k && !vis[u]) { q.push(u); vis[u] = true; } } } for (int i = 1; i < m + 1; i++) { G[x[i]].insert(y[i]); G[y[i]].insert(x[i]); } memset(vis, false, sizeof(vis)); int r = 0; for (int i = 1; i < n + 1; i++) if (deg[i] >= k) ++r; while ((int)(q).size()) q.pop(); print[m] = r; for (int i = m + 1 - 1; i >= 2; i--) { int u = x[i], v = y[i]; if (deg[u] < k || deg[v] < k) { G[u].erase(v); G[v].erase(u); --deg[u], --deg[v]; print[i - 1] = r; } else { --deg[u]; --deg[v]; G[u].erase(v); G[v].erase(u); if (!vis[u] && deg[u] < k) vis[u] = true, --r, q.push(u); if (!vis[v] && deg[v] < k) vis[v] = true, --r, q.push(v); while ((int)(q).size()) { int v = q.front(); q.pop(); vis[v] = true; for (int u : G[v]) { --deg[u]; if (!vis[u] && deg[u] < k) { vis[u] = true; --r; q.push(u); } } G[v].clear(); } print[i - 1] = r; } } for (int i = 1; i < m + 1; i++) cout << print[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
cpp
#include <bits/stdc++.h> template <class C, class E> inline bool contains(const C& container, const E& element) { return container.find(element) != container.end(); } template <class T> inline void checkmin(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T& a, T b) { if (b > a) a = b; } using namespace std; vector<pair<int, int> > fr; vector<set<int> > gr; int n, m, k; vector<bool> inTrip; vector<int> deg; int inTripCount; void Remove(int a, int b) { gr[a].erase(b); gr[b].erase(a); --deg[a]; --deg[b]; } vector<bool> visited; void RemAll(queue<int>& toRemove) { if (toRemove.empty()) return; while (!toRemove.empty()) { int iRem = toRemove.front(); inTrip[iRem] = false; --inTripCount; toRemove.pop(); vector<int> rem2; for (int br : gr[iRem]) { --deg[br]; gr[br].erase(iRem); if (deg[br] < k && !visited[br]) { rem2.push_back(br); visited[br] = true; toRemove.push(br); } } gr[iRem].clear(); deg[iRem] = 0; } } void Process(pair<int, int> p) { if (!contains(gr[p.first], p.second)) return; Remove(p.first, p.second); queue<int> toRemove; if (deg[p.first] < k) toRemove.push(p.first); if (deg[p.second] < k) toRemove.push(p.second); RemAll(toRemove); } void Init() { queue<int> toRemove; for (int i = (0), _b = ((n)-1); i <= _b; i++) { if (deg[i] < k) toRemove.push(i); } RemAll(toRemove); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << std::setprecision(15); cout << std::fixed; cin >> n >> m >> k; fr.resize(m); gr.resize(n); deg.resize(n); visited.resize(n); inTrip.resize(n, true); inTripCount = n; for (int i = (0), _b = ((m)-1); i <= _b; i++) { int a, b; cin >> a >> b; --a; --b; fr[i] = make_pair(a, b); gr[a].insert(b); gr[b].insert(a); ++deg[a]; ++deg[b]; } Init(); vector<int> res; for (int i = (m - 1), _b = (0); i >= _b; i--) { res.push_back(inTripCount); if (i != 0) Process(fr[i]); } for (int i = (m - 1), _b = (0); i >= _b; i--) { cout << res[i] << endl; } return 0; }