Submission #609748

#TimeUsernameProblemLanguageResultExecution timeMemory
609748dualityKeys (IOI21_keys)C++17
100 / 100
1186 ms62144 KiB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#include "keys.h"

int key[300000];
vpii adjList[300000];
int parent[300000];
int find(int n) {
    if (parent[n] != n) parent[n] = find(parent[n]);
    return parent[n];
}
queue<int> Q;
int visited[300000],ans[300000],seen[300000];
vi poss[300000];
int doBFS(int s,int n) {
    int i;
    vi vv,ee;
    Q.push(s);
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();

        if (find(u) != find(s)) {
            for (int v: vv) seen[key[v]] = 0;
            for (int c: ee) poss[c].clear();
            while (!Q.empty()) Q.pop();
            return u;
        }
        if (visited[u]) continue;
        visited[u] = 1,vv.pb(u),seen[key[u]] = 1;
        while (!poss[key[u]].empty()) Q.push(poss[key[u]].back()),poss[key[u]].pop_back();
        for (auto [v,c]: adjList[u]) {
            if (seen[c]) Q.push(v);
            else ee.pb(c),poss[c].pb(v);
        }
    }
    for (int v: vv) ans[v] = vv.size(),seen[key[v]] = 0;
    for (int c: ee) poss[c].clear();
    while (!Q.empty()) Q.pop();
    return -1;
}
vector<int> find_reachable(vector<int> r,vector<int> u,vector<int> v,vector<int> c) {
    int i,n = r.size(),m = c.size();
    copy(r.begin(),r.end(),key);
    for (i = 0; i < m; i++) {
        adjList[u[i]].pb(mp(v[i],c[i]));
        adjList[v[i]].pb(mp(u[i],c[i]));
    }
    for (i = 0; i < n; i++) parent[i] = i,ans[i] = 1e9;
    int f = 1;
    while (f) {
        f = 0;
        vpii vv;
        fill(visited,visited+n,0);
        for (i = 0; i < n; i++) {
            if (find(i) == i) {
                int v = doBFS(i,n);
                if (v != -1) vv.pb(mp(i,v)),f = 1;
            }
        }
        for (auto [u,v]: vv) {
            if (find(u) != find(v)) parent[find(u)] = find(v);
        }
    }
    int x = *min_element(ans,ans+n);
    for (i = 0; i < n; i++) ans[i] = (ans[i] == x);
    return vi(ans,ans+n);
}

Compilation message (stderr)

keys.cpp: In function 'int doBFS(int, int)':
keys.cpp:70:9: warning: unused variable 'i' [-Wunused-variable]
   70 |     int i;
      |         ^
#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...