QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#86596#4809. Maximum Rangenanoblob#WA 3ms5528kbC++175.6kb2023-03-10 11:19:402023-03-10 11:19:43

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-10 11:19:43]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:5528kb
  • [2023-03-10 11:19:40]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long
typedef int uci;
#define int long long
#define F first
#define S second
typedef complex<double> cd;

seed_seq seq{
    (uint64_t) chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count(),
    (uint64_t) __builtin_ia32_rdtsc(),
    (uint64_t) (uintptr_t) make_unique<char>().get()
};
mt19937 rng(seq);
// mt19937_64 lrng(seq);

struct debugger{
    template <typename T>
    debugger& operator<<(T &a){
        #ifdef DEBUG
            cerr << a;
        #endif
        return *this;
    }
    template <typename T>
    debugger& operator<<(T &&a){
        #ifdef DEBUG
            cerr << a;
        #endif
        return *this;
    }
} deb;

const double PI = acos(-1.0);
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;

//! function insert

//THINK FIRST, CODE SECOND
//DON'T GET STUCK ON ONE STRATEGY
//CALM DOWNNN FOR ONCE IN YOUR LIFE
//REDUCE
//COUGH E??!?!?!! O.O
//uwu i see you ryan

int lift[100000][32];
int cc[100000][32];
int depth[100000];

int find(int v, vector<int> &uf){
    if(uf[v] == v)
        return v;
    return uf[v] = find(uf[v], uf);
}

void dfs(int v, int p, int d, vector<vector<int>> &adj, vector<map<int, int>> &costs){
    depth[v] = d;
    lift[v][0] = p;
    cc[v][0] = costs[v][p];
    for(int i{1}; i < 32; ++i){
        lift[v][i] = lift[lift[v][i]][i];
        cc[v][i] = max(cc[v][i], cc[lift[v][i]][i]);
    }
    for(int i : adj[v]){
        if(i == p)
            continue;
        dfs(i, v, d+1, adj, costs);
    }
}

int goup(int u, int amt){
    int c{};
    while(amt > 0){
        if(amt&1){
            u = lift[u][c];
        }
        c++;
        amt >>= 1;
    }
    return u;
}

int getmax(int u, int amt){
    int out = -LINF;
    int c{};
    while(amt > 0){
        if(amt&1){
            out = max(out, cc[u][c]);
            u = lift[u][c];
        }
        c++;
        amt >>= 1;
    }
    return out;
}

int getdiff(int u, int v, int small){
    int start = u, end = v;
    if(depth[u] < depth[v]){
        swap(u, v);
        swap(start, end);
    }
    u = goup(u, depth[u] - depth[v]);

    if(u == v && depth[start] - depth[end] == 1){
        return LINF;
    }

    int t = getmax(start, depth[start]-depth[end]);
    if(u == v)
        return abs(t-small);

    for(int i = 31; i >= 0; --i){
        if(lift[u][i] != lift[v][i]){
            t = max(t, max(cc[u][i], cc[v][i]));
            u = lift[u][i];
            v = lift[v][i];
        }
    }

    return abs(small - max(t, max(lift[u][0], lift[v][0])));
}
vector<int> cycle;
bool findcycle(int v, int p, int end, vector<vector<int>> &adj){
    if(end == v){
        cycle.push_back(v);
        return true;
    }
    for(int i : adj[v]){
        if(p == i)
            continue;
        if(findcycle(i, v, end, adj)){
            cycle.push_back(v);
            return true;
        }
    }
    return false;
}
void solve() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n);
    vector<map<int, int>> costs(n);
    vector<ar<int, 3>> q(m);
    for(int i{}; i < m; ++i){
        int u, v, c;
        cin >> u >> v >> c;
        u--, v--;
        costs[u][v] = c;
        costs[v][u] = c;
        q[i][0] = c;
        q[i][1] = u;
        q[i][2] = v;
    }
    sort(q.rbegin(), q.rend());
    for(int i{}; i < n; ++i)
        costs[i][i] = -LINF;
    vector<int> uf(n);
    iota(uf.begin(), uf.end(), 0);
    for(int o{}; o < m; ++o){
        auto t = q[o];

        int c = t[0], u = t[1], v = t[2];
        int i = u, j = v;
        u = find(u, uf);
        v = find(v, uf);
        if(u != v){
            adj[i].push_back(j);
            adj[j].push_back(i);
            uf[u] = v;
        }
    }

    for(int i{}; i < 32; ++i)
        lift[0][i] = 0;
    dfs(0, 0, 0, adj, costs);

    int biggest{-1};
    int start = -1, end = -1;
    for(int i = m-1; i >= 0; --i){
        int diff = getdiff(q[i][1], q[i][2], q[i][0]);
        if(diff < LINF){
            if(diff > biggest){
                biggest = diff;
                start = q[i][1];
                end = q[i][2];
            }
        }
    }

    cout << biggest << '\n';
    findcycle(start, -1, end, adj);
    cout << cycle.size() << '\n';
    for(int i : cycle)
        cout << i+1 << ' ' ;
    cout << '\n';

}

uci main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

        // cout << "Case #" << t  << ": ";
        solve();
}

/*
    random number generator stuff
    num = rng(); gives integer number
    num = uniform_int_distribution<int>(a, b)(rng); -> bounds [a, b]
    num = uniform_real_distribution<double>(a, b)(rng); -> bounds [a, b)
    can also instantiate distributions and call on generator:
    uniform_int_distribution<int> thing(a, b);
    num = thing(rng);
*/
// struct custom_hash {
//     static uint64_t splitmix64(uint64_t x) {
//         // http://xorshift.di.unimi.it/splitmix64.c
//         x += 0x9e3779b97f4a7c15;
//         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//         return x ^ (x >> 31);
//     }
//     size_t operator()(uint64_t x) const {
//         static const uint64_t FIXED_RANDOM = lrng();
//         return splitmix64(x + FIXED_RANDOM);
//     }
// };

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 5528kb

input:

5 7
1 2 1
1 3 -2
2 3 1
3 4 3
4 5 1
1 5 -1
2 5 2

output:

2
5
3 4 5 2 1 

result:

wrong answer Cycle has range 5, but output says 2