QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#558426#8941. Even or Odd Spanning Treeleafmaple#WA 109ms7680kbC++202.5kb2024-09-11 16:04:032024-09-11 16:04:04

Judging History

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

  • [2024-09-11 16:04:04]
  • 评测
  • 测评结果:WA
  • 用时:109ms
  • 内存:7680kb
  • [2024-09-11 16:04:03]
  • 提交

answer

#include <bits/stdc++.h>
#define xs(a) cout<<setiosflags(ios::fixed)<<setprecision(a);
#define endl '\n'
using namespace std;
using ll = long long;
const int N=3e5+5;
#define int long long

vector<array<int,2>>g[N];

int fa[N];
int find(int x){
    if(x == fa[x]) return x;
    return fa[x] = find(fa[x]); 
}
int st[N][25][2];
int f[N][25], dep[N];
void dfs(int u, int par){
    dep[u] = dep[par] + 1;
    for(int i=1; i<=20; i++){
        f[u][i] = f[f[u][i-1]][i-1];
        for(int k=0; k<2; k++){
            st[u][i][k] = max(st[u][i-1][k], st[f[u][i-1]][i-1][k]);
        }
    }
    for(auto [v, w]: g[u]) if(v != par){
        f[v][0] = u;
        st[v][0][w%2] = w; 
        dfs(v, u);
    }
}

int lca(int u, int v, int k){
    if(dep[u] < dep[v])swap(u, v);
    int ans = -1;
    for(int i=20; i>=0; i--){
        if(dep[f[u][i]] >= dep[v]){
            ans = max(ans, st[u][i][k]);
            u = f[u][i];
        }
    }
    if(u == v)return ans;
    for(int i=20; i>=0; i--){
        if(f[u][i] == f[v][i]) continue;
        ans = max({ans, st[u][i][k], st[v][i][k]});
        u = f[u][i], v = f[v][i];
    }
    ans = max({ans, st[u][0][k], st[v][0][k]});
    
    return ans;
}

void solve(){
    int n, m; cin >> n >> m;
    for(int i=1; i<=n; i++) {
        g[i].clear();
        fa[i] = i;
        for(int j=0; j<=20; j++) st[i][j][0] = st[i][j][1] = 0;
    }
    vector<array<int,3>>edg;
    int ans = 0;
    for(int i=1; i<=m; i++){
        int u, v, w; cin >> u >> v >> w;
        edg.push_back({w, u, v});
    }
    sort(edg.begin(), edg.end());
    vector<int>vis(m+5);
    int fk = 0;
    for(int i=0; i<edg.size(); i++){
        auto [w, u, v] = edg[i];
        int fu = find(u), fv = find(v);
        if(fu == fv)continue;
        ans += w;
        fk++;
        vis[i] = 1;
        fa[fu] = fv;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    if(fk != n-1){
        cout << "-1 -1"<<endl;
        return ;
    }
    dfs(1, 0);
    int res = 1e18;
    for(int i=0; i<edg.size(); i++)if(!vis[i]){
        auto [w, u, v] = edg[i];
        int x = lca(u, v, 1 - (w&1));
        if(x == -1)continue;
        res = min(res, ans - x + w);
    }
    if(res == 1e18)res = -1;
    if(ans%2) cout << res << ' ' << ans << endl;
    else cout << ans << ' ' << res << endl;

}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
    int T; cin >> T;
    while(T--)solve();


	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 109ms
memory: 7680kb

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 3159441841
1262790434 1309454727
1263932600 1307926149
1189242112 1180186165
2248358640 2261370885
3776889482 3738936375
1093500444 1058677117
3433711014 3402127725
1201099898 1187873655
1395482806 1410162043
3456885934 3486092007
3943951826 3988876469
2479987500 2535532661
2909126794 293...

result:

wrong answer 116th numbers differ - expected: '1207927187', found: '1120243322'