Submission #1282335

#TimeUsernameProblemLanguageResultExecution timeMemory
1282335ducanh0811Railway (BOI17_railway)C++20
100 / 100
72 ms36988 KiB
#include <bits/stdc++.h>
bool M1;
#define int long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
using namespace std;

#define MAXN 100005
int n, m, k; 
vector<pair<int, int>> g[MAXN]; 
int sta[MAXN], fin[MAXN]; 
int dep[MAXN], up[MAXN][20]; 
int timer = 0; 
int f[MAXN]; 
vector<int> ans; 

///++++++++++++++++++++++++++++++++++++++///

void dfs(int u, int p) { 
    sta[u] = ++timer; 
    for (pair<int, int> &x : g[u]) { 
        int v, w; tie(v, w) = x; 
        if (v == p) continue;
        dep[v] = dep[u] + 1; 
        up[v][0] = u;  
        dfs(v, u); 
    }
    fin[u] = timer; 
}   

void prepare_LCA() { 
    FOR(j, 1, 19) FOR(i, 1, n) up[i][j] = up[up[i][j - 1]][j - 1]; 
}

int LCA(int a, int b)  {
    if (dep[a] < dep[b]) swap(a, b); 
    int h = dep[a] - dep[b]; 
    for (int i = 0; (1 << i) <= h; ++i) { 
        if (h >> i & 1) a = up[a][i]; 
    }
    if (a == b) return a; 
    h = __lg(dep[a]); 
    for (int i = h; i >= 0; --i) { 
        if (up[a][i] != up[b][i]) { 
            a = up[a][i]; 
            b = up[b][i]; 
        }
    }
    return up[a][0]; 
}

void update(int a, int b) { 
    f[a]--; 
    f[b]++; 
}

bool inside(int a, int b){ 
    return (sta[a] <= sta[b] && fin[b] <= fin[a]);
}

void dp(int u, int p) { 
    for (pair<int, int> &x : g[u]) { 
        int v, id; tie(v, id) = x; 
        if (v == p) continue; 
        dp(v, u); 
        f[u] += f[v];
        if (f[v] >= k) ans.push_back(id); 
    }
}

void solve() { 
    cin >> n >> m >> k; 
    FOR(i, 1, n - 1) { 
        int u, v; cin >> u >> v;
        g[u].push_back({v, i}); 
        g[v].push_back({u, i});
    }
    dfs(1, 0); 
    prepare_LCA();
    FOR(i, 1, m) { 
        int numNode; cin >> numNode; 
        vector<int> vecNode; 
        FOR(j, 1, numNode) { 
            int curNode; cin >> curNode; 
            vecNode.push_back(curNode); 
        }
        sort(vecNode.begin(), vecNode.end(), [&](const int &a, const int &b){ 
            return sta[a] < sta[b]; 
        }); 
        vector<int> vecLCA;
        FOR(j, 0, vecNode.size() - 2) { 
            vecLCA.push_back(LCA(vecNode[j], vecNode[j + 1])); 
        } 
        vecNode.insert(vecNode.end(), vecLCA.begin(), vecLCA.end()); 
        sort(vecNode.begin(), vecNode.end(), [&](const int &a, const int &b){ 
            return sta[a] < sta[b]; 
        });
        stack<int> st;  
        for (int &curNode : vecNode) { 
            while (st.size() && !inside(st.top(), curNode)) st.pop(); 
            if (st.size()) update(st.top(), curNode); 
            st.push(curNode); 
        }
    }
    dp(1, 0);
    sort(ans.begin(), ans.end()); 
    cout << ans.size() << '\n';
    for (int &x : ans) cout << x << ' ';  
}

///++++++++++++++++++++++++++++++++++++++///

#define task "test"
int32_t main() {
    if (fopen(task".inp","r")) {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    solve();
    bool M2;
    cerr << "++++++++++++++++++++++++++++\n";
    cerr << "Time: " << clock() << "ms" << '\n';
    cerr << "Memory: " << abs(&M2 - &M1) / 1024 / 1024 << "MB" << '\n';
    cerr << "++++++++++++++++++++++++++++\n";
    return 0;
}


Compilation message (stderr)

railway.cpp: In function 'int32_t main()':
railway.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
railway.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |         freopen(task".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...