# 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 Problem Set - 3204 Connect them 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)