Submission #1095903

#TimeUsernameProblemLanguageResultExecution timeMemory
1095903vjudge1Railway (BOI17_railway)C++17
100 / 100
191 ms37576 KiB
#include <bits/stdc++.h>
using namespace std;

vector<long long> adj[200005];
long long visited, m=1, k=1, check[200005], tour[200005], nxt[200005], par[200005], sz[200005], pos[200005], chain[200005], head[200005], a[200005];
map<long long, long long> edge;

struct sortt
{
    bool operator()(const long long &x, const long long &y) const
    {
        if (chain[x]==chain[y]) {return pos[x]>pos[y];}
        else {return chain[x]>chain[y];};
    };
};

multiset<long long, sortt> s;
vector<long long> ans;

void dfs(long long x, long long p)
{
    par[x]=p;
    sz[x]=1;
    for (auto y: adj[x])
    {
        if (y!=p)
        {
            dfs(y, x);
            sz[x]+=sz[y];
            if (sz[y]>sz[nxt[x]]) {nxt[x]=y;};
        };
    };
}

void hld(long long x, long long p)
{
    if (head[m]==0) {head[m]=x;};
    chain[x]=m; tour[k]=edge[x*200000+p]; pos[x]=k; k++;
    if (nxt[x]) {hld(nxt[x], x);};
    for (auto y: adj[x])
    {
        if (y!=p && y!=nxt[x])
        {
            m++;
            hld(y, x);
        };
    };
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
	long long n, q, k, i, u, v;
	cin >> n >> q >> k;
	for (i=1; i<n; i++)
    {
        cin >> u >> v;
        edge[u*200000+v]=edge[v*200000+u]=i;
        adj[u].push_back(v);
        adj[v].push_back(u);
    };
    dfs(1, 0);
    hld(1, 0);
    while (q--)
    {
        visited++;
        cin >> u;
        while (u--) {cin >> v; s.insert(v);};
        while (chain[*s.begin()]!=chain[*prev(s.end())])
        {
            u=*s.begin();
            if (check[chain[u]]!=visited) {a[pos[head[chain[u]]]]++; a[pos[u]+1]--; check[chain[u]]=visited;};
            s.erase(*s.begin());
            u=par[head[chain[u]]];
            s.insert(u);
        };
        a[pos[*prev(s.end())]+1]++; a[pos[*s.begin()]+1]--;
        s.clear();
    };
    for (i=1; i<=n; i++) {a[i]+=a[i-1]; if (a[i]>=k) {ans.push_back(tour[i]);};};
    sort(ans.begin(), ans.end());
    cout << ans.size() << "\n";
    for (auto x: ans) {cout << x << " ";};
}
#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...