Submission #118674

# Submission time Handle Problem Language Result Execution time Memory
118674 2019-06-19T10:52:02 Z someone_aa Highway Tolls (IOI18_highway) C++17
11 / 100
1500 ms 42904 KB
#include "highway.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
int n, m;
vector<int>w;
vector<int>gs[maxn];
vector<pair<int,int> > g[maxn];

vector<int>gt[maxn];

int dist[maxn][2], parent[maxn][2];
bool visited[maxn];

ll dista;

map<pair<int,int>, int> ind;

void bfs(int st, int d) {
    memset(visited,false,sizeof(visited));
    queue<int>q;
    q.push(st);

    parent[st][d] = -1;
    visited[st] = true;

    while(!q.empty()) {
        int curr = q.front();
        q.pop();

        for(auto i:g[curr]) {
            if(!visited[i.first]) {
                visited[i.first] = true;
                dist[i.first][d] = dist[curr][d] + 1;
                parent[i.first][d] = i.second;
                q.push(i.first);
            }
        }
    }
}

set<pair<int,int> > sta, stb;

void preprocess() {
    for(int i=0;i<n;i++) {
        if(dist[i][0] == dist[i][1]) continue; // skip this node

        if(dist[i][0] < dist[i][1]) {
            for(int j:gs[i]) {
                if(dist[j][0] < dist[j][1]) {
                    sta.insert(mp(min(i, j), max(i, j)));
                }
            }
        }
        else {
            for(int j:gs[i]) {
                if(dist[j][0] > dist[j][1]) {
                    stb.insert(mp(min(i, j), max(i, j)));
                }
            }
        }
    }
}

int solve(set<pair<int,int> > edges, int root, int d) {
    // build bfs tree

    memset(visited,false,sizeof(visited));
    //memset(parent,-1,sizeof(parent));

    for(auto i:edges) {
        gt[i.first].pb(i.second);
        gt[i.second].pb(i.first);
    }

    vector<int>v, v2;

    queue<int>q;
    q.push(root);

    visited[root] = true;
    parent[root][d] = -1;

    while(!q.empty()) {
        int curr = q.front();
        q.pop();

        v.pb(parent[curr][d]);
        v2.pb(curr);
        for(auto i:gt[curr]) {
            if(!visited[i]) {
                visited[i] = true;
                parent[i][d] = curr;
                dist[i][d] = dist[curr][d] + 1;
                q.push(i);
            }
        }
    }

    if(v.size() == 1) return v2[0];
    // perform binary search

    for(int i=0;i<v.size();i++) {
        if(v[i] == -1) continue; // root
        int cind = ind[mp(v[i], v2[i])];
        w[cind] = 1;
    }

    int li = 0, ri = v.size() - 1;

    while(li < ri) {
        int mid = (li + ri) / 2;

        // all before mid put them as low cost and other as high cost
        for(int i=0;i<=mid;i++) {
            if(v[i] == -1) continue; // root
            int cind = ind[mp(v[i], v2[i])];
            w[cind] = 0;
        }

        ll cdist = ask(w);
        for(int i=0;i<=mid;i++) {
            if(v[i] == -1) continue; // root
            int cind = ind[mp(v[i], v2[i])];
            w[cind] = 1;
        }

        if(cdist == dista) {
            ri = mid;
        }
        else {
            li = mid + 1;
        }
    }

    for(int i=0;i<v.size();i++) {
            if(v[i] == -1) continue; // root
            int cind = ind[mp(v[i], v2[i])];
            w[cind] = 0;
        }

    return v2[li];
}

void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
    n = N; m = U.size();
    int li=0, ri=m-1;

    for(int i=0;i<m;i++) {
        ind[mp(U[i], V[i])] = ind[mp(V[i], U[i])] = i;
        gs[U[i]].pb(V[i]);
        gs[V[i]].pb(U[i]);

        g[U[i]].pb(mp(V[i], i));
        g[V[i]].pb(mp(U[i], i));

        w.pb(0);
    }

    dista = ask(w);
    while(li<ri) {
        int mid = (li+ri)/2;
        for(int i=li;i<=mid;i++) {
            w[i] = 1;
        }
        ll distb = ask(w);
        for(int i=li;i<=mid;i++) {
            w[i] = 0;
        }

        if(dista == distb) {
            li = mid + 1;
        }
        else {
            ri = mid;
        }
    }
    int x = li;
    int u = U[x], v = V[x];

    //cout<<u<<" "<<v<<"\n";

    bfs(u, 0);
    bfs(v, 1);
    preprocess();

    int xx = solve(sta, u, 0);
    int yy = solve(stb, v, 1);

    //cout<<"Ans: "<<xx<<" "<<yy<<"\n";
    answer(xx, yy);
}

Compilation message

highway.cpp: In function 'int solve(std::set<std::pair<int, int> >, int, int)':
highway.cpp:106:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<v.size();i++) {
                 ~^~~~~~~~~
highway.cpp:139:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<v.size();i++) {
                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7396 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7396 KB Output is correct
4 Correct 9 ms 7484 KB Output is correct
5 Correct 8 ms 7396 KB Output is correct
6 Correct 9 ms 7464 KB Output is correct
7 Correct 10 ms 7416 KB Output is correct
8 Correct 9 ms 7392 KB Output is correct
9 Correct 8 ms 7416 KB Output is correct
10 Correct 9 ms 7464 KB Output is correct
11 Correct 9 ms 7416 KB Output is correct
12 Correct 9 ms 7464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 7800 KB Output is correct
2 Correct 64 ms 11208 KB Output is correct
3 Execution timed out 1528 ms 40732 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 45 ms 10964 KB Output is correct
2 Correct 88 ms 14500 KB Output is correct
3 Correct 131 ms 17936 KB Output is correct
4 Correct 489 ms 37896 KB Output is correct
5 Correct 391 ms 37936 KB Output is correct
6 Correct 782 ms 39064 KB Output is correct
7 Correct 451 ms 39560 KB Output is correct
8 Correct 588 ms 38368 KB Output is correct
9 Correct 421 ms 37988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 7848 KB Output is correct
2 Correct 66 ms 10872 KB Output is correct
3 Correct 549 ms 33604 KB Output is correct
4 Correct 814 ms 40792 KB Output is correct
5 Correct 775 ms 40968 KB Output is correct
6 Correct 713 ms 40932 KB Output is correct
7 Correct 742 ms 40816 KB Output is correct
8 Correct 731 ms 40816 KB Output is correct
9 Correct 1254 ms 40316 KB Output is correct
10 Correct 1070 ms 40672 KB Output is correct
11 Execution timed out 1747 ms 39968 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 82 ms 11092 KB Output is correct
2 Correct 66 ms 11592 KB Output is correct
3 Execution timed out 2055 ms 42720 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 10972 KB Output is correct
2 Correct 111 ms 11632 KB Output is correct
3 Correct 788 ms 39560 KB Output is correct
4 Incorrect 1181 ms 42904 KB Output is incorrect: {s, t} is wrong.
5 Halted 0 ms 0 KB -