Submission #1176647

#TimeUsernameProblemLanguageResultExecution timeMemory
1176647paulxaxaRailway (BOI17_railway)C++20
0 / 100
156 ms40252 KiB
#include <bits/stdc++.h>

#define NMAX 100000
#define LOG 10

#define ll long long int
#define BASE 32

#define MOD 1000000007


using namespace std;

ifstream fin("cod.in");
ofstream fout("cod.out");

vector<pair<int,int>> adj[NMAX+1];

int timer=0,euler[NMAX+1];
vector<int> In[NMAX+1],Out[NMAX+1];
int n,m,k;

int d[NMAX+1],parent[NMAX+1][LOG+1];

int Lca(int x,int y)
{
    if(d[x] < d[y])
    {
        swap(x,y);
    }
    for(int i=LOG;i>=0;i--)
    {
        if(d[x]-(1<<i)>=d[y])
        {
            x = parent[x][i];
        }
    }
    for(int i=LOG;i>=0;i--)
    {
        if(parent[x][i]!=parent[y][i])
        {
            x = parent[x][i];
            y = parent[y][i];
        }
    }
    if(x!=y)
    {
        x = parent[x][0];
    }
    return x;
}

void dfs1(int x,int p)
{
    euler[x] = ++timer;
    parent[x][0] = p;
    d[x] = d[p] + 1;
    for(auto e : adj[x])
    {
        if(e.first!=p)
        {
            dfs1(e.first,x);
        }
    }
}

vector<int> res;
map<int,int> mp[NMAX+1];
int cnt[NMAX+1];

void dfs(int x,int p)
{
    for(auto [y,id] : adj[x])
    {
        if(y!=p)
        {
            dfs(y,x);
            if(cnt[y]>=k)
            {
                res.push_back(id);
            }
            if(mp[y].size() > mp[x].size())
            {
                mp[x].swap(mp[y]);
                cnt[x] = cnt[y];
            }
            for(auto j : mp[y])
            {
                int t = mp[x][j.first];
                if(!t && j.second)
                {
                    cnt[x]++;
                }
                mp[x][j.first] += j.second;
            }
        }
    }

    for(int j : In[x])
    {
        if(mp[x][j]==0)
        {
            cnt[x]++;
        }
        mp[x][j]++;
    }
    for(int j : Out[x])
    {

        if(mp[x][j]==1)
        {
            cnt[x]--;
        }
        mp[x][j]--;

    }
}
int main()
{
    cin >> n >> m >> k;
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin >> x >> y;
        adj[x].push_back({y,i});
        adj[y].push_back({x,i});
    }

    dfs1(1,0);

    for(int j=1;j<=LOG;j++)
    {
        for(int i=1;i<=n;i++)
        {
            parent[i][j] = parent[parent[i][j-1]][j-1];
        }
    }

    for(int i=1;i<=m;i++)
    {
        vector<int> v;
        int s;
        cin >> s;
        for(int j=1;j<=s;j++)
        {
            int x;
            cin >> x;
            v.push_back(x);
        }
        sort(v.begin(),v.end(),[](int a,int b){return euler[a] < euler[b];});
        for(int j=1;j<v.size();j++)
        {
             int lca = Lca(v[j],v[j-1]);
             Out[lca].push_back(i);
             Out[lca].push_back(i);
             In[v[j]].push_back(i);
             In[v[j-1]].push_back(i);
        }
    }

    dfs(1,0);
    cout << res.size() << '\n';
    sort(res.begin(),res.end());
    for(int j : res)
    {
        cout << j << " ";
    }
}

#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...