QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358501#3047. Wind of ChangeFroranzenWA 960ms77304kbC++1410.5kb2024-03-19 20:19:432024-03-19 20:19:43

Judging History

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

  • [2024-03-19 20:19:43]
  • 评测
  • 测评结果:WA
  • 用时:960ms
  • 内存:77304kb
  • [2024-03-19 20:19:43]
  • 提交

answer


#include <bits/stdc++.h>
#define rep(i, f, t) for(int i(f); i <= t; ++i)
#define re(i, t) for(int i(1); i <= t; ++i)
#define per(i, t, f) for(int i(t); i >= f; --i)
#define pe(i, t) for(int i(t); i >= 1; --i)
#define ste(i, f, t, s) for(int i(f); i <= t; i += s)
#define ets(i, t, f, s) for(int i(t); i >= f; i -= s)
#define each(i, x) for(auto &i : (x))
#define nx(i, u) for(int i(head[u]); i; i = e[i].nxt) 
typedef long long ll;
typedef long double lb;
typedef unsigned long long ull;
// #define int long long
using namespace std;
// typedef pair <double, int> pdi;
typedef pair <int, int> pii;
// typedef pair <string, bool> psb;
#define pb push_back
#define fi first
#define se second 
#define ix(l, r) ((l + r) | (l != r))
#define ls (ix(l, mid))
#define rs (ix(mid + 1, r))
#define mp(i, j) (make_pair(i, j))
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define i128 __int128
#define exp 1e-7
#define look_memory cerr<<abs(&sT-&eD)/1024.0/1024<<'\n'
int sT;    
 
struct IO {
    #define MAXSIZE 1<<21
    #define isdigit(x) (x >= '0' && x <= '9')
    #define isspace(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
    char ibuf[MAXSIZE], obuf[MAXSIZE], *s1, *s2, *s3, endl, blank;
    int round[10] = {0, 0, 0, 0, 0, 1, 1, 1, 1}, sta[65], precisions;
    bool fail;
    FILE *in_stream, *out_stream;
    IO(FILE *_stream = stdin, FILE *__stream = stdout) { reset(_stream,__stream); }
    #if DEBUG
    #else
        ~IO() {close();}
    #endif
    inline void reset (FILE *_stream = stdin, FILE *__stream = stdout, bool reset = true) {
        s1 = s3 = ibuf, s2 = obuf, in_stream = _stream, out_stream = __stream, fail = false;
        if(reset) { endl = '\n'; blank = ' '; precisions = 6; }
    }
    inline void flush_in() {s3 = (s1 = ibuf) + fread(ibuf, 1, MAXSIZE, in_stream); }
    inline void flush_out() { fwrite(obuf, 1, s2-obuf, out_stream), s2 = obuf; }
    inline void flush_out_with_stream() { flush_out(); fflush(out_stream); } 
    inline char get() {
        #if DEBUG  
            return getchar();
        #endif
        return s1 == s3 && (flush_in(), fail = s1 == s3) ? 0 : *s1++;
    }
    inline void put(char ch) {
        #if DEBUG
            putchar(ch);
        #else
            s2-obuf == MAXSIZE ? flush_out(), 0 : 0, *s2++=ch;
        #endif
    }
    template <class T>
    inline void read(T &x) {
        bool sign = false; char c = get(); x = 0;
        while(!isdigit(c) && c) {sign=c=='-'; c = get(); }
        while(isdigit(c)) { x = (x<<1) + (x<<3) + (c^'0'); c = get(); }
        sign ? x = ~x+1 : 0;
    }
    inline void read(double &x) {
        bool sign = false; char c = get(); x = 0;
        while(!isdigit(c) && c) { sign=c=='-'; c = get(); }
        while(isdigit(c)) { x = x * 10 + (c^'0'); c = get(); }
        if(c=='.') { c = get(); double tmp = 1; while(isdigit(c)) { tmp /= 10, x += tmp * (c^'0'); c = get(); } }
        sign ? x = -x : 0;
    }
    inline void read(long double &x) {
        bool sign = false; register char c = get(); x = 0;
        while(!isdigit(c) && c) { sign=c=='-'; c = get(); }
        while(isdigit(c)) { x = x * 10 + (c^'0'); c = get(); }
        if(c == '.') { c = get(); register long double tmp = 1; while(isdigit(c)) { tmp /= 10, x += tmp * (c^'0'); c = get(); } }
        sign ? x = -x : 0;
    }
    inline void read(char *s) {
        char c = get();
        while(isspace(c)) c = get();
        while(!isspace(c) && c) { *s++=c; c = get(); }
        *s = '\0';
    }
    inline void read(char &c) {
        do
            c = get();
        while(isspace(c));
    }
    template <class T, class ...Args>
    inline void read(T &x, Args &...args) { read(x), read(args...); }
    template <class T>
    inline void write(T x) {
        int top = 0;
        if(x<0) { put('-'); sta[top++] = ~(x%10)+1, x /= 10; x = ~x+1; }
        else sta[top++] = x%10, x /= 10;
        while(x) sta[top++] = x%10 ,x /= 10;
        while(top) put(sta[--top]^'0');
    }
    inline void write(double y) {
        int top = 0;
        if(y<0) { put('-'); y=-y; }
        int x = y; y -=x;
        write(x);
        if(y) {
            do
                sta[top++] = y*10, y = y*10 - sta[top-1];
            while(top<precisions-1);
            sta[top++] = y*10 + round[(int)((y*10-((int)(y*10)))*10)];
        }
        put('.');
        for(int i(0); i<top; ++i) put(sta[i]^'0');
        for(int i(top); i<precisions; ++i) put('0');
    }
    inline void write(long double y) {
        register int top = 0;
        if(y<0) { put('-'); y=-y; }
        int x = y; y -= x;
        write(x);
        if(y) {
            do
                sta[top++] = y*10, y = y*10 - sta[top-1];
            while(top<precisions-1);
            sta[top++] = y*10 + round[(int)((y*10-((int)(y*10)))*10)];
        }
        put('.');
        for(register int i(0); i < top; ++i) put(sta[i]^'0');
        for(register int i(top); i < precisions; ++i) put('0');
    }
    inline void write(const char ch) { put(ch); }
    inline void write(char *s) { while(*s!='\0') put(*s++); }
    inline void write(const char *s) { while(*s!='\0') put(*s++); }
    inline void write(const std::string str) { write(str.c_str()); }
    inline IO &precision(const int x) { precisions=x; return *this; }
    template <class T,class ...Args>
    inline void write(T x,Args ...args) { write(x), blank?put(blank), 0:0, write(args...); }
    template <class ...Args>
    inline void writeln(Args ...args) { write(args...), endl?put(endl), 0:0; }
    template <class T>
    inline IO &operator>>(T &x) { read(x); return *this; }
    inline IO &operator>>(IO &x) { return *this; }
    template <class T>
    inline IO &operator<<(const T x) { write(x); return *this; }
    inline IO &operator<<(IO &x) { return *this; }
    inline operator bool() { return !fail; }
    template <class T>
    inline operator T() { T x; read(x); return x; }
    inline void open(FILE *_stream=stdin,FILE *__stream=stdout) { close(), reset(_stream, __stream, false); }
    inline void close() { flush_out_with_stream(); fclose(in_stream), fclose(out_stream); }
    #define read(x) io>>x
    #define out(x) io<<x
}io;

const int N = 5e5 + 5;
int n, u, v, w;

int rt, sum, mxp[N], siz[N], vis[N];
ll ans[N];

struct node {
    int to, nxt, val;
}e[N<<1];

int head[N], cnt;

void add (int u, int v, int w) {
    e[++cnt] = (node){v, head[u], w};
    head[u] = cnt;
}

ll d[N];
int dep[N], fath[N], son[N], sz[N]; 

void dfs_1 (int u, int fa) {
    fath[u] = fa;
    sz[u] = 1;
    nx(i, u) {
        int v = e[i].to;
        if(v == fa) continue;
        dep[v] = dep[u] + 1;
        d[v] = d[u] + e[i].val;
        dfs_1(v, u);
        sz[u] += sz[v];
        if(sz[v] > sz[son[u]]) son[u] = v;
    }
}

int top[N], dfn[N], id[N], tnt;

void dfs_2 (int u, int topf) {
    top[u] = topf;
    dfn[u] = ++tnt; id[tnt] = u;
    if(!son[u]) return ;
    dfs_2(son[u], topf);
    nx(i, u) {
        int v = e[i].to;
        if(v == fath[u] || v == son[u]) continue;
        dfs_2(v, v);
    }
}

int lca (int u, int v) {
    while(top[u] != top[v]) {
        if(dep[top[u]] < dep[top[v]]) swap(u, v);
        u = fath[top[u]];
    }
    return dep[u] < dep[v] ? u : v;
}

int sta[N], R;
ll val[N], mxn;
vector<int>G[N];
int lc;
int h[N], len;
int used[N]; 
ll f[N], g[N];

void dfs (int u) {
    f[u] = g[u] = INF;
    for(int v : G[u]) {
        dfs(v);
        ll res = f[v] + d[v] - d[u];
        if(res < f[u]) {
            g[u] = f[u];
            f[u] = res;
        }
        else if(f[v] < g[u]) {
            g[u] = res;
        }
    }
    if(used[u]) {
        ans[u-n] = min(ans[u-n], f[u] + val[u]);
        if(f[u] > val[u]) {
            g[u] = f[u];
            f[u] = val[u];
        }
        else if(g[u] > val[u]) {
            g[u] = val[u];
        }
    }
}

void dfs_5 (int u, ll w) {
    if(used[u]) {
        ans[u-n] = min(ans[u-n], val[u] + w);
    }
    for(int v : G[u]) {
        ll res = f[v] + d[v] - d[u];
        if(res == f[u]) {
            dfs_5(v, min(w, g[u]) + d[v] - d[u]);
        }
        else {
            dfs_5(v, min(w, f[u]) + d[v] - d[u]);
        }
    }
}       

void dfs_4 (int u) {
    for(int v : G[u]) {
        dfs_4(v);
    }
    G[u].clear();
}

void build () {
    re(i, len) h[i] = dfn[h[i]];
    sort(h + 1, h + len + 1);
    re(i, len) h[i] = id[h[i]], used[h[i]] = 1;
    sta[R = 1] = n + 1;
    re(i, len) {
        if(h[i] == n + 1) continue; 
        int v = lca(sta[R], h[i]);    
        if(v != sta[R]) {
            while(dfn[v] < dfn[sta[R-1]]) {
                G[sta[R-1]].pb(sta[R]); 
                --R;
            }
            if(v == sta[R-1]) {
                G[sta[R-1]].pb(sta[R]); 
                --R;
            }
            else {
                G[v].pb(sta[R]); 
                sta[R] = v;
            }
        }
        sta[++R] = h[i];
    }
    re(i, R-1) {
        G[sta[i]].pb(sta[i+1]); 
    } 
    dfs(n+1);
    dfs_5(n+1, INF);
    re(i, len) used[h[i]] = 0;
    dfs_4(n+1);
}

int op = 1;

void getrt (int u, int fa) {
    siz[u] = 1; mxp[u] = 0;
    nx(i, u) {
        int v = e[i].to;
        if(vis[v]) continue;
        if(v == fa) continue;
        getrt(v, u);
        siz[u] += siz[v];
        mxp[u] = max(mxp[u], siz[v]);
    } 
    mxp[u] = max(mxp[u], sum - siz[u]); 
    if(mxp[u] < mxp[rt]) {
        rt = u;
    }   
}

void dfs_3 (int u, int fa) {
    siz[u] = 1;
    h[++len] = u + n;
    nx(i, u) {
        int v = e[i].to;
        if(v == fa) continue;
        if(vis[v]) continue;
        val[v] = val[u] + e[i].val;
        dfs_3(v, u);
        siz[u] += siz[v];
    }
}

void solve (int u) {
    len = 0;
    val[u] = 0;
    dfs_3(u, 0);
    re(i, len) val[h[i]] = val[h[i]-n]; 
    build();
}

void dfs_0 (int u) {
    solve(u);
    vis[u] = 1;
    op = 0;
    nx(i, u) {
        int v = e[i].to;
        if(vis[v]) continue;
        sum = siz[v];
        mxp[rt = 0] = n;
        getrt(v, u); 
        dfs_0(rt);
    }
}

int main () {
    io >> n; 
    re(i, n-1) {
        io >> u >> v >> w;
        add(u, v, w), add(v, u, w);
    } 
    re(i, n-1) {
        io >> u >> v >> w;
        u += n, v += n;
        add(u, v, w), add(v, u, w);
    }
    re(i, n) ans[i] = INF;
    dfs_1(n + 1, 0);
    dfs_2(n + 1, n + 1);
    mxp[rt = 0] = n; sum = n;
    getrt(1, 0);
    dfs_0(rt);
    re(i, n) io << ans[i] << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 960ms
memory: 77304kb

input:

250000
137466 116241 624409157
188961 163586 148491970
13958 122239 46409636
186120 44010 919858866
72320 102560 92679302
158544 236582 882613876
22331 114267 646741536
210782 42861 679450428
56693 45397 898958944
71188 114724 904191407
15203 225401 210626781
31071 144225 278121829
110354 83972 4557...

output:

41101981722
24783524958
17386222407
38045922430
25610233683
20965687585
28581128255
10929203519
9150749133
30625084420
25595872600
26129987591
27046353907
17070234497
23912017481
18635208366
22946884015
14181900087
24113819218
39573277018
25295838334
22248020256
14082699994
28220426146
33315844289
1...

result:

wrong answer 992nd lines differ - expected: '20275302551', found: '23865114629'