796D - Police Stations - CodeForces Solution


constructive algorithms dfs and similar dp graphs shortest paths trees *2100

Please click on ads to support us..

C++ Code:

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <iostream>
#include<stdio.h>
#include <algorithm>
#include<vector>
#include<queue>

using namespace std;

vector<pair<int, int>> paths[300005];
int visited[300005];
int repeat[300005];
queue<pair<int, int>> q;
int input()
{
	int x;
	scanf("%d", &x);
	return x;
}
int main() {
	ios::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	setbuf(stdout, NULL);
	//freopen("C:\\Users\\rushank.jain\\source\\repos\\codeforces\\codeforces\\input.txt", "r", stdin);
	int n, k, d;
	scanf("%d%d%d", &n, &k, &d);
	for (int i = 0; i < k; i++)
	{
		int x;
		scanf("%d", &x);
		q.push({ x,0 });
	}

	for (int i = 0; i < n-1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		paths[v].push_back({ u,i + 1 });
		paths[u].push_back({ v,i + 1 });
	}
	while (!q.empty())
	{
		pair<int, int> curr = q.front();
		q.pop();

		if (visited[curr.first]) continue;

		visited[curr.first] = 1;

		for (auto it : paths[curr.first])
		{
			if (it.first != curr.second)
			{
				if (visited[it.first])
				{
					repeat[it.second] = 1;
				}
				else q.push({ it.first,curr.first });
			}
		}

	}

	int count = 0;
	for (int i = 1; i <= n-1; i++)
	{
		if (repeat[i]) count++;
	}
	cout << count << "\n";
	for (int i = 1; i <= n-1; i++)
	{
		if(repeat[i])cout << i << " ";
	}
	cout << "\n";
	

	return 0;
}


Comments

Submit
0 Comments
More Questions

1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut