[Codeforces 1037E] Trip

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.