제출 #930962

#제출 시각아이디문제언어결과실행 시간메모리
930962RegulusRailway (BOI17_railway)C++17
100 / 100
125 ms43860 KiB
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 1e5+5;
const int LogN = 17;
int tin[N], tout[N], timer, anc[LogN+5][N], cnt[N];
vector<pll> g[N];
vector<int> v[N], ans;

inline void dfs0(int x, int pre)
{
    tin[x] = ++timer, anc[0][x] = pre;
    for (auto e : g[x])
    {
        int v = e.F, id = e.S;
        if (v == pre) continue;
        dfs0(v, x);
    }
    tout[x] = ++timer;
}

inline bool is_anc(int x, int y)
{ return tin[x] <= tin[y] && tout[y] <= tout[x]; }
inline int get_lca(int x, int y)
{
    if (is_anc(x, y)) return x;
    if (is_anc(y, x)) return y;
    for (int i=LogN; i >= 0; --i)
        if (anc[i][x] && !is_anc(anc[i][x], y)) x = anc[i][x];
    return anc[0][x];
}

inline set<int> dfs(int x, int pre)
{
    set<int> st, tmp;

    for (auto e : g[x])
    {
        int v = e.F, id = e.S;
        if (v == pre) continue;
        tmp = dfs(v, x);
        cnt[id] = SZ(tmp);

        if (SZ(st) < SZ(tmp)) swap(st, tmp);
        for (int xx : tmp) st.insert(xx);
    }
    for (int d : v[x]) if (d > 0) st.insert(d);
    for (int d : v[x]) if (d < 0) st.erase(-d);

    return st;
}

int main(void)
{ IO
    int n, i, Q, K;

    cin >> n >> Q >> K;
    for (i=1; i <= n-1; ++i)
    {
        int x, y; cin >> x >> y;
        g[x].pb(y, i), g[y].pb(x, i);
    }

    dfs0(1, 0);
    for (i=1; i <= LogN; ++i)
        for (int j=1; j <= n; ++j) anc[i][j] = anc[i-1][anc[i-1][j]];

    for (i=1; i <= Q; ++i)
    {
        int k, pre = 0; cin >> k;
        for (int j=0; j < k; ++j)
        {
            int x; cin >> x;
            pre = (pre)?get_lca(pre, x) : x;
            v[x].pb(i);
        }
        v[pre].pb(-i);
    }

    for (i=1; i <= n; ++i)
    {
        sort(all(v[i]));
        v[i].resize(unique(all(v[i]))-v[i].begin());
    }

    dfs(1, 0);
    for (i=1; i <= n-1; ++i)
        if (cnt[i] >= K) ans.pb(i);
    
    cout << SZ(ans) << '\n';
    for (int i : ans) cout << i << ' ';
    cout << '\n';

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

railway.cpp: In function 'void dfs0(int, int)':
railway.cpp:27:22: warning: unused variable 'id' [-Wunused-variable]
   27 |         int v = e.F, id = e.S;
      |                      ^~
#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...