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
java
//package contests.CF1037; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class E { static ArrayList<Integer>[] adj; static int[] vis, going; static int NO = 3, YES = 2, VIS = 1; static int k; public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); adj = new ArrayList[n]; for (int i = 0; i < adj.length; i++) { adj[i] = new ArrayList<>(); } vis = new int[n]; going = new int[n]; int m = sc.nextInt(); k = sc.nextInt(); HashSet<Long> e = new HashSet<>(); int[][] edges = new int[m][2]; for (int i = 0; i < m; i++) { edges[i][0] = sc.nextInt()-1; edges[i][1] = sc.nextInt()-1; adj[edges[i][0]].add(edges[i][1]); adj[edges[i][1]].add(edges[i][0]); e.add(hash(edges[i][0], edges[i][1])); } TreeSet<Integer> set = new TreeSet<>(new Comparator<Integer>() { @Override public int compare(Integer i, Integer j) { ; if(going[i] != going[j]) return going[i] - going[j]; return i - j; } }); Arrays.fill(vis, YES); for (int i = 0; i < going.length; i++) { going[i] = adj[i].size(); } for (int i = 0; i < n; i++) { set.add(i); } while(!set.isEmpty() && going[set.first()] < k){ int a = set.pollFirst(); vis[a] = NO; for (int b : adj[a]) { if(vis[b] == YES && e.contains(hash(a, b))){ set.remove(b); going[b]--; set.add(b); e.remove(hash(a, b)); } } } int[] ans = new int[m]; for (int q = m-1; q >= 0; q--) { ans[q] = set.size(); int u = edges[q][0]; int v = edges[q][1]; if(vis[u] == YES) { set.remove(u); if(vis[v] == YES && e.contains(hash(u, v))) going[u]--; set.add(u); } if(vis[v] == YES) { set.remove(v); if(vis[u] == YES && e.contains(hash(u, v))) going[v]--; set.add(v); } e.remove(hash(u, v)); while(!set.isEmpty() && going[set.first()] < k){ int a = set.pollFirst(); vis[a] = NO; for (int b : adj[a]) { if(vis[b] == YES && e.contains(hash(a, b))){ set.remove(b); going[b]--; set.add(b); e.remove(hash(a, b)); } } } } ans[0] = set.size(); for (int i = 0; i < m; i++) { pw.println(ans[i]); } pw.flush(); pw.close(); } static long hash(int a, int b){ return Math.min(a, b) * 1000000l + Math.max(a, b); } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
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; s.remove(u); for (int v : (Set<Integer>) g[u]) { deg[v]--; } } 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; long long read() { long long x = 0; char f = 1, ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void write(long long x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void writeln(long long x) { write(x); puts(""); } const int N = 600000, M = 22344; int n, m, k; queue<int> q; bool in[N]; struct Edge { int u, v, nxt, pre, fg; } e[M]; int head[N], en, d[N]; void add(int x, int y) { e[++en].u = x, e[en].v = y, e[en].nxt = head[x]; if (!head[x]) e[head[x]].pre = en; head[x] = en; } int ans[N], cnt = 0; void work() { while (!q.empty()) { int x = q.front(); q.pop(); if (!in[x]) continue; --cnt; in[x] = 0; for (int i = head[x]; i; i = e[i].nxt) { int y = e[i].v; e[i ^ 1].fg = e[i].fg = 1; if (--d[y] < k) q.push(y); } } } void delta(int i) { if (e[i].fg) return; --d[e[i].u]; int x = e[i].u; if (!e[i].pre) head[x] = e[i].nxt, e[head[x]].pre = 0; else e[e[i].pre].nxt = e[i].nxt, e[e[i].nxt].pre = e[i].pre; } int main() { n = read(), m = read(), k = read(); for (int i = 1; i <= n; ++i) in[i] = 1; cnt = n; en = 1; for (int i = 1; i <= m; ++i) { int x = read(), y = read(); add(x, y); add(y, x); ++d[x], ++d[y]; } for (int i = 1; i <= n; ++i) if (d[i] < k) q.push(i); work(); int t = 0; for (int i = en; i >= 1; i -= 2) { ans[++t] = cnt; delta(i); delta(i - 1); if (d[e[i].u] < k) q.push(e[i].u); if (d[e[i].v] < k) q.push(e[i].v); work(); } for (int i = t; i >= 1; --i) writeln(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 INF = 0x3f3f3f3f; const int MAXN = 200005, MOD = 1e9 + 7; struct ii { long long a, b; bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); } }; int N, M, K, ans, valid[MAXN]; ii arr[MAXN]; vector<int> edge[MAXN]; void gagalin(int now) { ans--; for (auto i : edge[now]) { if (valid[i] >= K) { valid[i]--; if (valid[i] == K - 1) gagalin(i); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> M >> K; for (int i = 0; i < M; i++) { cin >> arr[i].a >> arr[i].b; edge[arr[i].a].push_back(arr[i].b); edge[arr[i].b].push_back(arr[i].a); } for (int i = 1; i <= N; i++) valid[i] = edge[i].size(); ans = N; for (int i = 1; i <= N; i++) { if (valid[i] < K) { gagalin(i); } } vector<int> vans; for (int i = M - 1; i >= 0; i--) { cout << valid[2] << " v\n"; vans.push_back(ans); if (valid[arr[i].a] < K || valid[arr[i].b] < K) continue; if (valid[arr[i].a] == K) { valid[arr[i].a]--; if (valid[arr[i].a] == K - 1) gagalin(arr[i].a); } else { valid[arr[i].b]--; if (valid[arr[i].b] == K - 1) gagalin(arr[i].b); } } for (int i = vans.size() - 1; i >= 0; i--) cout << vans[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
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() { queue<int> q; for (int i = 1; i <= n; i++) if (deg[i] && deg[i] < k) q.push(i); 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] && 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 = m; i; i--) { Del(); 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]]--; } } 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 = 1e5 + 10; set<pair<int, int> > sq; set<int> to[maxn]; int n, m, k; int a[maxn]; struct node { int l, r; } q[maxn]; void update() { while (sq.size() > 0 && (*(sq.begin())).first < k) { auto x = *(sq.begin()); sq.erase(sq.begin()); for (auto j = to[x.second].begin(); j != to[x.second].end();) { auto value = *j; if (sq.count({a[value], value})) { sq.erase(sq.find({a[value], value})); a[value]--; if (a[value] > 0) sq.insert({a[value], value}); to[x.second].erase(j++); } else j++; } } } int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int from, t; cin >> from >> t; q[i].l = from, q[i].r = t; to[from].insert(t); to[t].insert(from); a[from]++, a[t]++; } for (int i = 1; i <= n; i++) sq.insert({a[i], i}); vector<int> ans; update(); ans.push_back(sq.size()); for (int i = m - 1; i >= 1; i--) { if (sq.count({a[q[i].l], q[i].l}) && sq.count({a[q[i].r], q[i].r})) { sq.erase(sq.find({a[q[i].l], q[i].l})); sq.erase(sq.find({a[q[i].r], q[i].r})); a[q[i].l]--, a[q[i].r]--; if (a[q[i].l] > 0) sq.insert({a[q[i].l], q[i].l}); if (a[q[i].r] > 0) sq.insert({a[q[i].r], q[i].r}); to[q[i].l].erase(q[i].r); to[q[i].r].erase(q[i].l); update(); } ans.push_back(sq.size()); } reverse(ans.begin(), ans.end()); for (auto &i : ans) { cout << 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
//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] && deg[i] < k) { dlt += dfs(i, k, deg, del, edges, edgesS); } } for (int i = 0; i < n; i++) { if (!del[i] && deg[i] < k) { 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); 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 (!del[t1] && !del[t2]) { deg[t1]--; deg[t2]--; if (!del[t1] && deg[t1] < k) { tr += dfs(t1, k, deg, del, edges, edgesS); } if (!del[t2] && deg[t2] < k) { tr += dfs(t2, 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; del[u] = true; cnt++; for (int idx : edgeS[u]) { int o = get(edges.get(idx), idx); if (del[o]) continue; deg[o]--; if (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
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]; 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()) { 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; auto x = s.lower_bound(make_pair(d[p[i].first], p[i].first)); if (x != s.end()) { s.erase(x); d[p[i].first]--; s.insert(make_pair(d[p[i].first], p[i].first)); } x = s.lower_bound(make_pair(d[p[i].second], p[i].second)); if (x != s.end()) { s.erase(x); d[p[i].second]--; s.insert(make_pair(d[p[i].second], 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
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])); } } 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(); set.add(1000000l * from[i] + to[i]); } 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 si = g[x].size(); for (int i = 0; i < si; i++) { int to = g[x][i]; 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() { while (!s.empty() && (*s.begin()).first < k) { del((*s.begin()).second); 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))] = 1; } for (int i = 0; i < n; i++) { s.insert(make_pair(deg[i], i)); } for (int i = m - 1; i >= 0; i--) { tu(); 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; struct edge { int v, id; }; map<int, int> D; vector<edge> E[200005]; int ans[200005], n, m, u, v, k, x[200005], y[200005]; bool vis[200005]; queue<int> Q; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &x[i], &y[i]); E[x[i]].push_back((edge){y[i], i}); E[y[i]].push_back((edge){x[i], i}); D[x[i]]++; D[y[i]]++; } for (auto p : D) if (p.second < k) Q.push(p.first); while (!Q.empty()) { u = Q.front(); Q.pop(); D.erase(u); for (auto &j : E[u]) if (D.count(j.v) && !vis[j.id]) if (--D[j.v] < k) Q.push(j.v), vis[j.id] = true; } ans[m] = D.size(); for (int i = m; i > 1; i--) { if (D.count(x[i]) && D.count(y[i]) && !vis[i]) { if (--D[x[i]] < k) Q.push(x[i]); if (--D[y[i]] < k) Q.push(y[i]); vis[i] = true; } while (!Q.empty()) { u = Q.front(); Q.pop(); D.erase(u); for (auto &j : E[u]) if (D.count(j.v) && !vis[j.id]) if (--D[j.v] < k) Q.push(j.v), vis[j.id] = true; } ans[i - 1] = D.size(); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
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) { 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
java
import java.io.*; import java.math.BigInteger; import java.util.*; public class Test { static int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static long readLong() { long ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static String readString() { StringBuilder b = new StringBuilder(); try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c >= '0' && c <= '9') { start = true; b.append((char) (c)); } else if (start) break; } } catch (IOException e) { } return b.toString().trim(); } static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); void start() { int n = readInt(), m = readInt(), k = readInt(); Set<Integer>[] g = new Set[n+1]; boolean[] trip = new boolean[n+1]; for (int i = 1; i <= n; i++) g[i] = new HashSet<>(); int[] q = new int[n+1]; int ans = 0; while (m-- > 0) { int u = readInt(), v = readInt(); g[u].add(v); g[v].add(u); if (trip[u] && trip[v]) { writer.println(ans); continue; } if (g[u].size() < k || g[v].size() < k) { writer.println(ans); continue; } int a = 0, b = 0; if (!trip[u] && !trip[v]) { if (g[u].size() > k && g[v].size() > k) { writer.println(ans); continue; } if (g[v].size() == k) { int t = v; v = u; u = t; } List<Integer> checked = new ArrayList<>(); boolean ok = true; loop: for (int x : g[u]) { for (int y : checked) if (!g[y].contains(x)) { ok = false; break loop; } checked.add(x); } if (ok) { checked.add(u); for (int x : checked) { if (trip[x]) continue; ans++; trip[x] = true; q[b++] = x; } } } else { if (trip[v]) { int t = v; v = u; u = t; } int in = 0; for (int x : g[v]) if (trip[x]) in++; if (in >= k) { ans++; trip[v] = true; q[b++] = v; } } while (a < b) { int x = q[a++]; for (int y : g[x]) { if (!trip[y] && g[y].size() >= k) { int in = 0; for (int z : g[y]) if (trip[z]) in++; if (in >= k) { ans++; trip[y] = true; q[b++] = y; } } } } writer.println(ans); } } public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int p[300005], c[300005], a[300005]; int main() { ios_base::sync_with_stdio(false); int n, i, u, v, j, f; cin >> n; for (i = 0; i < n - 1; i++) { cin >> u >> v; p[v] = u; c[u]++; } for (i = 0; i < n; i++) { cin >> a[i]; } i = 0; j = 1; f = 0; while (i < n) { for (j; j < n && c[a[i]] > 0; j++) { if (p[a[j]] != a[i]) { f = 1; break; } c[a[i]]--; } if (c[a[i]] > 0) f = 1; if (f == 1) break; i++; } if (f == 0) cout << "Yes"; else cout << "No"; }
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 = (int)2e5 + 5; int n, m, k; vector<pair<int, int> > out[maxn]; int la[maxn]; int pos[maxn]; int deg[maxn]; int add[maxn]; bool dead[maxn]; set<pair<int, int>, greater<pair<int, int> > > s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; fill(la, la + n, -1); for (auto i = 0; i < m; ++i) { int v, u; cin >> v >> u; --v; --u; out[v].push_back(make_pair(i, u)); out[u].push_back(make_pair(i, v)); ++deg[v]; ++deg[u]; if (deg[v] == k) { pos[v] = out[v].size() - 1; la[v] = i; } if (deg[u] == k) { pos[u] = out[u].size() - 1; la[u] = i; } } for (auto i = 0; i < n; ++i) if (la[i] == -1) la[i] = m; for (auto i = 0; i < n; ++i) cout << i << " " << la[i] << endl; for (auto i = 0; i < n; ++i) s.insert(make_pair(la[i], i)); while (!s.empty()) { int v = s.begin()->second; dead[v] = true; s.erase(s.begin()); for (auto e : out[v]) { int u = e.second; if (dead[u] || la[u] == la[v]) continue; if (la[u] < e.first) continue; s.erase(make_pair(la[u], u)); ++pos[u]; while (pos[u] != (int)out[u].size() && dead[out[u][pos[u]].second]) ++pos[u]; if (pos[u] == (int)out[u].size()) la[u] = m; else la[u] = out[u][pos[u]].first; la[u] = min(la[u], la[v]); s.insert(make_pair(la[u], u)); } } for (auto i = 0; i < n; ++i) add[la[i]]++; int curr = 0; for (auto i = 0; i < m; ++i) { curr += add[i]; cout << curr << '\n'; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const long long LINF = 1e18; const int INF = 1e9; const int M = 1e9 + 7; const double EPS = 1e-9; using namespace std; vector<vector<pair<int, int> > > g; int main(int argc, const char* argv[]) { std::ios_base::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; g.resize(n); int u[200100], v[200100]; for (int i = 0; i < (int)(m); i++) { cin >> u[i] >> v[i]; g[--u[i]].push_back(make_pair(--v[i], i)); g[v[i]].push_back(make_pair(u[i], i)); } int used[200100] = {0}; int deg[200100]; set<int> deleted; for (int i = 0; i < (int)(n); i++) { deg[i] = g[i].size(); if (deg[i] < k) deleted.insert(i); } int res[200100]; for (int curS = (int)(m - 1); curS >= 0; curS--) { if (curS != m - 1 && !used[curS + 1]) { used[curS] = 1; deg[u[curS]]--; deg[v[curS]]--; if (deg[u[curS]] < k) deleted.insert(u[curS]); if (deg[v[curS]] < k) deleted.insert(v[curS]); } for (auto it = deleted.begin(); it != deleted.end(); it++) { for (auto p = g[*it].begin(); p != g[*it].end(); p++) { if (used[p->second]) continue; used[p->second] = 1; deg[p->first]--; if (deg[p->first] < k) deleted.insert(p->first); deg[*it]--; } } res[curS] = n - deleted.size(); } for (int i = 0; i < (int)(m); i++) cout << res[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
python3
def findSolution(graph,edges,m,k,n): toBeRemoved = [] numOfConnections = [len(lst) for lst in graph] for i in range(0,n): if numOfConnections[i]<k: toBeRemoved.append(i) while len(toBeRemoved) >0: node = toBeRemoved.pop(0) for i in graph[node]: numOfConnections[i]-=1 if numOfConnections[i]<k: toBeRemoved.append(i) remaining = len(list(filter(lambda x: x>=k,numOfConnections))) result = [0 for i in range(m)] result[-1] = remaining #Because at the end, all the remaining would be friends and would be going #next, we loop in reverse order and keep on removing one edge for next stage and compute answer at that stage for i in range(m-1,0,-1): u,v = edges[i] if numOfConnections[u]<k or numOfConnections[v]<k: #This means that the current ith edge doesn't contribute to result. So its ans is the previous val only result[i] = remaining continue #As this equals k, its contribution should be removed for next iterations (as edge is removed at each stage).. So remove one of the nodes in this edge if numOfConnections[u] == k: toBeRemoved.append(u) numOfConnections[u]-=1 elif numOfConnections[v] == k: toBeRemoved.append(v) numOfConnections[v]-=1 #Below condition means that the current edge doesn't make any difference as there are already more than k friends if numOfConnections[u] > k and numOfConnections[v] > k: numOfConnections[u]-=1 numOfConnections[v] -= 1 graph[u].remove(v) graph[v].remove(u) result[i-1] = remaining continue while len(toBeRemoved)>0: node = toBeRemoved.pop(0) remaining -= 1 for j in graph[node]: numOfConnections[j]-=1 if numOfConnections[j]==k-1: toBeRemoved.append(j) result[i-1] = remaining for i in result: print(i) n,m,k = map(int,input().split()) graph = [[] for i in range(n)] edges = [] for _ in range(m): u,v = map(int,input().split()) u-=1 v-=1 graph[u].append(v) graph[v].append(u) edges.append((u,v)) findSolution(graph,edges,m,k,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, k, ans[200005], cnt[200005]; vector<int> a[200005]; pair<int, int> ed[200005]; set<pair<int, int> > s; set<pair<int, int> >::iterator it; bool dd[200005]; void Delete() { while (s.size()) { it = s.begin(); pair<int, int> temp = *it; if (temp.first >= k) break; dd[temp.second] = 0; cnt[temp.second] = 0; s.erase(it); for (int v : a[temp.second]) if (dd[v]) { it = s.lower_bound(make_pair(cnt[v], v)); s.erase(it); --cnt[v]; s.insert(make_pair(cnt[v], v)); } } } void Pro(int v) { it = s.lower_bound(make_pair(cnt[v], v)); s.erase(it); --cnt[v]; s.insert(make_pair(cnt[v], v)); } 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 >> ed[i].first >> ed[i].second; ++cnt[ed[i].first]; ++cnt[ed[i].second]; a[ed[i].first].push_back(ed[i].second); a[ed[i].second].push_back(ed[i].first); } for (int i = (1); i <= (n); ++i) { s.insert(make_pair(cnt[i], i)); dd[i] = 1; } Delete(); ans[m] = s.size(); for (int i = (m - 1); i >= (1); --i) { if (dd[ed[i + 1].first] && dd[ed[i + 1].second]) { Pro(ed[i + 1].first); Pro(ed[i + 1].second); Delete(); } ans[i] = s.size(); } 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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1000007, inf = 0x3f3f3f3f; set<int> e[N]; int n, m, k; pair<int, int> edges[N]; set<int> st; int main() { ios::sync_with_stdio(false); srand(time(NULL)); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; e[u].insert(v); e[v].insert(u); edges[i] = {u, v}; st.insert(u); } queue<int> q; for (int u = 1; u <= n; ++u) { if (e[u].size() >= k) st.insert(u); else q.push(u); } vector<int> ans; for (int i = m - 1; i >= 0; --i) { while (q.size()) { int u = q.front(); q.pop(); for (int v : e[u]) { if (st.count(v)) { e[v].erase(u); if (e[v].size() < k) { st.erase(v); q.push(v); } } } } ans.push_back(st.size()); int u, v; u = edges[i].first, v = edges[i].second; e[u].erase(v); e[v].erase(u); if (st.count(u)) { if (e[u].size() < k) { st.erase(u); q.push(u); } } if (st.count(v)) { if (e[v].size() < k) { st.erase(v); q.push(v); } } } reverse(ans.begin(), ans.end()); for (int i = 0; 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> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define ll long long int #define ld long double #define ff first #define ss second #define pb push_back #define mp make_pair const int M=2e5+5; set< pair<int,int> > s; set<int> vtr[M]; int u[M],ans[M],v[M],d[M]; int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n,m,k,i,j; cin>>n>>m>>k; for(i=1;i<=m;i++) { cin>>u[i]>>v[i]; d[u[i]]++; vtr[u[i]].insert(v[i]); vtr[v[i]].insert(u[i]); d[v[i]]++; } for(i=1;i<=n;i++) s.insert(mp(d[i],i)); for(i=m;i>=1;i--) { // cout<<i<<"------"<<endl; // for(j=1;j<=n;j++) // cout<<d[j]<<" "; // cout<<endl; while(s.size()) { if((*s.begin()).ff<k) { int vt=(*s.begin()).ss; d[vt]=0; for(auto it=vtr[vt].begin();it!=vtr[vt].end();it++) { int x=*it; if(d[x]>=k) { s.erase(mp(d[x],x)); d[x]--; s.insert(mp(d[x],x)); } } s.erase(s.begin()); } else break; } ans[i]=s.size(); if(d[u[i]]>=k && d[v[i]]>=k) { s.erase(mp(d[u[i]],u[i])); d[u[i]]--; s.insert(mp(d[u[i]],u[i])); s.erase(mp(d[v[i]],v[i])); d[v[i]]--; s.insert(mp(d[v[i]],v[i])); vtr[u[i]].erase(v[i]); vtr[v[i]].erase(u[i]); } } for(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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 7; const long long INF = 1e18 + 7; pair<int, int> a[N]; int d[N], ans[N]; int n, m, k; set<pair<int, int> > s; vector<pair<int, int> > g[N]; int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { scanf("%d%d", &a[i].first, &a[i].second); g[a[i].first].push_back(make_pair(a[i].second, i)); g[a[i].second].push_back(make_pair(a[i].first, i)); } for (int i = 1; i <= n; i++) { d[i] = g[i].size(); s.insert(make_pair(d[i], i)); } for (int i = m; i >= 1; i--) { while (!s.empty()) { set<pair<int, int> >::iterator it = s.begin(); pair<int, int> v = *it; if (v.first < k) { s.erase(v); for (pair<int, int> to : g[v.second]) { if (to.second > i) break; s.erase(make_pair(d[to.first], to.first)); d[to.first]--; if (d[to.first] >= k) s.insert(make_pair(d[to.first], to.first)); } } else break; } ans[i] = s.size(); if (d[a[i].first] >= k && d[a[i].second] >= k) { s.erase(make_pair(d[a[i].first], a[i].first)); d[a[i].first]--; if (d[a[i].first] >= k) s.insert(make_pair(d[a[i].first], a[i].first)); else { for (pair<int, int> to : g[a[i].first]) { if (to.second >= i) break; s.erase(make_pair(d[to.first], to.first)); d[to.first]--; if (d[to.first] >= k) s.insert(make_pair(d[to.first], to.first)); } } s.erase(make_pair(d[a[i].second], a[i].second)); d[a[i].second]--; if (d[a[i].second] >= k) s.insert(make_pair(d[a[i].second], a[i].second)); else { for (pair<int, int> to : g[a[i].second]) { if (to.second >= i) break; s.erase(make_pair(d[to.first], to.first)); d[to.first]--; if (d[to.first] >= k) s.insert(make_pair(d[to.first], to.first)); } } } } 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; 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() { queue<int> q; for (int i = 1; i <= n; i++) if (deg[i] > 0 && deg[i] < k) q.push(i); 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 = m; i; i--) { Del(); 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]]--; } } 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 N = 2E5 + 10; set<int> adj[N]; set<pair<int, int> > S; int n, m, k; int x[N], y[N]; int ans[N]; void Read_Input() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", &x[i], &y[i]); adj[x[i]].insert(y[i]); adj[y[i]].insert(x[i]); } } void Erase(int u) { for (auto v : adj[u]) { S.erase(pair<int, int>(adj[v].size(), v)); adj[v].erase(u); if (adj[v].size()) S.insert(pair<int, int>(adj[v].size(), v)); } adj[u].clear(); } void Remove() { while (S.size() && S.begin()->first < k) { int u = S.begin()->second; Erase(u); S.erase(S.begin()); } } void Solve() { for (int i = 1; i <= n; i++) S.insert(pair<int, int>(adj[i].size(), i)); Remove(); for (int i = m; i >= 1; i--) { ans[i] = S.size(); int u = x[i], v = y[i]; S.erase(pair<int, int>(adj[u].size(), u)); S.erase(pair<int, int>(adj[v].size(), v)); adj[u].erase(v); adj[v].erase(u); if (adj[u].size()) S.insert(pair<int, int>(adj[u].size(), u)); if (adj[v].size()) S.insert(pair<int, int>(adj[v].size(), v)); Remove(); } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); } int main() { Read_Input(); 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
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); 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; 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) if (deg[i] < k) 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> using namespace std; struct con { int x, y; } a[200002]; struct node { int id, d; } p[200002]; int head[200002], ver[200002 * 2], nxt[200002 * 2], l; int n, m, k, i, j, d[200002], ans[200002], sum; bool cut[200002]; void insert(int x, int y) { l++; ver[l] = y; nxt[l] = head[x]; head[x] = l; d[y]++; } int my_comp(const node &x, const node &y) { return x.d < y.d; } void dfs(int x, int last) { d[x]--; for (int i = head[x]; i; i = nxt[i]) { if (ver[i] == last) { ver[i] = 0; break; } } if (d[x] >= k || cut[x]) return; sum--; cut[x] = 1; for (int i = head[x]; i; i = nxt[i]) { int y = ver[i]; if (y == last) ver[i] = 0; else if (!cut[y] && y != 0) dfs(y, x); } } int main() { cin >> n >> m >> k; for (i = 1; i <= m; i++) { int u, v; cin >> u >> v; insert(u, v); insert(v, u); a[i].x = u, a[i].y = v; } for (i = 1; i <= n; i++) p[i] = (node){i, d[i]}; sort(p + 1, p + n + 1, my_comp); for (i = 1; i <= n; i++) { int x = p[i].id; if (d[x] < k) { d[x] = 0; cut[x] = 1; for (int j = head[x]; j; j = nxt[j]) d[ver[j]]--; } } for (i = 1; i <= n; i++) { if (d[i] >= k) ans[m]++; } sum = ans[m]; for (i = m - 1; i >= 1; i--) { int x = a[i + 1].x, y = a[i + 1].y; if (!cut[x] && !cut[y]) dfs(x, y), dfs(y, x); ans[i] = sum; } for (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]) { 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[bstCustom.first().y]--; bstCustom.add(new pair(degree[i], i)); set.add(i * 1000000000l + 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]]) { 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; struct edge { int u, v; }; struct adj { int v, key; }; int n, m, k; vector<edge> edges; set<pair<int, int>> s; stack<int> res; bool deleted[200000] = {false}; int deg[200000] = {0}; vector<adj> graph[200000]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m >> k; int u, v; for (int i = 0; i < m; ++i) { cin >> u >> v; deg[u - 1]++; deg[v - 1]++; graph[u - 1].push_back({v - 1, i}); graph[v - 1].push_back({u - 1, i}); edges.push_back({u - 1, v - 1}); } for (int i = 0; i < n; ++i) { s.insert({deg[i], i}); } int i = m - 1; while (i >= 0) { while (!s.empty() && (k > s.begin()->first || s.begin()->first > s.size())) { auto it = *s.begin(); for (auto e : graph[it.second]) { if (!deleted[e.key]) { s.erase({deg[e.v], e.v}); deg[e.v]--; if (deg[e.v] >= k) s.insert({deg[e.v], e.v}); deleted[e.key] = true; } } s.erase(s.begin()); } res.push(s.size()); if (!s.empty() && !deleted[i]) { s.erase({deg[edges[i].u], edges[i].u}); s.erase({deg[edges[i].v], edges[i].v}); deg[edges[i].v]--; deg[edges[i].u]--; s.insert({deg[edges[i].u], edges[i].u}); s.insert({deg[edges[i].v], edges[i].v}); deleted[i] = true; } --i; } while (!res.empty()) { cout << res.top() << "\n"; res.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
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 200011; struct edge { int x, y, pre, nxt; } E[MAXN << 1]; vector<int> G[MAXN]; int e[MAXN]; int head[MAXN], vis[MAXN], deg[MAXN], ans[MAXN]; int n, m, k, tot = -1, cnt; inline void AddEdge(int x, int y) { E[++tot] = (edge){x, y, -1, head[x]}; if (head[x] != -1) E[head[x]].pre = tot; head[x] = tot; E[++tot] = (edge){y, x, -1, head[y]}; if (head[y] != -1) E[head[y]].pre = tot; head[y] = tot; } inline void Del(int x, int id) { if (head[x] == id) head[x] = E[id].nxt; if (E[id].pre != -1) E[E[id].pre].nxt = E[id].nxt; if (E[id].nxt != -1) E[E[id].nxt].pre = E[id].pre; } inline void Erase(int x) { if (vis[x]) return; vis[x] = 1; --cnt; for (int i = head[x]; ~i; i = E[i].nxt) { int to = E[i].y; --deg[to]; Del(to, i ^ 1); if (deg[to] < k) Erase(to); } } queue<int> Q; inline void Calc() { for (int i = 1; i <= n; ++i) { if (deg[i] < k) { vis[i] = 1; Q.push(i); } } while (!Q.empty()) { int now = Q.front(); Q.pop(); for (int i = 0; i < (int)G[now].size(); ++i) { --deg[G[now][i]]; if (deg[G[now][i]] == 0) { vis[G[now][i]] = 1; Q.push(G[now][i]); } } } for (int i = 1; i <= n; ++i) { if (!vis[i]) ++cnt; } } int main() { memset(head, -1, sizeof head); scanf("%d%d%d", &n, &m, &k); for (int i = 1, x, y; i <= m; ++i) { scanf("%d%d", &x, &y); G[x].push_back(y); G[y].push_back(x); AddEdge(x, y); e[i] = tot - 1; ++deg[x]; ++deg[y]; } cnt = 0; Calc(); ans[m] = cnt; for (int j = 1; j <= n; ++j) { printf("%d ", deg[j]); } puts(""); for (int i = m; i > 1; --i) { if (vis[E[e[i]].x] || vis[E[e[i]].y]) { ans[i - 1] = cnt; continue; } Del(E[e[i]].x, e[i]); Del(E[e[i]].y, e[i] ^ 1); --deg[E[e[i]].x]; --deg[E[e[i]].y]; if (deg[E[e[i]].x] < k) Erase(E[e[i]].x); if (deg[E[e[i]].y] < k) Erase(E[e[i]].y); ans[i - 1] = cnt; for (int j = 1; j <= n; ++j) { printf("%d ", deg[j]); } puts(""); } 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 n, Head[400010], Next[400010], Go[400010], A[400010], Fa[400010], D[400010], Cnt = 0; void addedge(int x, int y) { Go[++Cnt] = y; Next[Cnt] = Head[x]; Head[x] = Cnt; } void DFS(int x = 1) { for (int T = Head[x]; T; T = Next[T]) if (Go[T] != Fa[x]) Fa[Go[T]] = x, DFS(Go[T]); } signed main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); addedge(a, b); addedge(b, a); D[a]++, D[b]++; } for (int i = 1; i <= n; i++) scanf("%d", &A[i]); if (A[1] != 1) { puts("No"); return 0; } DFS(1); int l = 1; for (int i = 2; i <= n; i++) { if (Fa[A[i]] != A[l]) { puts("No"); return 0; } D[A[l]]--; D[A[i]]--; while (D[A[l]] == 0 && l <= n) l++; } puts("Yes"); 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]]) { 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]--; degree[bstCustom.first().y]--; 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
// 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) ){ 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]--; } else if(ma[y] <= k){ ma.erase(y);remove_node(y); if(ma.count(x)) ma[x]--; } 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> int n, m, k, ans, d[200010], u[200010], v[200010], t[200010]; std::set<int> next[200010]; inline void remove(int x) { if (!d[x]) return; ans--; d[x] = 0; for (auto y : next[x]) { next[y].erase(x); if (d[y] && --d[y] < k) remove(y); } next[x].clear(); } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d", u + i, v + i); next[u[i]].insert(v[i]); next[v[i]].insert(u[i]); d[u[i]]++; d[v[i]]++; } for (int i = 1; i <= n; i++) if (d[i]) ans++; for (int i = 1; i <= n; i++) if (d[i] < k) remove(i); for (int i = m; i; i--) { t[i] = ans; if (d[v[i]] && d[u[i]]) { next[v[i]].erase(u[i]); next[u[i]].erase(v[i]); if (d[u[i]] && --d[u[i]] < k) remove(u[i]); if (d[v[i]] && --d[v[i]] < k) remove(v[i]); } } for (int i = 1; i <= m; i++) printf("%d\n", t[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; vector<int> ad[200005]; set<int> s; vector<int> r; set<int> w; int k; int ff(int x) { int q, e, count1 = 0, cc = 1; for (int i = 0; i < ad[x].size(); i++) { q = ad[x][i]; if (w.find(q) != w.end()) { continue; cc++; } if (s.find(q) == s.end()) { if (ad[q].size() >= k) { w.insert(q); e = ff(q); if (e >= k) { count1++; } } } else if (s.find(q) != s.end()) count1++; if (count1 >= k) { s.insert(x); return count1; ; } } if (cc + count1 < k) r.push_back(x); return count1; } int main() { int n, m, a, b, x, y; cin >> n >> m >> k; while (m--) { cin >> x >> y; ad[x].push_back(y); ad[y].push_back(x); if (s.find(x) == s.end()) { if (ad[x].size() >= k) { w.insert(x); ff(x); } } for (int i = 0; i < r.size(); i++) { w.erase(r[i]); } r.clear(); int v = w.size(); int c = 0; if (v > k) { for (int i = 0; i < ad[x].size(); i++) { if (w.find(ad[x][i]) != w.end()) { c++; } } if (c >= k) { s.insert(x); for (int i = 0; i < ad[x].size(); i++) { if (w.find(ad[x][i]) != w.end()) { s.insert(ad[x][i]); } } } } w.clear(); if (s.find(y) == s.end()) { if (ad[y].size() >= k) { ff(y); } } x = y; for (int i = 0; i < r.size(); i++) { w.erase(r[i]); } r.clear(); v = w.size(); c = 0; if (v > k) { for (int i = 0; i < ad[x].size(); i++) { if (w.find(ad[x][i]) != w.end()) { c++; } } if (c >= k) { s.insert(x); for (int i = 0; i < ad[x].size(); i++) { if (w.find(ad[x][i]) != w.end()) { s.insert(ad[x][i]); } } } } w.clear(); cout << s.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
cpp
#include <bits/stdc++.h> using namespace std; template <class T> using v2d = vector<vector<T>>; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int maxN = 2e5 + 10; const int maxM = 2e5 + 10; int n, m, k, cnt[maxN], cur, ans[maxM]; vector<int> adj[maxN]; bool flag[maxN], h[maxN]; pair<int, int> edges[maxM]; set<pair<int, int>> erased; void dfs1(int u) { flag[u] = 1; h[u] = 1; for (auto &v : adj[u]) { if (!flag[v]) { dfs1(v); } cnt[u] += h[v]; } if (cnt[u] < k) { h[u] = 0; } } void dfs2(int u) { flag[u] = 1; h[u] = 0; cur--; for (auto &v : adj[u]) { if (!flag[v] && h[v] && !erased.count(make_pair(u, v))) { cnt[v]--; if (cnt[v] < k) { dfs2(v); } } } } void solve() { cin >> n >> m >> k; for (int i = 1; i <= (int)(m); ++i) { int u, v; cin >> u >> v; if (u > v) { swap(u, v); } adj[u].emplace_back(v); adj[v].emplace_back(u); edges[i] = make_pair(u, v); } for (int i = 1; i <= (int)(n); ++i) { if (!flag[i]) { dfs1(i); } } for (int i = 1; i <= (int)(n); ++i) { cnt[i] = 0; for (auto &j : adj[i]) { cnt[i] += h[j]; } cur += h[i]; } fill(flag + 1, flag + n + 1, 0); for (int i = m; i; i--) { int u, v; tie(u, v) = edges[i]; ans[i] = cur; if (h[u] && h[v]) { cnt[u]--; cnt[v]--; erased.emplace(u, v); erased.emplace(v, u); if (!flag[u] && cnt[u] < k) { dfs2(u); } if (!flag[v] && cnt[v] < k) { dfs2(v); } } } for (int i = 1; i <= (int)(m); ++i) { cout << ans[i] << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T = 1; while (T--) { 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; int ANS = 0; int n, m, k; vector<vector<int> > adj; vector<int> gr, friends, trip, vis; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; adj = vector<vector<int> >(n); gr = friends = trip = vector<int>(n); 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); gr[a]++; gr[b]++; if (gr[a] > k) friends[b]++; if (gr[b] > k) friends[a]++; if (gr[a] == k) { for (int j = 0; j < adj[a].size(); ++j) { int to = adj[a][j]; friends[to]++; } } if (gr[b] == k) { for (int j = 0; j < adj[b].size(); ++j) { int to = adj[b][j]; friends[to]++; } } int ans = 0; for (int j = 0; j < n; ++j) { int cnt = 0; for (int u = 0; u < adj[j].size(); ++u) { cnt += friends[adj[j][u]] >= k; } ans += cnt >= k; } cout << ans << "\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 (bac[x[i]] && bac[y[i]]) { S.erase(make_pair(bac[x[i]], x[i])); bac[x[i]]--; if (bac[x[i]] > 0) S.insert(make_pair(bac[x[i]], x[i])); S.erase(make_pair(bac[y[i]], y[i])); bac[y[i]]--; if (bac[y[i]] > 0) S.insert(make_pair(bac[y[i]], y[i])); a[x[i]].erase(y[i]); a[y[i]].erase(x[i]); } } for (int i = 1; i <= m; i++) cout << res[i] << '\n'; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
python3
from collections import defaultdict n, m, k = map(int, input().split()) xys = [tuple(map(int, input().split())) for _ in range(m)][::-1] gv = defaultdict(set) for x, y in xys: gv[x].add(y) gv[y].add(x) q = [u for u, vs in tuple(gv.items()) if len(vs) < k] i = 0 while i < len(q): u = q[i] i += 1 if u not in gv: continue for v in gv[u]: gv[v].remove(u) del gv[u] anss = [len(gv)] for x, y in xys: try: gv[x].remove(y) except KeyError: pass try: gv[y].remove(x) except KeyError: pass q = [] if len(gv[x]) < k: q.append(x) if len(gv[y]) < k: q.append(y) i = 0 while i < len(q): u = q[i] i += 1 if u not in gv: continue for v in gv[u]: gv[v].remove(u) if len(gv[v]) < k: q.append(v) del gv[u] anss.append(len(gv)) print('\n'.join(map(str, reversed(anss[:-1]))))
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int ms = 200200; int n, m, k; int ans = 0; int size[ms], par[ms]; std::pair<int, int> edges[ms]; std::vector<int> use[ms], dels[ms]; int deg[ms]; bool del[ms]; int getPar(int x) { return x == par[x] ? x : par[x] = getPar(par[x]); } void makeUnion(int a, int b) { a = getPar(a); b = getPar(b); if (a == b) return; if (size[a] < size[b]) { std::swap(a, b); } size[a] += size[b]; ans = std::max(size[a], ans); par[b] = a; } int main() { std::cin >> n >> m >> k; for (int i = 1; i <= n; i++) { par[i] = i; size[i] = 1; } for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); use[u].push_back(i); use[v].push_back(i); deg[u]++; deg[v]++; edges[i] = std::pair<int, int>(u, v); } std::queue<int> q; for (int i = 1; i <= n; i++) { if (deg[i] < k) { q.push(i); } } del[m] = true; for (int i = m; i >= 0; i--) { if (!del[i]) { int u = edges[i].first, v = edges[i].second; if (deg[u] == k) { q.push(u); } if (deg[v] == k) { q.push(v); } deg[u]--; deg[v]--; del[i] = true; dels[i].push_back(i); } while (!q.empty()) { int on = q.front(); q.pop(); for (auto e : use[on]) { if (!del[e]) { int u = edges[e].first, v = edges[e].second; if (deg[u] == k) { q.push(u); } if (deg[v] == k) { q.push(v); } deg[u]--; deg[v]--; del[e] = true; dels[i].push_back(e); } } } } for (int i = 0; i < m; i++) { for (auto e : dels[i]) { int u = edges[e].first, v = edges[e].second; makeUnion(u, v); } printf("%d\n", 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
java
/* Author: Ronak Agarwal, reader part not written by me*/ import java.io.* ; import java.util.* ; import static java.lang.Math.min ; import static java.lang.Math.max ; import static java.lang.Math.abs ; import static java.lang.Math.log ; import static java.lang.Math.pow ; import static java.lang.Math.sqrt ; /* Thread is created here to increase the stack size of the java code so that recursive dfs can be performed */ public class Codeshefcode{ public static void main(String[] args) throws IOException{ new Thread(null,new Runnable(){ public void run(){ exec_Code() ;} },"Solver",1l<<27).start() ; } static void exec_Code(){ try{ Solver Machine = new Solver() ; Machine.Solve() ; Machine.Finish() ;} catch(Exception e){ e.printStackTrace() ; print_and_exit() ;} catch(Error e){ e.printStackTrace() ; print_and_exit() ; } } static void print_and_exit(){ System.out.flush() ; System.exit(-1) ;} } /* Implementation of the Reader class */ class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar,numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is;} public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();} if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString() ; } public String s(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); }while (!isSpaceChar(c)); return res.toString(); } public long l(){ int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1 ; c = read() ; } long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10 ; res += c - '0' ; c = read(); }while(!isSpaceChar(c)); return res * sgn; } public int i(){ 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) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } /* All the useful functions,constants,renamings are here*/ class Template{ /* Constants Section */ final int INT_MIN = Integer.MIN_VALUE ; final int INT_MAX = Integer.MAX_VALUE ; final long LONG_MIN = Long.MIN_VALUE ; final long LONG_MAX = Long.MAX_VALUE ; static long MOD = 1000000007 ; static Reader ip = new Reader() ; static PrintWriter op = new PrintWriter(System.out) ; /* Methods for writing */ static void p(Object o){ op.print(o) ; } static void pln(Object o){ op.println(o) ;} static void Finish(){ op.flush(); op.close(); } /* Implementation of operations modulo MOD */ static long inv(long a){ return powM(a,MOD-2) ; } static long m(long a,long b){ return (a*b)%MOD ; } static long d(long a,long b){ return (a*inv(b))%MOD ; } static long powM(long a,long b){ if(b<0) return powM(inv(a),-b) ; long ans=1 ; for( ; b!=0 ; a=(a*a)%MOD , b/=2) if((b&1)==1) ans = (ans*a)%MOD ; return ans ; } /* Renaming of some generic utility classes */ final static class mylist extends ArrayList<Integer>{} final static class myset extends TreeSet<pair>{} final static class mystack extends Stack<Integer>{} final static class mymap extends TreeMap<Integer,Integer>{} } /* Implementation of the pair class, useful for every other problem */ class pair implements Comparable<pair>{ int x ; int y ; pair(int _x,int _y){ x=_x ; y=_y ;} public int compareTo(pair p){ return (this.x<p.x ? -1 : (this.x>p.x ? 1 : (this.y<p.y ? -1 : (this.y>p.y ? 1 : 0)))) ; } } /* Main code starts here */ class Solver extends Template{ int n,m,k; myset s; int d[]; mylist N[]; void correctSet(){ while(!s.isEmpty()){ if(s.first().x>=k) break; int u = s.pollFirst().y; for(int v : N[u]) if(s.contains(new pair(d[v],v))){ s.remove(new pair(d[v],v)); d[v]--; s.add(new pair(d[v],v)); } } } public void Solve() throws IOException{ n = ip.i(); m = ip.i(); k = ip.i(); pair E[] = new pair[m]; N = new mylist[n+1]; d = new int[n+1]; for(int i=1 ; i<=n ; i++) N[i] = new mylist(); for(int i=0 ; i<m ; i++){ int u = ip.i(); int v = ip.i(); E[i] = new pair(u,v); N[u].add(v); N[v].add(u); d[u]++; d[v]++; } s = new myset(); for(int i=1 ; i<=n ; i++) s.add(new pair(d[i],i)); correctSet(); mylist res = new mylist(); for(int i=(m-1) ; i>=0 ; i--){ res.add(s.size()); int u = E[i].x; int v = E[i].y; boolean uins = s.contains(new pair(d[u],u)); boolean vins = s.contains(new pair(d[v],v)); if(!uins || !vins) continue; s.remove(new pair(d[u],u)); s.remove(new pair(d[v],v)); d[u]--; d[v]--; N[u].remove(N[u].size()-1); N[v].remove(N[v].size()-1); s.add(new pair(d[u],u)); s.add(new pair(d[v],v)); correctSet(); } Collections.reverse(res); for(int elem : res) pln(elem); } }
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; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, k; cin >> n >> m >> k; vector<vector<pair<long long, long long> > > friends(n); vector<long long> pointers(n); for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; friends[a - 1].push_back(make_pair(b - 1, i)); friends[b - 1].push_back(make_pair(a - 1, i)); } queue<long long> q; set<pair<long long, long long> > good; for (long long i = 0; i < n; i++) { if (friends[i].size() >= k) { good.insert(make_pair(friends[i][k - 1].second, i)); pointers[i] = k - 1; } else { pointers[i] = -1; q.push(i); } } long long u = m; vector<long long> ans(m); fill(ans.begin(), ans.end(), 0); while (good.size()) { while (q.size()) { long long V = q.front(); q.pop(); for (long long i = 0; i < friends[V].size(); i++) { long long to = friends[V][i].first, tm = friends[V][i].second; if (pointers[to] == -1) continue; long long T = friends[to][pointers[to]].second; if (tm > T) continue; pointers[to]++; while (pointers[to] < friends[to].size()) { long long K = friends[to][pointers[to]].first; if (pointers[K] == -1 || friends[to][pointers[to]].second >= u) pointers[to]++; else break; } if (pointers[to] >= friends[to].size()) { good.erase(good.find(make_pair(T, to))); pointers[to] = -1; q.push(to); } else { good.erase(good.find(make_pair(T, to))); T = friends[to][pointers[to]].second; good.insert(make_pair(T, to)); } } } long long S = good.size(); if (S == 0) break; set<pair<long long, long long> >::iterator it = good.end(); it--; pair<long long, long long> P = *it; long long tt = P.first; for (long long i = tt; i < u; i++) ans[i] = S; u = tt; while (good.size()) { set<pair<long long, long long> >::iterator it = good.end(); it--; pair<long long, long long> P = *it; long long ttt = P.first; if (tt != ttt) { break; } good.erase(it); pointers[P.second] = -1; q.push(P.second); } } for (long long 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; template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) { out << p.first << ' ' << p.second; } template <class T> istream& operator>>(istream& in, vector<T>& v) { for (auto& x : v) in >> x; return in; } template <class T> ostream& operator<<(ostream& out, vector<T>& v) { for (auto x : v) out << x << ' '; return out; } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, m, k; cin >> n >> m >> k; vector<set<long long> > adj(100005); vector<pair<long long, long long> > edges; map<long long, vector<long long> > deg; map<long long, long long> nodedeg; for (long long i = (0); i < (m); ++i) { long long a, b; cin >> a >> b; edges.push_back({a, b}); adj[a].insert(b); adj[b].insert(a); } for (long long i = (1); i < (n + 1); ++i) { deg[adj[i].size()].push_back(i); nodedeg[i] = adj[i].size(); } set<pair<long long, long long> > pq; bool flagarr[100005]; memset(flagarr, 0, sizeof(flagarr)); ; for (auto i : deg) { for (auto j : i.second) { pq.insert({i.first, j}); flagarr[j] = 1; } } while (!pq.empty() && pq.begin()->first < k) { auto nd = *pq.begin(); pq.erase(pq.begin()); flagarr[nd.second] = 0; for (auto i : adj[nd.second]) { pq.erase({nodedeg[i], i}); nodedeg[i]--; pq.insert({nodedeg[i], i}); } } vector<long long> finans; long long i = m - 1; set<pair<long long, long long> > edges_till_now; while (i >= 0) { finans.push_back(pq.size()); auto nd1 = edges[i]; edges_till_now.insert(edges[i]); edges_till_now.insert({edges[i].second, edges[i].first}); if (flagarr[nd1.first] && flagarr[nd1.second]) { pq.erase({nodedeg[nd1.first], nd1.first}); nodedeg[nd1.first]--; pq.insert({nodedeg[nd1.first], nd1.first}); pq.erase({nodedeg[nd1.second], nd1.second}); nodedeg[nd1.second]--; pq.insert({nodedeg[nd1.second], nd1.second}); while (!pq.empty() && pq.begin()->first < k) { auto nd = *pq.begin(); pq.erase(pq.begin()); for (auto i : adj[nd.second]) { if (edges_till_now.find({i, nd.second}) != edges_till_now.end()) continue; if (pq.find({nodedeg[i], i}) != pq.end()) { pq.erase({nodedeg[i], i}); nodedeg[i]--; pq.insert({nodedeg[i], i}); } } } } i--; } reverse((finans).begin(), (finans).end()); for (auto i : finans) cout << 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; int n, m, k; struct edge { int u, v, nxt; } e[200005 * 2]; int cnt, head[200005]; void adde(int u, int v) { e[++cnt].u = u; e[cnt].v = v; e[cnt].nxt = head[u]; head[u] = cnt; } int vis[200005 * 2], d[200005], ans[200005], res; struct ege2 { int u, v; } edge[200005]; queue<int> q; void solve() { while (!q.empty()) { int u = q.front(); q.pop(); d[u] = 0; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; if (vis[i]) continue; vis[i] = vis[i ^ 1] = 1; d[v]--; if (d[v] == k - 1) { --res; q.push(v); } } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; ++i) { scanf("%d%d", &edge[i].u, &edge[i].v); adde(edge[i].u, edge[i].v); adde(edge[i].v, edge[i].u); d[edge[i].u]++; d[edge[i].v]++; } res = n; for (int i = 1; i <= n; ++i) if (d[i] < k) { res--; q.push(i); } solve(); ans[m] = res; for (int i = m; i >= 2; --i) { if (vis[i << 1]) { ans[i - 1] = ans[i]; continue; } vis[i << 1] = vis[i << 1 | 1] = 1; int u = edge[i].u, v = edge[i].v; d[u]--; d[v]--; if (d[u] == k - 1) { --res; q.push(u); } if (d[v] == k - 1) { --res; q.push(v); } solve(); ans[i - 1] = res; } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int deg[maxn]; vector<int> G[maxn]; pair<int, int> o[maxn]; int ans[maxn], vis[maxn]; int main() { int n, m, k, u, v; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { scanf("%d%d", &u, &v); ; G[u].push_back(v); G[v].push_back(u); deg[u]++; deg[v]++; o[i] = make_pair(u, v); } queue<int> q; for (int i = 1; i <= n; i++) { if (deg[i] < k) { q.push(i); vis[i] = 1; } } while (!q.empty()) { int tmp = q.front(); q.pop(); for (auto x : G[tmp]) { deg[x]--; if (!vis[x] && deg[x] < k) { vis[x] = 1; q.push(x); } } } for (int i = 1; i <= n; i++) { if (!vis[i]) ans[m]++; } for (int i = m; i > 1; i--) { ans[i - 1] = ans[i]; u = o[i].first, v = o[i].second; if (!vis[u] && !vis[v]) { deg[u]--; deg[v]--; if (deg[u] < k) { ans[i - 1]--; vis[u] = 1; for (auto x : G[u]) deg[x]--; } if (deg[v] < k) { ans[i - 1]--; vis[v] = 1; for (auto x : G[v]) deg[x]--; } } if (ans[i - 1] < k) ans[i - 1] = 0; } 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 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; 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 (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) { int d = (vis[z] == 0) + (vis[u] == 0) + (vis[v] == 0); ans += d; 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
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]]) { 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[bstCustom.first().y]--; 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> #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]--; ans[i] -= rem(it.ff); c[it.ss]--; 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> using namespace std; const int nmax = 2e5 + 5; const int mod = 1e9 + 7; vector<int> g[nmax]; int destroyed; pair<int, int> ej[nmax]; int deg[nmax], ans[nmax]; bool vis[nmax]; void dhongsho(int u, int k, int p) { if (vis[u]) return; vis[u] = true; destroyed++; for (auto v : g[u]) { if (v == p or vis[v]) continue; deg[v]--; if (deg[v] < k) dhongsho(v, k, u); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k, u, v; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> u >> v; g[u].push_back(v), g[v].push_back(u); ej[i] = {u, v}; deg[u]++, deg[v]++; } for (int u = 1; u <= n; u++) { if (deg[u] < k) dhongsho(u, k, -1); } for (int i = m; i >= 1; i--) { ans[i] = n - destroyed; u = ej[i].first, v = ej[i].second; if (!vis[u] and !vis[v]) { deg[u]--, deg[v]--; g[u].pop_back(), g[v].pop_back(); if (deg[u] < k) dhongsho(u, k, v); if (deg[v] < k) dhongsho(v, k, u); } } for (int i = 1; i <= m; i++) { cout << ans[i] << "\n"; } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
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()) { if (!done[vv[i].first]) { deg[vv[i].first]--; pq.push(make_pair(-deg[vv[i].first], vv[i].first)); } if (!done[vv[i].second]) { 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 i = p.second; 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 = 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; inline int read() { int sum = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -1; ch = getchar(); } while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar(); return sum * ff; } const int mod = 1e9 + 7; const int mo = 998244353; const int N = 2e5 + 5; int n, m, s, du[N], ok[N], ux[N], uy[N], sum[N], cnt, ans; vector<int> G[N]; inline void del(int x) { if (du[x] >= s || ok[x]) return; queue<int> q; while (!q.empty()) q.pop(); q.push(x); ans--; ok[x] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = (0); i <= ((int)G[x].size() - 1); i++) { int v = G[x][i]; --du[v]; if (ok[v] || du[v] >= s) continue; q.push(v); ok[v] = 1; ans--; } } } int main() { n = read(); m = read(); s = read(); for (int i = (1); i <= (m); i++) { int x, y; x = read(), y = read(); G[x].push_back(y); G[y].push_back(x); du[y]++, du[x]++; ux[++cnt] = x, uy[cnt] = y; } ans = n; for (int i = (1); i <= (n); i++) del(i); sum[m] = ans; for (int i = (m); i >= (1); i--) { int x = ux[i], y = uy[i]; if (!ok[x]) du[x]--; if (!ok[y]) du[y]--; G[x].pop_back(); G[y].pop_back(); if (!ok[x] && du[x] < s) del(x); if (!ok[y] && du[y] < s) del(y); sum[i - 1] = ans; } for (int i = (1); i <= (m); i++) printf("%d\n", sum[i]); return 0; }
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]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k; cin >> n >> m >> k; for (long long i = 1; i <= m; i++) { cin >> x[i] >> y[i]; vp[x[i]].push_back(y[i]); vp[y[i]].push_back(x[i]); deg[x[i]]++; deg[y[i]]++; } set<pair<long long, long long> > s; for (long long i = 1; i <= n; i++) { s.insert({deg[i], i}); } long long ans = n; set<pair<long long, long long> > ae; vector<long long> v; for (long long i = m; i >= 1; i--) { while (s.size() > 0) { auto it = *s.begin(); s.erase(it); if (it.first >= k) break; for (auto it1 : vp[it.second]) { if (ae.find({it1, it.second}) == ae.end()) { ae.insert({it1, it.second}); ae.insert({it.second, it1}); s.erase({deg[it1], it1}); deg[it1]--; 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]], y[i]}); s.erase({deg[y[i]], y[i]}); deg[x[i]]--; if (deg[x[i]] > 0) { s.insert({deg[x[i]], x[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> 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 (a[x[i]].size() && a[y[i]].size()) { 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; long long getBit(long long num, int idx) { return ((num >> idx) & 1ll) == 1ll; } long long setBit1(long long num, int idx) { return num or (1ll << idx); } long long setBit0(long long num, int idx) { return num & ~(1ll << idx); } long long flipBit(long long num, int idx) { return num ^ (1ll << idx); } const int mod = 1000000007; long long n, m, k, indeg[(int)5e5 + 10]; set<long long> graph[(int)5e5 + 10]; long long ara[(int)5e5 + 10], bara[(int)5e5 + 10], ans[(int)5e5 + 10], rev[(int)5e5 + 10], track; void funck(long long v) { rev[v] = 1; track--; for (long long to : graph[v]) { graph[to].erase(v); } for (long long to : graph[v]) { if (--indeg[to] < k) { funck(to); } } indeg[v] = 0; graph[v].clear(); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { long long u, v; cin >> u >> v; u--; v--; indeg[u]++; indeg[v]++; ara[i] = u; bara[i] = v; graph[u].insert(v); graph[v].insert(u); } track = n; for (int i = 0; i < n; ++i) { if (rev[i] == 0 && indeg[i] < k) { funck(i); } } for (long long i = m - 1; i >= 0; i--) { ans[i] = track; if (graph[ara[i]].find(bara[i]) != graph[ara[i]].end()) { indeg[ara[i]]--; indeg[bara[i]]--; graph[ara[i]].erase(bara[i]); graph[bara[i]].erase(ara[i]); if (rev[ara[i]] == 0 && indeg[ara[i]] < k) funck(ara[i]); if (rev[bara[i]] == 0 && indeg[bara[i]] < k) funck(bara[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> const int ms = 200200; int n, m, k; int ans = 0; int size[ms], par[ms]; int getPar(int x) { return x == par[x] ? x : getPar(par[x]); } void makeUnion(int a, int b) { a = getPar(a); b = getPar(b); if (a == b) return; size[a] += size[b]; ans = std::max(size[a], ans); par[b] = a; } int deg[ms], when[ms]; std::vector<int> edges[ms], times[ms]; struct Edge { int u, v, t; }; bool operator<(Edge a, Edge b) { return a.t < b.t; } int main() { std::cin >> n >> m >> k; for (int i = 1; i <= n; i++) { par[i] = i; size[i] = 1; when[i] = (int)1e8; } std::vector<Edge> gen; for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); Edge cur; cur.u = u; cur.v = v; cur.t = i; gen.push_back(cur); deg[u]++; deg[v]++; if (deg[u] == k) { when[u] = i; } if (deg[v] == k) { when[v] = i; } edges[u].push_back(v); edges[v].push_back(u); times[u].push_back(i); times[v].push_back(i); } for (int i = 1; i <= n; i++) { for (auto j = 0; j < (int)edges[i].size(); j++) { edges[i][j] = std::max(times[i][j], when[edges[i][j]]); } std::sort(edges[i].begin(), edges[i].end()); } for (int i = 1; i <= n; i++) { if ((int)edges[i].size() < k || edges[i][k - 1] > m) { when[i] = m + 1; } else { when[i] = std::max(when[i], edges[i][k - 1]); } } for (int i = 0; i < m; i++) { int u = gen[i].u; int v = gen[i].v; gen[i].t = std::max(gen[i].t, std::max(when[u], when[v])); } std::sort(gen.begin(), gen.end()); for (int i = 0, j = 0; i < m; i++) { while (j < (int)gen.size() && gen[j].t == i) { makeUnion(gen[j].u, gen[j].v); j++; } printf("%d\n", 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> 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[x[i]].find(y[i]) != a[x[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
java
import java.io.*; import java.util.*; public class Trips { public static void main(String[] args) { FastScanner scan=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int n=scan.nextInt(), m=scan.nextInt(); k=scan.nextInt(); deg=new int[n]; a=new ArrayList[n]; for(int i=0;i<n;i++) a[i]=new ArrayList<>(); int[] u=new int[m], v=new int[m]; for(int i=0;i<m;i++) { u[i]=scan.nextInt()-1; v[i]=scan.nextInt()-1; a[u[i]].add(new edge(i,v[i])); a[v[i]].add(new edge(i,u[i])); deg[u[i]]++; deg[v[i]]++; } left=n; ind=m; bad=new boolean[n]; for(int i=0;i<n;i++) { if(deg[i]<k) { go(i); } } // System.out.println(Arrays.toString(deg)); int[] res=new int[m]; res[m-1]=left; for(ind=m-1;ind>0;ind--) { if(!bad[v[ind]]) deg[u[ind]]--; if(!bad[u[ind]]) deg[v[ind]]--; if(!bad[v[ind]]&&deg[v[ind]]<k) { go(v[ind]); } if(!bad[u[ind]]&&deg[u[ind]]<k) { go(u[ind]); } res[ind-1]=left; // System.out.println(Arrays.toString(deg)); } for(int i:res) out.println(i); out.close(); } static int left,ind; static boolean[] bad; static int[] deg; static ArrayList<edge>[] a; static int k; public static void go(int at) { bad[at]=true; ArrayDeque<Integer> q=new ArrayDeque<>(); q.offer(at); while(!q.isEmpty()) { int c=q.poll(); left--; for(edge nxt:a[c]) { if(nxt.id>=ind) continue; if(!bad[nxt.v]&&--deg[nxt.v]<k) { q.offer(nxt.v); bad[nxt.v]=true; } } } } static class edge { int id,v; edge(int id, int v) { this.id=id; this.v=v; } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try {st = new StringTokenizer(br.readLine());} catch (Exception e) {e.printStackTrace();} return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } } }
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<int> v, graph[200005]; vector<pair<long long, long long> > ed; set<int> s; multiset<int> s2; int deg[200005], cnt[200005], ans[200005], vis[200005], out[200005]; map<pair<long long, long long>, int> mp; void dfs(int u) { vis[u] = 1; v.push_back(u); for (int j = 0; j < graph[u].size(); j++) { int v = graph[u][j]; if (vis[v]) { continue; } dfs(v); } } void f(int k, int y = -1) { while (s.size()) { int z = *s.begin(); s.erase(s.begin()); for (int j = 0; j < graph[z].size(); j++) { int u = graph[z][j]; if (mp.count({min(z, u), max(z, u)})) continue; deg[u]--; mp[{min(z, u), max(z, u)}] = 1; if (deg[u] == k - 1) { s.insert(u); cnt[out[u]]--; out[u] = -1; } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k, i, j; cin >> n >> m >> k; for (i = 1; i <= m; i++) { int x, y; cin >> x >> y; ed.push_back(pair<long long, long long>(x, y)); graph[x].push_back(y); graph[y].push_back(x); deg[x]++; deg[y]++; } int ct = 0; for (i = 1; i <= n; i++) { if (!vis[i]) { ct++; v.clear(); s.clear(); dfs(i); cnt[ct] = v.size(); for (j = 0; j < v.size(); j++) { out[v[j]] = ct; } for (j = 0; j < v.size(); j++) { if (deg[v[j]] < k) { s.insert(v[j]); out[v[j]] = -1; cnt[ct]--; } } f(k); s2.insert(cnt[ct]); ans[m - 1] = max(ans[m - 1], cnt[ct]); } } for (i = (int)ed.size() - 2; i >= 0; i--) { int x = ed[i + 1].first; int y = ed[i + 1].second; ans[i] = ans[i + 1]; if (mp.count({min(x, y), max(x, y)})) continue; mp[{min(x, y), max(x, y)}] = 1; deg[x]--; deg[y]--; s.clear(); s2.erase(s2.find(cnt[out[x]])); int zz = out[x]; if (deg[x] == k - 1) { s.insert(x); cnt[out[x]]--; out[x] = -1; } if (deg[y] == k - 1) { if (i == 1) cout << "^^^^^^^^^" << endl; s.insert(y); cnt[out[y]]--; out[y] = -1; } f(k, 2); s2.insert(cnt[zz]); ans[i] = *s2.rbegin(); } for (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; fri[ind--]=ans; //System.out.println(ans+" "+cnt); continue; } if(tr[y].size()<k) { reduce(y); ans-=cnt; 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> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; const long long INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000; const int inf = 1000 * 1000 * 1000; const long double PI = acos(-1.0); const long long mod1 = inf + 7; const long long mod2 = inf + 9; const int MAXN = 200001; const long double EPS = 1e-9; int hp = 179; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); srand(time(NULL)); ; int n, m, k; cin >> n >> m >> k; vector<vector<pair<int, int>>> g(n + 1); vector<pair<int, int>> ribs; vector<int> d(n + 1); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; g[a].push_back({b, i}); g[b].push_back({a, i}); d[a]++; d[b]++; ribs.push_back({a, b}); } set<pair<int, int>> s; for (int i = 1; i <= n; ++i) { s.insert({g[i].size(), i}); } vector<int> ans; while (s.size() && s.begin()->first < k) { int v = s.begin()->second; for (int j = 0; j < g[v].size(); ++j) { int u = g[v][j].first; if (s.find({d[u], u}) != s.end()) { s.erase({d[u], u}); d[u]--; s.insert({d[u], u}); } } s.erase({d[v], v}); } for (int i = m - 1; i >= 0; --i) { ans.push_back(s.size()); int v = ribs[i].first, u = ribs[i].second; if (s.find({d[v], v}) != s.end() && s.find({d[u], u}) != s.end()) { s.erase({d[u], u}); d[u]--; s.insert({d[u], u}); s.erase({d[v], v}); d[v]--; s.insert({d[v], v}); while (s.size() && s.begin()->first < k) { v = s.begin()->second; for (int j = 0; j < g[v].size(); ++j) { u = g[v][j].first; if (g[v][j].second >= i) { continue; } if (s.find({d[u], u}) != s.end()) { s.erase({d[u], u}); d[u]--; s.insert({d[u], u}); } } s.erase({d[v], v}); } } } for (int i = 0; i < ans.size(); ++i) cout << ans[i] << "\n"; ; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int d[N], w[N]; vector<int> v[N]; int n, m, k, ans = 0; void addW(int x) { for (int to : v[x]) { w[to]++; if (w[to] == k) ans++; } } void addD(int x) { for (int to : v[x]) { d[to]++; if (d[to] == k) { addW(to); } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); x--, y--; v[x].push_back(y); v[y].push_back(x); if (v[x].size() > k) { d[y]++; if (d[x] >= k) { w[y]++; if (w[y] == k) ans++; } } if (v[y].size() > k) { d[x]++; if (d[y] >= k) { w[x]++; if (w[x] == k) ans++; } } if (v[x].size() == k) { addD(x); if (d[x] == k) addW(x); } if (v[y].size() == k) { addD(y); if (d[y] == k) addW(y); } printf("%d\n", ans); } 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 vis[N + 2]; vector<int> com[N + 2]; map<int, int> mp[N + 2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; i++) com[i].push_back(i); int ans = 0; while (m--) { int u, v; cin >> u >> v; mp[u][v] = 1, mp[v][u] = 1; if (vis[u] & vis[v]) ; else { int cnt = 0; for (auto x : com[u]) cnt += mp[v].count(x); if (cnt >= k || cnt == com[u].size()) { com[u].push_back(v); if (com[u].size() > k + 1) ans += (vis[v] ^ 1), vis[v] = 1; else if (com[u].size() == k + 1) { for (auto x : com[u]) ans += (vis[x] ^ 1), vis[x] = 1; } } cnt = 0; swap(u, v); for (auto x : com[u]) cnt += mp[v].count(x); if (cnt >= k || cnt == com[u].size()) { com[u].push_back(v); if (com[u].size() > k + 1) ans += (vis[v] ^ 1), vis[v] = 1; else if (com[u].size() == k + 1) { for (auto x : com[u]) ans += (vis[x] ^ 1), vis[x] = 1; } } } cout << ans << "\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+1]; 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
python2
'''input 8 12 3 1 2 1 3 2 3 3 4 2 4 1 4 5 6 6 7 5 7 4 5 1 5 2 5 ''' 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 a,b=b,a if F[a]==k-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
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<int> from(m), to(m); vector<vector<pair<int, int> > > adj(n); for (int i = (int)(0); i < (int)(m); ++i) { cin >> from[i] >> to[i]; from[i]--, to[i]--; adj[from[i]].emplace_back(to[i], i); adj[to[i]].emplace_back(from[i], i); } vector<int> alive(m, 1); vector<int> deg(n); set<pair<int, int> > g; for (int i = (int)(0); i < (int)(n); ++i) { deg[i] = adj[i].size(); if (deg[i]) g.emplace(deg[i], i); } vector<int> ans(m); for (int i = (int)(m - 1); i >= (int)(0); --i) { while (g.size() && g.begin()->first < k) { for (auto &p : adj[g.begin()->second]) { if (alive[p.second]) { alive[p.second] = 0; g.erase({deg[p.first], p.first}); g.emplace(--deg[p.first], p.first); } } g.erase(g.begin()); } ans[i] = g.size(); if (alive[i]) { int u = from[i], v = to[i]; g.erase({deg[u], u}); g.emplace(--deg[u], u); g.erase({deg[v], v}); g.emplace(--deg[v], v); alive[i] = 0; } } for (int i = (int)(0); i < (int)(m); ++i) cout << ans[i] << "\n"; return 0; }
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 4 4 2 2 3 1 2 1 3 1 4 ''' n,m,k=[int(x) for x in raw_input().split()] F=[0]*n E=[0]*n C=[[] for x in range(n)] tot=0 for i in range(m): a,b=[int(x)-1 for x in raw_input().split()] C[a].append(b) C[b].append(a) F[a]+=1 F[b]+=1 if F[a]==k: for j in C[a]: E[j]+=1 if E[j]==k: tot+=1 elif F[a]>k: E[b]+=1 if E[b]==k: tot+=1 a,b=b,a if F[a]==k: for j in C[a]: E[j]+=1 if E[j]==k: tot+=1 elif F[a]>k: E[b]+=1 if E[b]==k: tot+=1 print tot
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()>0 && 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()>0 && 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; const long long mod = 1e9 + 7, inf = 1e9 + 3; void update(int a, vector<set<int> >& g, set<pair<int, int> >& s, int k) { for (int v : g[a]) { s.erase({g[v].size(), v}); g[v].erase(a); if (g[v].size() >= k) { s.insert({g[v].size(), v}); } } } int main() { cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; set<pair<int, int> > s; vector<int> ans(m, 0); vector<set<int> > g(n + 1, set<int>()); vector<pair<int, int> > q; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; q.push_back({a, b}); g[a].insert(b), g[b].insert(a); } for (int v = 1; v <= n; ++v) { s.insert({g[v].size(), v}); } for (int i = m - 1; i >= 0; --i) { while (s.size() > 0 && (*s.begin()).first < k) { update((*s.begin()).second, g, s, k); s.erase(s.begin()); } ans[i] = s.size(); int a = q[i].first, b = q[i].second; s.erase({g[a].size(), a}), s.erase({g[b].size(), b}); g[a].erase(b), g[b].erase(a); s.insert({g[a].size(), a}), s.insert({g[b].size(), b}); } for (int x : ans) cout << x << "\n"; return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
// clang-format off // very important, much wow, such don't touch this #define protected public using iii = int; using yeee = iii; #ifdef LOCAL const iii DEBUG = 10; #else const iii DEBUG = -1; #endif #define DBG(x) if(DEBUG >= x) #include <bits/stdc++.h> using namespace std; #define FOR(i, lo, hi) for(ll i = ll(lo); i < ll(hi); ++i) #define ROF(i, hi, lo) for(ll i = ll(hi); i >= (ll)(lo); --i) #define all(x) (x).begin(), (x).end() #define len(x) ll((x).size()) #define pb push_back #define apply(f, x, y) (x) = f((x), (y)) using ll = long long; using ull = unsigned long long; using ld = long double; using pll = pair<ll, ll>; using vb = vector<bool>; using vvb = vector<vb>; using vll = vector<ll>; using vvll = vector<vll>; using vpll = vector<pll>; using point = complex<ll>; /* template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; */ /*|||*/ template <class T> T get() { T x; cin >> x; return x; } template <class T1, class T2> ostream &operator<<(ostream &out, const pair<T1, T2> &cont) { out << "(" << cont.first << ", " << cont.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> &cont) { for(long long int i=0; i < (long long int)(cont.size()); ++i) out << (i ? " " : "") << cont[i]; return out; } template <class T> ostream &operator<<(ostream &out, const set<T> &cont) { out << "{"; for(const T &x : cont) out << x << ", "; out << "}"; return out; } template <class T, class U> ostream &operator<<(ostream &out, const map<T, U> &cont) { out << "{"; for(const pair<T, U> &x : cont) out << "(" << x.first << ": " << x.second << "), "; out << "}"; return out; } template <class T> ostream &operator<<(ostream &out, vector<vector<T>> &cont) { for(vector<T> &v : cont) { for(long long int i = 0; i < (long long int)(v.size()); ++i) { out << (i ? " " : "") << v[i]; } out << "\n"; } return out; } /*|||*/ void setup_io(string input_file = "", string output_file = "") { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(unsigned(time(0))); DBG(0 && input_file != "") freopen(input_file.c_str(), "r", stdin); DBG(0 && output_file != "") freopen(output_file.c_str(), "w", stdout); } /*|||*/ std::chrono::_V2::system_clock::time_point get_time() { return chrono::high_resolution_clock::now(); } ld get_duration(std::chrono::_V2::system_clock::time_point start_time, std::chrono::_V2::system_clock::time_point end_time) { return ld(chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count()) / 1e9; } /*|||*/ ll sign(ld x) { return (x > 0) - (x < 0); } ll pow(ll x, ll exp, ll mod) { ll res = 1, y = x; while(exp) { if(exp & 1) res = (res * y) % mod; y = (y * y) % mod; exp >>= 1; } return res; } /*|||*/ auto START_TIME = get_time(); const long long INF = LONG_LONG_MAX / 3; char newl = '\n'; // clang-format on ll f(vector<set<ll>> &E, vb &D, queue<ll> &q, ll &K) { ll res = 0; while(!q.empty()) { ll curr = q.front(); q.pop(); D[curr] = false; res++; for(ll nex : E[curr]) { if(!D[nex]) continue; E[nex].erase(curr); if(len(E[nex]) >= K) continue; q.push(nex); D[nex] = false; } E[curr].clear(); } return res; } yeee main() { setup_io(); // ll N, M, K; cin >> N >> M >> K; vll res(M + 1, 0); vpll EI(M); vector<set<ll>> E(N); FOR(i, 0, M) { ll a, b; cin >> a >> b; a--, b--; EI[i].first = a; EI[i].second = b; E[a].insert(b); E[b].insert(a); } vb D(N, true); queue<ll> q; FOR(i, 0, N) { if(len(E[i]) < K) q.push(i); } res[0] = N - (q.empty() ? 0 : f(E, D, q, K)); reverse(all(EI)); FOR(Mi, 0, M) { vll v(2); tie(v[0], v[1]) = EI[Mi]; res[Mi + 1] = res[Mi]; FOR(i, 0, 2) E[v[i]].erase(v[!i]); FOR(i, 0, 2) { if(!D[v[i]]) continue; if(len(E[v[i]]) >= K) continue; q.push(v[i]); res[Mi + 1] -= f(E, D, q, K); } } reverse(all(res)); FOR(i, 1, M + 1) cout << res[i] << newl; }
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) ){ 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); ma[y] = G[y].size(); } else if(ma[y] <= k){ ma.erase(y);remove_node(y); ma[x] = G[x].size(); } else{ ma[x]--; ma[y]--; G[x].erase(y); G[y].erase(x); } } ans[i] = ma.size(); } if(!ma.empty()) cerr<<ma.begin()->first<<" "<<ma.begin()->second<<"\n"; 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; const int N = 3e5 + 7; const long long INF = 1e18 + 7; pair<int, int> a[N]; int d[N], ans[N]; int n, m, k; set<pair<int, int> > s; vector<pair<int, int> > g[N]; int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { scanf("%d%d", &a[i].first, &a[i].second); g[a[i].first].push_back(make_pair(a[i].second, i)); g[a[i].second].push_back(make_pair(a[i].first, i)); } for (int i = 1; i <= n; i++) { d[i] = g[i].size(); s.insert(make_pair(d[i], i)); } for (int i = m; i >= 1; i--) { while (!s.empty()) { set<pair<int, int> >::iterator it = s.begin(); pair<int, int> v = *it; cout << v.first << ' ' << v.second << endl; if (v.first < k) { s.erase(v); for (pair<int, int> to : g[v.second]) { if (to.second > i) break; s.erase(make_pair(d[to.first], to.first)); d[to.first]--; if (d[to.first] >= k) s.insert(make_pair(d[to.first], to.first)); } } else break; } ans[i] = s.size(); if (d[a[i].first] >= k && d[a[i].second] >= k) { s.erase(make_pair(d[a[i].first], a[i].first)); d[a[i].first]--; s.insert(make_pair(d[a[i].first], a[i].first)); s.erase(make_pair(d[a[i].second], a[i].second)); d[a[i].second]--; s.insert(make_pair(d[a[i].second], a[i].second)); } } 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; map<int, int> m[200010]; int s[200010]; int main() { int n, a, b, i, j; cin >> n; for (i = 0; i < n - 1; i++) { scanf("%d%d", &a, &b); m[a][b] = 1; m[b][a] = 1; } for (i = 0; i < n; i++) scanf("%d", &s[i]); for (i = 0, j = 1; i < n; i++) while (m[s[i]][s[j]]) j++; if (j == n && s[0] == 1) cout << "Yes"; else cout << "No"; }
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 vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using ii = pair<int, int>; using lu = unsigned long long; using l = long long; using vs = vector<string>; using vii = vector<ii>; using vl = vector<l>; using vvl = vector<vl>; using vvvl = vector<vvl>; using ll = pair<l, l>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vvb = vector<vb>; using vd = vector<double>; using vvd = vector<vd>; using mll = unordered_map<l, l>; const l INF = numeric_limits<l>::max(); const double EPS = 1e-10; static constexpr auto PI = 3.1415926; const l e0 = 1, e3 = 1000, e5 = 100000, e6 = 10 * e5, e7 = 10 * e6, e8 = 10 * e7, e9 = 10 * e8; const char lf = '\n'; const l MOD = e9 + 7; struct Edge { l to; l from; l id; }; l MAXC = 1000; struct Graph { l v, e; vector<vector<Edge>> adj; vvl counter; l k; l good; Graph(l n, l k) : v(n), e(0), k(k), good(0) { adj.resize(v); counter.resize(v, vl(MAXC)); } l add_node() { adj.resize(++v); return v - 1; } void add_simple(l a, l b) { Edge ab; ab.to = b; adj[a].emplace_back(ab); e++; } void add_undirected(l a, l b) { Edge ab; ab.id = e; ab.from = a; ab.to = b; adj[a].emplace_back(ab); Edge ba; ba.id = e; ba.from = b; ba.to = a; adj[b].emplace_back(ba); e++; for (l i = l(0); i < l(MAXC - 1); i++) if (counter[a][i] == k) inc(b, i + 1); for (l i = l(0); i < l(MAXC - 1); i++) if (counter[b][i] == k) inc(a, i + 1); inc(a, 0); inc(b, 0); } void inc(l a, int j) { if (counter[a][j] == k) return; counter[a][j]++; if (counter[a][j] != k) return; if (j + 1 == MAXC) { good++; } else { for (auto edge : adj[a]) inc(edge.to, j + 1); } } void add_directed(l a, l b) { Edge ab; ab.id = e; ab.from = a; ab.to = b; adj[a].emplace_back(ab); e++; } }; void solve(istream& in, ostream& out) { l n, m, k; in >> n >> m >> k; MAXC = e5 / n; (MAXC); Graph g(n, k); for (l i = l(0); i < l(m); i++) { l a, b; in >> a >> b; a--; b--; g.add_undirected(a, b); out << g.good << lf; } } int main(int argc, char** argv) { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(cin, cout); }
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
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+1]; 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> using namespace std; int n, m, k, ANS[200005]; set<int> v[200005]; pair<int, int> q[200005]; set<int> s; void remove(int u) { if (v[u].size() < k && s.erase(u)) for (auto &i : v[u]) { v[i].erase(u); remove(i); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int d = 1; d <= m; ++d) { cin >> q[d].first >> q[d].second; v[q[d].first].insert(q[d].second); v[q[d].second].insert(q[d].first); } for (int d = m, x, y; d; --d) { ANS[d] = s.size(); tie(x, y) = q[d]; v[x].erase(y); v[y].erase(x); remove(x); remove(y); } for (int i = 1; i <= m; 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; int u[400100], v[400100], fir[200100], nxt[400100], cnt = 0, n, m, k; int dis[200100], ans = 0; void addedge(int ui, int vi) { ++cnt; u[cnt] = ui; v[cnt] = vi; nxt[cnt] = fir[ui]; fir[ui] = cnt; } void dfs(int u) { for (int i = fir[u]; i; i = nxt[i]) { if (!dis[v[i]]) { dis[v[i]] = dis[u] + 1; dfs(v[i]); } else { ans = max(ans, abs(dis[u] - dis[v[i]]) + 1); } } } void findmax(void) { memset(dis, 0, sizeof(dis)); ans = 1; for (int i = 1; i <= n; i++) if (!dis[i]) dfs(i); } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { int a, b; scanf("%d %d", &a, &b); addedge(a, b); addedge(b, a); findmax(); if (ans > k) { printf("%d\n", ans); } 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; int res[200010], x[200010], y[200010]; vector<int> v; set<int> a[200010]; bool vis[200010]; int main() { int n, m, k; while (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]); } int ans = n; for (int i = 1; i <= n; ++i) { if (a[i].size() < k) { v.push_back(i); vis[i] = 1; --ans; } } int tmp; for (int h = 0; h < v.size(); ++h) { tmp = v[h]; for (auto it : a[tmp]) { if (a[it].find(tmp) != a[it].end()) a[it].erase(a[it].find(tmp)); if (a[it].size() < k && !vis[it]) { v.push_back(it); vis[it] = 1; --ans; } } a[tmp].clear(); } v.clear(); res[m] = ans; for (int i = m; i > 1; --i) { if (a[x[i]].find(y[i]) != a[x[i]].end()) a[x[i]].erase(a[x[i]].find(y[i])); if (a[y[i]].find(x[i]) != a[y[i]].end()) a[y[i]].erase(a[y[i]].find(x[i])); if (!vis[x[i]] && a[x[i]].size() < k) { v.push_back(x[i]); vis[x[i]] = 1; --ans; } if (!vis[y[i]] && a[y[i]].size() < k) { v.push_back(y[i]); vis[y[i]] = 1; --ans; } while (v.size()) { tmp = v.back(); for (auto it : a[tmp]) { a[it].erase(a[it].find(tmp)); if (a[it].size() < k && !vis[it]) { v.push_back(it); vis[it] = 1; --ans; } } a[tmp].clear(); v.pop_back(); } res[i - 1] = ans; } for (int i = 1; i <= m; ++i) { cout << res[i] << endl; } } return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T, typename Pr = less<T>> using pq = priority_queue<T, vector<T>, Pr>; using i64 = long long int; using ii = pair<int, int>; using ii64 = pair<i64, i64>; vector<int> edge[200005]; int deg[200005]; int degk[200005]; int degkk[200005]; int ans = 0; int k; void addkk(int x) { degkk[x]++; if (degkk[x] == k) ans++; } void addk(int x) { degk[x]++; if (degk[x] == k) for (auto& e : edge[x]) addkk(e); } void add(int x) { deg[x]++; if (deg[x] == k) for (auto& e : edge[x]) addk(e); } void merge(int x, int y) { edge[x].push_back(y); edge[y].push_back(x); if (degk[y] >= k) addkk(x); if (degk[x] >= k) addkk(y); if (deg[y] >= k) addk(x); if (deg[x] >= k) addk(y); add(x); add(y); } int main() { int n, m; scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); merge(x, y); printf("%d\n", ans); } 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[x[i]].find(y[i]) != a[x[i]].end() && bac[x[i]] && bac[y[i]]) { S.erase(make_pair(bac[x[i]], x[i])); bac[x[i]]--; if (bac[x[i]] > 0) S.insert(make_pair(bac[x[i]], x[i])); S.erase(make_pair(bac[y[i]], y[i])); bac[y[i]]--; if (bac[y[i]] > 0) S.insert(make_pair(bac[y[i]], y[i])); a[x[i]].erase(y[i]); a[y[i]].erase(x[i]); } } for (int i = 1; i <= m; i++) cout << res[i] << '\n'; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
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 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-1] = 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 print(str(ans)[1:-1].replace(' ', '').replace(',', '\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 = 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 main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; 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); } int res = 0; 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) { int numGoing = 0; for (int to : adj[u]) { if (adjNew[to].count(u)) { cnt[to]++; if (cnt[to] >= k) { numGoing++; } } } if (numGoing >= k) { if (!going[u]) { res++; going[u] = true; } for (int to : adj[u]) { if (!going[to] && cnt[to] >= k) { res++; going[to] = true; } } } } if (degree[v] == k) { int numGoing = 0; for (int to : adj[v]) { if (adjNew[to].count(v)) { cnt[to]++; if (cnt[to] >= k) { numGoing++; } } } if (numGoing >= k) { if (!going[v]) { res++; going[v] = true; } for (int to : adj[v]) { if (!going[to] && cnt[to] >= k) { res++; going[to] = true; } } } } 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> #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++; 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
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])] || is[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 N, M, K; int X[200001], Y[200001]; vector<int> adj[200001]; set<int> adj2[200001]; int goadj[200001]; bool going[200001]; set<pair<int, int>> st; int ans = 0; vector<int> ants; void dfs(int node) { going[node] = false; queue<int> todo; ans--; for (set<int>::iterator it = adj2[node].begin(); it != adj2[node].end(); it++) { goadj[*it]--; adj2[*it].erase(node); if (going[*it] && goadj[*it] < K) { todo.push(*it); } } adj2[node].clear(); while (!todo.empty()) { if (going[todo.front()] && goadj[todo.front()] < K) dfs(todo.front()); todo.pop(); } } int main() { scanf("%d%d%d", &N, &M, &K); for (int i = 0; i < M; i++) { scanf("%d%d", &X[i], &Y[i]); goadj[X[i]]++; goadj[Y[i]]++; adj[X[i]].push_back(Y[i]); adj[Y[i]].push_back(X[i]); } for (int i = 1; i <= N; i++) st.insert({goadj[i], i}); while (st.size() >= 1) { int x = st.begin()->first; int y = st.begin()->second; if (x >= K) break; st.erase(*st.begin()); for (int i = 0; i < adj[y].size(); i++) { goadj[adj[y][i]]--; if (st.count({goadj[adj[y][i]] + 1, adj[y][i]}) == 1) { st.erase({goadj[adj[y][i]] + 1, adj[y][i]}); st.insert({goadj[adj[y][i]], adj[y][i]}); } } } for (set<pair<int, int>>::iterator it = st.begin(); it != st.end(); it++) { going[it->second] = true; ans++; } for (int i = 0; i < M; i++) if (going[X[i]] && going[Y[i]]) { adj2[X[i]].insert(Y[i]); adj2[Y[i]].insert(X[i]); } ants.push_back(ans); for (int i = M - 1; i > 0; i--) { if (going[X[i]] && going[Y[i]]) { goadj[X[i]]--; goadj[Y[i]]--; adj2[X[i]].erase(Y[i]); adj2[Y[i]].erase(X[i]); if (goadj[X[i]] == K - 1) dfs(X[i]); if (goadj[Y[i]] == K - 1) dfs(Y[i]); } ants.push_back(ans); } for (int i = ants.size() - 1; i >= 0; i--) cout << ants[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.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Array; import java.nio.charset.Charset; import java.util.*; public class CFContest { public static void main(String[] args) throws Exception { boolean local = System.getProperty("ONLINE_JUDGE") == null; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("E:\\DATABASE\\TESTCASE\\CFContest.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; public Task(FastIO io) { this.io = io; } @Override public void run() { solve(); } public void solve() { int n = io.readInt(); Node[] nodes = new Node[n + 1]; for (int i = 0; i <= n; i++) { nodes[i] = new Node(); nodes[i].id = i; } for (int i = 1; i < n; i++) { Node a = nodes[io.readInt()]; Node b = nodes[io.readInt()]; a.set.add(b); b.set.add(a); } int[] seq = new int[n + 1]; for (int i = 1; i <= n; i++) { int v = io.readInt(); seq[i] = v; if (v <= 0 || v > n) { io.cache.append("No"); return; } if (nodes[v].seq != -1) { io.cache.append("No"); return; } nodes[v].seq = i; } if(seq[1] != 1) { io.cache.append("No"); return; } Node root = nodes[seq[1]]; Deque<Node> deque = new ArrayDeque<>(n); deque.add(root); int offset = 2; while (!deque.isEmpty()) { Node head = deque.removeFirst(); int allowRange = offset + head.set.size(); for (; offset < allowRange; offset++) { Node next = nodes[seq[offset]]; if (!head.set.contains(next)) { io.cache.append("No"); return; } next.set.remove(head); deque.add(next); } } io.cache.append("Yes"); return; } } public static class Randomized { static Random random = new Random(); public static double nextDouble(double min, double max) { return random.nextDouble() * (max - min) + min; } public static void randomizedArray(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(long[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); long tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(double[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); double tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static void randomizedArray(float[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); float tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static <T> void randomizedArray(T[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); T tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } public static class Node { Set<Node> set = new HashSet<>(); int seq = -1; int id; @Override public String toString() { return "" + id; } } public static class FastIO { private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 8); public final StringBuilder cache = new StringBuilder(); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { long num = readLong(); if (next != '.') { return num; } next = read(); long divisor = 1; long later = 0; while (next >= '0' && next <= '9') { divisor = divisor * 10; later = later * 10 + next - '0'; next = read(); } if (num >= 0) { return num + (later / (double) divisor); } else { return num - (later / (double) divisor); } } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public void flush() { try { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } catch (IOException e) { throw new RuntimeException(e); } } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Memory { public static <T> void swap(T[] data, int i, int j) { T tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> void swap(int[] data, int i, int j) { int tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> void swap(char[] data, int i, int j) { char tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> int min(T[] data, int from, int to, Comparator<T> cmp) { int m = from; for (int i = from + 1; i < to; i++) { if (cmp.compare(data[m], data[i]) > 0) { m = i; } } return m; } public static <T> void move(T[] data, int from, int to, int step) { int len = to - from; step = len - (step % len + len) % len; Object[] buf = new Object[len]; for (int i = 0; i < len; i++) { buf[i] = data[(i + step) % len + from]; } System.arraycopy(buf, 0, data, from, len); } public static <T> void reverse(T[] data, int f, int t) { int l = f, r = t - 1; while (l < r) { swap(data, l, r); l++; r--; } } public static void copy(Object[] src, Object[] dst, int srcf, int dstf, int len) { if (len < 8) { for (int i = 0; i < len; i++) { dst[dstf + i] = src[srcf + i]; } } else { System.arraycopy(src, srcf, dst, dstf, len); } } } }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long const MAXN = 200005; vector<long long> v[MAXN]; set<long long> g[MAXN]; long long deg[MAXN]; set<pair<long long, long long>> st; pair<long long, long long> edge[MAXN]; long long ans[MAXN]; bool used[MAXN]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, k; cin >> n >> m >> k; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; a--, b--; edge[i] = make_pair(a, b); g[a].insert(b); g[b].insert(a); deg[a]++, deg[b]++; } if ((double)clock() / CLOCKS_PER_SEC > 0.01) { return 0; } for (long long i = 0; i < n; i++) { st.insert(make_pair(deg[i], i)); used[i] = 1; } while ((*(st.begin())).first < k) { long long a = (*(st.begin())).second; st.erase(st.begin()); used[a] = 0; deg[a]--; for (auto v : g[a]) { st.erase(make_pair(deg[v], v)); deg[v]--; st.insert(make_pair(deg[v], v)); g[v].erase(a); } } for (long long i = m - 1; i >= 0; i--) { if (st.size() == 0) break; ans[i] = st.size(); long long a = edge[i].first; long long b = edge[i].second; if (used[a] && used[b]) { st.erase(make_pair(deg[a], a)); deg[a]--; st.insert(make_pair(deg[a], a)); st.erase(make_pair(deg[b], b)); deg[b]--; st.insert(make_pair(deg[b], b)); g[a].erase(b); g[b].erase(a); while (st.size() && (*(st.begin())).first < k) { long long c = (*(st.begin())).second; st.erase(st.begin()); used[c] = 0; deg[c]--; for (auto v : g[c]) { st.erase(make_pair(deg[v], v)); deg[v]--; st.insert(make_pair(deg[v], v)); g[v].erase(c); } } } } for (long long 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
cpp
#include <bits/stdc++.h> using namespace std; int toNumber(string s) { int Number; if (!(istringstream(s) >> Number)) Number = 0; return Number; } string toString(int number) { ostringstream ostr; ostr << number; return ostr.str(); } long long ele(long long a, long long b) { if (b == 0) return 1; if (b % 2 == 0) { return ele((a * a), b / 2); } else { return a * ele((a * a), b / 2); } } long long mcd(long long a, long long b) { if (a == 0) return b; return mcd(b % a, a); } double d_abs(long a, long b) { if (a > b) { return a - b; } return b - a; } bool isPal(string x) { for (int i = 0; i < (int)(x.size()); i++) { if (x[i] != x[x.size() - i - 1]) return false; } return true; } vector<vector<int> > gr(200003); set<int> pueden; vector<pair<int, int> > aristas; int n, k; vector<bool> mire(200003, false); vector<int> cant(200003, 0); queue<int> q; void calc() { while (!q.empty()) { int t = q.front(); q.pop(); pueden.erase(t); for (int j = 0; j < (int)(gr[t].size()); j++) { if (pueden.find(gr[t][j]) == pueden.end()) continue; if (mire[gr[t][j]]) continue; cant[gr[t][j]]--; if (cant[gr[t][j]] < k) { mire[gr[t][j]] = true; q.push(gr[t][j]); } } } } void sacarArista(int u, int v) { gr[u].pop_back(); gr[v].pop_back(); cant[u]--; cant[v]--; if (pueden.find(u) == pueden.end() || pueden.find(v) == pueden.end()) return; if (cant[u] < k) { q.push(u); mire[u] = true; } if (cant[v] < k) { q.push(v); mire[v] = true; } calc(); } void solve(int caso) { int m; cin >> n >> m >> k; for (int i = 0; i < (int)(m); i++) { int a, b; cin >> a >> b; a--; b--; cant[a]++; cant[b]++; gr[a].push_back(b); gr[b].push_back(a); aristas.push_back(make_pair(a, b)); } for (int i = 0; i < (int)(n); i++) { pueden.insert(i); if (cant[i] < k) { q.push(i); mire[i] = true; } } calc(); vector<int> ans(m); ans[m - 1] = pueden.size(); for (int i = 0; i < (int)(m - 1); i++) { pair<int, int> uv = aristas[m - 1 - i]; sacarArista(uv.first, uv.second); ans[m - 2 - i] = pueden.size(); } for (int i = 0; i < (int)(m); i++) cout << ans[i] << "\n"; } int main() { int T; T = 1; for (int caso = 0; caso < (int)(T); caso++) { solve(caso); } }
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; struct P { int x, y, z; bool operator<(const P &a) const { return x < a.x; } }; vector<int> v[2]; int a, c, i, b, n, m, k, d; int o[222111]; int l[111]; int j[11111]; int e; int dx[10] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[10] = {1, 0, -1, 0, 1, -1, 1, -1}, dz[10] = {0, 0, 0, 0, 1, -1}; long long x, y, mod = 998244353, mod2 = 1000000009, mod3 = 2017; long long z; double pi = 3.14159265; P u[211111]; stack<int> s[222222], s1; queue<int> q; char r[1113]; bool as(P a, P b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; } int main() { scanf("%d %d %d", &a, &b, &c); for (int t = 1; t <= b; t++) { scanf("%d %d", &u[t].x, &u[t].y); s[u[t].x].push(u[t].y); s[u[t].y].push(u[t].x); o[u[t].x]++; o[u[t].y]++; } k = a; for (int t = 1; t <= a; t++) if (o[t] < c) q.push(t); for (; q.size(); k--, q.pop()) { for (; s[q.front()].size(); s[q.front()].pop()) { o[s[q.front()].top()]--; if (o[s[q.front()].top()] < c && o[s[q.front()].top()] >= 0) { q.push(s[q.front()].top()); o[s[q.front()].top()] = -1; } } } s1.push(k); for (int t = b; t > 1; t--) { if (s[u[t].x].size() && s[u[t].x].top() == u[t].y && s[u[t].y].size() && s[u[t].y].top() == u[t].x) { o[u[t].x]--; o[u[t].y]--; s[u[t].x].pop(); s[u[t].y].pop(); if (o[u[t].x] == c - 1) q.push(u[t].x); if (o[u[t].y] == c - 1) q.push(u[t].y); for (; q.size(); k--, q.pop()) { for (; s[q.front()].size(); s[q.front()].pop()) { o[s[q.front()].top()]--; if (o[s[q.front()].top()] == c - 1) q.push(s[q.front()].top()); } } } else { if (s[u[t].x].size() && s[u[t].x].top() == u[t].y) s[u[t].x].pop(); if (s[u[t].y].size() && s[u[t].y].top() == u[t].x) s[u[t].y].pop(); } s1.push(k); } for (; s1.size(); s1.pop()) printf("%d\n", s1.top()); }
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; vector<pair<int, int> > e; set<pair<int, int> > f; vector<int> adj[MAXN]; int n, m, k, deg[MAXN], num; bool kicked[MAXN]; vector<int> ans; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; ++i) { int u, v; scanf("%d %d", &u, &v); e.push_back(pair<int, int>(u, v)); f.insert(pair<int, int>(u, v)); adj[u].push_back(v); adj[v].push_back(u); deg[u]++; deg[v]++; } num = n; queue<int> q; for (int i = 1; i <= n; ++i) { if (deg[i] < k) { q.push(i); } } while (!q.empty()) { int top = q.front(); q.pop(); if (kicked[top]) continue; kicked[top] = true; num--; for (int j : adj[top]) if (f.count(pair<int, int>(top, j)) + f.count(pair<int, int>(j, top)) == 1) { deg[j]--; f.erase(pair<int, int>(j, top)); f.erase(pair<int, int>(top, j)); if (deg[j] < k) { printf("Unfriending %d %d\n", top, j); q.push(j); } } } reverse(e.begin(), e.end()); for (pair<int, int> i : e) { ans.push_back(num); if (f.count(i) == 0) continue; deg[i.first]--; deg[i.second]--; printf("Unfriend %d %d\n", i.first, i.second); f.erase(i); queue<int> q; if (deg[i.first] < k && !kicked[i.first]) q.push(i.first); if (deg[i.second] < k && !kicked[i.second]) q.push(i.second); while (!q.empty()) { int top = q.front(); q.pop(); if (kicked[top]) continue; kicked[top] = true; num--; for (int j : adj[top]) if (f.count(pair<int, int>(top, j)) + f.count(pair<int, int>(j, top)) == 1) { deg[j]--; f.erase(pair<int, int>(j, top)); f.erase(pair<int, int>(top, j)); if (deg[j] < k) { printf("Unfriending %d %d\n", top, j); q.push(j); } } } } reverse(ans.begin(), ans.end()); for (int i : ans) printf("%d\n", i); }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int k = 1, sum = 0; char c = getchar(); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') k = -1; for (; c >= '0' && c <= '9'; c = getchar()) sum = sum * 10 + c - '0'; return sum * k; } int n, m, k; int in[200000 + 10]; set<int> son[200000 + 10]; int ans[200000 + 10]; int tmp; bool del[200000 + 10]; int x[200000 + 10], y[200000 + 10]; inline void Delete(int x) { if (in[x] >= k || del[x]) return; queue<int> Q; Q.push(x); del[x] = 1; --tmp; while (!Q.empty()) { int t = Q.front(); del[t] = 1; Q.pop(); for (register set<int>::iterator it = son[t].begin(); it != son[t].end(); ++it) { int y = *it; --in[y]; if (in[y] < k && !del[y]) { Q.push(y); --tmp; } } } } int main() { n = read(); m = read(); k = read(); for (register int i = 0; i < m; ++i) { x[i] = read(), y[i] = read(); in[x[i]]++; in[y[i]]++; son[x[i]].insert(y[i]); son[y[i]].insert(x[i]); } tmp = n; for (register int i = 1; i <= n; ++i) Delete(i); ans[m] = tmp; for (register int i = m - 1; i >= 0; --i) { if (!del[y[i]]) --in[x[i]]; if (!del[x[i]]) --in[y[i]]; son[x[i]].erase(y[i]); son[y[i]].erase(x[i]); Delete(x[i]); Delete(y[i]); ans[i] = tmp; } for (register 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; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, k; cin >> n >> m >> k; vector<vector<pair<long long, long long> > > friends(n); vector<long long> pointers(n); for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; friends[a - 1].push_back(make_pair(b - 1, i)); friends[b - 1].push_back(make_pair(a - 1, i)); } queue<long long> q; set<pair<long long, long long> > good; for (long long i = 0; i < n; i++) { if (friends[i].size() >= k) { good.insert(make_pair(friends[i][k - 1].second, i)); pointers[i] = k - 1; } else { pointers[i] = -1; q.push(i); } } long long u = m; vector<long long> ans(m); fill(ans.begin(), ans.end(), 0); while (good.size()) { while (q.size()) { long long V = q.front(); q.pop(); for (long long i = 0; i < friends[V].size(); i++) { long long to = friends[V][i].first, tm = friends[V][i].second; if (pointers[to] == -1) continue; long long T = friends[to][pointers[to]].second; if (tm > T) continue; pointers[to]++; while (pointers[to] < friends[to].size()) { long long K = friends[to][pointers[to]].first; if (pointers[K] == -1) pointers[to]++; else break; } if (pointers[to] >= friends[to].size()) { good.erase(good.find(make_pair(T, to))); pointers[to] = -1; q.push(to); } else { good.erase(good.find(make_pair(T, to))); T = friends[to][pointers[to]].second; good.insert(make_pair(T, to)); } } } long long S = good.size(); if (S == 0) break; set<pair<long long, long long> >::iterator it = good.end(); it--; pair<long long, long long> P = *it; long long tt = P.first; for (long long i = tt; i < u; i++) ans[i] = S; u = tt; while (good.size()) { set<pair<long long, long long> >::iterator it = good.end(); it--; pair<long long, long long> P = *it; long long ttt = P.first; if (tt != ttt) { break; } good.erase(it); pointers[P.second] = -1; q.push(P.second); } } for (long long 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 maxn = 1e5 + 10; set<pair<int, int> > sq; set<int> to[maxn]; int n, m, k; int a[maxn]; struct node { int l, r; } q[maxn]; void update() { while (sq.size() > 0 && (*(sq.begin())).first < k) { auto x = *(sq.begin()); sq.erase(sq.begin()); for (auto j = to[x.second].begin(); j != to[x.second].end();) { auto value = *j; if (sq.count({a[value], value})) { sq.erase(sq.find({a[value], value})); a[value]--; if (a[value] > 0) sq.insert({a[value], value}); to[x.second].erase(j++); } else j++; } } } int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int from, t; cin >> from >> t; q[i].l = from, q[i].r = t; to[from].insert(t); to[t].insert(from); a[from]++, a[t]++; } for (int i = 1; i <= n; i++) sq.insert({a[i], i}); vector<int> ans; for (int i = m; i >= 1; i--) { update(); ans.push_back(sq.size()); if (sq.count({a[q[i].l], q[i].l}) && sq.count({a[q[i].r], q[i].r})) { sq.erase(sq.find({a[q[i].l], q[i].l})); sq.erase(sq.find({a[q[i].r], q[i].r})); a[q[i].l]--, a[q[i].r]--; if (a[q[i].l] > 0) sq.insert({a[q[i].l], q[i].l}); if (a[q[i].r] > 0) sq.insert({a[q[i].r], q[i].r}); to[q[i].l].erase(q[i].r); to[q[i].r].erase(q[i].l); } } reverse(ans.begin(), ans.end()); for (auto &i : ans) { cout << i << endl; } return 0; }
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> > G; set<pair<int, int> > S; stack<int> results; vector<pair<int, int> > E; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; G.resize(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; E.push_back(make_pair(u, v)); G[u].push_back(v); G[v].push_back(u); } vector<set<pair<int, int> >::iterator> iterators(n); for (int i = 0; i < n; i++) { pair<set<pair<int, int> >::iterator, bool> ret = S.insert(make_pair(G[i].size(), i)); iterators[i] = ret.first; } for (int i = m - 1; i >= 0; i--) { set<pair<int, int> >::iterator it = S.begin(); int dv = it->first, v = it->second; while (S.size() > 0 && dv < k) { S.erase(it); for (int i = 0; i < G[v].size(); i++) { set<pair<int, int> >::iterator neighbor = iterators[G[v][i]]; pair<int, int> p = make_pair(neighbor->first - 1, neighbor->second); pair<int, int> orig = make_pair(neighbor->first, neighbor->second); int neighbor_index = neighbor->second; if (S.count(orig) > 0) { S.erase(neighbor); pair<set<pair<int, int> >::iterator, bool> ret = S.insert(p); iterators[neighbor_index] = ret.first; } } if (S.size() > 0) { it = S.begin(); dv = it->first, v = it->second; } } results.push(S.size()); pair<int, int> edge = E[i]; vector<int>::iterator pos; for (pos = G[edge.first].begin(); pos != G[edge.first].end(); pos++) if ((*pos) == edge.second) break; G[edge.first].erase(pos); for (pos = G[edge.second].begin(); pos != G[edge.second].end(); pos++) if ((*pos) == edge.first) break; G[edge.second].erase(pos); set<pair<int, int> >::iterator it1, it2; it1 = iterators[edge.first]; it2 = iterators[edge.second]; pair<int, int> p1 = make_pair(it1->first, it1->second); pair<int, int> p2 = make_pair(it2->first, it2->second); if (S.count(p1) > 0 && S.count(p2) > 0) { S.erase(it1); S.erase(it2); pair<set<pair<int, int> >::iterator, bool> ret1 = S.insert(make_pair(p1.first - 1, p1.second)); pair<set<pair<int, int> >::iterator, bool> ret2 = S.insert(make_pair(p2.first - 1, p2.second)); iterators[p1.second] = ret1.first; iterators[p2.second] = ret2.first; } } while (!results.empty()) { cout << results.top() << endl; results.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
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; struct edge { int u, v, nxt; } e[200005 * 2]; int cnt, head[200005]; void adde(int u, int v) { e[++cnt].u = u; e[cnt].v = v; e[cnt].nxt = head[u]; head[u] = cnt; } int vis[200005 * 2], d[200005], ans[200005], res; struct ege2 { int u, v; } edge[200005]; queue<int> q; void solve() { while (!q.empty()) { int u = q.front(); q.pop(); d[u] = 0; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; if (vis[i]) continue; vis[i] = vis[i ^ 1] = 1; d[v]--; if (d[v] == k - 1) { --res; q.push(v); } } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; ++i) { scanf("%d%d", &edge[i].u, &edge[i].v); adde(edge[i].u, edge[i].v); adde(edge[i].v, edge[i].u); d[edge[i].u]++; d[edge[i].v]++; } res = n; for (int i = 1; i <= n; ++i) if (d[i] < k) { --res; q.push(i); } solve(); ans[m] = res; for (int i = m; i >= 2; --i) { if (vis[i << 1]) { ans[i - 1] = ans[i]; continue; } vis[i << 1] = vis[i << 1 | 1] = 1; int u = edge[i].u, v = edge[i].v; d[u]--; d[v]--; printf("%d %d ", u, v); if (d[u] == k - 1) { --res; q.push(u); } if (d[v] == k - 1) { --res; q.push(v); } solve(); ans[i - 1] = res; } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
1037_E. Trips
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
{ "input": [ "4 4 2\n2 3\n1 2\n1 3\n1 4\n", "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n", "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" ], "output": [ "0\n0\n3\n3\n", "0\n0\n0\n3\n3\n4\n4\n5\n", "0\n0\n0\n0\n3\n4\n4\n" ] }
{ "input": [ "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n", "2 1 1\n2 1\n" ], "output": [ "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n", "2\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, k; vector<int> used; vector<int> selected; vector<vector<int>> g; bool dfs(int cur) { 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; } return cnt > k; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k; g.resize(n); 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); selected.assign(n, 0); dfs(0); 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> using namespace std; int n, m, d[200005], k, res, vs[200005], ans[200005], pa; map<int, int> cnt[200005]; vector<int> ke[200005]; pair<int, int> a[200005]; void dfs(int u) { d[u]--; res--; vs[u] = 1; for (int v : ke[u]) if (vs[v] == 0) { cnt[u][v] = cnt[v][u] = 1; d[v]--; if (d[v] < k) { d[v]++; dfs(v); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m >> k; res = n; for (int i = 1; i <= m; i++) { cin >> a[i].first >> a[i].second; d[a[i].first]++; d[a[i].second]++; ke[a[i].first].push_back(a[i].second); ke[a[i].second].push_back(a[i].first); } for (int i = 1; i <= n; i++) if (d[i] < k && vs[i] == 0) dfs(i); for (int i = m; i >= 1; i--) { ans[i] = res; 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] << '\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 = 212345; int n, m, k; vector<int> g[N]; set<pair<int, int> > st; int deg[N]; int main(void) { ios_base::sync_with_stdio(false); cin >> n >> m >> k; vector<pair<int, int> > edg; set<pair<int, int> > act; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].emplace_back(b); g[b].emplace_back(a); edg.emplace_back(a, b); act.emplace(min(a, b), max(a, b)); } for (int i = 1; i <= n; i++) { st.emplace(g[i].size(), i); deg[i] = g[i].size(); } vector<int> ans; for (int i = m - 1; i >= 0; i--) { while (st.size() and (*st.begin()).first < k) { int d, u; tie(d, u) = *st.begin(); st.erase(st.begin()); deg[u] = 0; for (int v : g[u]) { if (!act.count(pair<int, int>(min(u, v), max(u, v)))) continue; act.erase(pair<int, int>(min(u, v), max(u, v))); st.erase(pair<int, int>(deg[v], v)); deg[v]--; st.emplace(pair<int, int>(deg[v], v)); } } int a, b; tie(a, b) = edg[i]; ans.emplace_back(st.size()); if (act.count(pair<int, int>(a, b))) { act.erase(pair<int, int>(a, b)); st.erase(pair<int, int>(deg[a], a)); deg[a]--; st.emplace(pair<int, int>(deg[a], a)); st.erase(pair<int, int>(deg[b], b)); deg[b]--; st.emplace(pair<int, int>(deg[b], b)); } } 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> 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) { 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 (inTrip[p.first] && deg[p.first] < k) toRemove.push(p.first); if (inTrip[p.second] && 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; }
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[200001], y[200001]; vector<int> edge[200001]; int vis[200001]; int count_col[200001]; int ans[200001]; int count_edge[200001]; void dfs(int u, int c) { vis[u] = c; count_col[c]++; int i, j = edge[u].size(), v; for (i = 0; i < j; i++) { v = edge[u][i]; if (vis[v] == -1) { dfs(v, c); } } } set<pair<int, int> > max_c; set<pair<int, int> > k_node; set<pair<int, int> > done; void remove(int k) { set<pair<int, int> >::iterator it, it2; it = k_node.begin(); while (it != k_node.end()) { pair<int, int> pp = *it; if (pp.first >= k) break; int n_col = vis[pp.second]; pair<int, int> p = make_pair(-count_col[n_col], n_col); it2 = max_c.find(p); if (it2 != max_c.end()) { max_c.erase(it2); p.first++; count_col[n_col]--; max_c.insert(p); } k_node.erase(it); int u = pp.second; for (int i = 0; i < edge[u].size(); i++) { int v = edge[u][i]; if (done.count(make_pair(min(u, v), max(u, v))) > 0) { continue; } p = make_pair(count_edge[v], v); it = k_node.find(p); if (it != k_node.end()) { k_node.erase(it); p.first--; count_edge[v]--; k_node.insert(p); } done.insert(make_pair(min(u, v), max(u, v))); } it = k_node.begin(); } } int main() { int n; int m; int k; memset(count_col, 0, sizeof(count_col)); memset(vis, -1, sizeof(vis)); memset(ans, 0, sizeof(ans)); memset(count_edge, 0, sizeof(count_edge)); scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%d %d", &x[i], &y[i]); edge[x[i]].push_back(y[i]); edge[y[i]].push_back(x[i]); count_edge[x[i]]++; count_edge[y[i]]++; } int col = 0; for (int i = 1; i <= n; i++) { if (vis[i] == -1) { dfs(i, col); col++; } } for (int i = 1; i <= n; i++) { k_node.insert(make_pair(count_edge[i], i)); } for (int i = 0; i < col; i++) { max_c.insert(make_pair(-count_col[i], i)); } for (int i = m - 1; i >= 0; i--) { remove(k); set<pair<int, int> >::iterator it = max_c.begin(); pair<int, int> pp = *it; ans[i] = -pp.first; if (done.count(make_pair(min(x[i], y[i]), max(x[i], y[i]))) > 0) { continue; } pair<int, int> p = make_pair(count_edge[x[i]], x[i]); it = k_node.find(p); if (it != k_node.end()) { k_node.erase(it); p.first--; count_edge[x[i]]--; k_node.insert(p); } p = make_pair(count_edge[y[i]], y[i]); it = k_node.find(p); if (it != k_node.end()) { k_node.erase(it); p.first--; count_edge[y[i]]--; k_node.insert(p); } done.insert(make_pair(min(x[i], y[i]), max(x[i], y[i]))); } 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; const int mx = 10000; set<int> se[mx]; int del[mx] = {0}; int dep[mx] = {0}; int ans[mx]; struct node { int fr, to; } ed[mx]; int cnt; int n, m, k; void dfs(int st) { if (del[st] || dep[st] >= k) return; queue<int> q; q.push(st); del[st] = 1; --cnt; while (!q.empty()) { int t = q.front(); q.pop(); for (auto &v : se[t]) { --dep[v]; if (dep[v] < k && !del[v]) { q.push(v); del[v] = 1; --cnt; } } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); ed[i].fr = x; ed[i].to = y; dep[x]++; dep[y]++; se[x].insert(y); se[y].insert(x); } cnt = n; for (int i = 1; i <= n; i++) { dfs(i); } ans[m] = cnt; for (int i = m; i >= 1; --i) { if (!del[ed[i].fr]) --dep[ed[i].fr]; if (!del[ed[i].to]) --dep[ed[i].to]; se[ed[i].fr].erase(ed[i].to); se[ed[i].to].erase(ed[i].fr); dfs(ed[i].fr); dfs(ed[i].to); ans[i - 1] = cnt; } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); }