三天前开始做这题,一直没能AC..
这道题大概的意思就是给一个DAG,求生成的最小生成树是否唯一。思路很简单,生成一个次小生成树,比较权值就行了。
我的方法:暴搜!每次删掉一条最小生成树的边。
http://poj.org/problem?id=1679
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 25717 Accepted: 9178

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

令人蛋疼的是,三天一直没能AC,然后才知道自己的代码太过杂乱了..不好维护,改了下坏习惯,重新写了一个:)

AC代码如下:

Source Code

Problem: 1679 User: aclolicon
Memory: 780K Time: 79MS
Language: G++ Result: Accepted
    • Source Code

#include<algorithm>
#include<cstdio>
#include<iostream>
struct edge{int x,y,z;}e[105];
struct edge2{int x,y,z;}mx[105];
int g[105][105];
bool used[105];
int mc[105];
bool isf=0;
int t,m,n;
/*edge e[105];
edge m[105];*/
int mec=-1,eec=-1;
bool mi=1;
int ans,nans;

#define INF 0x3f3f3f3f
using namespace std;
int solve(){
int cnt=0;
int res=0;
while(1){
int v=-1;
for(int i=0;i<n;i++){
if (!used[i]&&(v==-1||mc[i]<mc[v])) v=i;
// cout<<"ohfuck"<<v<<','<<i<<endl;
}
if(v==-1||mc[v]==INF) break;
used[v]=1;
// cout<<v<<endl;
res+=mc[v];
cnt++;
if(mi){
eec++;
e[eec].x=mx[v].x;
e[eec].y=mx[v].y;
e[eec].z=mc[v];
// cout<<eec<<':'<<v<<'?'<<mx[v].x<<','<<mx[v].y<<endl;
}
for (int i=0;i<n;i++) {
if(!used[i]) {
if (mc[i]>g[v][i]){
mc[i]=g[v][i];
if(mi){
mx[i].x=v;
mx[i].y=i;
mx[i].z=INF;
}
}
}
// cout<<mc[i]<<','<<g[v][i]<<endl;
}
}
if (cnt!=n) isf=1;
return res;
}
void init(bool x){
for (int i=0;i<105;i++){
used[i]=0;
mc[i]=INF;
if (x){
for (int j=0;j<105;j++){
g[i][j]=INF;
}
}

}
mc[0]=0;
isf=0;
}
int main(){
// freopen("c.in","r",stdin);
cin>>t;
while(t--){
init(1);
eec=-1;
cin>>n>>m;
int x,y,z;
for (int i=0;i<m;i++){
cin>>x>>y>>z;
x--;
y--;
g[x][y]=g[y][x]=z;
}
//Create a Min Tree
mi=1;
ans=solve();
mi=0;
//delete!!!
// cout<<eec<<endl;
int lt=0;
for (int i=1;i<=eec;i++){
// cout<<"help!"<<i<<','<<eec<<endl;
init(0);
// cout<<e[i].x<<','<<e[i].y<<endl;
g[e[i].x][e[i].y]=INF;
nans=solve();
// cout<<nans<<','<<isf<<endl;
g[e[i].x][e[i].y]=e[i].z;
if (isf) continue;
lt++;
if (nans==ans){
cout<<"Not Unique!"<<endl;
break;
}
// cout<<"me"<<endl;
}
if(ans!=nans||lt==0) cout<<ans<<endl;
}
return 0;
}
于是继续修炼啦~