CodeForces 796D

Posted lesning

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 796D相关的知识,希望对你有一定的参考价值。

不能一个一个bfs,要一起bfs

技术图片

 

 

#include<iostream>
#include<vector>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 300100
using namespace std;
vector<int>G[maxn];
void insert(int be, int en) {
	G[be].push_back(en);
}
int n, m, k;
int vis[maxn];
queue<int>que;
int bfs() {
	while (!que.empty()) {
		int x = que.front();
		que.pop();
		for (int i = 0; i < G[x].size(); i++) {
			int p = G[x][i];
			if (!vis[p]) {
				vis[p] = vis[x];
				que.push(p);
			}
		}
	}
	return 0;
}
struct Node {
	int be, en;
}cnn[maxn];
vector<int>ins;

int main() {
	int be, en;
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= m; i++) {
		scanf("%d", &be);
		if(vis[be] == 0) que.push(be);
		vis[be] = i;
	}
	for (int i = 1; i < n; i++) {
		scanf("%d %d", &be, &en);
		insert(be, en);
		insert(en, be);
		cnn[i].be = be;
		cnn[i].en = en;
	}
	int cnt = 0;
	bfs();
	for (int i = 1; i < n; i++) {
		be = cnn[i].be;
		en = cnn[i].en;
		if (vis[be] != vis[en]) {
			ins.push_back(i);
		}
	}
	printf("%d
", ins.size());
	for (int i = 0;i< ins.size(); i++) {
		printf("%d ", ins[i]);
	}
	printf("
");
	return 0;
}

  

以上是关于CodeForces 796D的主要内容,如果未能解决你的问题,请参考以下文章

796D(bfs)

CF796D Police Stations BFS+染色

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

c_cpp Codeforces片段

Codeforces 86C Genetic engineering(AC自动机+DP)

CodeForces 1005D Polycarp and Div 3(思维贪心dp)