QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65324#4580. Bicycle Tourtriplem5ds#WA 4ms21076kbC++233.2kb2022-11-29 20:29:002022-11-29 20:29:01

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-29 20:29:01]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:21076kb
  • [2022-11-29 20:29:00]
  • 提交

answer

/// Brrrr Brrrr
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")

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

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define mp(x,y) make_pair(x,y)
#define popCnt(x) (__builtin_popcountll(x))
#define int ll

using ll = long long;
using ii = pair<int, int>;
using ull = unsigned long long;

const int N = 1e6+5, LG = 18, MOD = (119 << 23) + 1;
const long double PI = acos(-1);
const long double EPS = 1e-7;

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};

int n, m;
int h[100005];
vector<int> add[100005], del[100005];
vector<int> adj[100005];
int p[100005];
int pa[N][LG], lvl[N];
int in[N], out[N], timer;
void dfs(int u, int p){
  in[u] = ++timer;
  for(int k = 1; k < LG; k++)
    pa[u][k] = pa[pa[u][k-1]][k-1];
  for(auto v : adj[u])
    if(v != p){
       lvl[v] = lvl[u] + 1;
       pa[v][0] = u;
       dfs(v, u);
     }
  out[u] = timer;
}
int LCA(int u, int v){
  if(lvl[u] > lvl[v])
    swap(u,v);
   int d = lvl[v] - lvl[u];
   for(int k = 0; k < LG; k++)
    if(d >> k & 1)
      v = pa[v][k];
   if(u == v)return u;
   for(int i = LG - 1; i >= 0; --i)
    if(pa[u][i] != pa[v][i]){
      u = pa[u][i];
      v = pa[v][i];
    }
  return pa[u][0];
}
int get(int i){return i == p[i]? i : p[i]=get(p[i]);}
multiset<int> mt[100005];
int ans[100005];
void dfs2(int node, int par) {

    for(auto v : adj[node]) if(v!=par){
        dfs2(v, node);
        if(mt[v].size() > mt[node].size()) {
            swap(mt[node], mt[v]);
        }
        for(auto x : mt[v])mt[node].insert(x);
        mt[v].clear();
    }
    for(auto x : add[node])mt[node].insert(x);
    if(mt[node].size())ans[node] = *mt[node].begin();
    for(auto x : del[node])mt[node].erase(mt[node].find(x));
}
void doWork() {

    memset(ans,-1,sizeof ans);

    cin >> n >> m;
    f(i,1,n+1)
        cin >> h[i], p[i]=i;
    vector<vector<int>> edges, extra;
    f(i,0,m) {
        int u, v;
        cin >> u >> v;
        edges.push_back({abs(h[u]-h[v]), u, v});
    }

    sort(all(edges));

    for(auto e : edges) {
        int w = e[0], v = e[1], u = e[2];
        if(get(u)==get(v)) {
            extra.push_back(e);
        }   else {
            adj[u].pb(v);
            adj[v].pb(u);
            p[get(u)] = get(v);
        }
    }

    dfs(1, 1);

    for(auto e : extra) {
        int w = e[0], v = e[1], u = e[2];
        if(lvl[u] > lvl[v])swap(u,v);
        int lca = LCA(u,v);
        if(lca == u)add[v].pb(w), del[u].pb(w);
        else {
            add[u].pb(w);
            add[v].pb(w);
            del[lca].pb(w);
        }
    }

    dfs2(1, 1);
    f(i,1,n+1)
        cout << ans[i] << " \n"[i==n];


}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE


    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }
    return 0;
}

///Look at my code ya codeomk

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 21076kb

input:

8 11
5 2 7 0 10 6 6 6
1 2
1 3
2 3
2 4
2 5
2 7
3 5
1 6
6 7
6 8
7 8

output:

0 4 5 -1 8 0 0 0

result:

wrong answer 1st lines differ - expected: '4 4 5 -1 8 0 0 0', found: '0 4 5 -1 8 0 0 0'