[PA2014]Kuglarz
[PA2014]Kuglarz
题目大意:
有一个长度为\(n(n\le2000)\)的0/1
串,你可以花\(c_{i,j}\)的钱,询问区间\([i,j]\)的异或和。问至少要多少元才能知道原来的序列。
思路:
最小生成树。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=2001,M=2001000;
struct Edge {
int u,v,w;
bool operator < (const Edge &rhs) const {
return w<rhs.w;
}
};
Edge edge[M];
class DisjointSet {
private:
int anc[N];
int find(const int &x) {
return x==anc[x]?x:anc[x]=find(anc[x]);
}
public:
void reset(const int &n) {
for(register int i=0;i<=n;i++) anc[i]=i;
}
void merge(const int &x,const int &y) {
anc[find(x)]=find(y);
}
bool same(const int &x,const int &y) {
return find(x)==find(y);
}
};
DisjointSet s;
int main() {
const int n=getint();
int m=0;
for(register int i=0;i<n;i++) {
for(register int j=i+1;j<=n;j++) {
edge[m++]=(Edge){i,j,getint()};
}
}
std::sort(&edge[0],&edge[m]);
s.reset(n);
int64 ans=0;
for(register int i=0;i<m;i++) {
const int &u=edge[i].u,&v=edge[i].v;
if(s.same(u,v)) continue;
s.merge(u,v);
ans+=edge[i].w;
}
printf("%lld\n",ans);
return 0;
}