Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
set<int> tt[200005];
set<int> ans;
vector<pair<int, int>> ve;
int n, m, k, x, y;
int temp[200005];
void del(int x) {
if (tt[x].size() < k && ans.find(x) != ans.end()) {
ans.erase(x);
for (auto i : tt[x]) {
tt[i].erase(x);
del(i);
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
tt[x].insert(y);
tt[y].insert(x);
ve.push_back(make_pair(x, y));
}
for (int i = 1; i <= n; i++) {
ans.insert(i);
}
for (int i = 1; i <= n; i++) {
del(i);
}
for (int i = m - 1; i >= 0; i--) {
temp[i] = ans.size();
tt[ve[i].first].erase(ve[i].second);
tt[ve[i].second].erase(ve[i].first);
del(ve[i].first);
del(ve[i].second);
}
for (int i = 0; i < m; i++) {
printf("%d\n", temp[i]);
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, deg[200010];
set<pair<int, int>> s;
pair<int, int> p[200010];
vector<int> rez;
set<int> v[200010];
map<bool, int> mp[200010];
void rem() {
while (!s.empty() and s.begin()->first < k) {
x = s.begin()->second;
for (auto it : v[x]) {
s.erase({deg[it], it});
--deg[it];
v[it].erase(x);
if (deg[it] > 0) s.insert({deg[it], it});
}
s.erase({deg[x], x});
deg[x] = 0;
v[x].clear();
}
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> p[i].first >> p[i].second;
deg[p[i].first]++, deg[p[i].second]++;
v[p[i].first].insert(p[i].second), v[p[i].second].insert(p[i].first);
}
for (int i = 1; i <= n; i++) s.insert({deg[i], i});
for (int i = m - 1; i >= 0; i--) {
rem();
rez.push_back((int)s.size());
x = p[i].first, y = p[i].second;
if (deg[x] == 0 or deg[y] == 0) continue;
v[x].erase(y), v[y].erase(x);
s.erase({deg[x], x}), s.erase({deg[y], y});
--deg[x], --deg[y];
if (deg[x] > 0) s.insert({deg[x], x});
if (deg[y] > 0) s.insert({deg[y], y});
}
reverse(rez.begin(), rez.end());
for (auto ans : rez) cout << ans << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void amax(X& x, const Y& y) {
if (x < y) x = y;
}
template <class X, class Y>
void amin(X& x, const Y& y) {
if (x > y) x = y;
}
const int INF = 1e9 + 10;
const long long INFL = (long long)1e18 + 10;
const int MAX = 2e5 + 10;
int n, m, k;
int cnt[MAX];
pair<int, int> ed[MAX];
bool mark[MAX], removed[MAX];
vector<pair<int, int> > es[MAX];
stack<int> st;
void invalid_push(int v) {
if (!mark[v] && cnt[v] < k) {
mark[v] = true;
st.push(v);
}
}
void process() {
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> ed[i].first >> ed[i].second;
cnt[ed[i].first]++;
cnt[ed[i].second]++;
es[ed[i].first].push_back(make_pair(ed[i].second, i));
es[ed[i].second].push_back(make_pair(ed[i].first, i));
}
for (int i = 1; i <= n; i++) invalid_push(i);
vector<int> ans;
int res = n;
for (int i = m; i >= 1; i--) {
while (!st.empty()) {
int u = st.top();
st.pop();
res--;
for (auto e : es[u])
if (!mark[e.first] && !removed[e.second]) {
removed[e.second] = true;
cnt[e.first]--;
invalid_push(e.first);
}
}
ans.push_back(res);
if (!removed[i]) {
removed[i] = true;
cnt[ed[i].first]--;
cnt[ed[i].second]--;
invalid_push(ed[i].first);
invalid_push(ed[i].second);
}
}
for (int i = int(ans.size()) - 1; i >= 0; i--) cout << ans[i] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
process();
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
FastReader reader = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int n = reader.nextInt();
int m = reader.nextInt();
int k = reader.nextInt();
Node[] graph = new Node[n];
for (int i=0; i<n; i++)
graph[i] = new Node();
int[][] edges = new int[m][2];
int[] ans = new int[m];
for (int i=0; i<m; i++)
{
edges[i][0] = reader.nextInt()-1;
edges[i][1] = reader.nextInt()-1;
graph[edges[i][0]].list.add(edges[i][1]);
graph[edges[i][1]].list.add(edges[i][0]);
}
Set<Integer> set = new HashSet<Integer>();
Queue<Integer> q = new LinkedList<Integer>();
for (int i=0; i<n; i++)
{
set.add(i);
if (graph[i].list.size() < k)
{
q.add(i);
set.remove(i);
}
}
for (int i=m-1; i>=0; i--)
{
while (!q.isEmpty())
{
int u = q.remove();
//set.remove(u);
for (int v : graph[u].list)
if (set.contains(v))
{
graph[v].list.remove(u);
if (graph[v].list.size() < k)
{
q.add(v);
set.remove(v);
}
}
}
ans[i] = set.size();
int u = edges[i][0];
int v = edges[i][1];
if (set.contains(u) && set.contains(v))
{
graph[v].list.remove(u);
graph[u].list.remove(v);
if (graph[u].list.size() < k)
{
q.add(u);
set.remove(u);
}
if (graph[v].list.size() < k)
{
q.add(v);
set.remove(v);
}
}
}
for (int i=0; i<m; i++)
writer.println(ans[i]);
writer.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
class Node
{
boolean was = false;
Set<Integer> list = new HashSet<Integer>();
} |
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | 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[] going;
static boolean in[];
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<>();
}
in = new boolean[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(in, true);
for (int i = 0; i < going.length; i++) {
going[i] = adj[i].size();
}
for (int i = 0; i < n; i++) {
set.add(i);
}
update(set, e);
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(in[u] && in[v]) {
set.remove(u);
going[u]--;
set.add(u);
set.remove(v);
going[v]--;
set.add(v);
e.remove(hash(u, v));
}
update(set, e);
}
for (int i = 0; i < m; i++) {
pw.println(ans[i]);
}
pw.flush();
pw.close();
}
static void update(TreeSet<Integer> set, HashSet<Long> e){
while(!set.isEmpty() && going[set.first()] < k){
int a = set.pollFirst();
in[a] = false;
for (int b : adj[a]) {
if(in[b] && e.contains(hash(a, b))){
set.remove(b);
going[b]--;
set.add(b);
e.remove(hash(a, b));
}
}
}
}
static long hash(int a, int b){
return Math.min(a, b) * 1000000l + Math.max(a, b);
}
static class Scanner{
BufferedReader br;
StringTokenizer st;
public Scanner(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException {
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
set<long long> V[400010];
long long n, m, k, D[400010], A[400010], B[400010], ans;
signed main() {
scanf("%lld%lld%lld", &n, &m, &k);
ans = n;
for (long long i = 1; i <= m; i++) {
long long a, b;
scanf("%lld%lld", &a, &b);
A[i] = a;
B[i] = b;
D[a]++;
D[b]++;
V[a].insert(b);
V[b].insert(a);
}
vector<long long> Tp, Ans;
Ans.clear();
Tp.clear();
for (long long i = 1; i <= n; i++)
if (D[i] < k) Tp.push_back(i);
for (long long i = m; i >= 1; i--) {
for (long long j = 0; j < Tp.size(); j++) {
long long x = Tp[j];
if (D[x] <= -100000)
continue;
else
ans--;
for (set<long long>::iterator it = V[x].begin(); it != V[x].end(); it++) {
V[*it].erase(x);
if (--D[*it] < k) Tp.push_back(*it);
}
V[x].clear();
D[x] = -100000;
}
Ans.push_back(ans);
Tp.clear();
if (!V[A[i]].count(B[i])) continue;
D[A[i]]--;
D[B[i]]--;
V[A[i]].erase(B[i]);
V[B[i]].erase(A[i]);
if (D[A[i]] < k) Tp.push_back(A[i]);
if (D[B[i]] < k) Tp.push_back(B[i]);
}
for (long long i = (long long)Ans.size() - 1; i >= 0; i--)
printf("%lld\n", Ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int ind[N], ans[N];
set<int> vec[N];
int U[N], V[N];
bool vis[N];
int n, m, K;
int sum = 0;
queue<int> q;
void Top() {
while (!q.empty()) {
++sum;
int u = q.front();
q.pop();
ind[u] = 0;
set<int>::iterator it;
for (it = vec[u].begin(); it != vec[u].end();) {
int v = *it;
if (vis[v]) {
it++;
continue;
}
vec[u].erase(it++);
vec[v].erase(u);
ind[v]--;
if (!vis[v] && ind[v] < K) {
vis[v] = 1;
q.push(v);
}
}
}
}
int main() {
scanf("%d %d %d", &n, &m, &K);
for (int i = 0; i < m; ++i) {
scanf("%d %d", &U[i], &V[i]);
int u = U[i], v = V[i];
vec[u].insert(v);
vec[v].insert(u);
ind[u]++;
ind[v]++;
}
for (int i = 1; i < n + 1; ++i) {
if (ind[i] < K) {
vis[i] = 1;
q.push(i);
}
}
Top();
for (int i = m - 1; i >= 0; --i) {
ans[i] = n - sum;
int u = U[i], v = V[i];
if (vec[u].count(v)) ind[v]--, vec[u].erase(v);
if (vec[v].count(u)) ind[u]--, vec[v].erase(u);
if (!vis[v] && ind[v] < K) vis[v] = 1, q.push(v);
if (!vis[u] && ind[u] < K) vis[u] = 1, q.push(u);
Top();
}
for (int i = 0; i < m; ++i) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
template <class C, class E>
inline bool contains(const C& container, const E& element) {
return container.find(element) != container.end();
}
template <class T>
inline void checkmin(T& a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T& a, T b) {
if (b > a) a = b;
}
using namespace std;
vector<pair<int, int> > fr;
vector<set<int> > gr;
int n, m, k;
vector<bool> inTrip;
int inTripCount;
void Remove(int a, int b) {
gr[a].erase(b);
gr[b].erase(a);
}
int Deg(int a) { return gr[a].size(); }
vector<bool> InQueue;
void RemAll(queue<int>& toRemove) {
while (!toRemove.empty()) {
int iRem = toRemove.front();
inTrip[iRem] = false;
InQueue[iRem] = false;
--inTripCount;
toRemove.pop();
for (int br : gr[iRem]) {
gr[br].erase(iRem);
if (Deg(br) < k && !InQueue[br]) {
InQueue[br] = true;
toRemove.push(br);
}
}
gr[iRem].clear();
}
}
void Process(pair<int, int> p) {
if (!contains(gr[p.first], p.second)) return;
Remove(p.first, p.second);
queue<int> toRemove;
if (inTrip[p.first] && Deg(p.first) < k) {
InQueue[p.first] = true;
toRemove.push(p.first);
}
if (inTrip[p.second] && Deg(p.second) < k) {
InQueue[p.second] = true;
toRemove.push(p.second);
}
RemAll(toRemove);
}
void Init() {
queue<int> toRemove;
for (int i = (0), _b = ((n)-1); i <= _b; i++) {
if (Deg(i) < k) {
InQueue[i] = true;
toRemove.push(i);
}
}
RemAll(toRemove);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << std::setprecision(15);
cout << std::fixed;
cin >> n >> m >> k;
fr.resize(m);
gr.resize(n);
InQueue.resize(n);
inTrip.resize(n, true);
inTripCount = n;
for (int i = (0), _b = ((m)-1); i <= _b; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
fr[i] = make_pair(a, b);
gr[a].insert(b);
gr[b].insert(a);
}
Init();
vector<int> res;
for (int i = (m - 1), _b = (0); i >= _b; i--) {
res.push_back(inTripCount);
if (i != 0) Process(fr[i]);
}
for (int i = (m - 1), _b = (0); i >= _b; i--) {
cout << res[i] << endl;
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | python2 | import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')
n,m,k = map(int,raw_input().split(" "))
edges = [set([]) for i in range(n+1)]
pairs = []
for i in range(m):
x,y = map(int,raw_input().split(" "))
pairs.append((x,y))
edges[x].add(y)
edges[y].add(x)
rank = [0]*(1+n)
for i in range(1,n+1):
rank[i] = len(edges[i])
def check(s,tmp):
while tmp:
v = tmp.pop()
if rank[v]<k:
s.remove(v)
for node in edges[v]:
if not node in s:
continue
rank[node] -= 1
if rank[node]<k :
tmp.add(node)
s = set(range(1,n+1))
check(s,set(s))
ans = [len(s)]
for i in range(m-1)[::-1]:
x,y = pairs[i+1]
if x in s and y in s:
rank[x] -= 1
rank[y] -= 1
edges[x].remove(y)
edges[y].remove(x)
check(s,set([x,y]))
ans.append(len(s))
print "\n".join(map(str,ans[::-1]))
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java |
import java.io.*;
import java.util.*;
/*
Solution Sketch:
*/
public class trips {
static final boolean stdin = true;
static final String filename = "";
static FastScanner br;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
if (stdin) {
br = new FastScanner();
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
else {
br = new FastScanner(filename + ".in");
pw = new PrintWriter(new FileWriter(filename + ".out"));
}
X solver = new X();
solver.solve(br, pw);
}
static class X {
public void solve(FastScanner br, PrintWriter pw) throws IOException {
int n = br.nextInt(); int m = br.nextInt(); int k = br.nextInt(); int[] deg = new int[n];
TreeSet<Pair> s = new TreeSet<Pair>();
LinkedList<Integer>[] adj = new LinkedList[n];
LinkedList<Pair> edges = new LinkedList<Pair>();
for(int i = 0; i < adj.length; i++) {
adj[i] = new LinkedList<Integer>();
}
for(int i = 0; i < m; i++) {
int u = br.nextInt(); int v = br.nextInt();
u--; v--;
adj[u].add(v);
adj[v].add(u);
edges.add(new Pair(u,v));
deg[u]++; deg[v]++;
}
for(int i = 0; i < n; i++) {
s.add(new Pair(deg[i], i));
}
boolean[] vis = new boolean[n];
while(!s.isEmpty()) {
// pw.println("set first: " + (s.first().f + " " + s.first().s));
if(s.first().f < k) {
Pair p = s.pollFirst();
for(int e : adj[p.s]) {
if(vis[e]) {continue;}
s.remove(new Pair(deg[e], e));
deg[e]--;
s.add(new Pair(deg[e], e));
}
vis[p.s] = true;
}
else {
break;
}
}
TreeSet<Pair> visited = new TreeSet<Pair>();
Collections.reverse(edges);
ArrayList<Integer> ans = new ArrayList<Integer>();
for(Pair pr : edges) {
ans.add(s.size());
//
// pw.println();
// pw.println("DEGREES b4:");
// for(int i = 0; i < n; i++){
// pw.print(deg[i] + " ");
// }
// pw.println("Edges: " + (pr.f+1) + " " + (pr.s+1));
if(!vis[pr.f] && !vis[pr.s]) {
// pw.println("EDGE RUN");
s.remove(new Pair(deg[pr.f], pr.f));
s.remove(new Pair(deg[pr.s], pr.s));
deg[pr.f]--;
deg[pr.s]--;
s.add(new Pair(deg[pr.f],pr.f));
s.add(new Pair(deg[pr.s], pr.s));
// pw.println("DEGREES:");
// for(int i = 0; i < n; i++){
// pw.print(deg[i] + " ");
// }
// pw.println();
// pw.println("SET:");
// for(Pair curr : s) {
// pw.print(curr.f + " " + curr.s + " ");
// }
// pw.println();
// pw.println("VISITED:");
// for(int i = 0; i < n; i++) {
// pw.print(vis[i] + " ");
// }
while(!s.isEmpty()) {
if(s.first().f < k) {
Pair p = s.pollFirst();
for(int e : adj[p.s]) {
if(visited.contains(new Pair(p.s, e))) {
continue;
}
if(vis[e] || ((p.s == pr.f && e == pr.s) || (p.s == pr.s && e == pr.f))) {
continue;
}
s.remove(new Pair(deg[e], e));
deg[e]--;
s.add(new Pair(deg[e], e));
}
vis[p.s] = true;
}
else {
break;
}
}
visited.add(new Pair(pr.f, pr.s));
visited.add(new Pair(pr.s, pr.f));
}
}
Collections.reverse(ans);
for(int a : ans) {
pw.println(a);
}
pw.close();
}
public static class Pair implements Comparable<Pair>{
int f,s;
public Pair(int f, int s) {
this.f = f;
this.s = s;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if(this.f == o.f) {
return this.s - o.s;
}
return this.f - o.f;
}
}
}
//fastscanner class
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int deg[N], del[N], u[N], v[N], aa[N], edg[N];
vector<pair<int, int> > adj[N];
int remove(queue<int> &q, int k) {
int ans = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
if (!del[x]) del[x] = 1, ans--;
for (auto z : adj[x]) {
int idx = z.second, y = z.first;
if (edg[idx]) continue;
edg[idx] = 1;
deg[x]--;
deg[y]--;
if (deg[y] < k && !del[y]) {
q.push(y);
}
}
}
return ans;
}
int main() {
ios_base ::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> u[i] >> v[i];
adj[u[i]].push_back({v[i], i});
adj[v[i]].push_back({u[i], i});
deg[u[i]]++;
deg[v[i]]++;
}
int ans = n;
queue<int> q;
for (int i = 1; i <= n; i++)
if (deg[i] < k) q.push(i);
ans += remove(q, k);
for (int i = m; i >= 1; i--) {
aa[i] = ans;
if (edg[i]) continue;
deg[u[i]]--;
deg[v[i]]--;
edg[i] = 1;
if (!del[u[i]] && deg[u[i]] < k) q.push(u[i]);
if (!del[v[i]] && deg[v[i]] < k) q.push(v[i]);
ans += remove(q, k);
}
for (int i = 1; i <= m; i++) cout << aa[i] << "\n";
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool cmax(T& a, const U& b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool cmin(T& a, const U& b) {
return b < a ? a = b, 1 : 0;
}
void _BG(const char* s) { cerr << s << endl; };
template <class T, class... TT>
void _BG(const char* s, T a, TT... b) {
for (int c = 0; *s && (c || *s != ','); ++s) {
cerr << *s;
for (char x : "([{") c += *s == x;
for (char x : ")]}") c -= *s == x;
}
cerr << " = " << a;
if (sizeof...(b)) {
cerr << ", ";
++s;
}
_BG(s, b...);
}
bool RD() { return 1; }
bool RD(char& a) { return scanf(" %c", &a) == 1; }
bool RD(char* a) { return scanf("%s", a) == 1; }
bool RD(double& a) { return scanf("%lf", &a) == 1; }
bool RD(int& a) { return scanf("%d", &a) == 1; }
bool RD(long long& a) { return scanf("%lld", &a) == 1; }
template <class T, class... TT>
bool RD(T& a, TT&... b) {
return RD(a) && RD(b...);
}
void PT(const char& a) { putchar(a); }
void PT(char const* const& a) { fputs(a, stdout); }
void PT(const double& a) { printf("%.16f", a); }
void PT(const int& a) { printf("%d", a); }
void PT(const long long& a) { printf("%lld", a); }
template <char s = ' ', char e = '\n'>
void PL() {
if (e) PT(e);
}
template <char s = ' ', char e = '\n', class T, class... TT>
void PL(const T& a, const TT&... b) {
PT(a);
if (sizeof...(b) && s) PT(s);
PL<s, e>(b...);
}
const int N = 212345;
int n, m, k;
set<int> g[N];
pair<int, int> e[N];
bool die[N];
int ans, QAQ[N];
vector<int> rm;
void chk(int v) {
if (((int)(g[v]).size()) < k && !die[v]) {
rm.push_back(v);
die[v] = 1;
--ans;
}
}
void del(int u, int v) {
g[u].erase(v);
g[v].erase(u);
chk(u);
chk(v);
}
int main() {
RD(n, m, k);
for (int i(0); i < (m); ++i) {
int u, v;
RD(u, v);
--u, --v;
e[i] = {u, v};
g[u].insert(v);
g[v].insert(u);
}
ans = n;
for (int i(0); i < (n); ++i) chk(i);
for (int i((m)-1); i >= (0); --i) {
while (((int)(rm).size())) {
int u = ((rm).back());
(rm).pop_back();
while (((int)(g[u]).size())) del(u, *begin(g[u]));
}
QAQ[i] = ans;
del(e[i].first, e[i].second);
}
for (int i(0); i < (m); ++i) PL(QAQ[i]);
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 11;
typedef int i_N[maxN];
int N, M, K;
set<int> adj[maxN];
i_N deg, mark;
int ans;
pair<int, int> edge[maxN];
void dfs(int u) {
if (mark[u]++) return;
ans--;
for (set<int>::iterator it = adj[u].begin(); it != adj[u].end(); it++) {
int v = *it;
deg[v]--;
adj[v].erase(u);
if (deg[v] < K) dfs(v);
}
adj[u].clear();
}
int main() {
ios_base::sync_with_stdio(0);
cin >> N >> M >> K;
for (int i = 1; i <= M; i++) {
int u, v;
cin >> u >> v;
adj[u].insert(v);
adj[v].insert(u);
deg[u]++;
deg[v]++;
edge[i] = pair<int, int>(u, v);
}
ans = N;
for (int i = 1; i <= N; i++)
if (deg[i] < K) dfs(i);
vector<int> v;
for (int i = M; i; i--) {
v.push_back(ans);
int u = edge[i].first, v = edge[i].second;
if (adj[u].find(v) != adj[u].end()) {
deg[u]--;
deg[v]--;
adj[u].erase(v);
adj[v].erase(u);
}
if (deg[u] < K) dfs(u);
if (deg[v] < K) dfs(v);
}
reverse(v.begin(), v.end());
for (int i = 0; i < M; i++) cout << v[i] << '\n';
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, k;
set<int> g[N];
pair<int, int> p[N];
set<pair<int, int>> st;
bool inSet[N];
int ans[N], deg[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].insert(v);
g[v].insert(u);
deg[u]++, deg[v]++;
p[i] = {u, v};
}
for (int i = 1; i <= n; i++) {
st.insert({deg[i], i});
inSet[i] = true;
}
for (int i = m; i >= 1; i--) {
while (!st.empty() && st.begin()->first < k) {
int x = st.begin()->second;
for (auto y : g[x]) {
if (inSet[y]) {
st.erase({deg[y], y});
deg[y]--;
st.insert({deg[y], y});
}
}
inSet[x] = false;
st.erase({deg[x], x});
}
ans[i] = st.size();
int u = p[i].first;
int v = p[i].second;
g[u].erase(v);
g[v].erase(u);
if (!inSet[u] || !inSet[v]) continue;
st.erase({deg[u], u});
deg[u]--;
st.insert({deg[u], u});
st.erase({deg[v], v});
deg[v]--;
st.insert({deg[v], v});
}
for (int i = 1; i <= m; i++) {
cout << ans[i] << '\n';
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-ffloat-store")
using namespace std;
const int MN = 200010;
set<int> e[MN];
int from[MN], to[MN], deg[MN], vis[MN], res[MN];
int n, m, k, cnt;
void deletev(int v) {
if (deg[v] >= k || vis[v]) return;
queue<int> q;
q.push(v);
vis[v] = 1;
--cnt;
while (!q.empty()) {
int top = q.front();
q.pop();
for (auto& c : e[top]) {
--deg[c];
if (deg[c] < k && !vis[c]) {
q.push(c);
vis[c] = 1;
--cnt;
}
}
}
}
struct E {
void solve(std::istream& cin, std::ostream& cout) {
cin >> n >> m >> k;
memset(deg, (0), sizeof(deg));
memset(from, (0), sizeof(from));
memset(to, (0), sizeof(to));
memset(deg, (0), sizeof(deg));
memset(vis, (0), sizeof(vis));
memset(res, (0), sizeof(res));
e->clear();
for (int i = (0); i < (m); ++i) {
cin >> from[i] >> to[i];
--from[i], --to[i];
++deg[from[i]], ++deg[to[i]];
e[from[i]].insert(to[i]);
e[to[i]].insert(from[i]);
}
cnt = n;
for (int i = (0); i < (n); ++i) {
deletev(i);
}
res[m] = cnt;
for (int i = (m - 1); i >= (0); --i) {
if (!vis[from[i]]) --deg[to[i]];
if (!vis[to[i]]) --deg[from[i]];
e[from[i]].erase(to[i]);
e[to[i]].erase(from[i]);
deletev(from[i]);
deletev(to[i]);
res[i] = cnt;
}
for (int i = (1); i <= (m); ++i) cout << res[i] << "\n";
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
E solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int INF = (int)1e9;
long long INFINF = (long long)1e18 + 10;
const long double PI = 3.14159265358979323846;
long long powermodm(long long x, long long n, long long mod) {
long long result = 1;
while (n > 0) {
if (n % 2 == 1) result = (result * x) % mod;
x = (x * x) % mod;
n = n / 2;
}
return result;
}
long long GCD(long long, long long);
long long LCM(long long, long long);
long long power(int, int);
long long choose(long long, long long);
int ones(long long);
void extendedEuclid(long long, long long);
long long MMI(long long, long long);
void fastscan(int &);
set<int> adj[200005];
vector<int> degree(200005);
vector<int> a1(200005);
vector<int> b1(200005);
vector<pair<int, int> > pair1;
set<pair<int, int> > set1;
vector<int> vis(200005);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<int> ans;
for (long long i = 0; i < m; i++) {
cin >> a1[i] >> b1[i];
degree[a1[i]]++;
degree[b1[i]]++;
adj[a1[i]].insert(b1[i]);
adj[b1[i]].insert(a1[i]);
}
for (long long i = 0; i < n; i++) {
set1.insert({degree[i + 1], i + 1});
}
for (int i = m - 1; i >= 0; i--) {
while (!set1.empty()) {
pair<int, int> temppair;
temppair = *set1.begin();
if (temppair.first < k) {
int temp = temppair.second;
vis[temp] = 1;
degree[temp]--;
set1.erase(temppair);
for (auto k = adj[temp].begin(); k != adj[temp].end(); k++) {
set1.erase({degree[*k], *k});
degree[*k]--;
if (vis[*k] == 0) {
set1.insert({degree[*k], *k});
}
}
adj[temp].clear();
} else {
break;
}
}
ans.push_back(set1.size());
set1.erase({degree[a1[i]], a1[i]});
set1.erase({degree[b1[i]], b1[i]});
adj[a1[i]].erase(b1[i]);
adj[b1[i]].erase(a1[i]);
if (vis[a1[i]] == 0) {
degree[b1[i]]--;
}
if (vis[b1[i]] == 0) {
degree[a1[i]]--;
}
if (vis[a1[i]] == 0) {
set1.insert({degree[a1[i]], a1[i]});
}
if (vis[b1[i]] == 0) {
set1.insert({degree[b1[i]], b1[i]});
}
}
reverse(ans.begin(), ans.end());
for (long long i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
return 0;
}
long long GCD(long long a, long long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
long long LCM(long long a, long long b) {
return (max(a, b) / GCD(a, b)) * min(a, b);
}
long long power(int a, int n) {
unsigned long long int result = 1, x = a;
while (n > 0) {
if (n % 2 == 1) result = result * x;
x = x * x;
n = n / 2;
}
return result;
}
long long choose(long long n, long long k) {
if (k == 0) return 1;
return (n * choose(n - 1, k - 1)) / k;
}
int ones(long long n) {
int c = 0;
while (n) {
n = n & (n - 1);
c++;
}
return c;
}
long long d, x, y;
void extendedEuclid(long long A, long long B) {
if (B == 0) {
d = A;
x = 1;
y = 0;
} else {
extendedEuclid(B, A % B);
int temp = x;
x = y;
y = temp - (A / B) * y;
}
}
long long MMI(long long a, long long p) {
extendedEuclid(a, p);
if (d == 1 && p != 1)
return ((x % p) + p) % p;
else
return -1;
}
void fastscan(int &number) {
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c == '-') {
negative = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48;
if (negative) number *= -1;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n;
vector<set<int> > g(200000);
vector<int> used(200000);
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> x(m);
vector<int> y(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i];
x[i]--;
y[i]--;
g[x[i]].insert(y[i]);
g[y[i]].insert(x[i]);
}
set<pair<int, int> > s;
vector<int> v(n);
for (int i = 0; i < n; i++) {
s.insert(make_pair(g[i].size(), i));
v[i] = g[i].size();
}
while (s.size()) {
int a = s.begin()->first;
int b = s.begin()->second;
if (a < k) {
s.erase(make_pair(a, b));
v[b] = 0;
for (auto i = g[b].begin(); i != g[b].end(); i++)
if (v[*i] >= k) {
s.erase(make_pair(v[*i], *i));
v[*i]--;
s.insert(make_pair(v[*i], *i));
}
} else
break;
}
vector<int> ans(m);
ans[m - 1] = s.size();
for (int i = m - 1; i >= 1; i--) {
int fl = 0;
if (s.find(make_pair(v[x[i]], x[i])) != s.end()) fl++;
if (s.find(make_pair(v[y[i]], y[i])) != s.end()) fl++;
if (fl != 2)
ans[i - 1] = ans[i];
else {
s.erase(make_pair(v[x[i]], x[i]));
s.erase(make_pair(v[y[i]], y[i]));
v[x[i]]--;
v[y[i]]--;
s.insert(make_pair(v[x[i]], x[i]));
s.insert(make_pair(v[y[i]], y[i]));
g[x[i]].erase(y[i]);
g[y[i]].erase(x[i]);
while (s.size()) {
int a = s.begin()->first;
int b = s.begin()->second;
if (a < k) {
s.erase(make_pair(a, b));
v[b] = 0;
for (auto i = g[b].begin(); i != g[b].end(); i++)
if (v[*i] >= k) {
s.erase(make_pair(v[*i], *i));
v[*i]--;
s.insert(make_pair(v[*i], *i));
}
} else
break;
}
ans[i - 1] = s.size();
}
}
for (int i = 0; i < m; i++) cout << ans[i] << " ";
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c <= '9' && c >= '0') x = x * 10 + c - '0', c = getchar();
return x * f;
}
void print(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 5000007;
int n, m, k;
int d[maxn];
bool del[maxn];
using namespace std;
map<pair<int, int>, int> mp;
struct node {
int v, next;
} edge[maxn];
int head[maxn], num = 0;
inline void add_edge(int u, int v) {
edge[++num].v = v;
edge[num].next = head[u];
head[u] = num;
}
int ans;
int u[maxn], v[maxn];
int Ans[maxn];
queue<int> q;
void solve(int x) {
if (d[x] >= k || del[x]) return;
del[x] = 1;
q.push(x);
ans--;
while (!q.empty()) {
int U = q.front();
q.pop();
for (int i = head[U]; i; i = edge[i].next) {
int V = edge[i].v;
if (del[V]) continue;
if (mp.count(make_pair(U, V)) == 0) d[V]--;
if (d[V] < k) {
del[V] = true;
ans--;
q.push(V);
}
}
}
}
int main() {
n = read(), m = read();
k = read();
for (int i = 1; i <= m; ++i) {
u[i] = read(), v[i] = read();
add_edge(u[i], v[i]);
add_edge(v[i], u[i]);
d[v[i]]++;
d[u[i]]++;
}
ans = n;
for (int i = 1; i <= n; ++i) solve(i);
mp.clear();
for (int i = m; i >= 1; --i) {
Ans[i] = ans;
if (!del[u[i]]) d[v[i]]--;
if (!del[v[i]]) d[u[i]]--;
mp[pair<int, int>(u[i], v[i])] = 1;
mp[pair<int, int>(v[i], u[i])] = 1;
solve(u[i]);
solve(v[i]);
}
for (int i = 1; i <= m; ++i) print(Ans[i]), putchar('\n');
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import com.sun.org.apache.bcel.internal.generic.ALOAD;
import java.io.*;
import java.util.*;
public class Main {
public class pair implements Comparable<pair> {
int i, w;
pair(int x, int y) {
i = x;
w = y;
}
public int compareTo(pair p) {
if (w == p.w) return i - p.i;
return w - p.w;
}
}
public class edge {
int v;
boolean p;
edge(int y) {
v = y;
p = true;
}
}
public void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int k = nextInt();
ArrayList<ArrayList<edge>> q = new ArrayList<>();
int[] d = new int[n];
for (int i = 0; i < n; i++) {
q.add(new ArrayList<>());
}
pair[] z = new pair[m];
pair[] f = new pair[m];
for (int i = 0; i < m; i++) {
int x = nextInt() - 1;
int y = nextInt() - 1;
d[x]++;
d[y]++;
q.get(x).add(new edge(y));
q.get(y).add(new edge(x));
f[i] = new pair(q.get(x).size() - 1, q.get(y).size() - 1);
z[i] = new pair(x, y);
}
TreeSet<pair> w = new TreeSet<>();
for (int i = 0; i < n; i++) {
w.add(new pair(i, d[i]));
}
int res = n;
int[] ans = new int[m];
for (int i = 0; i < m; i++) {
while (w.size() > 0 && w.first().w < k) {
int p = w.pollFirst().i;
for (int j = 0; j < q.get(p).size(); j++) {
int v = q.get(p).get(j).v;
if (q.get(p).get(j).p) {
q.get(p).get(j).p = false;
w.remove(new pair(v, d[v]));
d[v]--;
w.add(new pair(v, d[v]));
}
}
}
ans[i] = w.size();
int h = m - 1 - i;
int x1 = z[h].i;
int y1 = z[h].w;
if (q.get(x1).get(f[h].i).p && q.get(y1).get(f[h].w).p) {
q.get(x1).get(f[h].i).p = false;
q.get(y1).get(f[h].w).p = false;
if (w.contains(new pair(x1, d[x1]))) {
w.remove(new pair(x1, d[x1]));
d[x1]--;
w.add(new pair(x1, d[x1]));
}
if (w.contains(new pair(y1, d[y1]))) {
w.remove(new pair(y1, d[y1]));
d[y1]--;
w.add(new pair(y1, d[y1]));
}
}
}
for (int i = m - 1; i >= 0; i--) {
out.println(ans[i]);
}
}
BufferedReader br;
StringTokenizer sc;
PrintWriter out;
String nextToken() throws IOException {
while (sc == null || !sc.hasMoreTokens()) {
try {
sc = new StringTokenizer(br.readLine());
} catch (Exception e) {
sc = null;
}
}
return sc.nextToken();
}
boolean hasNext() throws IOException {
while (sc == null || !sc.hasMoreTokens()) {
try {
sc = new StringTokenizer(br.readLine());
} catch (Exception e) {
return false;
}
}
return true;
}
Integer nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
Long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Main().run();
}
public void run() throws IOException {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// br = new BufferedReader(new FileReader("advent.in"));
// out = new PrintWriter(new File("advent.out"));
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
} |
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << " = " << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << " = " << h << ", ";
_dbg(sdbg + 1, a...);
}
const int maxn = (1e6) + 7;
const int maxk = 20;
const int inf = (1e9) + 7;
const long long LLinf = (1e18) + 7;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
queue<int> q;
bool vis[maxn];
int res;
int n, m, k;
int odp[maxn];
set<int> wek[maxn];
pair<int, int> tab[maxn];
void rob() {
while (((int)(q).size())) {
int a = q.front();
q.pop();
if (vis[a] == 0) continue;
vis[a] = 0;
res--;
for (auto s : wek[a])
if (vis[s] == 1) {
wek[s].erase(a);
if (((int)(wek[s]).size()) < k) q.push(s);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
res = n;
for (int i = 1; i <= m; i++) {
cin >> tab[i].first >> tab[i].second;
wek[tab[i].first].insert(tab[i].second);
wek[tab[i].second].insert(tab[i].first);
}
for (int i = 1; i <= n; i++) {
vis[i] = 1;
if (((int)(wek[i]).size()) < k) q.push(i);
}
rob();
odp[m] = res;
for (int i = m - 1; i >= 0; i--) {
wek[tab[i + 1].first].erase(tab[i + 1].second);
wek[tab[i + 1].second].erase(tab[i + 1].first);
if (vis[tab[i + 1].first] == 1 && ((int)(wek[tab[i + 1].first]).size()) < k)
q.push(tab[i + 1].first);
if (vis[tab[i + 1].second] == 1 &&
((int)(wek[tab[i + 1].second]).size()) < k)
q.push(tab[i + 1].second);
rob();
odp[i] = res;
}
for (int i = 1; i <= m; i++) cout << odp[i] << "\n";
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.lang.*;
import java.math.*;
import java.util.*;
import java.io.*;
public class Main {
void solve() {
n=ni(); m=ni(); k=ni();
g=new TreeSet[n+1];
int x[]=new int[m+1];
int y[]=new int[m+1];
for(int i=1;i<=n;i++) g[i]=new TreeSet<>();
for(int i=1;i<=m;i++){
x[i]=ni();y[i]=ni();
g[x[i]].add(y[i]);
g[y[i]].add(x[i]);
}
degree=new int[n+1];
vis=new boolean[n+1];
for(int i=1;i<=n;i++) degree[i]=g[i].size();
ans=n;
build(n);
int res[]=new int[m+1];
for(int i=m;i>=1;i--){
res[i]=ans;
g[x[i]].remove(y[i]);
g[y[i]].remove(x[i]);
if(!vis[x[i]] && !vis[y[i]]){
degree[x[i]]--;
degree[y[i]]--;
if(degree[x[i]]<k) dfs(x[i]);
if(degree[y[i]]<k && !vis[y[i]]) dfs(y[i]);
}
}
for(int i=1;i<=m;i++) pw.println(res[i]);
}
int n,m,k;
int degree[];
TreeSet<Integer> g[];
boolean vis[];
int ans=0;
void build(int n){
for(int i=1;i<=n;i++){
if(degree[i]<k && !vis[i]) dfs(i);
}
}
void dfs(int v){
vis[v]=true;
ans--;
for(int u : g[v]){
if(!vis[u]){
degree[u]--;
if(degree[u]<k) dfs(u);
}
}
}
long M=(long)1e9+7;
InputStream is;
PrintWriter pw;
String INPUT = "";
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
pw.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new Main().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
} |
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int N, M, K;
int A[MAXN];
int B[MAXN];
set<int> adj[MAXN];
int deg[MAXN];
set<pair<int, int> > st;
bool inGraph[MAXN];
int ans[MAXN];
void update(int x) {
if (!inGraph[x]) return;
set<pair<int, int> >::iterator it = st.find(make_pair(deg[x], x));
st.erase(it);
deg[x]--;
st.insert(make_pair(deg[x], x));
}
void fix() {
while (!st.empty() && st.begin()->first < K) {
int cur = st.begin()->second;
st.erase(st.begin());
inGraph[cur] = false;
for (auto nxt : adj[cur]) {
adj[nxt].erase(cur);
update(nxt);
}
adj[cur].clear();
}
}
int main() {
scanf("%d %d %d", &N, &M, &K);
for (int i = 0; i < M; i++) {
scanf("%d %d", &A[i], &B[i]);
deg[A[i]]++;
deg[B[i]]++;
adj[A[i]].insert(B[i]);
adj[B[i]].insert(A[i]);
}
for (int i = 1; i <= N; i++) {
st.insert(make_pair(deg[i], i));
inGraph[i] = true;
}
for (int i = M - 1; i >= 0; i--) {
fix();
ans[i] = st.size();
adj[A[i]].erase(B[i]);
adj[B[i]].erase(A[i]);
if (inGraph[B[i]]) update(A[i]);
if (inGraph[A[i]]) update(B[i]);
}
for (int i = 0; i < M; i++) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : ", ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T>
ostream &operator<<(ostream &o, tuple<T...> const &t) {
o << "(";
_ot<0>(o, t);
o << ")";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, pair<T, U> const &p) {
o << "(" << p.first << ", " << p.second << ")";
return o;
}
template <class T,
class = typename iterator_traits<typename T::iterator>::value_type,
class = typename enable_if<!is_same<T, string>::value>::type>
ostream &operator<<(ostream &o, const T &a) {
for (auto ite = a.begin(); ite != a.end(); ++ite)
o << (ite == a.begin() ? "" : " ") << *ite;
return o;
}
const int N = 2e5;
vector<pair<int, int>> g[N];
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
ll n, m, k;
cin >> n >> m >> k;
vector<pair<int, int>> v;
vector<int> fri(n);
vector<int> vfri(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
v.emplace_back(a, b);
g[a].emplace_back(b, i);
g[b].emplace_back(a, i);
fri[a]++;
fri[b]++;
vfri[a]++;
vfri[b]++;
}
vector<int> no(n);
int col = 1;
vector<int> used(n);
ll now = n;
int tim = m - 1;
function<void(int)> check = [&](int i) {
if (no[i]) return;
if (vfri[i] < k) {
no[i] = 1;
now--;
auto ng = g[i];
ng.clear();
for (auto p : g[i]) {
int j, t;
tie(j, t) = p;
if (t > tim) continue;
ng.emplace_back(j, t);
if (!no[j]) {
vfri[j]--;
check(j);
}
}
g[i] = ng;
}
};
for (int i = 0; i < n; i++)
if (!no[i]) check(i);
vector<int> ans(m);
for (int i = m - 1; i >= 0; i--) {
tim--;
ans[i] = now;
if (i == 0) break;
int a, b;
tie(a, b) = v[i];
fri[a]--;
fri[b]--;
if (!no[a]) vfri[b]--;
if (!no[b]) vfri[a]--;
if (!no[a]) check(a);
if (!no[b]) check(b);
(42);
}
for (int i = 0; i < m; i++) cout << ans[i] << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.Set;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.HashSet;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.ArrayDeque;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Rustam Musin ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static class TaskE {
int n;
int m;
int k;
Set<Integer>[] g;
boolean[] removed;
public void solve(int testNumber, InputReader in, OutputWriter out) {
n = in.readInt();
m = in.readInt();
k = in.readInt();
g = new Set[n];
for (int i = 0; i < n; i++) {
g[i] = new HashSet<>();
}
int[][] queries = in.readIntTable(m, 2);
for (int i = 0; i < m; i++) {
MiscUtils.decreaseByOne(queries[i]);
g[queries[i][0]].add(queries[i][1]);
g[queries[i][1]].add(queries[i][0]);
}
int answer = n;
removed = new boolean[n];
for (int i = 0; i < n; i++) {
if (g[i].size() < k) {
answer -= remove(i);
}
}
int[] res = new int[m];
for (int i = m - 1; i >= 0; i--) {
res[i] = answer;
int u = queries[i][0];
int v = queries[i][1];
g[u].remove(v);
g[v].remove(u);
if (g[u].size() < k) {
answer -= remove(u);
}
if (g[v].size() < k) {
answer -= remove(v);
}
}
for (int x : res) {
out.printLine(x);
}
}
int remove(int v) {
if (removed[v]) {
return 0;
}
removed[v] = true;
Queue<Integer> q = new ArrayDeque<>();
q.add(v);
int rem = 0;
while (!q.isEmpty()) {
rem++;
v = q.poll();
for (int to : g[v]) {
g[to].remove(v);
if (!removed[to] && g[to].size() < k) {
removed[to] = true;
q.add(to);
}
}
g[v].clear();
}
return rem;
}
}
static class MiscUtils {
public static void decreaseByOne(int[]... arrays) {
for (int[] array : arrays) {
for (int i = 0; i < array.length; i++) {
array[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 int[][] readIntTable(int rowCount, int columnCount) {
int[][] table = new int[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readIntArray(columnCount);
}
return table;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
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 readInt() {
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 static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
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 printLine(int i) {
writer.println(i);
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class E {
static TreeSet<Node> allNodes=new TreeSet<>();
static Node[] nodes;
public static void main(String[] args) {
FastScanner fs=new FastScanner();
int n=fs.nextInt(), m=fs.nextInt(), k=fs.nextInt();
nodes=new Node[n];
for (int i=0; i<n; i++) nodes[i]=new Node(i);
Edge[] edges=new Edge[m];
for (int i=0; i<m; i++) {
int a=fs.nextInt()-1, b=fs.nextInt()-1;
edges[i]=new Edge(a, b);
nodes[a].adj.add(b);
nodes[b].adj.add(a);
nodes[a].degree++;
nodes[b].degree++;
}
for (Node nn:nodes) {
allNodes.add(nn);
}
int[] answers=new int[m];
while (!allNodes.isEmpty()&&allNodes.first().degree<k) {
Node toRemove=allNodes.first();
allNodes.remove(toRemove);
for (int nIndex:toRemove.adj) {
Node nn=nodes[nIndex];
if (allNodes.contains(nn)) {
allNodes.remove(nn);
nn.degree--;
allNodes.add(nn);
}
}
}
for (int i=m-1; i>=0; i--) {
answers[i]=allNodes.size();
Edge e=edges[i];
Node a=nodes[e.from], b=nodes[e.to];
if (allNodes.contains(a)&&allNodes.contains(b)) {
allNodes.remove(a);
allNodes.remove(b);
a.degree--;
b.degree--;
a.adj.remove(e.to);
b.adj.remove(e.from);
allNodes.add(a);
allNodes.add(b);
}
while (!allNodes.isEmpty()&&allNodes.first().degree<k) {
Node toRemove=allNodes.first();
allNodes.remove(toRemove);
for (int nIndex:toRemove.adj) {
Node nn=nodes[nIndex];
if (allNodes.contains(nn)) {
allNodes.remove(nn);
nn.degree--;
allNodes.add(nn);
}
}
}
}
PrintWriter out=new PrintWriter(System.out);
for (int i=0; i<m; i++) {
out.println(answers[i]);
}
out.close();
}
static class Edge {
int from, to;
public Edge(int from, int to) {
this.from=from;
this.to=to;
}
}
static class Node implements Comparable<Node> {
int degree=0;
int index;
HashSet<Integer> adj=new HashSet<>();
public Node(int index) {
this.index=index;
}
public int compareTo(Node o) {
int degDiff=Integer.compare(degree, o.degree);
return degDiff==0?Integer.compare(index, o.index):degDiff;
}
public int hashcode() {
return index;
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) {
a[i]=nextInt();
}
return a;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Vadim Semenov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static final class TaskE {
public void solve(int __, InputReader in, PrintWriter out) {
int vertices = in.nextInt();
int edges = in.nextInt();
int threshold = in.nextInt();
int[] u = new int[edges];
int[] v = new int[edges];
int[] degree = new int[vertices];
for (int edge = 0; edge < edges; ++edge) {
u[edge] = in.nextInt() - 1;
v[edge] = in.nextInt() - 1;
degree[u[edge]]++;
degree[v[edge]]++;
}
int[][] graph = new int[vertices][];
Arrays.setAll(graph, vertex -> new int[degree[vertex]]);
Arrays.fill(degree, 0);
for (int edge = 0; edge < edges; ++edge) {
graph[u[edge]][degree[u[edge]]++] = edge;
graph[v[edge]][degree[v[edge]]++] = edge;
}
int[] queue = new int[vertices];
int head = 0;
int tail = 0;
for (int vertex = 0; vertex < vertices; ++vertex) {
if (degree[vertex] < threshold) {
queue[tail++] = vertex;
}
}
int[] answer = new int[edges];
boolean[] removed = new boolean[edges];
for (int day = edges; day-- > 0; ) {
while (head < tail) {
int vertex = queue[head++];
for (int edge : graph[vertex]) {
if (removed[edge]) continue;
removed[edge] = true;
int next = vertex ^ u[edge] ^ v[edge];
if (degree[next] == threshold) {
queue[tail++] = next;
}
degree[next]--;
}
}
answer[day] = vertices - tail;
if (!removed[day]) {
removed[day] = true;
if (degree[v[day]] == threshold) {
queue[tail++] = v[day];
}
if (degree[u[day]] == threshold) {
queue[tail++] = u[day];
}
degree[u[day]]--;
degree[v[day]]--;
}
}
for (int ans : answer) {
out.println(ans);
}
}
}
static class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
}
public int nextInt() {
return Integer.parseInt(next());
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(readLine());
}
return tokenizer.nextToken();
}
public String readLine() {
String line;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
const int maxn = 2e5 + 3;
int n, m, k, rest, eu[maxn], ev[maxn], ans[maxn];
std::set<int> G[maxn];
bool poped[maxn];
std::queue<int> q;
void delEdge(int u, int v) {
G[u].erase(v);
G[v].erase(u);
if (G[u].size() < k && !poped[u]) {
poped[u] = true;
--rest;
q.push(u);
}
if (G[v].size() < k && !poped[v]) {
poped[v] = true;
--rest;
q.push(v);
}
}
void erase() {
while (!q.empty()) {
int u = q.front();
q.pop();
std::set<int> E = G[u];
for (const auto v : E) {
delEdge(u, v);
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; ++i) {
scanf("%d%d", eu + i, ev + i);
G[eu[i]].insert(ev[i]);
G[ev[i]].insert(eu[i]);
}
rest = n;
for (int i = 1; i <= n; ++i) {
if (G[i].size() < k) {
poped[i] = true;
--rest;
q.push(i);
}
}
erase();
for (int i = m - 1; i >= 0; --i) {
ans[i] = rest;
delEdge(eu[i], ev[i]);
erase();
}
for (int i = 0; i < m; ++i) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, k, u;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &k);
vector<pair<int, int> > adj[n + 5];
pair<int, int> edges[m + 5];
set<pair<int, int> > degree_set;
set<pair<int, int> >::iterator it;
bool isthere[n + 5];
int deg[n + 5];
int ans[m + 5];
memset(ans, 0, sizeof(ans));
memset(deg, 0, sizeof(deg));
memset(isthere, true, sizeof(isthere));
for (i = 0; i < m; i++) {
scanf("%d", &edges[i].first);
scanf("%d", &edges[i].second);
adj[edges[i].first].push_back({edges[i].second, i});
adj[edges[i].second].push_back({edges[i].first, i});
++deg[edges[i].first];
degree_set.insert({deg[edges[i].first], edges[i].first});
degree_set.erase({deg[edges[i].first] - 1, edges[i].first});
++deg[edges[i].second];
degree_set.insert({deg[edges[i].second], edges[i].second});
degree_set.erase({deg[edges[i].second] - 1, edges[i].second});
}
for (i = m - 1; i >= 0; i--) {
while (!degree_set.empty() && (*(degree_set.begin())).first < k) {
u = (*(degree_set.begin())).second;
for (auto k1 : adj[u]) {
if (isthere[k1.first] == true && k1.second <= i) {
degree_set.erase({deg[k1.first], k1.first});
deg[k1.first]--;
degree_set.insert({deg[k1.first], k1.first});
}
}
degree_set.erase({deg[u], u});
isthere[u] = false;
}
ans[i] = degree_set.size();
if (isthere[edges[i].first] == true && isthere[edges[i].second] == true) {
degree_set.erase({deg[edges[i].first], edges[i].first});
deg[edges[i].first]--;
degree_set.insert({deg[edges[i].first], edges[i].first});
degree_set.erase({deg[edges[i].second], edges[i].second});
deg[edges[i].second]--;
degree_set.insert({deg[edges[i].second], edges[i].second});
}
}
for (i = 0; i < m; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct P {
int x, y, z;
bool operator<(const P &a) const { return x < a.x; }
};
vector<int> v[222228];
int a, c, i, b, n, m, k, d;
int o[221111];
int l[221111];
int j[1111][9];
int e;
int dx[10] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[10] = {1, 0, -1, 0, 1, -1, 1, -1},
dz[10] = {0, 0, 0, 0, 1, -1};
long long x, y, mod = 1000000007, mod2 = 1000000009, mod3 = 2017;
long long z;
double pi = 3.14159265;
P u[222221];
stack<int> s;
queue<int> q, q1;
map<int, int> p[222222];
char r[13];
bool as(P a, P b) { return a.x > b.x; }
int main() {
scanf("%d %d %d", &a, &b, &c);
for (int t = 1; t <= b; t++) {
scanf("%d %d", &n, &m);
u[t] = {n, m};
p[min(n, m)][max(m, n)] = 1;
v[n].push_back(m);
v[m].push_back(n);
o[n]++;
o[m]++;
}
k = a;
for (int t = 1; t <= a; t++)
if (o[t] < c) {
k--;
l[t] = 1;
q.push(t);
}
for (; q.size(); q.pop())
for (int h = 0; h < v[q.front()].size(); h++)
if (!l[v[q.front()][h]]) {
o[v[q.front()][h]]--;
if (o[v[q.front()][h]] < c) {
k--;
l[v[q.front()][h]] = 1;
q.push(v[q.front()][h]);
}
}
s.push(k);
for (int t = b; t > 1; t--)
if (l[u[t].y] == 0 && !l[u[t].x] &&
p[min(u[t].x, u[t].y)][max(u[t].x, u[t].y)]) {
p[min(u[t].x, u[t].y)][max(u[t].x, u[t].y)] = 0;
o[u[t].x]--;
o[u[t].y]--;
if (l[u[t].x] == 0 && o[u[t].x] < c) {
k--;
l[u[t].x] = 1;
q.push(u[t].x);
}
if (l[u[t].y] == 0 && o[u[t].y] < c) {
k--;
l[u[t].y] = 1;
q.push(u[t].y);
}
for (; q.size(); q.pop())
for (int h = 0; h < v[q.front()].size(); h++)
if (!l[v[q.front()][h]] && p[min(q.front(), v[q.front()][h])]
[max(q.front(), v[q.front()][h])]) {
p[min(q.front(), v[q.front()][h])]
[max(q.front(), v[q.front()][h])] = 0;
o[v[q.front()][h]]--;
if (o[v[q.front()][h]] < c) {
k--;
l[v[q.front()][h]] = 1;
q.push(v[q.front()][h]);
}
}
s.push(k);
} else
s.push(k);
for (; s.size(); s.pop()) printf("%d\n", s.top());
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m, k;
cin >> n >> m >> k;
vector<set<long long>> g(n + 1);
vector<long long> deg(n + 1, 0);
vector<pair<long long, long long>> v(m);
for (long long i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
g[a].insert(b);
g[b].insert(a);
deg[a]++;
deg[b]++;
v[i].first = a;
v[i].second = b;
}
long long ans = n;
queue<long long> q;
vector<long long> vis(n + 1, 0);
for (long long i = 1; i <= n; i++) {
if (deg[i] < k) {
q.push(i);
vis[i] = 1;
}
}
while (!q.empty()) {
long long d = q.front();
q.pop();
ans--;
for (auto it : g[d]) {
g[it].erase(d);
deg[it]--;
if (deg[it] == k - 1) q.push(it);
}
g[d].clear();
}
vector<long long> fans(m);
for (long long i = m - 1; i >= 0; i--) {
fans[i] = ans;
long long a = v[i].first;
long long b = v[i].second;
if (g[a].find(b) != g[a].end()) {
g[a].erase(b);
deg[a]--;
if (deg[a] == k - 1) q.push(a);
}
if (g[b].find(a) != g[b].end()) {
g[b].erase(a);
deg[b]--;
if (deg[b] == k - 1) q.push(b);
}
while (!q.empty()) {
long long d = q.front();
q.pop();
ans--;
for (auto it : g[d]) {
g[it].erase(d);
deg[it]--;
if (deg[it] == k - 1) q.push(it);
}
g[d].clear();
}
}
for (long long i = 0; i < m; i++) {
cout << fans[i] << "\n";
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 3e5 + 10;
long long x[N], y[N], deg[N];
vector<long long> vp[N];
long long removed[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n, m, k;
cin >> n >> m >> k;
for (long long i = 1; i <= m; i++) {
cin >> x[i] >> y[i];
vp[x[i]].push_back(y[i]);
vp[y[i]].push_back(x[i]);
deg[x[i]]++;
deg[y[i]]++;
}
set<pair<long long, long long> > s;
for (long long i = 1; i <= n; i++) {
s.insert({deg[i], i});
}
long long ans = n;
set<pair<long long, long long> > ae;
vector<long long> v;
for (long long i = m; i >= 1; i--) {
while (s.size() > 0) {
auto it = *s.begin();
s.erase(it);
if (it.first >= k) break;
removed[it.second] = 1;
for (auto it1 : vp[it.second]) {
if (ae.find({it1, it.second}) == ae.end()) {
ae.insert({it1, it.second});
ae.insert({it.second, it1});
if (!removed[it1]) {
s.erase({deg[it1], it1});
deg[it1]--;
s.insert({deg[it1], it1});
}
}
}
ans--;
}
if (ae.find({x[i], y[i]}) == ae.end()) {
ae.insert({x[i], y[i]});
ae.insert({y[i], x[i]});
s.erase({deg[x[i]], x[i]});
s.erase({deg[y[i]], y[i]});
if (!removed[x[i]]) {
deg[x[i]]--;
s.insert({deg[x[i]], x[i]});
}
if (!removed[y[i]]) {
deg[y[i]]--;
s.insert({deg[y[i]], y[i]});
}
}
v.push_back(ans);
}
reverse(v.begin(), v.end());
for (auto it : v) {
cout << it << "\n";
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int max6 = 1e6 + 6;
const int oo = 2e9 + 9;
const long long inf = 2e18 + 18;
vector<pair<int, int> > g[max6];
pair<int, int> a[max6];
int q[max6];
int block[max6];
int deg[max6];
int del[max6];
int res[max6];
int n, m, need;
int main() {
cin >> n >> m >> need;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back({v, i});
g[v].push_back({u, i});
a[i] = {u, v};
deg[u]++, deg[v]++;
}
int l = 1, r = 0;
for (int i = 1; i <= n; ++i)
if (deg[i] < need) q[++r] = i, del[i] = 1;
int maxMem = n;
for (int i = m; i >= 1; --i) {
while (l <= r) {
int u = q[l++];
maxMem--;
for (auto it : g[u]) {
int id = it.second;
int v = it.first;
if (block[id]) continue;
if (del[v]) continue;
block[id] = 1;
deg[v]--;
if (deg[v] < need) {
del[v] = 1;
q[++r] = v;
}
}
}
res[i] = maxMem;
int u = a[i].first;
int v = a[i].second;
if (del[u] || del[v]) continue;
deg[u]--;
deg[v]--;
block[i] = 1;
if (deg[u] < need) {
q[++r] = u;
del[u] = 1;
}
if (deg[v] < need) {
q[++r] = v;
del[v] = 1;
}
}
for (int i = 1; i <= m; ++i) cout << res[i] << "\n";
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int get() {
char ch;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
;
if (ch == '-') {
int s = 0;
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return -s;
}
int s = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return s;
}
const int N = 2e5 + 5;
struct edge {
int x, nxt;
} e[N * 2];
int h[N], tot;
int d[N];
struct EDGE {
int x, y;
} ed[N];
int n, m, k;
int ans[N];
int cnt;
bool bz[N];
bool er[N];
int update(int &now) {
for (; now && er[now / 2]; now = e[now].nxt)
;
return now;
}
void del(int x) {
if (bz[x]) return;
bz[x] = 1;
cnt--;
for (; update(h[x]);) {
int p = h[x];
d[x]--, d[e[p].x]--;
er[p / 2] = 1;
if (d[e[p].x] < k) del(e[p].x);
}
}
void inse(int x, int y) {
e[++tot].x = y;
e[tot].nxt = h[x];
h[x] = tot;
}
int main() {
n = get();
m = get();
k = get();
tot = 1;
for (int i = 1; i <= m; i++) {
ed[i].x = get();
ed[i].y = get();
inse(ed[i].x, ed[i].y);
inse(ed[i].y, ed[i].x);
d[ed[i].x]++, d[ed[i].y]++;
}
cnt = n;
for (int i = 1; i <= n; i++)
if (d[i] < k) del(i);
for (int i = m; i >= 1; i--) {
int x = ed[i].x, y = ed[i].y;
ans[i] = cnt;
if (er[i])
;
else {
er[i] = 1;
d[x]--, d[y]--;
if (d[x] < k) del(x);
if (d[y] < k) del(y);
}
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pradyumn
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static class TaskE {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int K = in.nextInt();
List<int[]>[] g = new List[n];
int[] from = new int[m];
int[] to = new int[m];
for (int i = 0; i < n; ++i) g[i] = new ArrayList<>();
for (int i = 0; i < m; ++i) {
from[i] = in.nextInt() - 1;
to[i] = in.nextInt() - 1;
g[from[i]].add(new int[]{to[i], i});
g[to[i]].add(new int[]{from[i], i});
}
boolean[] alive = new boolean[m];
ArrayUtils.fill(alive, true);
int[] deg = new int[n];
int[] queue = new int[n];
int sizeQ = 0;
int ptr = 0;
for (int i = 0; i < n; ++i) {
deg[i] = g[i].size();
if (deg[i] < K) {
queue[sizeQ++] = i;
}
}
int[] ans = new int[m];
for (int i = m - 1; i >= 0; --i) {
while (ptr < sizeQ) {
int cur = queue[ptr++];
for (int[] edge : g[cur]) {
if (!alive[edge[1]]) continue;
alive[edge[1]] = false;
if (deg[edge[0]] == K) {
queue[sizeQ++] = edge[0];
}
--deg[edge[0]];
--deg[cur];
}
}
ans[i] = n - sizeQ;
if (alive[i]) {
if (deg[from[i]] == K) queue[sizeQ++] = from[i];
if (deg[to[i]] == K) queue[sizeQ++] = to[i];
--deg[from[i]];
--deg[to[i]];
alive[i] = false;
}
}
for (int aa : ans) out.println(aa);
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int pnumChars;
public FastReader(InputStream stream) {
this.stream = stream;
}
private int pread() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = pread();
while (isSpaceChar(c))
c = pread();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = pread();
}
int res = 0;
do {
if (c == ',') {
c = pread();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = pread();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
static class ArrayUtils {
public static void fill(boolean[] array, boolean value) {
Arrays.fill(array, value);
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
vector<set<int>> g(n);
vector<int> deg(n);
set<pair<int, int>> cur;
vector<pair<int, int>> edges;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
deg[a]++;
deg[b]++;
edges.emplace_back(a, b);
g[a].insert(b);
g[b].insert(a);
}
for (int i = 0; i < n; i++) cur.insert(make_pair(deg[i], i));
auto solve = [&]() {
while (true) {
if (cur.empty()) break;
auto u = cur.begin();
vector<int> rev;
if (u->first < k) {
for (int i : g[u->second]) {
rev.push_back(i);
}
cur.erase(u);
for (int i : rev) {
if (cur.find(make_pair(deg[i], i)) != cur.end()) {
cur.erase(make_pair(deg[i], i));
cur.insert(make_pair(deg[i] - 1, i));
}
deg[i]--;
}
} else
break;
}
};
solve();
vector<int> ans;
ans.push_back(cur.size());
for (int i = m - 1; i > -1; i--) {
int u = edges[i].first, v = edges[i].second;
if (cur.find(make_pair(deg[u], u)) != cur.end() &&
cur.find(make_pair(deg[v], v)) != cur.end()) {
cur.erase(make_pair(deg[u], u));
deg[u]--;
cur.insert(make_pair(deg[u], u));
cur.erase(make_pair(deg[v], v));
deg[v]--;
cur.insert(make_pair(deg[v], v));
}
g[u].erase(v);
g[v].erase(u);
solve();
ans.push_back(cur.size());
}
ans.pop_back();
reverse(ans.begin(), ans.end());
for (int i : ans) cout << i << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int k;
cin >> k;
vector<set<int>> g(n);
vector<int> x(m), y(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i], x[i]--, y[i]--;
g[x[i]].emplace(y[i]);
g[y[i]].emplace(x[i]);
}
int ans = n;
queue<int> que;
vector<int> alive(n, true);
for (int i = 0; i < n; i++)
if ((int)g[i].size() < k) {
que.emplace(i), alive[i] = false, ans--;
}
vector<int> res(m);
for (int i = m - 1; i >= 0; i--) {
while (!que.empty()) {
int u = que.front();
que.pop();
for (int v : g[u]) g[v].erase(u);
for (int v : g[u])
if (alive[v] && (int)g[v].size() < k) {
que.emplace(v), alive[v] = false, ans--;
}
g[u].clear();
}
res[i] = ans;
int u = x[i], v = y[i];
g[u].erase(v), g[v].erase(u);
if (alive[u] && (int)g[u].size() < k) {
que.emplace(u), alive[u] = false, ans--;
}
if (alive[v] && (int)g[v].size() < k) {
que.emplace(v), alive[v] = false, ans--;
}
}
for (int e : res) cout << e << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vvll = vector<vll>;
stack<ll> st;
vector<ll> deg;
vector<vector<ll>> g;
vector<pair<ll, ll>> e;
ll k;
set<pair<ll, ll>> deleted;
ll fixStuff() {
ll del = 0;
while (!st.empty()) {
ll i = st.top();
st.pop();
if (deg[i] < 0) continue;
deg[i] = -1;
del++;
for (ll j = 0; j < (ll)g[i].size(); ++j) {
if (deleted.count(make_pair(i, g[i][j]))) continue;
if (deleted.count(make_pair(g[i][j], i))) continue;
deg[g[i][j]]--;
if (deg[g[i][j]] < k) {
st.push(g[i][j]);
}
}
}
return del;
}
int main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
ll n, m;
cin >> n >> m >> k;
deg = vector<ll>(n);
g = vector<vector<ll>>(n);
e = vector<pair<ll, ll>>(m);
for (ll i = 0; i < (ll)m; ++i) {
ll u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
deg[u]++;
deg[v]++;
e[i].first = u;
e[i].second = v;
}
for (ll i = 0; i < (ll)n; ++i)
if (deg[i] < k) st.push(i);
vector<ll> res;
ll cnt = n - fixStuff();
for (ll i = m - 1; i >= (ll)0; --i) {
res.push_back(cnt);
if (deg[e[i].first] < 0 || deg[e[i].second] < 0) continue;
deg[e[i].first]--;
deg[e[i].second]--;
deleted.insert(make_pair(e[i].first, e[i].second));
if ((deg[e[i].first] < k && deg[e[i].first] >= 0)) st.push(e[i].first);
if ((deg[e[i].second] < k && deg[e[i].second] >= 0)) st.push(e[i].second);
if (!st.empty()) cnt -= fixStuff();
}
reverse(res.begin(), res.end());
for (ll i = 0; i < (ll)res.size(); ++i) cout << res[i] << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedInputStream;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.io.FilterInputStream;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Jenish
*/
public class Main {
public static void main(String[] args) {
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[] degree;
ArrayList<Integer>[] arrayList;
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int m = in.scanInt();
int k = in.scanInt();
degree = new int[n + 1];
arrayList = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
arrayList[i] = new ArrayList<>();
}
int edges[][] = new int[m][2];
for (int i = 0; i < m; i++) {
int x = in.scanInt();
int y = in.scanInt();
edges[i][0] = x;
edges[i][1] = y;
degree[x]++;
degree[y]++;
arrayList[x].add(y);
arrayList[y].add(x);
}
TreeSet<pair> treeSet = new TreeSet<>();
HashSet<Long> set = new HashSet<>();
for (int i = 1; i <= n; i++) {
treeSet.add(new pair(degree[i], i));
}
boolean inside[] = new boolean[n + 1];
Arrays.fill(inside, true);
while (!treeSet.isEmpty() && treeSet.first().degree < k) {
pair temp = treeSet.first();
for (int i = 0; i < arrayList[temp.index].size(); i++) {
int adj = arrayList[temp.index].get(i);
if (!inside[adj]) continue;
if (set.contains(adj * 1000000000l + temp.index) || set.contains(temp.index * 1000000000l + adj))
continue;
treeSet.remove(new pair(degree[adj], adj));
degree[adj]--;
degree[temp.index]--;
treeSet.add(new pair(degree[adj], adj));
set.add(adj * 1000000000l + temp.index);
}
inside[temp.index] = false;
treeSet.remove(temp);
}
long ans[] = new long[m];
ans[m - 1] = treeSet.size();
for (int i = m - 1; i > 0; i--) {
int x = edges[i][0];
int y = edges[i][1];
if (inside[x] && inside[y]) {
treeSet.remove(new pair(degree[x], x));
degree[x]--;
treeSet.add(new pair(degree[x], x));
treeSet.remove(new pair(degree[y], y));
degree[y]--;
treeSet.add(new pair(degree[y], y));
set.add(1000000000l * x + y);
}
while (!treeSet.isEmpty() && treeSet.first().degree < k) {
pair temp = treeSet.first();
for (int j = 0; j < arrayList[temp.index].size(); j++) {
int adj = arrayList[temp.index].get(j);
if (!inside[adj]) continue;
if (set.contains(adj * 1000000000l + temp.index) || set.contains(temp.index * 1000000000l + adj))
continue;
treeSet.remove(new pair(degree[adj], adj));
degree[adj]--;
degree[temp.index]--;
treeSet.add(new pair(degree[adj], adj));
set.add(adj * 1000000000l + temp.index);
}
inside[temp.index] = false;
treeSet.remove(temp);
}
ans[i - 1] = treeSet.size();
}
for (int i = 0; i < m; i++) {
out.println(ans[i]);
}
}
class pair implements Comparable<pair> {
int degree;
int index;
public pair(int degree, int index) {
this.degree = degree;
this.index = index;
}
public int compareTo(pair o) {
if (this.degree == o.degree) return this.index - o.index;
return this.degree - o.degree;
}
}
}
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 I = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
I *= 10;
I += n - '0';
n = scan();
}
}
return neg * I;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
int dir[8][2] = {-1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1, -1, 1, 1};
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> x) {
os << "(" << x.first << ", " << x.second << ")";
return os;
}
template <typename S, typename T>
inline bool Min(S &a, const T &b) {
return a > b ? a = b, true : false;
}
template <typename S, typename T>
inline bool Max(S &a, const T &b) {
return a < b ? a = b, true : false;
}
template <typename S, typename T>
inline void Adm(S &a, const T &b) {
a = (a + b) % MOD;
if (a < 0) a += MOD;
}
template <typename T>
inline bool IsPri(T x) {
if (x < 2) return false;
for (T i = 2; i * i <= x; ++i)
if (x % i == 0) return false;
return true;
}
template <typename T>
inline T _Gcd(T a, T b) {
while (b) {
T t = b;
b = a % b;
a = t;
}
return a;
}
template <typename T>
inline int _BitCnt(T x) {
int cnt = 0;
while (x) ++cnt, x &= x - 1;
return cnt;
}
inline long long Pow(long long a, long long n) {
long long t = 1;
a %= MOD;
while (n > 0) {
if (n & 1) t = t * a % MOD;
a = a * a % MOD, n >>= 1;
}
return t % MOD;
}
inline int read() {
static char buf[1000000], *p1 = buf, *p2 = buf;
register int x = false;
register char ch =
p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
? EOF
: *p1++;
;
register bool sgn = false;
while (ch != '-' && (ch < '0' || ch > '9'))
ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
? EOF
: *p1++;
;
if (ch == '-')
sgn = true,
ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
? EOF
: *p1++;
;
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + (ch ^ 48),
ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
? EOF
: *p1++;
;
return sgn ? -x : x;
}
const int N = 2e5 + 100;
int d[N], del[N];
vector<pair<int, int> > e[N];
int u[N], v[N], ans[N];
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u[i], &v[i]);
e[u[i]].push_back({v[i], i}), e[v[i]].push_back({u[i], i});
++d[u[i]], ++d[v[i]];
}
set<pair<int, int> > st;
for (int i = 1; i <= n; ++i) st.insert({d[i], i});
for (int i = m; i >= 1; --i) {
while (!st.empty() && st.begin()->first < k) {
int u = st.begin()->second;
st.erase(st.begin()), del[u] = 1;
for (auto it : e[u]) {
int v = it.first, id = it.second;
if (!del[v] && id <= i) st.erase({d[v], v}), st.insert({--d[v], v});
}
}
ans[i] = ((int)(st).size());
if (!del[u[i]] && !del[v[i]]) {
st.erase({d[u[i]], u[i]}), st.insert({--d[u[i]], u[i]});
st.erase({d[v[i]], v[i]}), st.insert({--d[v[i]], v[i]});
}
}
for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long N = 201102;
vector<long long> g[N];
set<pair<long long, long long> > s;
long long n, m, k, v, u, d[N], a[N], b[N], ans[N];
pair<long long, long long> x;
set<pair<long long, long long> >::iterator it;
map<long long, bool> e[N];
int main() {
scanf("%lld%lld%lld", &n, &m, &k);
for (long long i = 1; i <= m; i++) {
scanf("%lld%lld", &v, &u);
d[v]++, d[u]++;
g[v].push_back(u), g[u].push_back(v);
a[i] = v, b[i] = u;
}
for (long long i = 1; i <= n; i++) s.insert({d[i], i});
for (long long i = m + 1; i >= 1 and s.size(); i--) {
v = a[i];
if (e[a[i]][b[i]]) {
ans[i] = s.size();
continue;
}
e[b[i]][a[i]] = e[a[i]][b[i]] = 1;
if (d[v] >= k) {
s.erase({d[v], v});
d[v]--;
s.insert({d[v], v});
}
v = b[i];
if (d[v] >= k) {
s.erase({d[v], v});
d[v]--;
s.insert({d[v], v});
}
while (s.size()) {
v = (*s.begin()).second;
if (d[v] >= k) break;
s.erase({d[v], v});
d[v] = 0;
for (long long j = 0; j < g[v].size(); j++) {
u = g[v][j];
if (d[u] < k or e[u][v] or s.find({d[u], u}) == s.end()) continue;
e[u][v] = e[v][u] = 1;
s.erase({d[u], u});
d[u]--;
s.insert({d[u], u});
}
}
ans[i] = s.size();
}
for (long long i = 2; i <= m + 1; i++) printf("%lld\n", ans[i]);
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline void upd1(T1& a, const T2& b) {
a = a < b ? a : b;
}
template <class T1, class T2>
inline void upd2(T1& a, const T2& b) {
a = a > b ? a : b;
}
template <class T>
inline bool equ(const T& a, const T& b) {
return !memcmp(&a, &b, sizeof a);
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
struct ano {
operator long long() {
long long x = 0, y = 0, c = getchar();
while (c < 48) y = c == 45, c = getchar();
while (c > 47) x = x * 10 + c - 48, c = getchar();
return y ? -x : x;
}
} buf;
const int N = 2e5 + 5;
set<int> t[N];
int d[N], f[N];
int main() {
int n = buf, m = buf, k = buf;
vector<array<int, 2>> e;
set<array<int, 2>> c;
for (int i = 0; i < m; ++i) {
int u = buf, v = buf;
if (u > v) swap(u, v);
c.insert({u, v});
e.push_back({u, v});
t[u].insert(v);
t[v].insert(u);
++d[u];
++d[v];
}
queue<int> q;
for (int i = 1; i <= n; ++i)
if (d[i] < k) q.push(i);
int s = n;
for (int i = m - 1; ~i; --i) {
while (q.size()) {
int u = q.front();
q.pop();
for (int v : t[u]) {
c.erase({min(u, v), max(u, v)});
t[v].erase(u);
if (d[v]-- == k) q.push(v);
}
--s;
}
f[i] = s;
if (c.count(e[i])) {
int u = e[i][0], v = e[i][1];
t[u].erase(v);
t[v].erase(u);
if (d[u]-- == k) q.push(u);
if (d[v]-- == k) q.push(v);
}
}
for (int i = 0; i < m; ++i) printf("%d\n", f[i]);
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 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) {
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]--;
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) {
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
set<int> e[N];
int deg[N], x[N], y[N], ans[N], q[N], del[N];
int n, m, k, tail = 0, head = 1;
void delv(int u) {
if (deg[u] >= k || del[u]) return;
del[u] = 1;
q[++tail] = u;
}
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]].insert(y[i]);
e[y[i]].insert(x[i]);
++deg[x[i]], ++deg[y[i]];
}
for (int i = 1; i <= n; ++i) delv(i);
for (int i = m; i >= 1; --i) {
while (head <= tail) {
int u = q[head++];
for (auto &v : e[u]) {
--deg[v];
delv(v);
e[v].erase(u);
}
}
ans[i] = n - tail;
if (!del[x[i]] && !del[y[i]]) {
--deg[x[i]], --deg[y[i]];
delv(x[i]), delv(y[i]);
e[x[i]].erase(y[i]);
e[y[i]].erase(x[i]);
}
}
for (int i = 1; i <= m; ++i) printf("%d%c", ans[i], " \n"[i == m]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct DATA {
int first, second;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> adj(n + 1);
vector<int> degree(n + 1, 0);
set<pair<int, int>> good_set;
vector<bool> check(n + 1, true);
vector<int> ans(m + 1, true);
vector<DATA> edge(m + 1);
check[0] = false;
for (int i = 1; i <= m; ++i) {
cin >> edge[i].first >> edge[i].second;
adj[edge[i].first].push_back(edge[i].second);
adj[edge[i].second].push_back(edge[i].first);
degree[edge[i].first]++;
degree[edge[i].second]++;
}
for (int i = 1; i <= n; ++i) {
good_set.insert(pair<int, int>(degree[i], i));
}
while (!good_set.empty() && good_set.begin()->first < k) {
int node = good_set.begin()->second;
for (int i = 0; i < adj[node].size(); ++i) {
int v = adj[node][i];
if (check[v]) {
good_set.erase(pair<int, int>(degree[v], v));
--degree[v];
good_set.insert(pair<int, int>(degree[v], v));
}
}
good_set.erase(pair<int, int>(degree[node], node));
check[node] = false;
}
ans[m] = good_set.size();
for (int i = m; i > 1; --i) {
int x = edge[i].first;
int y = edge[i].second;
if (check[x] && check[y]) {
good_set.erase(pair<int, int>(degree[x], x));
degree[x]--;
good_set.insert(pair<int, int>(degree[x], x));
good_set.erase(pair<int, int>(degree[y], y));
degree[y]--;
good_set.insert(pair<int, int>(degree[y], y));
adj[x].erase(std::remove(adj[x].begin(), adj[x].end(), y), adj[x].end());
adj[y].erase(std::remove(adj[y].begin(), adj[y].end(), x), adj[y].end());
while (!good_set.empty() && good_set.begin()->first < k) {
int node = good_set.begin()->second;
for (int j = 0; j < adj[node].size(); ++j) {
int v = adj[node][j];
if (check[v]) {
good_set.erase(pair<int, int>(degree[v], v));
--degree[v];
good_set.insert(pair<int, int>(degree[v], v));
}
}
good_set.erase(pair<int, int>(degree[node], node));
check[node] = false;
}
}
ans[i - 1] = good_set.size();
}
for (int i = 1; i <= m; ++i) cout << ans[i] << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m, k, l, i, ans, a[N], b[N], in[N], x[N], y[N], z[N], head[N], go[N + N],
Next[N + N];
bool f[N], use[N + N];
inline void Add(int u, int v) {
Next[++l] = head[u], head[u] = l, go[l] = v, use[l] = 1;
}
inline void bfs(int x) {
int l, r, j, u, v;
if (in[x] >= k || !f[x]) return;
for (b[l = r = 1] = x, f[x] = 0, ans--; l <= r; l++) {
for (j = head[u = b[l]]; j; j = Next[j]) {
if (!use[j]) continue;
in[v = go[j]]--, in[u]--;
use[j] = use[j ^ 1] = 0;
if (f[v] && in[v] < k) b[++r] = v, f[v] = 0, ans--;
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (l = 1, i = 1; i <= m; i++)
scanf("%d%d", &x[i], &y[i]), Add(x[i], y[i]), Add(y[i], x[i]),
z[i] = l, in[x[i]]++, in[y[i]]++;
memset(f, 1, sizeof(f));
for (ans = n, i = 1; i <= n; i++) bfs(i);
for (i = m; i; i--) {
a[i] = ans;
if (!use[z[i]]) continue;
in[x[i]]--, in[y[i]]--, use[z[i]] = use[z[i] ^ 1] = 0;
bfs(x[i]), bfs(y[i]);
}
for (i = 1; i <= m; i++) printf("%d\n", a[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k, cnt;
struct Edge {
int u, v;
};
vector<Edge> vE;
vector<int> ans;
const int maxN = 2 * 100000 + 1;
set<int> edge[maxN];
set<int> nodes;
queue<int> Q;
int do_remove() {
while (!Q.empty()) {
int s = Q.front();
Q.pop();
if (edge[s].size() < k) {
for (auto iter1 = edge[s].begin(); iter1 != edge[s].end(); iter1++) {
int t = *iter1;
edge[t].erase(s);
if (edge[t].size() < k) Q.push(t);
}
edge[s].clear();
auto iter = nodes.find(s);
if (iter != nodes.end()) nodes.erase(iter);
};
}
return nodes.size();
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) nodes.insert(i);
cnt = n;
for (int i = 0; i < m; i++) {
int s, t;
cin >> s >> t;
vE.push_back(Edge{s, t});
edge[s].insert(t);
edge[t].insert(s);
}
for (int i = 1; i <= n; i++)
if (edge[i].size() < k) Q.push(i);
int ret = do_remove();
ans.push_back(ret);
for (int i = vE.size() - 1; i >= 1; i--) {
int s = vE[i].u;
int t = vE[i].v;
edge[s].erase(t);
edge[t].erase(s);
Q.push(s);
Q.push(t);
int ret = do_remove();
ans.push_back(ret);
}
for (int i = ans.size() - 1; i >= 0; i--) {
cout << ans[i] << endl;
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MN = 2e5 + 5;
int n, m, k, x, y;
set<int> al[MN], t;
vector<pair<int, int> > el;
void del(int u) {
if (!t.count(u) || al[u].size() >= k) return;
t.erase(u);
for (int v : al[u]) al[v].erase(u);
for (int v : al[u])
if (al[v].size() < k) del(v);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> x >> y;
el.push_back(pair<int, int>(x, y));
al[x].insert(y);
al[y].insert(x);
}
for (int i = 1; i <= n; i++) t.insert(i);
for (int i = 1; i <= n; i++) del(i);
int res[m + 1];
for (int i = el.size() - 1; i >= 0; i--) {
res[i] = t.size();
pair<int, int> e = el[i];
int x = e.first, y = e.second;
al[x].erase(y);
al[y].erase(x);
del(x);
del(y);
}
for (int i = 0; i < 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const int MAX = 200005;
int ans[MAX];
vector<int> P[MAX];
pair<int, int> edges[MAX];
bool O[MAX];
int SZ[MAX];
set<pair<int, int> > wziete;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n, m, k;
cin >> n >> m >> k;
int akt = 0;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
SZ[a]++;
SZ[b]++;
edges[i] = make_pair(a, b);
P[a].push_back(b);
P[b].push_back(a);
}
queue<int> Q;
for (int i = 1; i <= n; i++) {
if (O[i]) continue;
if (P[i].size() < k) {
O[i] = true;
Q.push(i);
}
}
while (!Q.empty()) {
int aktuell = Q.front();
Q.pop();
for (auto it : P[aktuell]) {
int l1 = it;
int l2 = aktuell;
if (l1 > l2) swap(l1, l2);
if (wziete.find(make_pair(l1, l2)) != wziete.end()) continue;
if (!O[it] && SZ[it] - 1 < k) {
O[it] = true;
Q.push(it);
}
SZ[it]--;
wziete.insert(make_pair(l1, l2));
}
}
for (int i = 1; i <= n; i++)
if (!O[i]) akt++;
ans[m] = akt;
for (int i = m; i >= 2; i--) {
int a = edges[i].first;
int b = edges[i].second;
if (a > b) swap(a, b);
if (wziete.find(make_pair(a, b)) != wziete.end()) {
ans[i - 1] = akt;
continue;
}
wziete.insert(make_pair(a, b));
SZ[a]--;
SZ[b]--;
if (!O[a] && SZ[a] < k) {
O[a] = true;
Q.push(a);
akt--;
}
if (!O[b] && SZ[b] < k) {
O[b] = true;
Q.push(b);
akt--;
}
while (!Q.empty()) {
int aktuell = Q.front();
Q.pop();
for (auto it : P[aktuell]) {
int l1 = it;
int l2 = aktuell;
if (l1 > l2) swap(l1, l2);
if (wziete.find(make_pair(l1, l2)) != wziete.end()) continue;
if (!O[it] && SZ[it] - 1 < k) {
O[it] = true;
Q.push(it);
akt--;
}
SZ[it]--;
wziete.insert(make_pair(l1, l2));
}
}
ans[i - 1] = akt;
}
for (int i = 1; i <= m; i++) cout << ans[i] << "\n";
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
*
*/
public class E {
static LinkedList<Pair>[] g;
static int k;
static boolean[] s;
static int size;
static int[] degree;
static Queue<Integer> q = new ArrayDeque<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
s = new boolean[n];
g = new LinkedList[n];
degree = new int[n];
int[][] f = new int[m][2];
for (int i = 0; i < m; i++) {
st = new StringTokenizer(reader.readLine());
int a = Integer.parseInt(st.nextToken())-1;
int b = Integer.parseInt(st.nextToken())-1;
if (g[a] == null) {
g[a] = new LinkedList<>();
}
if (g[b] == null) {
g[b] = new LinkedList<>();
}
g[a].add(new Pair(b,i));
g[b].add(new Pair(a,i));
degree[a]++;degree[b]++;
f[i][0] = a;
f[i][1] = b;
}
for (int i = 0; i < n; i++) {
if(degree[i]>0){
if(degree[i]>=k){
s[i] = true;
size ++;
}else{
q.add(i);
}
}
}
while (!q.isEmpty()){
int v = q.poll();
for (Pair u : g[v]) {
if(s[u.a]) {
degree[u.a]--;
if (degree[u.a] < k) {
q.add(u.a);
s[u.a] = false;
size--;
}
}
}
}
int[] ans = new int[m];
for (int i = m-1; i >=0; i--) {
ans[i] = size;
int a = f[i][0];
int b = f[i][1];
if(s[a] && s[b]) {
degree[a]--;degree[b]--;
if (degree[a] < k){
q.add(a);
s[a] = false;
size--;
}
if (degree[b] < k){
q.add(b);
s[b] = false;
size--;
}
while (!q.isEmpty()){
int v = q.poll();
for (Pair u : g[v]) {
if(u.b < i && s[u.a]) {
degree[u.a]--;
if (degree[u.a] < k) {
q.add(u.a);
s[u.a] = false;
size--;
}
}
}
}
}
}
for (int i = 0; i < m; i++) {
System.out.println(ans[i]);
}
}
static class Pair{
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
} |
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | 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;
public void solve(int testNumber, InputReader in, OutputWriter out) {
n = in.nextInt();
int m = in.nextInt();
k = in.nextInt();
g = new Set[n];
for (int i = 0; i < n; i++) {
g[i] = new HashSet();
}
deg = new int[n];
List<edge> edges = new ArrayList<>();
for (int i = 0; i < m; i++) {
int u = in.nextInt() - 1;
int v = in.nextInt() - 1;
edges.add(new edge(u, v));
g[u].add(v);
g[v].add(u);
deg[u]++;
deg[v]++;
}
s = new HashSet<>();
List<vertex> vertices = new ArrayList<>();
for (int i = 0; i < n; i++) {
s.add(i);
vertices.add(new vertex(i, deg[i]));
}
vertices.sort((v1, v2) -> v1.d - v2.d);
int i = 0;
for (i = 0; i < n; i++) {
if (vertices.get(i).d < k) {
int u = vertices.get(i).u;
if (s.contains(u))
dfs(u, -1, -1);
} else {
break;
}
}
//out.println(s.size());
List<Integer> ans = new ArrayList<>();
ans.add(s.size());
for (int j = m - 1; j > 0; j--) {
edge ce = edges.get(j);
g[ce.u].remove(ce.v);
g[ce.v].remove(ce.u);
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);
if (s.contains(ce.v))
dfs(ce.v, ce.u, ce.v);
}
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)) {
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k, cnt[222222], x[222222], y[222222], all, idx[222222], idy[222222],
ans[222222];
bool used[222222];
vector<pair<int, int> > g[222222];
void upd(int i) {
used[i] = 1;
all--;
for (int j = 0; j < g[i].size(); j++) {
if (!g[i][j].second) continue;
cnt[g[i][j].first]--;
if (used[g[i][j].first]) continue;
if (cnt[g[i][j].first] < k) upd(g[i][j].first);
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x[i], &y[i]);
idx[i] = g[x[i]].size();
idy[i] = g[y[i]].size();
g[x[i]].push_back(make_pair(y[i], 1));
g[y[i]].push_back(make_pair(x[i], 1));
}
all = n;
for (int i = 1; i <= n; i++) cnt[i] = g[i].size();
for (int i = 1; i <= n; i++) {
if (!used[i] && cnt[i] < k) upd(i);
}
ans[m] = all;
for (int i = m; i >= 2; i--) {
g[x[i]][idx[i]].second = 0;
g[y[i]][idy[i]].second = 0;
if (used[x[i]] | used[y[i]]) {
ans[i - 1] = all;
continue;
}
cnt[x[i]]--;
cnt[y[i]]--;
if (cnt[x[i]] < k) upd(x[i]);
if (!used[y[i]] && cnt[y[i]] < k) upd(y[i]);
ans[i - 1] = all;
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mx = 2e5 + 10, cut = 700;
int n, m, k, cnt[mx], lv, x[mx], y[mx], dead[mx];
vector<pair<int, int> > adj[mx];
vector<int> res;
void er(int a, int b) {
auto it = lower_bound(adj[a].begin(), adj[a].end(), make_pair(b, 0));
if (it->second) return;
it->second = 1;
cnt[a]--;
}
void f(int h) {
if (cnt[h] >= k || dead[h]) return;
dead[h] = 1;
lv--;
for (auto &it : adj[h])
if (!it.second) er(it.first, h);
for (auto &it : adj[h])
if (!it.second) f(it.first);
}
int main() {
scanf("%d%d%d", &n, &m, &k);
if (k > cut) {
for (int i = 0; i < m; i++) puts("0");
return 0;
}
for (int i = 0; i < m; i++) {
scanf("%d%d", x + i, y + i);
cnt[x[i]]++;
cnt[y[i]]++;
adj[x[i]].push_back({y[i], 0});
adj[y[i]].push_back({x[i], 0});
}
for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end());
lv = n;
for (int i = 1; i <= n; i++) f(i);
for (int i = m; i--;) {
res.push_back(lv);
er(x[i], y[i]);
er(y[i], x[i]);
f(x[i]);
f(y[i]);
}
reverse(res.begin(), res.end());
for (auto &it : res) printf("%d\n", it);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
const int MAX_N = 2e5 + 7;
struct bg_edge {
int a, b;
} edges[MAX_N];
struct bg_state {
int node;
long unsigned int deg;
bool operator>(const bg_state& o) const { return deg > o.deg; }
};
static inline void setio(void);
int n, m;
long unsigned int k;
std::set<int> gr[MAX_N];
int ans[MAX_N], in[MAX_N];
int main(void) {
setio();
memset(in, 1, sizeof(in));
std::cin >> n >> m >> k;
for (int i = 0, a, b; i < m; ++i) {
std::cin >> a >> b;
--a;
--b;
gr[a].insert(b);
gr[b].insert(a);
edges[i] = {a, b};
}
MinHeap<bg_state> pq;
for (int i = 0; i < n; ++i) pq.push(bg_state{i, gr[i].size()});
std::function<int(void)> doit = [&](void) {
int rem = 0;
while (pq.size() && pq.top().deg < k) {
bg_state now = pq.top();
pq.pop();
if (now.deg != gr[now.node].size() || !in[now.node]) continue;
++rem;
in[now.node] = false;
for (const auto& nei : gr[now.node]) {
gr[nei].erase(now.node);
pq.push(bg_state{nei, gr[nei].size()});
}
}
return rem;
};
ans[m] = n;
for (int i = m; i > 0; --i) {
if (i < m) {
gr[edges[i].a].erase(edges[i].b);
gr[edges[i].b].erase(edges[i].a);
pq.push(bg_state{edges[i].a, gr[edges[i].a].size()});
pq.push(bg_state{edges[i].b, gr[edges[i].b].size()});
}
ans[i - 1] = ans[i] - doit();
}
for (int i = 0; i < m; ++i) std::cout << ans[i] << "\n";
return 0;
}
static inline void setio(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.precision(10);
std::cout << std::fixed;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int n, m, k, deg[N], ans[N], s[N], t[N];
map<int, bool> adj[N];
vector<int> g[N];
bool alive[N];
void solve() {
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q;
memset(alive, 1, sizeof(alive));
for (int u = 1; u <= n; u++) q.push(make_pair(deg[u], u));
while (q.size()) {
pair<int, int> now = q.top();
if (now.first >= k) break;
q.pop();
if (!alive[now.second]) continue;
alive[now.second] = 0;
for (int &v : g[now.second]) {
deg[v]--;
if (alive[v]) q.push(make_pair(deg[v], v));
}
}
int res = 0;
for (int u = 1; u <= n; u++) res += alive[u];
ans[m] = res;
for (int i = m; i >= 1; i--) {
if (alive[s[i]] && alive[t[i]]) {
deg[s[i]]--;
deg[t[i]]--;
adj[s[i]][t[i]] = 0;
adj[t[i]][s[i]] = 0;
q.push(make_pair(deg[s[i]], s[i]));
q.push(make_pair(deg[t[i]], t[i]));
pair<int, int> now = q.top();
while (q.size()) {
pair<int, int> now = q.top();
if (now.first >= k) break;
q.pop();
if (!alive[now.second]) continue;
--res;
alive[now.second] = 0;
for (int &v : g[now.second]) {
if (!adj[now.second][v]) continue;
--deg[v];
if (alive[v]) q.push(make_pair(deg[v], v));
}
}
}
ans[i - 1] = res;
}
for (int i = 1; i <= m; i++) printf("%d\n ", ans[i]);
}
int main() {
scanf("%d %d %d ", &n, &m, &k);
for (int i = 1; i <= m; i++) {
scanf("%d %d ", &s[i], &t[i]);
deg[s[i]]++;
deg[t[i]]++;
adj[s[i]][t[i]] = adj[t[i]][s[i]] = 1;
g[s[i]].push_back(t[i]);
g[t[i]].push_back(s[i]);
}
solve();
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
pair<int, int> edge[200009];
set<int> g[200009];
int vis[200009];
int ar[200009];
int ans;
int n, m, k;
void dfs(int v) {
if (g[v].size() >= k || vis[v] == 1) return;
--ans;
vis[v] = 1;
for (auto u : g[v]) {
g[u].erase(v);
dfs(u);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m >> k;
int x, y;
ans = n;
for (int i = 0; i < m; ++i) {
cin >> x >> y;
edge[i] = {x, y};
g[x].insert(y);
g[y].insert(x);
}
for (int i = 1; i <= n; ++i) dfs(i);
for (int i = m - 1; i >= 0; --i) {
ar[i] = ans;
g[edge[i].first].erase(edge[i].second);
g[edge[i].second].erase(edge[i].first);
dfs(edge[i].first);
dfs(edge[i].second);
}
for (int i = 0; i < m; ++i) cout << ar[i] << "\n";
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, fs;
std::vector<int> yol[200005];
pair<int, int> o[200005];
int ans, yaz[200005], say[200005], del[200005];
map<pair<int, int>, int> mp;
void sil(int k) {
if (del[k] == 1) return;
del[k] = 1;
ans--;
for (int i = 0; i < yol[k].size(); ++i) {
if (mp[make_pair(k, yol[k][i])] || mp[make_pair(yol[k][i], k)]) continue;
say[yol[k][i]]--;
if (say[yol[k][i]] < fs && del[yol[k][i]] == 0) sil(yol[k][i]);
}
}
int main() {
scanf("%d %d %d", &n, &m, &fs);
ans = n;
for (int i = 0; i < m; ++i) {
scanf("%d %d", &o[i].first, &o[i].second);
yol[o[i].first].push_back(o[i].second);
yol[o[i].second].push_back(o[i].first);
say[o[i].first]++;
say[o[i].second]++;
}
for (int i = 1; i <= n; ++i) {
if (say[i] < fs) {
sil(i);
}
}
for (int i = m - 1; i >= 0; --i) {
yaz[i] = ans;
if (del[o[i].first] == 0 && del[o[i].second] == 0) {
say[o[i].first]--;
say[o[i].second]--;
mp[o[i]] = 1;
}
if (say[o[i].first] < fs) sil(o[i].first);
if (say[o[i].second] < fs) sil(o[i].second);
}
for (int i = 0; i < m; ++i) printf("%d\n", yaz[i]);
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void del(int edge, vector<set<int>>& g, map<int, int>& degr, int k) {
degr.erase(degr.find(edge));
for (auto neigh : g[edge]) {
g[neigh].erase(edge);
auto it = degr.find(neigh);
if (it != degr.end()) {
if (--degr[neigh] < k) {
del(neigh, g, degr, k);
}
}
}
g[edge].clear();
}
int main() {
int n, m, k;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
vector<set<int>> g(n);
vector<pair<int, int>> edges;
map<int, int> degr;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].insert(b);
g[b].insert(a);
edges.push_back({a, b});
degr[a]++;
degr[b]++;
}
for (int i = 0; i < n; i++) {
if (degr.find(i) != degr.end() && degr[i] < k) {
del(i, g, degr, k);
}
}
std::list<int> answ;
for (int i = m - 1; i >= 0; i--) {
answ.push_front(degr.size());
auto edge = edges[i];
if (g[edge.second].find(edge.first) != g[edge.second].end()) {
g[edge.second].erase(edge.first);
if (--degr[edge.first] < k) del(edge.first, g, degr, k);
}
if (g[edge.first].find(edge.second) != g[edge.first].end()) {
g[edge.first].erase(edge.second);
if (--degr[edge.second] < k) del(edge.second, g, degr, k);
}
}
for (auto a : answ) {
cout << a << "\n";
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
while (cin >> n >> m >> k) {
set<pair<int, int> > st;
vector<vector<int> > G(n + 1);
vector<int> deg(n + 1), vis(n + 1), x(m), y(m), ans(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i];
deg[x[i]]++;
deg[y[i]]++;
G[x[i]].push_back(y[i]);
G[y[i]].push_back(x[i]);
}
queue<int> q;
for (int i = 1; i <= n; i++) {
if (deg[i] < k) {
vis[i] = true;
q.push(i);
}
}
for (int cnt = n, day = m - 1; day >= 0; day--) {
if (day != m - 1) {
int u = x[day + 1], v = y[day + 1];
if (!st.count(make_pair(u, v))) {
st.insert(make_pair(u, v));
st.insert(make_pair(v, u));
if (--deg[u] < k && !vis[u]) {
vis[u] = true;
q.push(u);
}
if (--deg[v] < k && !vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
cnt--;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!st.count(make_pair(u, v))) {
st.insert(make_pair(u, v));
st.insert(make_pair(v, u));
if (--deg[v] < k && !vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
}
ans[day] = cnt;
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1123456;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
void vout(T s) {
cout << s << endl;
exit(0);
}
long long bp(long long a, long long n) {
long long res = 1;
while (n) {
if (n % 2) res *= a;
a *= a;
n >>= 1;
}
return res;
}
set<long long> s[MAXN];
long long n, m, k;
long long kol[MAXN];
long long ans;
bool fl[MAXN];
pair<long long, long long> a[MAXN];
queue<long long> q;
void add(long long x) {
if (fl[x]) return;
ans--;
fl[x] = 1;
q.push(x);
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
ans = n;
vector<long long> res(m);
for (int i = 0; i < m; i++) {
cin >> a[i].first >> a[i].second;
kol[a[i].first]++;
kol[a[i].second]++;
s[a[i].first].insert(a[i].second);
s[a[i].second].insert(a[i].first);
}
for (int i = 1; i <= n; i++)
if (!fl[i] && kol[i] < k) {
add(i);
while (!q.empty()) {
long long x = q.front();
q.pop();
for (auto i : s[x]) {
kol[i]--;
if (kol[i] < k) add(i);
}
}
}
for (int i = m - 1; i >= 0; i--) {
res[i] = ans;
if (!fl[a[i].first] && !fl[a[i].second]) {
kol[a[i].first]--;
kol[a[i].second]--;
s[a[i].first].erase(a[i].second);
s[a[i].second].erase(a[i].first);
if (kol[a[i].first] < k) add(a[i].first);
if (kol[a[i].second] < k) add(a[i].second);
while (!q.empty()) {
long long x = q.front();
q.pop();
for (auto i : s[x]) {
kol[i]--;
if (kol[i] < k) add(i);
}
}
}
}
for (auto i : res) cout << i << "\n";
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k, a, b, x;
vector<int> edge[200005];
pair<int, int> amie[200005];
int deg[200005], ans[200005];
set<pair<int, int> > s, r;
void go(int y, int z) {
while (!s.empty() && s.begin()->first < k) {
a = s.begin()->second;
b = (int)edge[a].size();
s.erase(s.begin());
for (int i = 0; i < b; ++i) {
if ((a == y && edge[a][i] == z) || (a == z && edge[a][i] == y)) continue;
if (s.find(pair<int, int>(deg[edge[a][i]], edge[a][i])) != s.end() &&
r.find(pair<int, int>(a, edge[a][i])) == r.end() &&
r.find(pair<int, int>(edge[a][i], a)) == r.end()) {
s.erase(s.find(pair<int, int>(deg[edge[a][i]], edge[a][i])));
deg[edge[a][i]]--;
s.insert(pair<int, int>(deg[edge[a][i]], edge[a][i]));
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; ++i) {
scanf("%d%d", &amie[i].first, &amie[i].second);
edge[amie[i].first].push_back(amie[i].second);
edge[amie[i].second].push_back(amie[i].first);
deg[amie[i].first]++;
deg[amie[i].second]++;
}
for (int i = 1; i <= n; ++i) s.insert(pair<int, int>(deg[i], i));
go(-1, -1);
for (int i = m - 1; i >= 0; --i) {
ans[i] = (int)s.size();
if (s.find(pair<int, int>(deg[amie[i].first], amie[i].first)) != s.end() &&
s.find(pair<int, int>(deg[amie[i].second], amie[i].second)) !=
s.end()) {
s.erase(s.find(pair<int, int>(deg[amie[i].first], amie[i].first)));
s.erase(s.find(pair<int, int>(deg[amie[i].second], amie[i].second)));
deg[amie[i].first]--;
deg[amie[i].second]--;
s.insert(pair<int, int>(deg[amie[i].first], amie[i].first));
s.insert(pair<int, int>(deg[amie[i].second], amie[i].second));
r.insert(amie[i]);
go(amie[i].first, amie[i].second);
}
}
for (int i = 0; i < m; ++i) {
printf("%d\n", ans[i]);
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k;
struct Node {
int x, y;
} a[200010];
int deg[200010];
bool fl[200010];
set<int> v[200010];
int ans;
int ans1[200010];
int pos[200010];
void solve(int x) {
if (deg[x] >= k || !fl[x]) {
return;
}
queue<int> q;
q.push(x);
fl[x] = 0;
ans--;
while (!q.empty()) {
int y = q.front();
q.pop();
for (auto to : v[y]) {
deg[to]--;
if (deg[to] < k && fl[to]) {
fl[to] = 0;
q.push(to);
ans--;
}
}
}
}
int main(int argc, char const *argv[]) {
scanf("%d%d%d", &n, &m, &k);
memset(fl, 1, sizeof(fl));
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
deg[a[i].x]++;
deg[a[i].y]++;
v[a[i].x].insert(a[i].y);
v[a[i].y].insert(a[i].x);
}
ans = n;
for (int i = 1; i <= n; i++) {
solve(i);
}
for (int i = m; i; i--) {
ans1[i] = ans;
if (fl[a[i].y]) {
deg[a[i].x]--;
}
if (fl[a[i].x]) {
deg[a[i].y]--;
}
v[a[i].x].erase(a[i].y);
v[a[i].y].erase(a[i].x);
solve(a[i].x);
solve(a[i].y);
}
for (int i = 1; i <= m; i++) {
printf("%d\n", ans1[i]);
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int max_N = 2e5 + 10;
int x[max_N], y[max_N];
set<int> E[max_N];
void remove(int u);
int N, M, K;
int ans = 0;
int active[max_N];
int main() {
cin >> N >> M >> K;
for (int i = 1; i <= M; ++i) {
cin >> x[i] >> y[i];
E[x[i]].insert(y[i]);
E[y[i]].insert(x[i]);
}
for (int i = 1; i <= N; ++i) {
active[i] = 1;
}
ans = N;
for (int i = 1; i <= N; ++i) {
remove(i);
}
vector<int> ans;
for (int i = M; i >= 1; --i) {
ans.push_back(::ans);
int u = x[i], v = y[i];
E[u].erase(v);
E[v].erase(u);
remove(u);
remove(v);
}
reverse(ans.begin(), ans.end());
for (auto &x : ans) {
cout << x << '\n';
}
}
void remove(int u) {
if (E[u].size() >= K || !active[u]) {
return;
}
active[u] = 0;
ans--;
for (auto &v : E[u]) {
E[v].erase(u);
remove(v);
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
vector<vector<pair<int, int> > > g;
set<pair<int, int> > good;
vector<char> very_good;
vector<int> d;
int k;
void check(long long int i) {
while (!good.empty() && good.begin()->first < k) {
int v = good.begin()->second;
for (auto &y : g[v]) {
int x = y.first;
if (y.second >= i) continue;
if (very_good[x]) {
good.erase(make_pair(d[x], x));
d[x]--;
good.insert(make_pair(d[x], x));
}
}
good.erase(make_pair(d[v], v));
very_good[v] = false;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m >> k;
g.resize(n);
d.resize(n);
vector<pair<int, int> > f(m);
for (int i = 0; i < m; ++i) {
cin >> f[i].first >> f[i].second;
f[i].first--, f[i].second--;
g[f[i].first].push_back(make_pair(f[i].second, i));
g[f[i].second].push_back(make_pair(f[i].first, i));
d[f[i].first]++;
d[f[i].second]++;
}
for (int i = 0; i < n; ++i) {
good.insert(make_pair(d[i], i));
}
very_good.assign(n, true);
check(1e18);
vector<int> ans(m);
for (int i = m - 1; i >= 0; --i) {
ans[i] = good.size();
int v = f[i].first, u = f[i].second;
if (very_good[v] && very_good[u]) {
good.erase(make_pair(d[v], v));
d[v]--;
good.insert(make_pair(d[v], v));
good.erase(make_pair(d[u], u));
d[u]--;
good.insert(make_pair(d[u], u));
check(i);
}
}
for (int i = 0; i < m; ++i) cout << ans[i] << '\n';
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | python3 | from collections import deque
def solve(adj, m, k, uv):
n = len(adj)
nn = [len(a) for a in adj]
q = deque()
for i in range(n):
if nn[i] < k:
q.append(i)
while q:
v = q.popleft()
for u in adj[v]:
nn[u] -= 1
if nn[u] == k-1:
q.append(u)
res = [0]*m
nk = len([1 for i in nn if i >= k])
res[-1] = nk
for i in range(m-1, 0, -1):
u1, v1 = uv[i]
if nn[u1] < k or nn[v1] < k:
res[i - 1] = nk
continue
if nn[u1] == k:
q.append(u1)
nn[u1] -= 1
if not q and nn[v1] == k:
q.append(v1)
nn[v1] -= 1
if not q:
nn[u1] -= 1
nn[v1] -= 1
adj[u1].remove(v1)
adj[v1].remove(u1)
while q:
v = q.popleft()
nk -= 1
for u in adj[v]:
nn[u] -= 1
if nn[u] == k - 1:
q.append(u)
res[i - 1] = nk
return res
n, m, k = map(int, input().split())
a = [set() for i in range(n)]
uv = []
for i in range(m):
u, v = map(int, input().split())
a[u - 1].add(v - 1)
a[v - 1].add(u - 1)
uv.append((u-1, v-1))
res = solve(a, m, k, uv)
print(str(res)[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
using namespace std;
long long a[200005], b[200005], deg[200005];
vector<long long> adj[200005];
long long ans[200005], mark[200005];
int main() {
long long n, m, k, i, j;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while (cin >> n >> m >> k) {
set<pair<long long, long long> > s;
set<pair<long long, long long> >::iterator it;
for (i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
deg[a[i]]++;
deg[b[i]]++;
adj[a[i]].push_back(b[i]);
adj[b[i]].push_back(a[i]);
}
for (i = 1; i <= n; i++) {
mark[i] = 1;
s.insert(make_pair(deg[i], i));
}
map<pair<long long, long long>, long long> mp;
for (i = m; i >= 1; i--) {
for (j = 1;; j++) {
if (s.size() == 0) break;
it = s.begin();
long long x = it->first;
long long y = it->second;
if (x >= k) break;
mark[y] = 0;
s.erase(s.find({deg[y], y}));
for (j = 0; j < adj[y].size(); j++) {
long long p = adj[y][j];
if (mp[{y, p}] == 1) continue;
s.erase(s.find({deg[p], p}));
deg[p]--;
s.insert({deg[p], p});
mp[{y, p}] = mp[{p, y}] = 1;
}
}
ans[i] = s.size();
long long x = a[i];
long long y = b[i];
if (mp[{x, y}] == 1) continue;
mp[{x, y}] = mp[{y, x}] = 1;
s.erase(s.find({deg[x], x}));
deg[x]--;
s.insert({deg[x], x});
s.erase(s.find({deg[y], y}));
deg[y]--;
s.insert({deg[y], y});
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int> > edges(m, vector<int>(2));
vector<vector<int> > graph(n + 1);
vector<int> degrees(n + 1, 0);
for (int i = 0; i < m; ++i) {
scanf("%d %d", &edges[i][0], &edges[i][1]);
degrees[edges[i][0]]++;
degrees[edges[i][1]]++;
graph[edges[i][0]].push_back(edges[i][1]);
graph[edges[i][1]].push_back(edges[i][0]);
}
map<pair<int, int>, bool> sorted;
for (int i = 0; i < n; ++i)
sorted.insert(make_pair(make_pair(degrees[i + 1], i + 1), 1));
vector<int> ans(m);
for (int i = m - 1; i >= 0; --i) {
while ((!sorted.empty()) and sorted.begin()->first.first < k) {
int temp = sorted.begin()->first.second;
sorted.erase(sorted.begin());
degrees[temp] = 0;
for (int i = 0; i < graph[temp].size(); ++i) {
auto it =
sorted.find(make_pair(degrees[graph[temp][i]], graph[temp][i]));
degrees[graph[temp][i]]--;
if (it != sorted.end()) {
sorted.erase(it);
if (degrees[graph[temp][i]] >= 0)
sorted.insert(make_pair(
make_pair(degrees[graph[temp][i]], graph[temp][i]), 1));
}
}
}
ans[i] = sorted.size();
graph[edges[i][0]].pop_back();
graph[edges[i][1]].pop_back();
if (degrees[edges[i][0]] >= k and degrees[edges[i][1]] >= k) {
auto it1 = sorted.find(make_pair(degrees[edges[i][0]], edges[i][0]));
auto it2 = sorted.find(make_pair(degrees[edges[i][1]], edges[i][1]));
degrees[edges[i][0]]--;
degrees[edges[i][1]]--;
if (it1 != sorted.end()) {
sorted.erase(it1);
sorted.insert(
make_pair(make_pair(degrees[edges[i][0]], edges[i][0]), 1));
}
if (it2 != sorted.end()) {
sorted.erase(it2);
sorted.insert(
make_pair(make_pair(degrees[edges[i][1]], edges[i][1]), 1));
}
}
}
for (int i = 0; i < m; ++i) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1037e {
public static void main(String[] args) throws IOException {
int n = rni(), m = ni(), k = ni(), deg[] = new int[n];
List<p> e = new ArrayList<>();
TreeSet<p> set = new TreeSet<>((a, b) -> a.a == b.a ? a.b - b.b : a.a - b.a);
Graph g = graph(n);
for (int i = 0; i < m; ++i) {
p ed = new p(rni() - 1, ni() - 1);
g.c(ed.a, ed.b);
e.add(ed);
++deg[ed.a];
++deg[ed.b];
}
for (int i = 0; i < n; ++i) {
set.add(new p(deg[i], i));
}
while (!set.isEmpty() && set.first().a < k) {
p p = set.first();
set.remove(p);
for (int i : g.get(p.b)) {
if (deg[i] >= k) {
set.remove(new p(deg[i], i));
set.add(new p(--deg[i], i));
}
}
}
int ans[] = new int[m];
ans[m - 1] = set.size();
for (int i = m - 1; i > 0; --i) {
int a = e.get(i).a, b = e.get(i).b;
boolean del_a = deg[a] >= k, del_b = deg[b] >= k;
g.get(a).remove(b);
g.get(b).remove(a);
if (del_a && del_b) {
set.remove(new p(deg[a], a));
set.add(new p(--deg[a], a));
set.remove(new p(deg[b], b));
set.add(new p(--deg[b], b));
while (!set.isEmpty() && set.first().a < k) {
p p = set.first();
set.remove(p);
for (int j : g.get(p.b)) {
if (deg[j] >= k) {
set.remove(new p(deg[j], j));
set.add(new p(--deg[j], j));
}
}
}
}
ans[i - 1] = set.size();
}
for (int i = 0; i < m; ++i) {
prln(ans[i]);
}
close();
}
static Graph graph(int n) {
Graph g = new Graph();
for (int i = 0; i < n; ++i) {
g.add(new HashSet<>());
}
return g;
}
static Graph graph(int n, int m) throws IOException {
Graph g = graph(n);
for (int i = 0; i < m; ++i) {
g.c(rni() - 1, ni() - 1);
}
return g;
}
static Graph tree(int n) throws IOException {
return graph(n, n - 1);
}
static class Graph extends ArrayList<Set<Integer>> {
void cto(int u, int v) {
get(u).add(v);
}
void c(int u, int v) {
cto(u, v);
cto(v, u);
}
boolean is_leaf(int i) {
return get(i).size() == 1;
}
}
static class p {
int a, b;
p(int a_, int b_) {
a = a_;
b = b_;
}
@Override
public String toString() {
return "Pair{" + "a = " + a + ", b = " + b + '}';
}
public boolean asymmetricEquals(Object o) {
p p = (p) o;
return a == p.a && b == p.b;
}
public boolean symmetricEquals(Object o) {
p p = (p) o;
return a == p.a && b == p.b || a == p.b && b == p.a;
}
@Override
public boolean equals(Object o) {
return asymmetricEquals(o);
}
public int asymmetricHashCode() {
return Objects.hash(a, b);
}
public int symmetricHashCode() {
return Objects.hash(a, b) + Objects.hash(b, a);
}
@Override
public int hashCode() {
return asymmetricHashCode();
}
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}
} |
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using std::queue;
const int N = 2e5 + 6;
template <class T>
inline void read(T &x) {
T f = 1;
x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
x *= f;
}
struct Edge {
int u, v, id;
} e[N << 1];
int head[N], ecnt;
inline void addedge(int u, int v, int id) {
e[++ecnt].v = v;
e[ecnt].u = head[u];
head[u] = ecnt;
e[ecnt].id = id;
}
inline void add(int u, int v, int id) {
addedge(u, v, id);
addedge(v, u, id);
}
int n, m, k, du[N], a[N], b[N], del[N], u, ans, inq[N], Ans[N], len;
queue<int> q;
signed main() {
read(n);
read(m);
read(k);
for (int i = 1; i <= m; i++) {
read(a[i]);
read(b[i]);
du[a[i]]++;
du[b[i]]++;
add(a[i], b[i], i);
}
for (int i = 1; i <= n; i++)
if (du[i] < k) q.push(i), inq[i] = 1;
ans = n;
while (!q.empty()) {
u = q.front();
q.pop();
ans--;
du[u] = 0;
for (int i = head[u], v; i && (v = e[i].v); i = e[i].u) {
if (inq[v]) continue;
if (del[e[i].id]) continue;
du[v]--;
del[e[i].id] = 1;
if (du[v] < k) q.push(v), inq[v] = 1;
}
}
for (int i = m; i >= 1; i--) {
Ans[++len] = ans;
if (del[i]) continue;
du[a[i]]--;
du[b[i]]--;
del[i] = 1;
if (!inq[a[i]] && du[a[i]] < k) q.push(a[i]), inq[a[i]] = 1;
if (!inq[b[i]] && du[b[i]] < k) q.push(b[i]), inq[b[i]] = 1;
while (!q.empty()) {
u = q.front();
q.pop();
ans--;
du[u] = 0;
for (int i = head[u], v; i && (v = e[i].v); i = e[i].u) {
if (inq[v]) continue;
if (del[e[i].id]) continue;
du[v]--;
del[e[i].id] = 1;
if (du[v] < k) q.push(v), inq[v] = 1;
}
}
}
for (int i = m; i >= 1; i--) printf("%d\n", Ans[i]);
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long n, m, k, r[200005], redd[200005], t, xx[200005], yy[200005],
ans[200005];
vector<pair<long long, long long> > v[200005];
void red(long long x, long long time) {
if (time == 5) {
}
redd[x] = 1;
t++;
for (int i = 0; i < v[x].size(); i++) {
if (v[x][i].first > time) break;
r[v[x][i].second]--;
if (r[v[x][i].second] < k && redd[v[x][i].second] == 0) {
red(v[x][i].second, time);
}
}
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> xx[i] >> yy[i];
v[xx[i]].push_back(make_pair(i, yy[i]));
r[xx[i]]++;
v[yy[i]].push_back(make_pair(i, xx[i]));
r[yy[i]]++;
}
for (int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end());
ans[m] = n;
for (int i = 1; i <= n; i++) {
if (r[i] < k && redd[i] == 0) {
t = 0;
red(i, m);
ans[m] -= t;
}
}
for (int i = m; i >= 2; i--) {
for (int j = 1; j <= n; j++) {
}
if (redd[yy[i]] == 0) r[xx[i]]--;
t = 0;
if (r[xx[i]] < k && redd[xx[i]] == 0) {
red(xx[i], i);
ans[i - 1] = ans[i] - t;
continue;
}
if (redd[xx[i]] == 0) r[yy[i]]--;
if (r[yy[i]] < k && redd[yy[i]] == 0) {
if (xx[i] == 5) {
}
r[xx[i]]++;
red(yy[i], i);
}
ans[i - 1] = ans[i] - t;
}
for (int i = 1; i <= m; i++) {
cout << ans[i] << endl;
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.util.*;
import java.io.*;
import java.math.*;
public class G {
public static void main(String[] args) throws IOException {
//PrintWriter out = new PrintWriter(new File("out.txt"));
PrintWriter out = new PrintWriter(System.out);
//Reader in = new Reader(new FileInputStream("in.txt"));
Reader in = new Reader();
G solver = new G();
solver.solve(out, in);
out.flush();
out.close();
}
static int maxn = (int)1e4+5;
static long mod=(int)1e9+7;
static int n, m, t, k;
static ArrayList<Edge> adj[];
static boolean[] vis;
static int[] lvl;
void solve(PrintWriter out, Reader in) throws IOException{
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
lvl = new int[n+1];
vis = new boolean[n+1];
adj = new ArrayList[n+1];
for (int i = 1; i <= n; i++)
adj[i] = new ArrayList<Edge>();
int[][] edge = new int[m][2];
int u,v;
for (int i = 0; i < m; i++) {
u = in.nextInt();
v = in.nextInt();
edge[i][0] = u;
edge[i][1] = v;
lvl[u]++; lvl[v]++;
adj[u].add(new Edge(v, i));
adj[v].add(new Edge(u, i));
}
TreeSet<Node> pq = new TreeSet<>();
for (int i = 1; i <= n; i++)
pq.add(new Node(lvl[i], i));
Node elm;
int[] ans = new int[m];
for (int i = m-1; i >= 0; i--) {
while (pq.size()!= 0 && pq.first().lvl < k) {
elm = pq.pollFirst();
vis[elm.id] = true;
for (Edge e:adj[elm.id]) {
if (!vis[e.to] && e.id <= i) {
pq.remove(new Node(lvl[e.to], e.to));
lvl[e.to]--;
pq.add( new Node(lvl[e.to], e.to));
}
}
}
u = edge[i][0]; v = edge[i][1];
if (!vis[u] && !vis[v]) {
pq.remove(new Node(lvl[u], u));
lvl[u]--;
pq.add(new Node(lvl[u], u));
pq.remove(new Node(lvl[v], v));
lvl[v]--;
pq.add(new Node(lvl[v], v));
}
ans[i] = pq.size();
}
for (int i = 0; i < m; i++) {
out.println(ans[i]);
}
}
//<>
static class Node implements Comparable<Node> {
int lvl, id;
Node (int lvl, int id) {
this.lvl = lvl;
this.id = id;
}
public int compareTo(Node o) {
if (this.lvl != o.lvl) return this.lvl - o.lvl;
return this.id - o.id;
}
}
static class Edge {
int to, id;
Edge(int to, int id) {
this.to = to;
this.id = id;
}
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int 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 next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble()
{
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} |
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pradyumn
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static class TaskE {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int K = in.nextInt();
List<int[]>[] g = new List[n];
for (int i = 0; i < n; ++i) g[i] = new ArrayList<>();
int[] from = new int[m];
int[] to = new int[m];
for (int i = 0; i < m; ++i) {
int u = in.nextInt() - 1;
int v = in.nextInt() - 1;
g[u].add(new int[]{v, i});
g[v].add(new int[]{u, i});
from[i] = u;
to[i] = v;
}
int[] deg = new int[n];
int[] ans = new int[m];
int[] queue = new int[n];
int sizeQ = 0;
int ptr = 0;
boolean[] alive = new boolean[m];
Arrays.fill(alive, true);
for (int i = 0; i < n; ++i) {
deg[i] = g[i].size();
if (deg[i] < K) {
queue[sizeQ++] = i;
}
}
for (int step = m - 1; step >= 0; --step) {
while (ptr < sizeQ) {
int cur = queue[ptr++];
for (int[] edge : g[cur]) {
if (!alive[edge[1]]) continue;
alive[edge[1]] = false;
if (deg[edge[0]] == K) queue[sizeQ++] = edge[0];
--deg[edge[0]];
--deg[cur];
}
}
ans[step] = n - sizeQ;
if (alive[step]) {
int cur = from[step];
int next = to[step];
if (deg[cur] == K) queue[sizeQ++] = cur;
if (deg[next] == K) queue[sizeQ++] = next;
--deg[cur];
--deg[next];
alive[step] = false;
}
}
for (int aa : ans) out.println(aa);
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int pnumChars;
public FastReader(InputStream stream) {
this.stream = stream;
}
private int pread() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = pread();
while (isSpaceChar(c))
c = pread();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = pread();
}
int res = 0;
do {
if (c == ',') {
c = pread();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = pread();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int m, n, k, d[N];
set<int> ad[N];
int ans[N], dd[N];
pair<int, int> ed[N];
int main() {
;
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
ad[u].insert(v);
ad[v].insert(u);
d[u]++;
d[v]++;
ed[i] = {u, v};
}
queue<int> q;
for (int i = 1; i <= n; ++i)
if (d[i] < k) q.push(i), dd[i] = 1;
ans[m] = n;
while (!q.empty()) {
int uu = q.front();
q.pop();
for (auto &vv : ad[uu]) {
d[vv]--;
ad[vv].erase(uu);
if (d[vv] < k && !dd[vv]) {
dd[vv] = 1;
q.push(vv);
}
}
ans[m]--;
d[uu] = 0;
ad[uu].clear();
}
for (int i = m - 1; i >= 1; --i) {
ans[i] = ans[i + 1];
if (dd[ed[i + 1].first] || dd[ed[i + 1].second]) continue;
int u = ed[i + 1].first, v = ed[i + 1].second;
ad[u].erase(v);
ad[v].erase(u);
d[u]--;
d[v]--;
if (d[u] < k && !dd[u]) q.push(u), dd[u] = 1;
if (d[v] < k && !dd[v]) q.push(v), dd[v] = 1;
while (!q.empty()) {
int uu = q.front();
q.pop();
for (auto &vv : ad[uu]) {
d[vv]--;
ad[vv].erase(uu);
if (d[vv] < k && !dd[vv]) {
dd[vv] = 1;
q.push(vv);
}
}
ans[i]--;
d[uu] = 0;
ad[uu].clear();
}
}
for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | 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);
vector<bool> used(n);
fill(used.begin(), used.end(), false);
while (good.size()) {
while (q.size()) {
long long V = q.front();
q.pop();
used[V] = true;
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 (used[K] || 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
const int N = 200005;
using namespace std;
int n, m, k, deg[N], ans[N];
set<pair<int, int> > st;
vector<pair<int, int> > g[N], h;
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
h.push_back({u, v});
g[u].push_back({v, i});
g[v].push_back({u, i});
deg[u]++;
deg[v]++;
}
for (int i = 1; i <= n; i++) st.insert({deg[i], i});
while (st.size() && (*(st.begin())).first < k) {
pair<int, int> e = *st.begin();
st.erase(e);
int u = e.second;
for (auto E : g[u]) {
int el = E.first;
if (st.count({deg[el], el})) {
st.erase({deg[el], el});
deg[el]--;
st.insert({deg[el], el});
}
}
}
for (int i = m - 1; i >= 0; i--) {
ans[i] = st.size();
int u = h[i].first, v = h[i].second;
if (st.count({deg[u], u}) && st.count({deg[v], v})) {
st.erase({deg[u], u});
deg[u]--;
st.insert({deg[u], u});
st.erase({deg[v], v});
deg[v]--;
st.insert({deg[v], v});
while (st.size() && (*(st.begin())).first < k) {
pair<int, int> e = *st.begin();
st.erase(e);
int u = e.second;
for (auto E : g[u]) {
int el = E.first;
if (E.second >= i) continue;
if (st.count({deg[el], el})) {
st.erase({deg[el], el});
deg[el]--;
st.insert({deg[el], el});
}
}
}
}
}
for (int i = 0; i < m; i++) cout << ans[i] << '\n';
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int deg[200005];
vector<pair<int, int> > g[200005];
bool notInSet[200005];
int main() {
int n, m, k, x, y, i;
cin >> n >> m >> k;
vector<pair<int, int> > v;
for (i = 0; i < m; i++) {
cin >> x >> y;
v.push_back({x, y});
deg[x]++, deg[y]++;
g[x].push_back({y, i});
g[y].push_back({x, i});
}
set<pair<int, int> > s;
for (i = 1; i <= n; i++) {
s.insert({deg[i], i});
}
while (!s.empty() && deg[s.begin()->second] < k) {
int u = s.begin()->second;
for (auto it : g[u]) {
int x = it.first;
if (!notInSet[x]) {
s.erase(s.find({deg[x], x}));
deg[x]--;
s.insert({deg[x], x});
}
}
s.erase({deg[u], u});
notInSet[u] = 1;
}
vector<int> res(m);
for (i = m - 1; i >= 0; i--) {
res[i] = s.size();
x = v[i].first, y = v[i].second;
if (!notInSet[x] && !notInSet[y]) {
s.erase(s.find({deg[x], x}));
s.erase(s.find({deg[y], y}));
deg[x]--, deg[y]--;
s.insert({deg[y], y});
s.insert({deg[x], x});
while (!s.empty() && deg[s.begin()->second] < k) {
int u = s.begin()->second;
for (auto it : g[u]) {
int x = it.first;
int y = it.second;
if (y >= i) continue;
if (!notInSet[x]) {
s.erase(s.find({deg[x], x}));
deg[x]--;
s.insert({deg[x], x});
}
}
s.erase({deg[u], u});
notInSet[u] = 1;
}
}
}
for (i = 0; i < m; i++) cout << res[i] << endl;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int d[MAXN];
int a[MAXN], b[MAXN], f[MAXN], ans[MAXN];
int n, m, k;
int q[MAXN], fr, re;
vector<int> E[MAXN];
set<long long> S;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; ++i) {
scanf("%d%d", a + i, b + i);
d[a[i]]++, d[b[i]]++;
E[a[i]].push_back(b[i]);
E[b[i]].push_back(a[i]);
}
for (int i = 1; i <= n; ++i) {
if (d[i] < k) {
f[i] = 1;
q[re++] = i;
}
}
ans[m] = n;
while (fr < re) {
int u = q[fr++];
ans[m]--;
for (int i = 0; i < E[u].size(); ++i) {
int v = E[u][i];
if (f[v]) continue;
if (--d[v] < k) {
q[re++] = v;
f[v] = 1;
}
}
}
for (int i = m - 1; i >= 0; --i) {
ans[i] = ans[i + 1];
int u1 = a[i], u2 = b[i];
if (f[u1] || f[u2]) continue;
S.insert((long long)u1 * n + u2);
S.insert((long long)u2 * n + u1);
d[u1]--, d[u2]--;
if (d[u1] < k) {
fr = re = 0;
q[re++] = u1;
f[u1] = 1;
while (fr < re) {
int u = q[fr++];
ans[i]--;
for (int i = 0; i < E[u].size(); ++i) {
int v = E[u][i];
if (f[v] || S.find((long long)u * n + v) != S.end()) continue;
if (--d[v] < k) {
f[v] = 1;
q[re++] = v;
}
}
}
}
if (!f[u2] && d[u2] < k) {
fr = re = 0;
q[re++] = u2;
f[u2] = 1;
while (fr < re) {
int u = q[fr++];
ans[i]--;
for (int i = 0; i < E[u].size(); ++i) {
int v = E[u][i];
if (f[v] || S.find((long long)u * n + v) != S.end()) continue;
if (--d[v] < k) {
f[v] = 1;
q[re++] = v;
}
}
}
}
}
for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int max_n = 200005;
struct edge {
int u, v, next;
} G[max_n * 2];
int head[max_n];
int total;
bool visit[max_n * 2];
int in[max_n];
set<pair<int, int> > s;
int n, m, k;
int u, v;
int ans[max_n];
void add_edge(int u, int v) {
G[total].u = u;
G[total].v = v;
G[total].next = head[u];
head[u] = total++;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
total = 0;
memset(head, -1, sizeof(head));
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
in[u]++;
in[v]++;
}
for (int i = 1; i <= n; i++) s.insert(pair<int, int>(in[i], i));
pair<int, int> p;
for (int i = 2 * m - 1; i >= 0; i -= 2) {
while (!s.empty()) {
p = *s.begin();
if (p.first < k) {
s.erase(p);
for (int j = head[p.second]; ~j; j = G[j].next) {
if (!visit[j]) {
v = G[j].v;
s.erase(pair<int, int>(in[v], v));
in[v]--;
s.insert(pair<int, int>(in[v], v));
visit[j] = visit[j ^ 1] = true;
}
}
} else
break;
}
ans[i / 2] = s.size();
if (!visit[i]) {
u = G[i].u;
v = G[i].v;
s.erase(pair<int, int>(in[u], u));
in[u]--;
s.insert(pair<int, int>(in[u], u));
s.erase(pair<int, int>(in[v], v));
in[v]--;
s.insert(pair<int, int>(in[v], v));
visit[i] = visit[i ^ 1] = true;
}
}
for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct edge {
int u, v;
};
int n, m, k, f[1000000 + 1], ans[1000000 + 1], q[1000000 + 1], lq = 0, rq = -1,
cur;
edge b[1000000 + 1];
vector<int> a[1000000 + 1], e[1000000 + 1];
bool remoed[1000000 + 1], removeti[1000000 + 1];
void removequeue() {
int x, y;
while (lq <= rq) {
x = q[lq];
lq++;
cur--;
for (int i = 0; i < a[x].size(); i++) {
y = a[x][i];
if (remoed[e[x][i]] == false) {
remoed[e[x][i]] = true;
f[y]--;
if (f[y] < k && removeti[y] == false) {
rq++;
q[rq] = y;
removeti[y] = true;
}
}
}
}
}
void removemany() {
for (int i = 1; i <= n; i++) {
if (f[i] < k) {
rq++;
q[rq] = i;
removeti[i] = true;
}
}
removequeue();
}
void solve() {
removemany();
for (int i = m - 1; i >= 0; i--) {
ans[i] = cur;
if (remoed[i] == false) {
remoed[i] = true;
f[b[i].u]--;
f[b[i].v]--;
if (f[b[i].u] < k && removeti[b[i].u] == false) {
rq++;
q[rq] = b[i].u;
removeti[b[i].u] = true;
}
if (f[b[i].v] < k && removeti[b[i].v] == false) {
rq++;
q[rq] = b[i].v;
removeti[b[i].v] = true;
}
}
removequeue();
}
for (int i = 0; i < m; i++) cout << ans[i] << "\n";
}
void readit() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> b[i].u >> b[i].v;
a[b[i].u].push_back(b[i].v);
a[b[i].v].push_back(b[i].u);
e[b[i].u].push_back(i);
e[b[i].v].push_back(i);
f[b[i].u]++;
f[b[i].v]++;
}
cur = n;
}
int main() {
readit();
solve();
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author BSRK Aditya
*/
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);
ETrips solver = new ETrips();
solver.solve(1, in, out);
out.close();
}
static class ETrips {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt(), m = in.readInt(), k = in.readInt();
ArrayList<Integer>[] adj = new ArrayList[n];
HashMap<P, Boolean> edgeMap = new HashMap<>();
P[] edges = new P[m];
for (int i = 0; i < n; ++i)
adj[i] = new ArrayList<>();
for (int i = 0; i < m; ++i) {
int x = in.readInt() - 1, y = in.readInt() - 1;
P e = new P(Integer.min(x, y), Integer.max(x, y));
edges[i] = e;
edgeMap.put(e, true);
adj[x].add(y);
adj[y].add(x);
}
//PriorityQueue<P> vertices = new PriorityQueue<>();
TreeSet<P> vertices = new TreeSet<>();
HashMap<Integer, Integer> vc = new HashMap<>();
for (int i = 0; i < n; ++i) {
vertices.add(new P(adj[i].size(), i));
vc.put(i, adj[i].size());
}
int count = n;
while (!vertices.isEmpty() && vertices.first().x < k) {
count--;
P p = vertices.pollFirst();
int v1 = p.y;
for (int v2 : adj[v1]) {
P e = new P(Integer.min(v1, v2), Integer.max(v1, v2));
if (edgeMap.get(e)) {
edgeMap.put(e, false);
int v2c = vc.get(v2);
vertices.remove(new P(v2c, v2));
vertices.add(new P(v2c - 1, v2));
vc.put(v2, v2c - 1);
}
}
}
int[] ans = new int[m];
ans[m - 1] = count;
for (int i = m - 2; i >= 0; i--) {
P e = edges[i + 1];
if (edgeMap.get(e)) {
edgeMap.put(e, false);
int v1 = e.x, v2 = e.y;
int v1c = vc.get(v1), v2c = vc.get(v2);
vertices.remove(new P(v1c, v1));
vertices.add(new P(v1c - 1, v1));
vc.put(v1, v1c - 1);
vertices.remove(new P(v2c, v2));
vertices.add(new P(v2c - 1, v2));
vc.put(v2, v2c - 1);
while (!vertices.isEmpty() && vertices.first().x < k) {
count--;
P p = vertices.pollFirst();
int v3 = p.y;
for (int v4 : adj[v3]) {
P e2 = new P(Integer.min(v3, v4), Integer.max(v3, v4));
if (edgeMap.get(e2)) {
edgeMap.put(e2, false);
int v4c = vc.get(v4);
vertices.remove(new P(v4c, v4));
vertices.add(new P(v4c - 1, v4));
vc.put(v4, v4c - 1);
}
}
}
}
ans[i] = count;
}
for (int i = 0; i < m; ++i) out.printLine(ans[i]);
}
class P implements Comparable<P> {
final int x;
final int y;
P(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(P o) {
int v1 = Integer.compare(x, o.x);
if (v1 != 0) return v1;
else return Integer.compare(y, o.y);
}
public boolean equals(Object obj) {
P p = (P) obj;
return x == p.x && y == p.y;
}
public int hashCode() {
return x + 3 * y;
}
}
}
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 print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
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 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 readInt() {
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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | 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++) pq.push(make_pair(-deg[i], i));
for (int i = m - 1; i >= 0; i--) {
while (!pq.empty()) {
pair<int, int> p = pq.top();
pq.pop();
if (-p.first >= k) {
pq.push(p);
break;
}
if (done[p.second]) continue;
done[p.second] = true;
good--;
int ix = p.second;
for (set<int>::iterator it = adj[ix].begin(); it != adj[ix].end(); it++) {
deg[*it]--;
adj[*it].erase(ix);
pq.push(make_pair(-deg[*it], *it));
}
deg[ix] = 0;
adj[ix].clear();
}
ans[i] = good;
if (adj[vv[i].first].find(vv[i].second) != adj[vv[i].first].end()) {
deg[vv[i].first]--;
pq.push(make_pair(-deg[vv[i].first], vv[i].first));
deg[vv[i].second]--;
pq.push(make_pair(-deg[vv[i].second], vv[i].second));
adj[vv[i].first].erase(vv[i].second);
adj[vv[i].second].erase(vv[i].first);
}
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int n, m, k, X[MAXN], Y[MAXN], deg[MAXN], cnt;
vector<pair<int, int> > E[MAXN];
bool removed[MAXN];
void ukloni(int x, int tijme) {
if (!removed[x] && deg[x] < k) {
removed[x] = true;
cnt--;
for (auto e : E[x])
if (e.second < tijme) {
deg[e.first]--;
ukloni(e.first, tijme);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < m; ++i) {
cin >> X[i] >> Y[i];
E[X[i]].push_back(pair<int, int>(Y[i], i));
E[Y[i]].push_back(pair<int, int>(X[i], i));
deg[X[i]]++;
deg[Y[i]]++;
}
cnt = n;
for (int i = 1; i < n + 1; ++i) ukloni(i, MAXN);
vector<int> sol;
for (int i = m - 1; i >= 0; --i) {
sol.push_back(cnt);
if (!removed[X[i]] && !removed[Y[i]]) {
deg[X[i]]--;
deg[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long M = 2e5 + 10;
vector<long long> v[200009], deg(M), vis(M);
map<pair<long long, long long>, long long> ed;
void dfs(long long s, long long k, long long p, long long &ans) {
vis[s] = 1;
ans--;
for (auto u : v[s]) {
pair<long long, long long> p1;
p1.first = u;
p1.second = s;
if (u != p && ed.find(p1) == ed.end()) {
deg[u]--;
deg[s]--;
ed[make_pair(u, s)] = 1;
ed[make_pair(s, u)] = 1;
}
if (vis[u] == 0 && deg[u] < k) {
dfs(u, k, s, ans);
}
}
return;
}
int main() {
long long n, m, k;
cin >> n >> m >> k;
vector<pair<long long, long long> > edge;
for (long long i = 0; i < m; i++) {
long long a, b;
scanf("%lld%lld", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
edge.push_back(make_pair(a, b));
deg[a]++;
deg[b]++;
}
long long ans = n;
vector<long long> ans1(m);
vis[0] = 1;
for (long long i = 1; i <= n; i++) {
if (deg[i] < k && vis[i] == 0) {
dfs(i, k, 0, ans);
}
}
for (long long i = m - 1; i >= 0; i--) {
ans1[i] = ans;
long long a = edge[i].first;
long long b = edge[i].second;
if (vis[a] == 0 && vis[b] == 0 && ed.find(make_pair(a, b)) == ed.end()) {
deg[a]--;
deg[b]--;
ed[make_pair(a, b)] = 1;
ed[make_pair(b, a)] = 1;
}
if (deg[a] < k && vis[a] == 0) {
dfs(a, k, b, ans);
}
if (deg[b] < k && vis[b] == 0) {
dfs(b, k, a, ans);
}
}
for (long long i = 0; i < m; i++) {
printf("%lld\n", ans1[i]);
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
struct edge {
int u, v, ne, num;
} e[maxn];
int n, m, k, sum;
int d[maxn], no[maxn], vis[maxn];
int head[maxn], len;
int ans[maxn];
void add(int u, int v, int num) {
e[len].u = u;
e[len].v = v;
e[len].num = num;
e[len].ne = head[u];
head[u] = len++;
}
void del(int x) {
sum--;
no[x] = 1;
for (int i = head[x]; i != -1; i = e[i].ne) {
int v = e[i].v;
if (no[v] || vis[e[i].num]) continue;
vis[e[i].num] = 1;
d[v]--;
if (d[v] < k) del(v);
}
return;
}
int main() {
memset(head, -1, sizeof(head));
cin >> n >> m >> k;
for (int i = 1, x, y; i <= m; i++) {
scanf("%d %d", &x, &y);
add(x, y, i);
add(y, x, i);
d[x]++;
d[y]++;
}
sum = n;
for (int i = 1; i <= n; i++) {
if (d[i] >= k || no[i]) continue;
del(i);
}
ans[m] = sum;
for (int i = len - 1; i > 0; i -= 2) {
int u = e[i].u, v = e[i].v;
if (!vis[e[i].num]) {
vis[e[i].num] = 1;
if (!no[u]) {
d[u]--;
if (d[u] < k) del(u);
}
if (!no[v]) {
d[v]--;
if (d[v] < k) del(v);
}
}
ans[(i + 1) / 2 - 1] = sum;
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, m, k, used[N], w[N], all, deg[N];
bool c[N];
vector<set<int> > g(N);
vector<int> res;
vector<pair<int, int> > reb;
queue<int> q;
void bfs() {
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto& u : g[v]) {
if (deg[u] == k) {
deg[u]--;
q.push(u);
} else
deg[u]--;
}
}
}
void ddfs(int v) {
for (auto& u : g[v]) {
if (w[u] == 1) {
w[u] = 0;
c[u] = 0;
all--;
ddfs(u);
} else
w[u]--;
}
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].insert(b);
g[b].insert(a);
deg[a]++;
deg[b]++;
reb.push_back({a, b});
}
for (int i = 0; i < n; i++) {
if (deg[i] < k) {
q.push(i);
}
}
bfs();
for (int i = 0; i < n; i++)
if (deg[i] >= k) c[i] = 1;
for (int i = 0; i < n; i++) {
int x = 0;
for (auto& v : g[i]) x += c[v];
w[i] = max(0, x - k + 1);
all += c[i];
}
res.push_back(all);
for (int i = m - 1; i >= 0; i--) {
int a = reb[i].first, b = reb[i].second;
if (c[a] && c[b]) {
if (w[a] == 1) {
w[a] = 0;
all--;
c[a] = 0;
ddfs(a);
} else if (w[b] == 1) {
w[b] = 0;
all--;
c[b] = 0;
ddfs(b);
} else {
w[a]--;
w[b]--;
}
}
g[a].erase(b);
g[b].erase(a);
res.push_back(all);
}
for (int i = m - 1; i >= 0; i--) cout << res[i] << '\n';
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct Edge {
int t, id, next;
Edge() {}
Edge(int a, int b, int c) : t(a), id(b), next(c) {}
};
Edge e[400005];
int head[200005], ans[200005], k;
int cur;
int d[200005], sum;
bool col[200005];
void dfs(int x) {
col[x] = 0;
sum--;
for (int i = head[x]; i; i = e[i].next)
if (col[e[i].t] && e[i].id < cur) {
int u = e[i].t;
d[u]--;
if (d[u] < k) dfs(u);
}
}
pair<int, int> a[200005];
int main() {
int n, m;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
e[2 * i - 1] = Edge(y, i, head[x]);
head[x] = 2 * i - 1;
e[2 * i] = Edge(x, i, head[y]);
head[y] = 2 * i;
d[x]++;
d[y]++;
a[i] = pair<int, int>(x, y);
}
for (int i = 1; i <= n; i++) col[i] = 1;
sum = n;
cur = m + 1;
for (int i = 1; i <= n; i++)
if (col[i] && d[i] < k) dfs(i);
for (int i = m; i > 0; i--) {
ans[i] = sum;
int x = a[i].first, y = a[i].second;
cur--;
if (col[x] && col[y]) {
d[x]--;
d[y]--;
if (col[x] && d[x] < k) dfs(x);
if (col[y] && d[y] < k) dfs(y);
}
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int n, m, k, indi[(int)5e5 + 110];
set<int> gra[(int)5e5 + 110];
int arr[(int)5e5 + 110], bra[(int)5e5 + 110], result[(int)5e5 + 110],
donno[(int)5e5 + 110];
int track;
void funck(int u) {
queue<int> pq;
pq.push(u);
while (!pq.empty()) {
u = pq.front();
pq.pop();
if (donno[u]) continue;
donno[u] = 1;
track--;
for (int go : gra[u]) {
gra[go].erase(u);
if (--indi[go] < k) pq.push(go);
}
}
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
u--;
v--;
gra[u].insert(v);
gra[v].insert(u);
indi[u]++;
indi[v]++;
arr[i] = u;
bra[i] = v;
}
track = n;
for (int i = 0; i < n; ++i) {
if (donno[i] == 0 and indi[i] < k) {
funck(i);
}
}
for (int i = m - 1; i >= 0; i--) {
result[i] = track;
if (donno[arr[i]] == 0 and donno[bra[i]] == 0) {
indi[arr[i]]--;
indi[bra[i]]--;
gra[arr[i]].erase(bra[i]);
gra[bra[i]].erase(arr[i]);
if (indi[arr[i]] < k) funck(arr[i]);
if (indi[bra[i]] < k) funck(bra[i]);
}
}
for (int i = 0; i < m; ++i) {
cout << result[i] << endl;
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | /**
* Created by Baelish on 9/2/2018.
*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class E {
public static void main(String[] args) throws Exception {
FastReader in = new FastReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
g = genList(n);
int u[] = new int[m], v[] = new int[m];
degree = new int[n];
dead = new boolean[n];
for (int i = 0; i < m; i++) {
u[i] = in.nextInt()-1;
v[i] = in.nextInt()-1;
g[u[i]].add(v[i]);
g[v[i]].add(u[i]);
degree[u[i]]++;
degree[v[i]]++;
}
for (int i = 0; i < n; i++) {
if(degree[i] < k) dfs(i);
}
int ans[] = new int[m];
ans[m-1] = n - count;
for (int i = m-1; i >= 1; i--) {
if(removedEdge.add( getHash(v[i], u[i]))) {
degree[u[i]]--;
degree[v[i]]--;
dfs(u[i]);
dfs(v[i]);
}
ans[i-1] = n - count;
if(ans[i-1] == 0) break;
}
for(int i : ans) pw.println(i);
pw.close();
}
static List<Integer> g[];
static boolean dead[];
static int count = 0, n, m, k;
static int degree[];
static HashSet<Long> removedEdge = new HashSet<>();
static long getHash(long u, long v){
return (min(u, v) << 30) | max(u, v);
}
static void dfs(int u){
if(dead[u] || degree[u] >= k) return;
count++;
dead[u] = true;
for(int v : g[u]){
if(!removedEdge.add( getHash(v, u))) continue;
degree[u]--;
degree[v]--;
dfs(v);
}
}
static <T>List<T>[] genList(int n){
List<T> list[] = new List[n];
for(int i = 0; i < n; i++) list[i] = new ArrayList<T>();
return list;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0, charArrayLength = (int) 1e3;
public FastReader(InputStream is) {
this.is = is;
}
public FastReader(InputStream is, int charArrayLength) {
this.is = is;
this.charArrayLength = charArrayLength;
}
public int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
public String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public String nextLine() {
int b = readByte();
StringBuilder sb = new StringBuilder();
while (b != '\n' || b != '\r') {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char[] next(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
public char nextChar() {
return (char) skip();
}
private char buff[];
public char[] nextCharArray() {
if (buff == null) buff = new char[charArrayLength];
int b = skip(), p = 0;
while (!(isSpaceChar(b))) {
buff[p++] = (char) b;
b = readByte();
}
return Arrays.copyOf(buff, p);
}
}
} |
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void func(void) {
freopen("input.c", "r", stdin);
freopen("output.c", "w", stdout);
}
void print(vector<long long> &v) {
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
printf("%lld ", v[i]);
}
printf("\n");
}
void print(vector<pair<long long, long long> > &v) {
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
printf("%lld %lld\n", v[i].first, v[i].second);
}
}
void print(double d) { cout << fixed << setprecision(10) << d << endl; }
void print(string s, double d) {
cout << s << " ";
cout << fixed << setprecision(10) << d << endl;
}
const int N = 2e5 + 100;
set<int> s[N];
set<pair<int, int> > keep;
int in[N];
vector<pair<int, int> > v;
int answer[N];
void setAnswer(int x, int i, int n) { answer[i] = x; }
void finIt(int n) {
for (int i = 0; i < n; i++) {
if (answer[i] == -1) answer[i] = 0;
}
}
int getAnswer(int k) {
while (keep.size()) {
auto it = keep.begin();
pair<int, int> x = *it;
if (x.first >= k) break;
int y = x.second;
keep.erase(it);
for (auto it1 = s[y].begin(); it1 != s[y].end(); it1++) {
int z = *it1;
keep.erase(keep.find({in[z], z}));
in[z]--;
keep.insert({in[z], z});
s[z].erase(y);
}
s[y].clear();
}
return keep.size();
}
int main() {
int n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m, fg = 0,
mx = 0, mx1 = 0;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
s[x].insert(y);
s[y].insert(x);
v.push_back({x, y});
in[x]++;
in[y]++;
}
for (int i = 1; i <= n; i++) {
keep.insert({in[i], i});
}
int nowAnswer = getAnswer(k);
answer[m - 1] = nowAnswer;
for (int i = v.size() - 1; i >= 1; i--) {
int x = v[i].first;
int y = v[i].second;
if (s[x].find(y) == s[x].end()) {
answer[i - 1] = nowAnswer;
continue;
}
keep.erase(keep.find({in[x], x}));
keep.erase(keep.find({in[y], y}));
in[x]--;
in[y]--;
keep.insert({in[x], x});
keep.insert({in[y], y});
s[x].erase(y);
s[y].erase(x);
nowAnswer = getAnswer(k);
answer[i - 1] = nowAnswer;
}
for (int i = 0; i < m; i++) {
printf("%d\n", answer[i]);
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> adj[N];
int deg[N], ans[N];
pair<int, int> ed[N];
queue<int> lazy;
bool rem_e[N], rem_v[N];
int k, tot;
void erase(int i) {
if (rem_e[i]) return;
rem_e[i] = true;
for (int u : {ed[i].first, ed[i].second}) {
if (deg[u] == k) {
lazy.push(u);
}
deg[u]--;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m >> k;
tot = n;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
ed[i] = {u, v};
adj[u].push_back(i);
deg[u]++;
adj[v].push_back(i);
deg[v]++;
}
for (int i = 1; i <= n; i++)
if (deg[i] < k) lazy.push(i);
for (int i = m - 1; i >= 0; i--) {
while (!lazy.empty()) {
int u = lazy.front();
lazy.pop();
if (rem_v[u]) continue;
rem_v[u] = true;
tot--;
for (int i : adj[u]) erase(i);
}
ans[i] = tot;
erase(i);
}
for (int i = 0; i < m; i++) {
cout << ans[i] << ' ';
}
cout << '\n';
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Random;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in Actual solution is at the top
*
* @author ilyakor
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static class TaskE {
int k;
ArrayList<ii>[] g;
int[] q;
int[] deg;
boolean[] alive;
int T;
int cur;
public void solve(int testNumber, InputReader in, OutputWriter out) {
Random random = new Random(0xdead);
int n = in.nextInt();
int m = in.nextInt();
k = in.nextInt();
// int n = 1000, m = 10000;
// k = 10;
g = new ArrayList[n];
for (int i = 0; i < n; ++i) {
g[i] = new ArrayList<>();
}
ArrayList<ii> edges = new ArrayList<>();
for (int i = 0; i < m; ++i) {
int x = in.nextInt() - 1;
int y = in.nextInt() - 1;
// int x = random.nextInt(n);
// int y;
// do {
// y = random.nextInt(n);
// } while (y == x);
g[x].add(new ii(y, i));
g[y].add(new ii(x, i));
edges.add(new ii(x, y));
}
deg = new int[n];
for (int i = 0; i < n; ++i) {
deg[i] = g[i].size();
}
T = m - 1;
cur = n;
alive = new boolean[n];
q = new int[2 * n + 10];
Arrays.fill(alive, true);
for (int i = 0; i < n; ++i) {
update(i);
}
int[] res = new int[m];
for (int i = m - 1; i >= 0; --i) {
res[i] = cur;
int x = edges.get(i).first, y = edges.get(i).second;
--T;
if (alive[x]) {
--deg[y];
}
if (alive[y]) {
--deg[x];
}
update(x);
update(y);
}
for (int i = 0; i < m; ++i) {
out.printLine(res[i]);
}
}
private void update(int x) {
if (!alive[x]) {
return;
}
int ql = 0, qr = 0;
q[ql++] = x;
while (ql > qr) {
x = q[qr++];
if (!alive[x]) {
continue;
}
Assert.assertTrue(g[x].size() >= deg[x]);
if (deg[x] >= k) {
while (g[x].get(g[x].size() - 1).second > T) {
g[x].remove(g[x].size() - 1);
}
} else {
for (ii e : g[x]) {
int y = e.first;
if (alive[y] && deg[y] >= k && e.second <= T) {
--deg[y];
if (deg[y] < k) {
q[ql++] = y;
}
}
}
g[x].clear();
deg[x] = 0;
alive[x] = false;
--cur;
}
}
}
}
static class Assert {
public static void assertTrue(boolean flag) {
// if (!flag)
// while (true);
if (!flag) {
throw new AssertionError();
}
}
}
static class ii implements Comparable<ii> {
public int first;
public int second;
public ii() {
}
public ii(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ii ii = (ii) o;
if (first != ii.first) {
return false;
}
if (second != ii.second) {
return false;
}
return true;
}
public int hashCode() {
int result = first;
result = 31 * result + second;
return result;
}
public int compareTo(ii o) {
if (first != o.first) {
return first < o.first ? -1 : 1;
}
if (second != o.second) {
return second < o.second ? -1 : 1;
}
return 0;
}
public String toString() {
return "{" +
"first=" + first +
", second=" + second +
'}';
}
}
static class InputReader {
private InputStream stream;
private byte[] buffer = new byte[10000];
private int cur;
private int count;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static boolean isSpace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int read() {
if (count == -1) {
throw new InputMismatchException();
}
try {
if (cur >= count) {
cur = 0;
count = stream.read(buffer);
if (count <= 0) {
return -1;
}
}
} catch (IOException e) {
throw new InputMismatchException();
}
return buffer[cur++];
}
public int readSkipSpace() {
int c;
do {
c = read();
} while (isSpace(c));
return c;
}
public int nextInt() {
int sgn = 1;
int c = readSkipSpace();
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = res * 10 + c - '0';
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
}
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 print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INFTY = 20000000;
const int MAX = 500100;
const int MOD = 10000000;
void coutTab(int* tab, int n) {
for (int i = 0; i < n; i++) {
cout << tab[i] << " ";
}
cout << "\n";
}
int n, m, k;
vector<set<int> > G(MAX);
int s[MAX];
bool was[MAX];
pair<int, int> E[MAX];
int res[MAX];
void addToQueue(queue<int>& Q, int i) {
if (s[i] < k && !was[i]) {
was[i] = 1;
Q.push(i);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> E[i].first >> E[i].second;
E[i].first--;
E[i].second--;
G[E[i].first].insert(E[i].second);
G[E[i].second].insert(E[i].first);
}
for (int i = 0; i < n; i++) s[i] = (G[i]).size();
queue<int> Q;
int cur = n;
for (int i = 0; i < n; i++) addToQueue(Q, i);
for (int i = 0; i < m; i++) {
while (!Q.empty()) {
int j = Q.front();
Q.pop();
for (auto l : G[j]) {
s[l]--;
addToQueue(Q, l);
}
cur--;
}
res[i] = cur;
pair<int, int> lst = E[m - i - 1];
G[lst.first].erase(lst.second);
G[lst.second].erase(lst.first);
if (!was[lst.second]) s[lst.first]--;
if (!was[lst.first]) s[lst.second]--;
addToQueue(Q, lst.first);
addToQueue(Q, lst.second);
}
reverse(res, res + m);
for (int i = 0; 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;
template <typename T>
void cmin(T &x, T y) {
if (y < x) x = y;
}
template <typename T>
void cmax(T &x, T y) {
if (y > x) x = y;
}
long long qpow(long long x, long long n, long long mod = MOD) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
namespace Solver {
void InitOnce() {}
int n, m, k;
int u[200005];
int v[200005];
int deg[200005];
vector<pair<int, int> > G[200005];
void Read() {
int res = scanf("%d%d%d", &n, &m, &k);
if (res == -1) exit(0);
for (int i = 1; i <= n; ++i) G[i].clear();
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u[i], &v[i]);
G[u[i]].emplace_back(v[i], i);
G[v[i]].emplace_back(u[i], i);
}
for (int i = 1; i <= n; ++i) deg[i] = G[i].size();
}
bool vis[200005];
int ans[200005];
bool inQ[200005];
queue<int> Q;
void delEdge(int u, int v, int id) {
if (vis[id]) return;
vis[id] = 1;
--deg[u];
--deg[v];
if (inQ[v] == 0 && deg[v] < k) {
Q.push(v);
inQ[v] = 1;
}
}
void delEdge2(int u, int v, int id) {
if (vis[id]) return;
vis[id] = 1;
--deg[u];
--deg[v];
if (inQ[u] == 0 && deg[u] < k) {
Q.push(u);
inQ[u] = 1;
}
if (inQ[v] == 0 && deg[v] < k) {
Q.push(v);
inQ[v] = 1;
}
}
void Solve() {
memset(vis, 0, sizeof(vis));
memset(ans, 0, sizeof(ans));
memset(inQ, 0, sizeof(inQ));
for (int i = 1; i <= n; ++i) {
if (deg[i] < k) {
Q.push(i);
inQ[i] = 1;
}
}
int cur = n;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
--cur;
for (pair<int, int> &p : G[u]) {
int v = p.first, id = p.second;
delEdge(u, v, id);
}
}
for (int i = 1; i <= n; ++i) {
if (deg[i]) assert(deg[i] >= k);
}
ans[m] = cur;
for (int i = m - 1; i >= 0; --i) {
int de = i + 1;
delEdge2(u[de], v[de], de);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
--cur;
for (pair<int, int> &p : G[u]) {
int v = p.first, id = p.second;
delEdge(u, v, id);
}
}
ans[i] = cur;
}
for (int i = 1; i <= n; ++i) {
assert(deg[i] == 0);
assert(inQ[i] == 1);
}
for (int i = 1; i <= m; ++i) assert(vis[i] == 1);
for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
return;
}
} // namespace Solver
int main() {
Solver::InitOnce();
while (true) {
Solver::Read();
Solver::Solve();
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, int> > arr(m);
vector<vector<int> > G(n);
vector<int> in(n), vis(n);
for (auto& it : arr) {
int u, v;
cin >> u >> v;
in[--u]++;
in[--v]++;
it = {u, v};
G[u].push_back(v);
G[v].push_back(u);
}
queue<int> q;
for (int i = 0; i < n; i++) {
if (in[i] < k) {
q.push(i);
vis[i] = 1;
}
}
int cur = n - q.size();
set<pair<int, int> > cnt;
while (!q.empty()) {
auto x = q.front();
q.pop();
for (auto& it : G[x]) {
auto node = minmax(x, it);
if (cnt.find(node) == cnt.end()) {
cnt.insert(node);
if (!vis[it] && --in[it] < k) {
vis[it] = 1;
q.push(it);
cur--;
}
}
}
}
reverse(arr.begin(), arr.end());
vector<int> ans;
for (auto& it : arr) {
ans.push_back(cur);
auto node = minmax(it.first, it.second);
if (cnt.find(node) == cnt.end()) {
cnt.insert(node);
if (!vis[it.first] && --in[it.first] < k) {
vis[it.first] = 1;
q.push(it.first);
cur--;
}
if (!vis[it.second] && --in[it.second] < k) {
vis[it.second] = 1;
q.push(it.second);
cur--;
}
while (!q.empty()) {
auto x = q.front();
q.pop();
for (auto& jt : G[x]) {
auto node = minmax(x, jt);
if (cnt.find(node) == cnt.end()) {
cnt.insert(node);
if (!vis[jt] && --in[jt] < k) {
vis[jt] = 1;
q.push(jt);
cur--;
}
}
}
}
}
}
reverse(ans.begin(), ans.end());
for (auto& it : ans) cout << it << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | //package Cf.Codefest18;
import java.io.*;
import java.util.*;
/**
* Author : NoRainInNorthrend
* Date: 2018/9/3 18:51
*/
public class E {
public static int n, m, k;
public static List<Pair>[] g;
public static int[] degree;
public static int[] from, to;
public static void main(String[] args) throws IOException {
FastScanner scanner = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
n = scanner.nextInt();
m = scanner.nextInt();
k = scanner.nextInt();
degree = new int[n + 1];
from = new int[m + 1];
to = new int[m + 1];
g = new List[n + 1];
Set<Pair> treeSet = new TreeSet<>();
boolean[] inSet = new boolean[n + 1];
for(int i = 0; i <= n; i++) g[i] = new ArrayList<>();
for(int i = 0; i < m; i++){
from[i] = scanner.nextInt();
to[i] = scanner.nextInt();
g[from[i]].add(new Pair(to[i], i));
g[to[i]].add(new Pair(from[i], i));
degree[from[i]]++;
degree[to[i]]++;
}
for(int i = 1; i <= n; i++){
treeSet.add(new Pair(degree[i], i));
inSet[i] = true;
}
Iterator iterator;
while(true){
iterator = treeSet.iterator();
if(!iterator.hasNext()) break;
Pair front = (Pair)iterator.next();
if(front.first < k){
int u = front.second;
for(Pair pair : g[u]){
int v = pair.first;
if(inSet[v]){
treeSet.remove(new Pair(degree[v], v));
treeSet.add(new Pair(--degree[v] , v));
}
}
inSet[u] = false;
treeSet.remove(front);
}else {
break;
}
}
int[] ans = new int[m];
for(int i = m - 1; i >= 0; i--){
ans[i] = treeSet.size();
int x = from[i], y = to[i];
if(inSet[x] && inSet[y]){
treeSet.remove(new Pair(degree[x], x));
treeSet.add(new Pair(--degree[x], x));
treeSet.remove(new Pair(degree[y], y));
treeSet.add(new Pair(--degree[y], y));
while(true){
iterator = treeSet.iterator();
if(!iterator.hasNext()) break;
Pair front = (Pair)iterator.next();
if(front.first < k){
int u = front.second;
for(Pair pair : g[u]){
if(pair.second >= i) continue;
int v = pair.first;
if(inSet[v]){
treeSet.remove(new Pair(degree[v], v));
treeSet.add(new Pair(--degree[v] , v));
}
}
inSet[u] = false;
treeSet.remove(front);
}else {
break;
}
}
}
}
for(int i = 0; i < m; i++) out.println(ans[i]);
out.flush();
}
static class Pair implements Comparable<Pair>{
public int first;
public int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int compareTo(Pair o) {
if(this.first == o.first){
if(this.second == o.second){
return 0;
}else{
return this.second > o.second ? 1 : -1;
}
}else{
return this.first > o.first ? 1 : -1;
}
}
}
private static class FastScanner {
StringTokenizer st;
BufferedReader br;
public FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | java | import java.io.*;
import java.util.*;
public class e {
public static void main(String[] args) {
JS in = new JS();
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
HashSet<Integer> adj[] = new HashSet[n];
for(int i = 0; i < n; i++) adj[i] = new HashSet<>();
int friends[] = new int[n];
Stack<Integer> ans = new Stack<>(), uq = new Stack<>(), vq = new Stack<>();
for(int i = 0; i < m; i++ ){
int u = in.nextInt()-1;
int v = in.nextInt()-1;
adj[u].add(v);
adj[v].add(u);
uq.add(u);
vq.add(v);
friends[u]++;
friends[v]++;
}
int a = n;
ArrayDeque<Integer> q= new ArrayDeque<>();
boolean good[] = new boolean[n];
Arrays.fill(good, true);
for(int i = 0; i < n; i++) {
if(friends[i] < k) {
q.add(i);
good[i] = false;
a--;
}
}
while(!q.isEmpty()) {
int u = q.poll();
for(int v : adj[u]) {
friends[v]--;
if(good[v] && friends[v] < k) {
good[v] = false;
a--;
q.add(v);
}
}
}
ans.add(a);
for(int i = 0; i < m; i++) {
int u = uq.pop();
int v = vq.pop();
if(good[u] && good[v]) {
friends[v]--;
friends[u]--;
adj[u].remove(v);
adj[v].remove(u);
if(friends[v] < k) {
a--;
q.add(v);
good[v] = false;
}
if(friends[u] < k){
a--;
q.add(u);
good[u] = false;
}
while(!q.isEmpty()) {
int uu = q.poll();
for(int vv : adj[uu]) {
friends[vv]--;
if(good[vv] && friends[vv] < k) {
good[vv] = false;
a--;
q.add(vv);
}
}
}
}
ans.add(a);
}
ans.pop();
PrintWriter out = new PrintWriter(System.out);
for(int i = 0; i < m; i++) {
out.println(ans.pop());
}
out.close();
}
static class JS {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
long long n, tot = 1, ans, m, k;
long long head[400005], d[400005], pr[400005], v[400005], vi[400005],
vdel[400005], num[400005];
struct node {
long long u, v;
} g[400005];
struct ed {
long long next, to;
} e[400005 * 2];
void ad(long long x, long long y) {
e[++tot].to = y;
e[tot].next = head[x];
head[x] = tot;
}
void del(long long x) {
if (vdel[x]) return;
ans--;
vdel[x] = 1;
for (int i = head[x]; i; i = e[i].next) {
int tt = e[i].to;
bool flag = 0;
if (d[tt] >= k) flag = 1;
if (vi[i]) continue;
d[tt]--;
if (flag && d[tt] < k) del(tt);
}
}
void dfs(long long x) {
v[x] = 1;
ans++;
for (int i = head[x]; i; i = e[i].next) {
int tt = e[i].to;
if (v[tt] || vi[i]) continue;
dfs(tt);
}
if (d[x] < k) del(x);
}
int main() {
n = read(), m = read(), k = read();
for (int i = 1; i <= m; i++) {
g[i].u = read(), g[i].v = read();
num[i] = tot + 1;
ad(g[i].u, g[i].v);
ad(g[i].v, g[i].u);
d[g[i].u]++;
d[g[i].v]++;
}
for (int i = 1; i <= n; i++) {
if (!v[i]) dfs(i);
}
for (int i = m; i >= 1; i--) {
pr[i] = ans;
long long u = g[i].u, v = g[i].v;
vi[num[i]] = vi[num[i] ^ 1] = 1;
if (d[u] >= k && d[v] >= k) {
d[u]--;
d[v]--;
if (d[u] < k) del(u);
if (d[v] < k) del(v);
} else if (d[u] >= k)
d[v]--;
else if (d[v] >= k)
d[u]--;
}
for (int i = 1; i <= m; i++) cout << pr[i] << endl;
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, v, nxt;
} e[400010];
int tot;
int first[200010];
int d[200010];
void build(int u, int v) {
e[++tot] = (Edge){u, v, first[u]};
first[u] = tot;
d[u]++;
return;
}
int ans[200010];
bool book[200010];
bool is[200010];
int n, m, k;
queue<int> q;
void getans(int w) {
if (w < m) {
ans[w] = ans[w + 1];
int u = e[(w + 1) * 2].u, v = e[(w + 1) * 2].v;
if (book[w + 1]) return;
d[u]--;
d[v]--;
book[w + 1] = true;
if (!is[u] && d[u] < k) q.push(u), ans[w]--, is[u] = true;
if (!is[v] && d[v] < k) q.push(v), ans[w]--, is[v] = true;
} else {
ans[w] = n;
for (int i = 1; i <= n; i++)
if (d[i] < k) q.push(i), ans[w]--, is[i] = true;
}
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = first[now]; i; i = e[i].nxt) {
if (book[(i + 1) / 2]) continue;
if (is[e[i].v]) continue;
book[(i + 1) / 2] = true;
d[e[i].v]--;
if (d[e[i].v] < k) {
q.push(e[i].v);
ans[w]--;
is[e[i].v] = true;
}
}
}
return;
}
int main() {
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
build(u, v);
build(v, u);
}
for (int i = m; i >= 1; i--) getans(i);
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <typename T>
void DBG(const char* name, T&& H) {
cerr << name << " = " << H << ')' << '\n';
}
template <typename T, typename... Args>
void DBG(const char* names, T&& H, Args&&... args) {
const char* NEXT = strchr(names + 1, ',');
cerr.write(names, NEXT - names) << " = " << H << " |";
DBG(NEXT + 1, args...);
}
using ll = long long;
using ld = long double;
const ll mod1 = 1e9 + 7;
const ld PI = acos(-1.0);
const ll maxN = 1e6 + 1;
const ll INF = 1e18;
void Solve() {
int n, m, k;
cin >> n >> m >> k;
vector<int> deg(n + 1, 0);
vector<pair<int, int>> friendship(m);
vector<set<int>> graph(n + 1);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
deg[a]++, deg[b]++;
graph[a].insert(b);
graph[b].insert(a);
friendship[i] = {a, b};
}
set<pair<int, int>> q;
for (int i = 1; i <= n; i++) {
q.insert({deg[i], i});
}
vector<int> ans(m, 0);
for (int i = m - 1; i >= 0; i--) {
while (q.size() && ((*q.begin()).first < k)) {
int ver = (*q.begin()).second;
for (auto j : graph[ver]) {
graph[j].erase(ver);
q.erase({deg[j], j});
q.insert({deg[j] - 1, j});
deg[j]--;
}
q.erase({deg[ver], ver});
}
ans[i] = q.size();
int a = friendship[i].first;
int b = friendship[i].second;
if (q.count({deg[a], a}) && q.count({deg[b], b})) {
q.erase({deg[a], a});
q.erase({deg[b], b});
graph[a].erase(b);
graph[b].erase(a);
deg[a]--;
deg[b]--;
q.insert({deg[a], a});
q.insert({deg[b], b});
}
}
for (auto i : ans) cout << i << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
while (tt--) {
Solve();
}
return 0;
}
|
1037_E. Trips | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7. | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} | {
"input": [
"16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n",
"2 1 1\n2 1\n"
],
"output": [
"0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n",
"2\n"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long double Pi = 3.14159265359;
const long long MOD = (long long)1e9 + 7;
const long long MAXN = (long long)2e5 + 10;
const long long INF = (long long)9223372036854775;
const long double EPS = (long double)1e-8;
set<long long> ans;
set<long long> g[MAXN];
vector<pair<long long, long long> > q;
vector<long long> anss;
queue<long long> bfsq;
long long vis[MAXN];
int main() {
long long n, m, k, u, v;
scanf("%lld", &n);
scanf("%lld", &m);
scanf("%lld", &k);
long long an = n;
for (int i = 0; i < m; i++) {
scanf("%lld", &u);
scanf("%lld", &v);
u--;
v--;
g[u].insert(v);
g[v].insert(u);
q.push_back(make_pair(u, v));
}
for (int i = 0; i < n; i++) ans.insert(i);
for (int i = 0; i < n; i++) {
if (g[i].size() < k) {
bfsq.push(i);
vis[i] = 1;
}
}
while (bfsq.size() != 0) {
for (auto adj : g[bfsq.front()]) {
g[adj].erase(bfsq.front());
if (g[adj].size() < k) {
if (vis[adj] == 0) bfsq.push(adj);
vis[adj] = 1;
}
}
ans.erase(bfsq.front());
bfsq.pop();
an--;
}
anss.push_back(an);
for (int i = 0; i < m; i++) {
u = q.back().first;
v = q.back().second;
if ((vis[u] == 1) or (vis[v] == 1)) {
anss.push_back(an);
q.pop_back();
continue;
}
g[u].erase(v);
g[v].erase(u);
if (g[u].size() < k) {
bfsq.push(u);
vis[u] = 1;
}
if (g[v].size() < k) {
bfsq.push(v);
vis[v] = 1;
}
while (bfsq.size() != 0) {
for (auto adj : g[bfsq.front()]) {
g[adj].erase(bfsq.front());
if (g[adj].size() < k) {
if (vis[adj] == 0) bfsq.push(adj);
vis[adj] = 1;
}
}
ans.erase(bfsq.front());
bfsq.pop();
an--;
}
anss.push_back(an);
q.pop_back();
}
anss.pop_back();
for (int i = 0; i < m; i++) {
printf("%lld\n", anss.back());
anss.pop_back();
}
return 0;
}
|
Subsets and Splits