QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#602119#8941. Even or Odd Spanning Treeinksamurai#WA 102ms3640kbC++232.3kb2024-09-30 19:53:382024-09-30 19:53:40

Judging History

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

  • [2024-09-30 19:53:40]
  • 评测
  • 测评结果:WA
  • 用时:102ms
  • 内存:3640kb
  • [2024-09-30 19:53:38]
  • 提交

answer

#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int) a.size()
#define all(a) a.begin(),a.end()
#define vec(...) vector<__VA_ARGS__>
#define _3M8yqhy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

struct dsu{
	int _n;
	vi si,par,leb;
	dsu(int n=0){
		init(n);
	}
	void init(int n=0){
		_n=n;
		par=vi(_n,0);
		si=vi(_n,0);
		leb=vi(_n,-1);
		rep(i,n){
			si[i]=1;
			par[i]=i;
		}	
	}
	int parent(int u){return par[u]=(par[u]==u?u:parent(par[u]));}
	bool same(int u,int v){return parent(u)==parent(v);}
	void merge(int u,int v){
		u=parent(u),v=parent(v);
		if(!same(u,v)){
			if(si[u]<si[v]) swap(u,v);
			leb[u]=max(leb[u],leb[v]);
			_n-=1;
			si[u]+=si[v];
			par[v]=u;
		}
	}
	int size(int v=-1){return v==-1?_n:si[parent(v)];}
};

const int inf=1e18;

void slv(){
	int n,m;
	cin>>n>>m;

	vec(array<int,3>) es;
	rep(i,m){
		int u,v,w;
		cin>>u>>v>>w;
		u-=1,v-=1;
		es.pb({w,u,v});
	}
	sort(all(es));

	dsu uf(n);
	vi usd(m);
	int now=0;
	vec(vec(pii)) adj(n);
	rep(i,sz(es)){
		auto [w,u,v]=es[i];
		if(!uf.same(u,v)){
			usd[i]=1;
			uf.merge(u,v);
			adj[u].pb({w,v}),adj[v].pb({w,u});
			now+=w;
		}
	}

	if(uf.size()!=1){
		cout<<"-1 -1\n";
		return;
	}


	vi dep(n),par(n);
	vi f(n);
	auto dfs=[&](auto self,int v)->void{
		for(auto [w,u]:adj[v]){
			if(u==par[v]) continue;
			f[u]=w;
			par[u]=v;
			dep[u]=dep[v]+1;
			self(self,u);
		}
	};
	dfs(dfs,0);

	int now1=inf;
	rep(i,sz(es)){
		auto [w,u,v]=es[i];
		if(usd[i]) continue;
		int mi=inf;
		int x=u,y=v;
		while(x!=y){
			if(dep[x]>dep[y]){
				if(f[x]%2!=w%2){
					mi=min(mi,f[x]);
				}
				x=par[x];
			}else{
				if(f[y]%2!=w%2){
					mi=min(mi,f[y]);
				}
				y=par[y];
			}
		}
		if(mi!=inf){
			now1=min(now1,now-mi+w);
		}
	}

	if(now1%2==0) swap(now,now1);
	if(now1==inf) now1=-1;
	if(now==inf) now=-1;
	cout<<now<<" "<<now1<<"\n";
}

signed main(){
_3M8yqhy;
	int t;
	cin>>t;
	rep(cs,t){
		slv();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

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: 102ms
memory: 3640kb

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 1332462897
1263932600 1395151561
1189242112 1180186165
2248358640 2277656157
3776889482 3738936375
1093500444 1058677117
3444549292 3402127725
1258609116 1187873655
1395482806 1411327105
3456885934 3486092007
3943951826 3988876469
2479987500 2733874051
2909126794 317...

result:

wrong answer 4th numbers differ - expected: '1309454727', found: '1332462897'