Submission #1080860

#TimeUsernameProblemLanguageResultExecution timeMemory
1080860cpptowinRailway (BOI17_railway)C++17
100 / 100
168 ms82208 KiB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if(x < 0) x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if(x < 0) x += mod;
}
int n,q,k;
vi adj[maxn],ke[maxn];
int pos[maxn],tail[maxn],h[maxn];
int par[maxn][20];
int cnt;
void dfs(int u)
{
    pos[u] = ++cnt;
    for(int v : ke[u]) if(v != par[u][0]) 
    {
        h[v] = h[u] + 1;
        par[v][0] = u;
        fo(i,1,19) par[v][i] = par[par[v][i - 1]][i - 1];
        dfs(v);
    }
    tail[u] = cnt;
}
int lca(int u, int v)
{
    if (h[u] < h[v])
        swap(u, v);
    int del = h[u] - h[v];
    fod(i, 19, 0) if (bit(del, i)) u = par[u][i];
    if (u == v)
        return u;
    fod(i, 19, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
    return par[u][0];
}
int p[maxn];
void dfs1(int u,int parent = 0)
{
    for(int v : adj[u]) if(v != parent) 
    {
        p[v]++;
        p[u]--;
        dfs1(v,u);
    }
}
void dfs2(int u,int parent = 0) 
{
    for(int v : ke[u]) if(v != parent) 
    {
        dfs2(v,u);
        p[u] += p[v];
    }
}
pii edge[maxn];
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> q >> k;
    fo(i,1,n - 1) 
    {
        int u,v;
        cin >> u >> v;
        ke[u].pb(v);
        ke[v].pb(u);
        edge[i] = {u,v};
    }
    dfs(1);
    while(q--) 
    {
        int m;
        cin >> m;
        vi arr(m);
        for(int &it : arr) cin >> it;
        sort(all(arr),[](int a,int b)
        {
            return pos[a] < pos[b];
        });
        fo(i,1,m - 1) arr.pb(lca(arr[i],arr[i - 1]));
        sort(all(arr));
        arr.erase(unique(all(arr)),arr.end());
        stack<int> st;
        sort(all(arr),[](int a,int b)
        {
            return pos[a] < pos[b];
        });
        for(int it : arr) 
        {
            if(st.empty()) st.push(it);
            else 
            {
                while(ss(st) and tail[st.top()] < pos[it]) st.pop();
                adj[st.top()].pb(it);
                st.push(it);
            }
        }
        dfs1(arr[0]);
        for(int it : arr) adj[it].clear();
    }
    dfs2(1);
    vi res;
    fo(i,1,n - 1)
    {
        auto [u,v] = edge[i];
        if(pos[u] < pos[v]) swap(u,v);
        if(p[u] >= k) res.pb(i);
    }
    cout << ss(res);en;
    for(int it : res) cout << it << ' ';
}

Compilation message (stderr)

railway.cpp:104:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  104 | main()
      | ^~~~
railway.cpp: In function 'int main()':
railway.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...