제출 #964342

#제출 시각아이디문제언어결과실행 시간메모리
964342steveonalex통행료 (IOI18_highway)C++17
100 / 100
171 ms18976 KiB
#include <bits/stdc++.h>
#include "highway.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 1e5 + 69;
int n, m;
vector<pair<int,int>> graph[N];
vector<pair<int, int>> DAG[N];

void dfs(int u, vector<int> &topo){
    for(pair<int, int> v: DAG[u]){
        dfs(v.first, topo);
        topo.push_back(v.second);
    }
}

// struct P{
//     ll i, w;
//     P(){}
//     P(ll _i, ll _w){i = _i, w = _w;}
// };

// struct compare{
//     bool operator () (P a, P b) {return a.w > b.w;}
// };

// namespace Grader{
//     int n, m;
//     int s, t;
//     int x, y;
//     vector<int> U, V;
//     int A, B;
//     int o_cnt = 0;

//     ll ask(vector<int> w){
//         o_cnt++;
//         vector<vector<P>> graph(n);
//         for(int i = 0; i<w.size(); ++i){
//             int u = U[i], v = V[i];
//             int cur_w = A;
//             if (w[i]) cur_w = B;
//             graph[u].push_back(P(v, cur_w));
//             graph[v].push_back(P(u, cur_w));
//         }
//         vector<ll> dis(n+1, 1e18);
//         dis[s] = 0;
//         priority_queue<P, vector<P>, compare> pq; 
//         pq.push(P(s, 0));
//         while(pq.size()){
//             P u= pq.top(); pq.pop();
//             for(P v: graph[u.i]) if (minimize(dis[v.i], dis[u.i] + v.w)) 
//                 pq.push(P(v.i, dis[v.i]));
//         }
//         return dis[t];
//     }

//     void answer(int _x, int _y){
//         x = _x, y = _y;
//     }

    void find_pair(int _n, vector<int> U, vector<int> V, int A, int B) {
        n = _n, m = U.size();
        for(int i = 0; i<n; ++i) {
            graph[i].clear();
            DAG[i].clear();
        }

        ll initial = ask(vector<int>(m));
        int l = 0, r = m - 1;
        while(l < r){
            int mid = (l + r) >> 1;
            vector<int> w(m);
            for(int i = 0; i<=mid; ++i) w[i] = 1;
            if (ask(w) > initial) r = mid;
            else l = mid + 1;
        }

        if (l == m - 1) {
            answer(U[l], V[l]);
            return;
        }

        for(int i = l + 1; i<m; ++i){
            int u = U[i], v = V[i];
            graph[u].push_back({v, i});
            graph[v].push_back({u, i});
        }

        int u = U[l], v = V[l];
        vector<int> dis(n, 1e9);
        dis[u] = dis[v] = 0;
        deque<int> q; q.push_back(u); q.push_back(v);
        while(q.size()){
            int u= q.front(); q.pop_front();
            if (dis[u] == initial / A - 1) continue;
            for(pair<int, int> v: graph[u]) if (minimize(dis[v.first], dis[u] + 1)){
                q.push_back(v.first);
                DAG[u].push_back(v);
            }
        }

        vector<int> S1, S2;
        dfs(u, S1); dfs(v, S2);

        vector<int> S = S1;
        for(int i: S2) S.push_back(i);
        remove_dup(S);

        vector<int> _w(m);
        for(int i = 0; i<m; ++i){
            if (!binary_search(ALL(S), i)) _w[i] = 1;
        }
        _w[l] = 0;
        
        int x, y;
        l = 0, r = S1.size();
        while(l < r){
            int mid = (l + r) >> 1;
            vector<int> w = _w;
            for(int i = 0; i<=mid; ++i) w[S1[i]] = 1;
            if (ask(w) > initial) r = mid;
            else l = mid + 1;
        }
        if (l == S1.size()) x = u;
        else{
            if (dis[U[S1[l]]] > dis[V[S1[l]]]) x = U[S1[l]];
            else x = V[S1[l]];
        }

        l = 0, r = S2.size();
        while(l < r){
            int mid = (l + r) >> 1;
            vector<int> w = _w;
            for(int i = 0; i<=mid; ++i) w[S2[i]] = 1;
            if (ask(w) > initial) r = mid;
            else l = mid + 1;
        }
        if (l == S2.size()) y = v;
        else{
            if (dis[U[S2[l]]] > dis[V[S2[l]]]) y = U[S2[l]];
            else y = V[S2[l]];
        }
        answer(x, y);
    }

//     pair<bool, int> solve(int _n, int _m){
//         n = _n, m = _m;
//         U.clear(); V.clear();
//         x = y = -1;
//         o_cnt = 0;
//         for(int i= 1; i<n; ++i){
//             U.push_back(rngesus(0, i-1));
//             V.push_back(i);
//             if (rngesus(0, 1)) swap(U.back(), V.back());
//         }
//         for(int i = n; i<=m; ++i){
//             int u = rngesus(0, n-1);
//             int v = u;
//             while(u == v) v = rngesus(0, n-1);
//             U.push_back(u);
//             V.push_back(v);
//         }

//         s = rngesus(0, n-1);
//         t = s;
//         while(t == s) t = rngesus(0, n-1);

//         A = 1, B = 2;

//         find_pair(n, U, V, A, B);

//         if (x > y) swap(x, y);
//         if (s > t) swap(s, t);
//         return make_pair(make_pair(x, y) == make_pair(s, t), o_cnt);
//     }
// }


// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     for(int iteration = 1; iteration <= 100; ++iteration){
//         pair<bool, int> verdict = Grader::solve(100, rngesus(99, 111));
//         if (verdict.first) cout << "AC\n";
//         else cout << "WA\n";

//         cout << "Operation count: " << verdict.second << "\n";

//         if (verdict.first == false) break;
//     }

//     return 0;
// }

컴파일 시 표준 에러 (stderr) 메시지

highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:166:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |         if (l == S1.size()) x = u;
      |             ~~^~~~~~~~~~~~
highway.cpp:180:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |         if (l == S2.size()) y = v;
      |             ~~^~~~~~~~~~~~
#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...