1 2 3 4

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\n", ins.size());
	for (int i = 0;i< ins.size(); i++) {
		printf("%d ", ins[i]);
	}
	printf("\n");
	return 0;
}

  

posted @ 2019-11-13 15:25  Lesning  阅读(152)  评论(0编辑  收藏  举报