[Topic link]
Http://codeforces.com/problemset/problem/1037/E
Algorithm
First off-line, consider the problem upside down and convert it to: Delete one edge at a time, and the maximum number of people to travel
Assuming that all points are selected, then, if the degree of a point < K, obviously this point cannot be selected, we need to delete it and all the edges adjacent to it
Obviously, all the points and edges will only be deleted once, so the time complexity is O (N + M)
Code
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 2e5 +Ten;intN, M, K;intDEG[MAXN],RES[MAXN],X[MAXN],Y[MAXN],Q[MAXN];BOOLVisited[maxn],del[maxn];vector<int>a[maxn],b[maxn];template<typename t> InlinevoidChkmax (T &x,t y) {x =Max (x, y);} Template<typename t> InlinevoidChkmin (T &x,t y) {x =min (x, y);} Template<typename t> InlinevoidRead (T &x) {T F=1; x =0; Charc =GetChar (); for(;!isdigit (c); c = GetChar ())if(c = ='-') F =-F; for(; IsDigit (c); c = GetChar ()) x = (x <<3) + (x <<1) + C-'0'; X*=F;}intMain () {read (n); read (m); Read (k); for(inti =1; I <= m; i++) {read (x[i]); Read (Y[i]); A[x[i]].push_back (Y[i]); A[y[i]].push_back (X[i]); B[x[i]].push_back (i); B[y[i]].push_back (i); Deg[x[i]]++; deg[y[i]]++; } intL =1, r =0, ans =N; for(inti =1; I <= N; i++) { if(Deg[i] <k) {Visited[i]=true; q[++R] =i; Ans--; } } for(inti = m; I >=1; i--) { while(L <=r) {intU = q[l++]; for(Unsigned j =0; J < A[u].size (); J + +) { if(!Del[b[u][j]]) {Deg[u]--; DEG[A[U][J]]--; DEL[B[U][J]]=true; if(!visited[a[u][j] && Deg[a[u][j]] <k) {ans--; VISITED[A[U][J]]=true; q[++R] =A[u][j]; } }}} Res[i]=ans; if(!Del[i]) {Del[i]=true; Deg[x[i]]--; Deg[y[i]]--; if(!visited[x[i] && Deg[x[i]] <k) {Visited[x[i]]=true; Ans--; q[++R] =X[i]; } if(!visited[y[i] && Deg[y[i]] <k) {Visited[y[i]]=true; Ans--; q[++R] =Y[i]; } } } for(inti =1; I <= m; i++) printf ("%d\n", Res[i]); return 0; }
[Codeforces 1037E] Trip