• poj 1659 Frogs' Neighborhood havel定理


    Frogs' Neighborhood
    Time Limit: 5000MS   Memory Limit: 10000K
    Total Submissions: 6048   Accepted: 2621   Special Judge

    Description

    未名湖附近共有N个大小湖泊L1L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N)。如果湖泊LiLj之间有水路相连,则青蛙FiFj互称为邻居。现在已知每只青蛙的邻居数目x1x2, ..., xn,请你给出每两个湖泊之间的相连关系。

    Input

    第一行是测试数据的组数T(0 ≤ T ≤ 20)。每组数据包括两行,第一行是整数N(2 < N < 10),第二行是N个整数,x1x2,..., xn(0 ≤ xi ≤ N)。

    Output

    对输入的每组测试数据,如果不存在可能的相连关系,输出"NO"。否则输出"YES",并用N×N的矩阵表示湖泊间的相邻关系,即如果湖泊i与湖泊j之间有水路相连,则第i行的第j个数字为1,否则为0。每两个数字之间输出一个空格。如果存在多种可能,只需给出一种符合条件的情形。相邻两组测试数据之间输出一个空行。

    Sample Input

    3
    7
    4 3 1 5 4 2 1 
    6
    4 3 1 4 2 0 
    6
    2 3 1 1 2 1 
    

    Sample Output

    YES
    0 1 0 1 1 0 1 
    1 0 0 1 1 0 0 
    0 0 0 1 0 0 0 
    1 1 1 0 1 1 0 
    1 1 0 1 0 1 0 
    0 0 0 1 1 0 0 
    1 0 0 0 0 0 0 
    
    NO
    
    YES
    0 1 0 0 1 0 
    1 0 0 1 1 0 
    0 0 0 0 0 1 
    0 1 0 0 0 0 
    1 1 0 0 0 0 
    0 0 1 0 0 0 
    ------------

    每次把顶点按度大小从大到小排序,取出度最大的点Vi,依次和度较大的那些顶点Vj连接,同时减去Vj的度。连接完之后就不再考虑Vi了,剩下的点再次排序然后找度最大的去连接……这样就可以构造出一个可行解。
    判断无解有两个地方,若某次选出的Vi的度比剩下的顶点还多,则无解;若某次Vj的度减成了负数,则无解。

    ------------

    /** head-file **/
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <list>
    #include <set>
    #include <map>
    #include <algorithm>
    
    /** define-for **/
    
    #define REP(i, n) for (int i=0;i<int(n);++i)
    #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
    #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
    #define REP_1(i, n) for (int i=1;i<=int(n);++i)
    #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
    #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
    #define REP_N(i, n) for (i=0;i<int(n);++i)
    #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
    #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
    #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
    #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
    #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
    
    /** define-useful **/
    
    #define clr(x,a) memset(x,a,sizeof(x))
    #define sz(x) int(x.size())
    #define see(x) cerr<<#x<<" "<<x<<endl
    #define se(x) cerr<<" "<<x
    #define pb push_back
    #define mp make_pair
    
    /** test **/
    
    #define Display(A, n, m) {                      
        REP(i, n){                                  
            REP(j, m) cout << A[i][j] << " ";       
            cout << endl;                           
        }                                           
    }
    
    #define Display_1(A, n, m) {                    
        REP_1(i, n){                                
            REP_1(j, m) cout << A[i][j] << " ";     
            cout << endl;                           
        }                                           
    }
    
    using namespace std;
    
    /** typedef **/
    
    typedef long long LL;
    
    /** Add - On **/
    
    const int direct4[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
    const int direct8[8][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
    const int direct3[6][3]={ {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };
    
    const int MOD = 1000000007;
    const int INF = 0x3f3f3f3f;
    const long long INFF = 1LL << 60;
    const double EPS = 1e-9;
    const double OO = 1e15;
    const double PI = acos(-1.0); //M_PI;
    
    struct node{
        int u;
        int v;
        friend bool operator<(const node& a,const node& b){
            return a.v<b.v;
        }
        friend bool operator>(const node& a,const node& b){
            return a.v>b.v;
        }
    }a[11];
    bool p[11][11];
    int n;
    
    bool solve()
    {
        REP_1(i,n)
        {
            sort(a+1,a+n+1,greater<node>());
            FOR_1(j,2,n)
            {
                if (a[1].v==0) break;
                if (a[j].v==0) return false;
                a[1].v--;
                a[j].v--;
                p[a[1].u][a[j].u]=1;
                p[a[j].u][a[1].u]=1;
            }
            if (a[1].v>0) return false;
        }
        return true;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            clr(p,0);
            scanf("%d",&n);
            REP_1(i,n){
                scanf("%d",&a[i].v);
                a[i].u=i;
            }
            if (solve())
            {
                puts("YES");
                REP_1(i,n)
                {
                    REP_1(j,n) printf("%d ",p[i][j]);
                    printf("
    ");
                }
            }
            else
            {
                puts("NO");
            }
            if (T) puts("");
        }
        return 0;
    }
    






  • 相关阅读:
    数据统计
    判断文件是否改变
    多列表统计之后的数组排序
    被弃用的php函数以及被那个代替
    curl数据采集
    MySQL教程
    使用多次join数据重复的问题
    ThinkPHP5高阶实战教程 --诠释为API开发而生
    SpringBoot使用thymeleaf模板
    SpringBoot与JPA
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681606.html
Copyright © 2020-2023  润新知