| ||||||||||
| 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 | |||||||||
同样的问题我也必须问下了 -_-~!In Reply To:n那么小,直接冒泡:) Posted by:JiangLY at 2005-07-26 11:21:48 排序了,贪心构造,每找到一个邻居后都排一次序,还是WA
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <memory>
using namespace std;
const int size = 11;
void Sort (int tab[][2],const int &n)
{
bool end;
int temp;
end = true;
for (int i = 0;i < n && end;i++)
{
end = false;
for (int j = 0;j < n - i - 1;j++)
{
if (tab[j][0] < tab[j + 1][0])
{
temp = tab[j][0];
tab[j][0] = tab[j + 1][0];
tab[j + 1][0] = temp;
temp = tab[j][1];
tab[j][1] = tab[j + 1][1];
tab[j + 1][1] = temp;
end = true;
}
}
}
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen ("Frogs' Neighborhood.txt","r",stdin);
#endif
int test;
int tab[size][2];
int relation[size][size];
int n,m;
scanf ("%d",&test);
for (;test >= 1;test--)
{
scanf ("%d",&n);
for (int i = 0;i < n;i++)
{
tab[i][1] = i;
scanf ("%d",&tab[i][0]);
}
bool end;
Sort (tab,n);
end = true;
memset (relation,0,sizeof (relation));
while (end)
{
int temp;
if (tab[0][0] != 0)
{
temp = tab[0][1];
for (int j = 0;j < n;j++)
{
m = tab[j][1];
if (temp != m && tab[j][0] != 0 && relation[temp][m] == 0)
{
relation[temp][m] = 1;
relation[m][temp] = 1;
tab[0][0]--;
tab[j][0]--;
break;
}
}
if (j == n)
{
end = false;
}
else
{
Sort (tab,n);
}
}
else
{
break;
}
}
if (end)
{
cout << "YES" << endl;
for (i = 0;i < n;i++)
{
for (int j = 0;j < n;j++)
{
cout << relation[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "NO" << endl;
}
if (test != 1)
{
cout << endl;
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator