#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define ALL(n) n.begin(), n.end()
#define mask(n) (1ll << (n))
#define ck(n, k) (((n) >> (k))&1)
#define LOG 17
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int f[100005], dfsnum[100005], lca[100005][20], hight[100005];
pair<int, int> edges[100005];
vector<int> adj[100005];
int num = 0;
void dfs(int pa, int u)
{
// cout << pa << " " << u << endl;
lca[u][0] = pa;
hight[u] = hight[pa] + 1;
for (auto v : adj[u]) if (v != pa)
{
dfs(u, v);
}
dfsnum[u] = ++num;
}
void preprocessLCA(int n)
{
dfs(0, 1);
for (int j = 1; j <= LOG; j++)
for (int i = 1; i <= n; i++)
lca[i][j] = lca[lca[i][j - 1]][j - 1];
}
int getLCA(int u, int v)
{
if (hight[u] < hight[v]) return getLCA(v, u);
for (int i = LOG; i >= 0; i--) if (hight[lca[u][i]] >= hight[v]) u = lca[u][i];
if (u == v) return u;
for (int i = LOG; i >= 0; i--) if (lca[u][i] != lca[v][i])
{
u = lca[u][i]; v = lca[v][i];
}
return lca[u][0];
}
void dfs1(int pa, int u)
{
for (auto v : adj[u]) if (v != pa)
{
dfs1(u, v);
f[u] += f[v];
}
}
void solution()
{
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
edges[i] = {x, y};
}
preprocessLCA(n);
while(m--)
{
int num;
cin >> num;
vector<int> ldk(num);
for (int i = 0; i < num; i++) cin >> ldk[i];
sort(ALL(ldk), [&](const int &x, const int &y)
{
return dfsnum[x] < dfsnum[y];
});
for (int i = 1; i < (int)ldk.size(); i++)
{
int par = getLCA(ldk[i - 1], ldk[i]);
f[ldk[i]]++; f[par]--;
if (i == 1)
{
f[ldk[0]]++; f[par]--;
}
}
}
dfs1(0, 1);
vector<int> ans;
for (int i = 1; i < n; i++)
{
if (hight[edges[i].fi] < hight[edges[i].se]) swap(edges[i].fi, edges[i].se);
if (f[edges[i].fi] >= k)
{
ans.pb(i);
}
}
cout << (int)ans.size() << "\n";
for (auto v : ans) cout << v << " ";
}
int main()
{
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solution();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
26144 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
21384 KB |
Output is correct |
2 |
Correct |
48 ms |
18000 KB |
Output is correct |
3 |
Correct |
48 ms |
17488 KB |
Output is correct |
4 |
Correct |
47 ms |
17488 KB |
Output is correct |
5 |
Correct |
49 ms |
17492 KB |
Output is correct |
6 |
Correct |
44 ms |
21424 KB |
Output is correct |
7 |
Correct |
48 ms |
21332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
21384 KB |
Output is correct |
2 |
Correct |
48 ms |
18000 KB |
Output is correct |
3 |
Correct |
48 ms |
17488 KB |
Output is correct |
4 |
Correct |
47 ms |
17488 KB |
Output is correct |
5 |
Correct |
49 ms |
17492 KB |
Output is correct |
6 |
Correct |
44 ms |
21424 KB |
Output is correct |
7 |
Correct |
48 ms |
21332 KB |
Output is correct |
8 |
Incorrect |
52 ms |
21272 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |