Submission #400274

# Submission time Handle Problem Language Result Execution time Memory
400274 2021-05-07T19:15:46 Z duality Railway Trip (JOI17_railway_trip) C++11
5 / 100
2000 ms 44044 KB
#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 ----------

int L[100000];
struct query { int A,B,i; };
vector<query> vv;
int ans[100000];
pii p[200000];
set<int> adjList[100000];
vi adjList2[200000];
vi S;
int size[200000],visited[200000];
int doDFS(int u,int p) {
    int i;
    size[u] = 1;
    for (i = 0; i < adjList2[u].size(); i++) {
        int v = adjList2[u][i];
        if (!visited[v] && (v != p)) size[u] += doDFS(v,u);
    }
    return size[u];
}
int centroid(int u) {
    int i,p = -1,r = u;
    doDFS(r,-1);
    while (1) {
        int h = -1;
        for (i = 0; i < adjList2[u].size(); i++) {
            int v = adjList2[u][i];
            if (!visited[v] && (v != p)) {
                if ((h == -1) || (size[v] > size[h])) h = v;
            }
        }
        if (((h == -1) || (size[h] <= size[r]/2)) && (size[r]-size[u] <= size[r]/2)) break;
        else p = u,u = h;
    }
    return u;
}
int dist[2][100000];
vi vvv;
queue<int> Q;
int doBFS(int u,int *dist) {
    int i;
    Q.push(u),dist[u] = 0,vvv.pb(u);
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();

        for (auto it = adjList[u].begin(); it != adjList[u].end(); it++) {
            int v = *it;
            if (dist[v] == -1) {
                dist[v] = dist[u]+1,vvv.pb(v);
                Q.push(v);
            }
        }
    }
    return 0;
}
int com[100000],tt = 0;
int doBFS2(int u) {
    int i;
    Q.push(u),com[u] = tt;
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();

        for (auto it = adjList[u].begin(); it != adjList[u].end(); it++) {
            int v = *it;
            if (com[v] != tt) {
                com[v] = tt;
                Q.push(v);
            }
        }
    }
    return 0;
}
int f(int d) {
    return (d == -1) ? 1e9:d;
}
int decompose(int u,vector<query> vv) {
    int i;
    u = centroid(u);
    doBFS(p[u].first,dist[0]);
    doBFS(p[u].second,dist[1]);
    vector<query> vv2;
    for (i = 0; i < vv.size(); i++) {
        ans[vv[i].i] = min(ans[vv[i].i],f(dist[0][vv[i].A])+f(dist[0][vv[i].B]));
        ans[vv[i].i] = min(ans[vv[i].i],f(dist[1][vv[i].A])+f(dist[1][vv[i].B]));
        vv2.pb(vv[i]);
    }
    for (auto it = adjList[p[u].first].begin(); it != adjList[p[u].first].end(); it++) adjList[*it].erase(p[u].first);
    for (auto it = adjList[p[u].second].begin(); it != adjList[p[u].second].end(); it++) adjList[*it].erase(p[u].second);
    adjList[p[u].first].clear(),adjList[p[u].second].clear();
    for (i = 0; i < vvv.size(); i++) {
        dist[0][vvv[i]] = dist[1][vvv[i]] = -1;
        tt++;
        if (com[vvv[i]] != tt) doBFS2(vvv[i]);
    }
    for (i = 0; i < vv.size(); i++) {
        if (com[vv[i].A] == com[vv[i].B]) vv2.pb(vv[i]);
    }
    vvv.clear();
    visited[u] = 1;
    for (i = 0; i < adjList2[u].size(); i++) {
        int v = adjList2[u][i];
        if (!visited[v]) decompose(v,vv2);
    }
    return 0;
}
int main() {
    int i;
    int N,K,Q,A,B;
    scanf("%d %d %d",&N,&K,&Q);
    for (i = 0; i < N; i++) scanf("%d",&L[i]);
    for (i = 0; i < Q; i++) scanf("%d %d",&A,&B),vv.pb((query){A-1,B-1,i}),ans[i] = 1e9;

    int c = 0;
    for (i = 0; i < N; i++) {
        if (i > 0) {
            p[c++] = mp(i-1,i);
            S.pb(c-1);
            adjList[i-1].insert(i);
            adjList[i].insert(i-1);
        }
        int e = 1;
        while ((S.size() >= 2) && (L[p[S[S.size()-2]].second] < L[i])) {
            if (e && (L[p[S[S.size()-2]].first] > L[p[S[S.size()-2]].second])) {
                adjList[p[S[S.size()-2]].first].insert(i);
                adjList[i].insert(p[S[S.size()-2]].first);
            }
            e &= L[p[S[S.size()-2]].first] < L[i];
            p[c++] = mp(p[S[S.size()-2]].first,i);
            adjList2[c-1].pb(S[S.size()-2]);
            adjList2[S[S.size()-2]].pb(c-1);
            adjList2[c-1].pb(S.back());
            adjList2[S.back()].pb(c-1);
            S.pop_back(),S.pop_back(),S.pb(c-1);
        }
    }
    for (i = 1; i < S.size(); i++) {
        adjList2[S[i-1]].pb(S[i]);
        adjList2[S[i]].pb(S[i-1]);
    }
    memset(dist,-1,sizeof(dist));
    decompose(S[0],vv);
    for (i = 0; i < Q; i++) printf("%d\n",ans[i]-1);

    return 0;
}

Compilation message

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:166:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  166 |     scanf("%d %d %d",&N,&K,&Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:167:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  167 |     for (i = 0; i < N; i++) scanf("%d",&L[i]);
      |                             ~~~~~^~~~~~~~~~~~
railway_trip.cpp:168:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  168 |     for (i = 0; i < Q; i++) scanf("%d %d",&A,&B),vv.pb((query){A-1,B-1,i}),ans[i] = 1e9;
      |                             ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 10572 KB Output is correct
2 Correct 8 ms 10572 KB Output is correct
3 Correct 8 ms 10580 KB Output is correct
4 Correct 8 ms 10572 KB Output is correct
5 Correct 9 ms 10700 KB Output is correct
6 Correct 10 ms 10712 KB Output is correct
7 Correct 9 ms 10712 KB Output is correct
8 Correct 10 ms 10764 KB Output is correct
9 Correct 7 ms 10444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 11864 KB Output is correct
2 Execution timed out 2086 ms 36928 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2092 ms 42072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2098 ms 44044 KB Time limit exceeded
2 Halted 0 ms 0 KB -