Police Stations CodeForces - 796D (bfs)

大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件.

 

所有黑点开始bfs, 贪心删边.

 

#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <string.h>
#include <queue>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define hr cout<<'\n'
#define pb push_back
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (lc|1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false);
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){b?exgcd(b,a%b,d,y,x),y-=a/b*x:x=1,y=0,d=a;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N = 4e5+10, INF = 0x3f3f3f3f;
int n, k, d, t;
int vis[N], vis2[N];
vector<pii> g[N];

int main() {
	scanf("%d%d%d", &n, &k, &d);
	REP(i,1,k) scanf("%d", &t), vis[t]=1;
	REP(i,1,n-1) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u].pb({v,i}),g[v].pb({u,i});
	}
	queue<int> q;
	vector<int> ans;
	REP(i,1,n) if (vis[i]) q.push(i);
	while (!q.empty()) {
		int u = q.front();q.pop();
		for (auto e:g[u]) {
			if (vis2[e.y]) continue;
			if (vis[e.x]) ans.pb(e.y);
			else q.push(e.x);
			vis[e.x] = vis2[e.y] = 1;
		}
	}
	printf("%d\n",int(ans.size()));
	for (int t:ans) printf("%d ",t);hr;
}

 

上一篇:android的Afinal框架下的数据库更新


下一篇:Dasha and Photos CodeForces - 761F (前缀优化)