Submission #1258887

#TimeUsernameProblemLanguageResultExecution timeMemory
1258887shidou26Railway (BOI17_railway)C++17
100 / 100
52 ms28096 KiB
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define filter(v) v.resize(unique(all(v)) - v.begin())
#define dbg(x) "[" #x << " = " << x << "]"

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T> bool maximize(T &a, T b) {
    if(a < b) return a = b, true;
    else return false;
}

template<typename T> bool minimize(T &a, T b) {
    if(a > b) return a = b, true;
    else return false;
}

template<typename T1, typename T2> T2 rnd(T1 l, T2 r) {
    return uniform_int_distribution<T2>(l, r)(rng);
}

typedef long long                  ll;
typedef long double                ld;
typedef pair<int, int>             pii;
typedef pair<long long, int>       pli;
typedef pair<long long, long long> pll;

const int N = 1e5 + 3;
const int LOG = 20;

int n, m, k;
int eu[N], ev[N];
vector<int> adj[N];

int timer = 0;
int tin[N], tout[N], b[N], dp[N];
int par[N][LOG];
vector<int> edges;

bool isAncestor(int u, int v) {return tin[u] <= tin[v] && tout[v] <= tout[u];}
int getLca(int u, int v) {
    if(isAncestor(u, v)) return u;
    if(isAncestor(v, u)) return v;
    for(int i = LOG - 1; i >= 0; i--)
        if(par[u][i] && !isAncestor(par[u][i], v))
            u = par[u][i];
    return par[u][0];
}

void dfs(int u, int p) {
    tin[u] = ++timer;

    for(int id : adj[u]) if(id != p) {
        int v = eu[id] ^ ev[id] ^ u;

        par[v][0] = u;
        for(int j = 1; j < LOG; j++) {
            par[v][j] = par[par[v][j - 1]][j - 1];
        }

        dfs(v, id);
    }

    tout[u] = timer;
}

void dfsDiff(int u, int p) {
    dp[u] = b[u];
//    cout << dbg(u) << dbg(dp[u]) << endl;

    for(int id : adj[u]) if(id != p) {
        int v = eu[id] ^ ev[id] ^ u;

        dfsDiff(v, id);
        dp[u] += dp[v];

        if(dp[v] >= 2 * k)
            edges.push_back(id);
    }
}

void process() {
    dfs(1, -1);

    while(m--) {
        int x; cin >> x;
        vector<int> nodes;
        while(x--) {
            int ver; cin >> ver;
            nodes.push_back(ver);
        }

        sort(all(nodes), [&](int u, int v){
            return tin[u] < tin[v];
        });

        for(int i = 0; i < sz(nodes); i++) {
            int j = (i + 1) % sz(nodes);
            b[nodes[i]]++;
            b[nodes[j]]++;
            b[getLca(nodes[i], nodes[j])] -= 2;
//            cout << dbg(nodes[i]) << dbg(nodes[j]) << dbg(getLca(nodes[i], nodes[j])) << endl;
        }
    }

    dfsDiff(1, -1);

    cout << sz(edges) << endl;
    sort(all(edges));
    for(int id : edges)
        cout << id << " ";
}

void input() {
    cin >> n >> m >> k;

    for(int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        eu[i] = u;
        ev[i] = v;
        adj[u].push_back(i);
        adj[v].push_back(i);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    #define task "SUADUONG"
    if(fopen(task".INP", "r")) {
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int testcase = 1; // cin >> testcase;
    for(int i = 1; i <= testcase; i++) {
        input();
        process();
    }

    return 0;
}

Compilation message (stderr)

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