Submission #1244391

#TimeUsernameProblemLanguageResultExecution timeMemory
1244391ssitaramRailway (BOI17_railway)C++20
100 / 100
145 ms37452 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m, k; cin >> n >> m >> k;
	vector<vector<pair<int, int>>> adj(n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b; cin >> a >> b;
		adj[--a].push_back({--b, i});
		adj[b].push_back({a, i});
	}
	vector<vector<int>> here(n);
	vector<int> c(m);
	for (int i = 0; i < m; ++i) {
		cin >> c[i];
		for (int j = 0; j < c[i]; ++j) {
			int v; cin >> v;
			here[--v].push_back(i);
		}
	}
	vector<set<pair<int, int>>> at(n);
	vector<int> ans;
	auto dfs = [&](auto&& self, int node, int p) -> void {
		for (int i : here[node]) {
			at[node].insert({i, 1});
		}
		for (pair<int, int>& i : adj[node]) {
			if (i.first == p) continue;
			self(self, i.first, node);
			if (at[i.first].size() >= k) {
				ans.push_back(i.second);
			}
			if (at[node].size() < at[i.first].size()) swap(at[node], at[i.first]);
			for (pair<int, int> j : at[i.first]) {
				auto it = at[node].lower_bound({j.first, 0});
				if (it == at[node].end() || it->first != j.first) {
					at[node].insert(j);
				} else {
					int tc = j.second + it->second;
					at[node].erase(it);
					if (tc != c[j.first]) at[node].insert({j.first, tc});
				}
			}
			at[i.first].clear();
		}
	};
	dfs(dfs, 0, -1);
	sort(ans.begin(), ans.end());
	cout << ans.size() << '\n';
	for (int i : ans) {
		cout << i + 1 << ' ';
	}
	cout << '\n';
	return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...