Submission #823505

#TimeUsernameProblemLanguageResultExecution timeMemory
823505becaidoKeys (IOI21_keys)C++17
0 / 100
15 ms30848 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "keys.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 3e5 + 5;

int n, m;
vector<pair<int, int>> adj[SIZE];

vector<int> cand, done;
vector<int> all[SIZE];
unordered_set<int> us[SIZE];
int to[SIZE], g[SIZE], vs[SIZE];

void clean(int x, int y = -1) {
    for (int pos : all[x]) g[pos] = y;
    all[x].clear();
    unordered_set<int> foo;
    swap(us[x], foo);
}

void deal() {
    fill(to, to + n, -1);
    for (int x : cand) {
        int tag = 0;
        for (int pos : all[x]) if (tag == 0) for (auto [np, c] : adj[pos]) if (us[x].count(c)) {
            if (g[np] == -2) {
                tag = 1;
                break;
            }
            if (g[np] == -1) {
                tag = 3;
                break;
            }
            if (g[np] != x) {
                tag = 2;
                to[x] = g[np];
                break;
            }
        }
        if (tag == 0) tag = 3;
        if (tag == 1 || tag == 3) {
            if (tag == 3) done.pb(x);
            clean(x, (tag == 1 ? -1 : -2));
        }
    }
    fill(vs, vs + n, 0);
    vector<int> tmp;
    for (int x : cand) if (to[x] != -1 && !vs[x]) {
        vector<int> chain, cyc;
        chain.pb(x), vs[x] = 1, x = to[x];
        while (!vs[x]) {
            chain.pb(x);
            vs[x] = 1;
            x = to[x];
        }
        if (vs[x] == 2) {
            for (int x : chain) {
                vs[x] = 2;
                clean(x);
            }
            continue;
        }
        for (int x : chain) vs[x] = 2;
        FOR (i, 0, chain.size() - 1) if (chain[i] == x) {
            FOR (j, 0, i - 1) clean(chain[j]);
            FOR (j, i + 1, chain.size() - 1) {
                int y = chain[j];
                for (int pos : all[y]) {
                    g[pos] = x;
                    all[x].pb(pos);
                }
                all[y].clear();
                for (int c : us[y]) us[x].insert(c);
                unordered_set<int> foo;
                swap(us[y], foo);
            }
            tmp.pb(x);
            break;
        }
    }
    cand = tmp;
    // for (int x : cand) cout << x << ' '; cout << '\n';
    // for (int x : done) cout << x << ' '; cout << '\n';
    // FOR (i, 1, n) cout << g[i] << ' '; cout << '\n';
}

vector<int> reach(int s, vector<int> &r) {
    unordered_set<int> us;
    vector<int> all;
    unordered_map<int, vector<int>> wait;
    queue<int> q;
    q.push(s), us.insert(r[s]), vs[s] = 1;
    while (q.size()) {
        int pos = q.front();
        q.pop();
        all.pb(pos);
        for (auto [np, c] : adj[pos]) if (!vs[np]) {
            if (us.count(c)) {
                vs[np] = 1;
                q.push(np);
                if (!us.count(r[np])) {
                    us.insert(r[np]);
                    for (int x : wait[r[np]]) if (!vs[x]) {
                        vs[x] = 1;
                        q.push(x);
                    }
                    wait[r[np]].clear();
                }
            } else {
                wait[c].pb(np);
            }
        }
    }
    return all;
}

vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) {
    n = r.size(), m = u.size();
    FOR (i, 0, m - 1) {
        adj[u[i]].pb(v[i], c[i]);
        adj[v[i]].pb(u[i], c[i]);
    }
    cand.resize(n);
    iota(cand.begin(), cand.end(), 0);
    FOR (i, 0, n - 1) {
        g[i] = i;
        all[i].pb(i);
        us[i].insert(r[i]);
    }
    while (cand.size()) deal();
    int mn = n + 1;
    vector<int> ans(n), ansp;
    fill(vs, vs + n, 0);
    for (int s : done) {
        debug(s);
        auto v = reach(s, r);
        if (v.size() < mn) {
            mn = v.size();
            ansp.clear();
        }
        if (v.size() == mn) ansp.insert(ansp.end(), v.begin(), v.end());
    }
    for (int x : ansp) ans[x] = 1;
    return ans;
}

/*
3 2
0 1 0
0 1 0
1 2 0
*/

#ifdef WAIMAI
int main() {
    int n, m;
    assert(2 == scanf("%d%d", &n, &m));
    vector<int> r(n), u(m), v(m), c(m);
    for(int i=0; i<n; i++) {
        assert(1 == scanf("%d", &r[i]));
    }
    for(int i=0; i<m; i++) {
        assert(3 == scanf("%d %d %d", &u[i], &v[i], &c[i]));
    }

    vector<int> ans = find_reachable(r, u, v, c);

    for (int i = 0; i < (int) ans.size(); ++i) {
        if(i) putchar(' ');
        putchar(ans[i]+'0');
    }
    printf("\n");
    return 0;
}
#endif

Compilation message (stderr)

keys.cpp: In function 'std::vector<int> find_reachable(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
keys.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
keys.cpp:157:9: note: in expansion of macro 'debug'
  157 |         debug(s);
      |         ^~~~~
keys.cpp:159:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  159 |         if (v.size() < mn) {
      |             ~~~~~~~~~^~~~
keys.cpp:163:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  163 |         if (v.size() == mn) ansp.insert(ansp.end(), v.begin(), v.end());
      |             ~~~~~~~~~^~~~~
#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...