题目
题解

Solution

知道了第x~y个杯子的奇偶性,就相当于知道了x和x-1之间的缝到y和y+1之间的缝的奇偶性
知道了缝a到缝b的奇偶性和缝b到缝c的奇偶性,我们就知道了缝a到缝c的奇偶性
要知道所有杯子底下有没有球,我们就要知道每个杯子左右两端的缝之间的奇偶性,也就相当于要知道任意两个缝之间的奇偶性
所以这就是一道花式最小生成树问题-_-知道奇偶性相当于连一条边,整个图联通了就都能知道了

Code

#include<bits/stdc++.h>
using namespace std;
const int N=2001003;
struct node{
	int x,y,w;
}e[N];
long long ans;
int fa[N],i,j,n,rx,ry,m,cnt;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
inline bool cmp(node a,node b){return a.w<b.w;}
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int main(){
	n=rd();
	for (i=0;i<n;i++)
		for (j=i+1;j<=n;j++) e[++m]=(node){i,j,rd()};
	sort(e+1,e+m+1,cmp);
	for (i=1;i<=n;i++) fa[i]=i;
	for (i=1;i<=m;i++){
		rx=find(e[i].x);ry=find(e[i].y);
		if (rx!=ry){
			cnt++;ans+=e[i].w;
			if (cnt==n) break;
			fa[rx]=ry;
		}
	}
	printf("%lld",ans);
}