# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
676384 | flashhh | Railway (BOI17_railway) | C++17 | 201 ms | 42196 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int numVertices, numPlans, minimumPlans;
vector<int> acceptedEdges;
vector<int> numVerticesOnPlan;
vector<vector<int> > plansOfVertice;
vector<vector<pair<int,int> > > adj;
void Read() {
cin >> numVertices >> numPlans >> minimumPlans;
numVerticesOnPlan.resize(numPlans);
adj.resize(numVertices);
plansOfVertice.resize(numVertices);
for (int numEdge = 0; numEdge < numVertices - 1; ++numEdge) {
int u,v; cin >> u >> v;
--u; --v;
adj[u].emplace_back(numEdge, v);
adj[v].emplace_back(numEdge, u);
}
for (int numPlan = 0; numPlan < numPlans; ++numPlan) {
int planQuantity; cin >> planQuantity;
numVerticesOnPlan[numPlan] = planQuantity;
while (planQuantity--) {
int vertice; cin >> vertice;
--vertice;
plansOfVertice[vertice].push_back(numPlan);
}
}
}
void calculate() {
vector<map<int,int> > myMap;
myMap.resize(numVertices);
auto dfs = [&] (int u, int cha, int prevEdge, auto&& dfs) -> void {
for (int plan : plansOfVertice[u]) ++myMap[u][plan];
for (auto [numEdge, v] : adj[u]) {
if (v == cha) continue;
dfs(v, u, numEdge, dfs);
if (myMap[u].size() < myMap[v].size()) swap(myMap[u], myMap[v]);
for (map<int,int> :: iterator it = myMap[v].begin(); it != myMap[v].end(); ++it) {
int plan = it -> first; int quantity = it -> second;
myMap[u][plan] += quantity;
if (myMap[u][plan] == numVerticesOnPlan[plan]) myMap[u].erase(plan);
}
}
if (myMap[u].size() >= minimumPlans) acceptedEdges.push_back(prevEdge);
};
dfs(0, -1, -1, dfs);
sort(acceptedEdges.begin(), acceptedEdges.end());
cout << acceptedEdges.size() <<'\n';
for (auto numEdge : acceptedEdges) cout << numEdge + 1 <<" "; cout <<'\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
Read();
calculate();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |