이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// author : Nguyễn Trọng Nguyễn - ITK22 NBK
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair <int, int>
#define fi first
#define sc second
#define maxn 100000
#define inf (int)1e9
#define lg 16
int n, m, k;
vector <int> adj[maxn + 5];
ii egde[maxn + 5];
int dp[maxn + 5];
int timer = 0, tin[maxn + 5], tout[maxn + 5];
int up[lg + 5][maxn + 5];
void init ()
{
cin >> n >> m >> k;
for (int i = 1; i < n; i++)
{
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
egde[i] = {u, v};
}
}
void dfs (int u = 1, int p = 1)
{
up[0][u] = p;
tin[u] = ++timer;
for (int i = 1; i <= lg; i++)
up[i][u] = up[i - 1][up[i - 1][u]];
for (auto v : adj[u])
if (v != p) dfs(v, u);
tout[u] = timer;
}
bool IsAncestor (int u, int v)
{
return tin[u] <= tin[v] and tout[u] >= tout[v];
}
int lca (int u, int v)
{
if (IsAncestor(u, v)) return u;
if (IsAncestor(v, u)) return v;
for (int e = lg; e >= 0; e--)
if (!IsAncestor(up[e][u], v))
u = up[e][u];
return up[0][u];
}
void dfs2 (int u = 1)
{
for (auto v : adj[u])
{
if (v == up[0][u]) continue;
dfs2(v);
dp[u] += dp[v];
}
}
bool cmp (int u, int v)
{
return tin[u] < tin[v];
}
void process ()
{
dfs();
while (m--)
{
int x; cin >> x;
vector <int> fix;
while (x--)
{
int u; cin >> u;
fix.push_back(u);
}
sort(fix.begin(), fix.end(), cmp);
int cur = fix[0];
fix.push_back(cur);
for (auto u : fix)
{
dp[u]++, dp[cur]++;
dp[lca(u, cur)] -= 2;
cur = u;
}
}
dfs2();
vector <int> ans;
for (int i = 1; i < n; i++) {
int u, v; tie(u, v) = egde[i];
int res = up[0][v] == u ? dp[v] : dp[u];
if (res >= 2 * k) ans.push_back(i);
}
cout << (int)ans.size() << '\n';
for (int v : ans) cout << v << ' ';
}
signed main ()
{
cin.tie(0)->sync_with_stdio(false);
init();
process();
return 0;
}
# | 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... |