QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#602111#8941. Even or Odd Spanning Treeracxa#WA 290ms9748kbC++173.8kb2024-09-30 19:46:002024-09-30 19:46:01

Judging History

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

  • [2024-09-30 19:46:01]
  • 评测
  • 测评结果:WA
  • 用时:290ms
  • 内存:9748kb
  • [2024-09-30 19:46:00]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back

using namespace std;

const int nmax = 200001;

int p[nmax][22];
ll max_even[nmax][22];
ll max_odd[nmax][22];
int in[nmax], out[nmax];
int d[nmax];
int timer =0;
void dfs(int v, int e, vector <vector<pii>>&g) {
    in[v] = timer++;
    p[v][0] = e;
    for(int j = 1; j < 22; j++) {
        p[v][j] = p[p[v][j - 1]][j -1];
        int x = p[v][j - 1];
        max_even[v][j] = max(max_even[v][j - 1], max_even[x][j - 1]);
        max_odd[v][j] = max(max_odd[v][j - 1], max_odd[x][j - 1]);
    }
    for(auto [to, len] : g[v]) {
        if(to == e) continue;
        d[to] = d[v] + 1;
        if(len % 2 == 0)
            max_even[to][0] = len;
        else max_odd[to][0] = len;
        dfs(to, v, g);
    }
    out[v] = timer++;
}

bool is_anc(int u, int v) {
    return in[u] <= in[v] && out[u] >= out[v];
}

int lca(int u, int v) {
    if(is_anc(u, v)) return u;
    if(is_anc(v, u)) return v;
    for(int j = 21; j >= 0; j--) {
        if(!is_anc(p[v][j - 1], u)) v =p[v][j - 1];
    }
    return p[v][0];
}

ll get_max_even(int v, int d) {
    ll ans = -1e18;
    while(d > 0) {
        int x = 31 - __builtin_clz(d);
        ans = max(ans, max_even[v][x]);
        v = p[v][x];
        d -= (1 << x);
    }
    return ans;
}

ll get_max_odd(int v, int d) {
    ll ans = -1e18;
    while(d > 0) {
        int x = 31 - __builtin_clz(d);
        ans = max(ans, max_odd[v][x]);
        v = p[v][x];
        d -= (1 << x);
    }
    return ans;
}

class DSU {
public :
    void union_sets(int u, int v) {
        u = find(u); v = find(v);
		if(u == v) {
			return;
		}
		if(sz[u] < sz[v]) swap(u, v);
        p[v] = u;
        sz[u] += sz[v];
    }
    int find(int x) {
     return p[x] == x ? x : p[x] = find(p[x]);
    }
    DSU(int n) {
        p.resize(n + 1);
        for(int i = 1; i <= n; i++) p[i] = i;
        sz.resize(n + 1, 1);
    }

    vector <int> p, sz;
};

void reset(int n) {
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < 22; j++) {
            max_even[i][j] = -1e18;
            max_odd[i][j] = -1e18;
            p[i][j] = 0;
            in[i] = out[i] = 0;
            d[i] = 0;
        }
    }
    timer = 0;
}

void solve() {
    int n, m; cin >> n >> m;
    reset(n);
    vector <pair<int, pii>>edges;
    for(int i= 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        int l; cin >> l;
        edges.pb({l, {u, v}});
    }
    sort(edges.begin(), edges.end());
    DSU ds(n);
    vector <vector <pii>>g(n + 1);
    ll tot = 0;
    for(auto x : edges) {
        ll len = x.f;
        int u = x.s.f, v = x.s.s;
        if(ds.find(u) != ds.find(v)) {
            g[u].pb({v, len});
            g[v].pb({u, len});
            ds.union_sets(u, v);
            tot += len;
        }
    }
    if(ds.sz[ds.find(1)] != n) {
        cout << -1 << ' ' << -1 << '\n';
        return;
    }
    dfs(1, 1, g);
    ll ans = 1e18;
    for(auto x : edges) {
        ll len = x.f;
        int u = x.s.f;
        int v = x.s.s;
        int l = lca(u, v);
        ll even = get_max_even(u, d[u] - d[l]);
        ll odd = get_max_odd(v, d[v] - d[l]);
        if(len % 2 == 0) {
            ans = min(ans, len - odd);
        }
        else ans = min(ans, len - even);
    }
    ll res[2];
    int x = tot % 2;
    
    res[x] = tot, res[x ^ 1] = tot + ans;
    for(int i = 0; i < 2; i++){
        if(res[i] > (ll)1e18) res[i] = -1;
        cout << res[i] << ' ';
    }
    cout << '\n';
}

int main() {
    int test; cin>> test;

    while(test--) {
    solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 9748kb

input:

3
2 1
1 2 5
3 1
1 3 1
4 4
1 2 1
1 3 1
1 4 1
2 4 2

output:

-1 5 
-1 -1
4 3 

result:

ok 6 numbers

Test #2:

score: -100
Wrong Answer
time: 290ms
memory: 9668kb

input:

10000
16 50
1 2 649251632
2 3 605963017
3 4 897721528
4 5 82446151
5 6 917109168
6 7 79647183
7 8 278566178
7 9 573504723
3 10 679859251
8 11 563113083
1 12 843328546
10 13 594049160
11 14 997882839
7 15 569431750
2 16 244494799
16 7 960172373
13 4 317888322
13 3 446883598
9 3 678142319
5 8 43208692...

output:

3140067932 3249730195 
1262790434 1319989017 
1263932600 1307926149 
1189652956 1180186165 
2248358640 2261370885 
3799472702 3738936375 
1093500444 1058677117 
3574675236 3402127725 
1258609116 1187873655 
1395482806 1440596255 
3456885934 3486092007 
3943951826 4005009799 
2479987500 2535532661 
2...

result:

wrong answer 2nd numbers differ - expected: '3159441841', found: '3249730195'