zoukankan      html  css  js  c++  java
  • HDU-ACM课堂作业 Degree Sequence of Graph G & Frogs' Neibroghood

    Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 94   Accepted Submission(s) : 41
    Problem Description
    Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep love and yearns for the boundless oceans. After graduation, he came to a coastal city and got a job in a marine transportation company. There, he held a position as a navigator in a freighter and began his new life.

    The cargo vessel, Wang Haiyang worked on, sails among 6 ports between which exist 9 routes. At the first sight of his navigation chart, the 6 ports and 9 routes on it reminded him of Graph Theory that he studied in class at university. In the way that Leonhard Euler solved The Seven Bridges of Knoigsberg, Wang Haiyang regarded the navigation chart as a graph of Graph Theory. He considered the 6 ports as 6 nodes and 9 routes as 9 edges of the graph. The graph is illustrated as below.



    According to Graph Theory, the number of edges related to a node is defined as Degree number of this node.

    Wang Haiyang looked at the graph and thought, If arranged, the Degree numbers of all nodes of graph G can form such a sequence: 4, 4, 3,3,2,2, which is called the degree sequence of the graph. Of course, the degree sequence of any simple graph (according to Graph Theory, a graph without any parallel edge or ring is a simple graph) is a non-negative integer sequence?

    Wang Haiyang is a thoughtful person and tends to think deeply over any scientific problem that grabs his interest. So as usual, he also gave this problem further thought, As we know, any a simple graph always corresponds with a non-negative integer sequence. But whether a non-negative integer sequence always corresponds with the degree sequence of a simple graph? That is, if given a non-negative integer sequence, are we sure that we can draw a simple graph according to it.?

    Let's put forward such a definition: provided that a non-negative integer sequence is the degree sequence of a graph without any parallel edge or ring, that is, a simple graph, the sequence is draw-possible, otherwise, non-draw-possible. Now the problem faced with Wang Haiyang is how to test whether a non-negative integer sequence is draw-possible or not. Since Wang Haiyang hasn't studied Algorithm Design course, it is difficult for him to solve such a problem. Can you help him?

     
    Input
    The first line of input contains an integer T, indicates the number of test cases. In each case, there are n+1 numbers; first is an integer n (n<1000), which indicates there are n integers in the sequence; then follow n integers, which indicate the numbers of the degree sequence.
     
    Output
    For each case, the answer should be "yes"or "no" indicating this case is "draw-possible" or "non-draw-possible"
     
    Sample Input
    2 6 4 4 3 3 2 2 4 2 1 1 1
     
    Sample Output
    yes no
     
    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include <iomanip>
    #include<math.h>
    using namespace std;
    
    bool cmp(int a, int b) {
        return a > b;
    }
    
    int main()
    {
        int t, n, a[1001];
        while (cin >> t) {
            while (t--)
            {
                memset(a, 0, sizeof(a));
                int sum = 0;
                cin >> n;
                for (int i = 0; i < n; i++)
                {
                    cin >> a[i];
                    sum += a[i];
                }
                //若不是偶数则非图
                if (sum % 2)
                    cout << "no" << endl;
                else
                {
                    int f = 0;
                    for (int k = 0; k < n; k++){
                        sort(a, a + n, cmp);
                        //全为0时结束
                        if (a[0] == 0)
                        {
                            f = 1;
                            break;
                        }
                        //对最大的度内的数减一,并除去最大的度
                        for (int i = 0; i < a[0]; i++)
                        {
                            a[i + 1]--;
                            //出现负数,则非图
                            if (a[i + 1] < 0)
                            {
                                f = 2;
                                break;
                            }
                        }
                        a[0] = 0;
                        if (f == 2)
                            break;
                    }
                    if (f == 1)
                        cout << "yes" << endl;
                    if (f == 2)
                        cout << "no" << endl;
                }
            }
        }
        return 0;
    }

     青蛙邻居的原理也是一样的,但是最后要输出一张青蛙邻居的示意图

    #include <iostream>
    #include <stack>
    #include <cstdlib>
    #include <cstring>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    
    struct node
    {
        int degree;
        int index;
    };
    
    #define N 25
    
    int cmp(node a, node b)
    {
        return a.degree>b.degree;
    }
    
    int edge[N][N];
    
    int main()
    {
        int t;
        while(cin>>t)
        {
            while(t--)
            {
                int n;
                memset(edge,0,sizeof(edge));
                node arr[1001];
                int dualSum=0;
                cin>>n;
                for(int i=0; i<n; i++)
                {
                    cin>>arr[i].degree;
                    arr[i].index=i;
                    dualSum+=arr[i].degree;
                }
                //先判断度的和是否为偶数
                if(dualSum%2==1)
                {
                    cout<<"no"<<endl;
                    continue;
                }
                //Heavel-Hakimi定理
                int flag=0;
    
                for(int i=0; i<n; i++)
                {
                    sort(arr, arr+n, cmp);
                    if(arr[0].degree==0)
                    {
                        flag=1;
                        break;
                    }
                    for(int j=0; j<arr[0].degree; j++)
                    {
                        arr[j+1].degree--;
                        int x=arr[0].index;
                        int y=arr[j+1].index;
                        if(arr[j+1].degree<0)
                        {
                            flag=2;
                            break;
                        }
                        edge[x][y]=edge[y][x]=1;
                    }
                    arr[0].degree=0;
                    if(flag==2)
                        break;
                }
    
                if(flag==1)
                {
                    cout<<"YES"<<endl;
                    for(int i=0; i<n; i++)
                    {
                        int j=0;
                        for(; j<n-1; j++)
                        {
                            cout<<edge[i][j]<<" ";
                        }
                        cout<<edge[i][j]<<endl;
                    }
                }
                else
                    cout<<"NO"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    循环控制Goto、Break、Continue
    linux多进/线程编程(6)——进程间通信之信号
    linux多进/线程编程(5)——进程间通信之mmap
    docker学习笔记(6)——docker场景问题汇总(centos7 由于内核版本低带来的一系列问题,docker彻底卸载,安装、启动日志报错分析)
    c/c++ 日常积累
    broken pipe 报错分析和解决办法
    c/c++ 常见字符串处理函数总结 strlen/sizeof strcpy/memcpy/strncpy strcat/strncat strcmp/strncmp sprintf/sscanf strtok/split/getline atoi/atof/atol
    docker学习笔记(5)——docker场景问题汇总(docker权限问题、docker文件目录、查看docker历史日志文件)
    linux多进/线程编程(4)——进程间通信之pipe和fifo
    linux多进/线程编程(3)——wait、waitpid函数和孤儿、僵尸进程
  • 原文地址:https://www.cnblogs.com/DaiShuSs/p/9772123.html
Copyright © 2011-2022 走看看