## Johnbug

It's too late to apologize, it's too late～

# ZOJ Problem Set - 3204 Connect them

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <cstdio>
using namespace std;
struct vertex
{
int val;
int u;
int v;
};
struct Mtree
{
int l;
int r;
}tree[200];
priority_queue<vertex> q;
int cmp(const void* a,const void* b)
{
if((*(Mtree*)a).l != (*(Mtree*)b).l)
return (*(Mtree*)a).l - (*(Mtree*)b).l;
else
return (*(Mtree*)a).r - (*(Mtree*)b).r;
}
bool operator< (const vertex &a,const vertex &b)
{
if(a.val != b.val){
if(a.val > b.val)return 1;
else return 0;}
else
{
if(a.u != b.u){
if(a.u > b.u) return 1;
else return 0;}
else{
if(a.v>b.v) return 1;
else return 0;
}
}
}
int id[101],sz[101],k;
bool cnet(int p,int q)
{
int i,j;
for(i = p;i != id[i];i = id[i]);
for(j = q;j != id[j];j = id[j]);
if(i == j) return 0;
if(sz[i] < sz[j])
{
id[i] = j;sz[j] += sz[i];
}
else
{
id[j] = i;sz[i] += sz[j];
}
return 1;
}
void kru()
{
while(!q.empty())
{
if(cnet(q.top().u,q.top().v) == 1)
{
tree[k].l = q.top().u;
tree[k].r = q.top().v;
k ++;
}
q.pop();
}
}
int main()
{
int i,w,cas,n,j;
vertex tmp;
scanf("%d",&cas);
while(cas --)
{
for(i = 0;i < 101;i ++) {id[i] = i;sz[i] = i;}
scanf("%d",&n);
for(i = 1;i <= n;i ++)
for(j = 1;j <= n;j ++)
{
scanf("%d",&w);
if(j > i&&w != 0){
tmp.val = w;
tmp.u = i;
tmp.v = j;
q.push(tmp);
}
}
k = 0;
kru();
qsort(tree,k,sizeof(tree[0]),cmp);
if(k != n - 1) printf("-1\n");
else{
printf("%d %d",tree[0].l,tree[0].r);
for(i = 1;i < k;i ++)
{
printf(" %d %d",tree[i].l,tree[i].r);
}
printf("\n");
}
while(!q.empty()) q.pop();
}
return 0;
}


#### ZOJ 3204 Connect them

2015-02-25 00:02:24

#### ZOJ 3204 Connect them 【最小生成树+输出选取的边】

2016-04-19 21:15:56

#### ZOJ - 3204 Connect them

2011-04-10 12:02:00

#### ZOJ——3204 Connect them

2014-08-05 10:45:17

#### HDU1518 & POJ2362 & ZOJ1909 Square(DFS,剪枝是关键呀)

2016-03-23 21:34:50

#### ZOJ Problem Set - 1002（DFS）

2016-02-26 23:39:40

#### ZOJ-3953-Intervals【贪心】【17th浙大校赛】

2017-04-10 12:22:17

#### ZOJ Problem Set - 3938 (模拟)

2016-04-23 23:35:30

#### ZOJ2334 Monkey King 【左偏树模板题】

2017-04-19 16:50:00

#### ZOJ Problem Set 1002: Fire Net

2010-07-31 00:34:00