對。我就將它叫作最大生成樹。
做法是對圖的每乙個連通分量做最大生成樹。不在樹里的牆,就是要去掉的牆。
沒有環的圖的最大生成樹就是它自己,所以根本就不會有什麼「沒有環的圖做了最大生成樹之後就會額外少一條邊」這種無厘頭的問題。當然,這樣的圖,最小生成樹也是它自己。
這根本就不是乙個計算幾何的問題:座標是廢的;這題考的是拓撲的性質。國王在哪個位置也沒有差,因為無論他在**,他總是要到整個二維平面上的每乙個地方的,所以就算他在(1927,0817)也一樣。
我真蠢。
短板,短板 never ends。
是時候將並查集這樣的資料結構打包成類了。
#include
#include
#include
#include
#include
typedef
long
long ll;
struct edge;
edge g[200005]; int edgep;
int n,m;
int t;
bool cmp(edge a, edge b)
int root[100005];
void init()
int rootof(int x)
bool merge(int a, int b)
int main()
ll res = 0;
int cnt = 0;
for(int i=0;iint a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[edgep].from=a,g[edgep].to=b,g[edgep].cost=c;
edgep++;
res+=c;
}sort(g,g+m,cmp);
for(int i=0;iif(merge(g[i].from,g[i].to))
}printf("%d %lld\n",m-cnt,res);
}return
0;}
HDU 3367最大生成樹
不能直接跑最大生成樹,但是還可以存在乙個環,這樣一下就用krusal判環來寫了就是比普通的最大生成樹多了合併判斷。於網路 include include include using namespace std typedef struct nn node node edg 100005 int fa...
hdu 3367 最大生成樹 判環
不能最大生成樹 最大的一條邊,但是為什麼呢?if fx fy 如果兩棵樹上不是都有回環,那麼可以合併,fx掛在fy上,如果其中乙個有環,fy標記為有環 if fx fy circle x circle y 如果兩棵樹上都沒有回環,那麼x和y還能合併,環數變為1 include includeusin...
hdu 3367 偽森林,not 最大生成樹
感覺題意很扭曲。不是求最大生成樹!給乙個圖,求pseudoforest偽森林,要求每個連通分量最多可以有乙個環。求能構成的最大值。錯誤寫法,求出最大生成樹 最大的邊 正確寫法 在求最大生成樹的思路的基礎上每次判斷一下環的問題 6 7 0 1 9 0 2 6 1 2 8 3 4 5 4 5 5 3 5...