| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
本题数据太弱之前,我直接用kruskal判断,加边过程中,如果同一个集合中出现过相同边,没有判断相同的边是否在同在一个环上,就输出Not Unique,竟然A了。。。
比如
3 2
1 2 1
2 3 1
我输出了 Not Unique!竟也AC了。。。
错误代码如下:
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
class UnionFindSet
{
public:
UnionFindSet(int n):father(n,-1),num(n,1){}
int Union(int a,int b)
{
int u1=Find(a),u2=Find(b);
if(num[u1]>num[u2])
{
num[u1]+=num[u2];
return father[u2]=u1;
}
else
{
num[u2]+=num[u1];
return father[u1]=u2;
}
}
int Find(int a)
{
return father[a]==-1?a:(father[a]=Find(father[a]));
}
void assign(int n)
{
father.assign(n,-1);
num.assign(n,1);
}
private:
std::vector<int> father;
std::vector<int> num;
};
struct Edge
{
Edge(int a,int b,int v):a(a),b(b),v(v){}
int a,b,v;
};
bool operator<(const struct Edge& e1,const struct Edge&e2)
{
return e1.v<e2.v;
}
const int MAX=10010;
bool chooce[MAX];
int main()
{
vector<Edge> edges;
UnionFindSet u(1);
int n,v,e,a,b,l;
cin>>n;
while(n--)
{
cin>>v>>e;
u.assign(v+1);
for(int i=0;i!=e;++i)
{
cin>>a>>b>>l;
edges.push_back(Edge(a,b,l));
}
sort(edges.begin(),edges.end());
bool findone=false;
int lastfindi;
try{
for(int i=0;i!=edges.size();++i)
{
if(findone && edges[i].v!=edges[i-1].v)
{
findone=false;
u.Union(edges[lastfindi].a,edges[lastfindi].b);
chooce[lastfindi]=true;
}
if(u.Find(edges[i].a)!=u.Find(edges[i].b))
{
if(findone) throw true;
findone=true;
lastfindi=i;
}
}
int sum=0;
for(int i=0;i!=e;i++)
if(chooce[i]) sum+=edges[i].v;
cout<<sum<<endl;
}
catch(...)
{
cout<<"Not Unique!"<<endl;
}
memset(chooce,0,sizeof(chooce));
edges.clear();
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator