zoukankan      html  css  js  c++  java
  • 第六讲 图(上)

    06-图1:列出连通集.
    Description:

    给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N-1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。

    Input:

    输入第1行给出2个整数N(0, 10)和E,分别是图的顶点数和边数。随后E行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。

    Output:

    按照"{v1, v2..., vk}"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。

    SampleInput:

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

    SampleOutput:

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

    Codes:
    //#define LOCAL
    
    #include <cstdio>
    #include <queue>
    using namespace std;
    
    #define M 10
    int ne, nv, check[M], g[M][M];
    
    void bG() {
        int i, v1, v2;
        scanf("%d%d", &nv, &ne);
        for(i=0; i<ne; ++i) {
            scanf("%d%d", &v1, &v2);
            g[v1][v2] = g[v2][v1] = 1;
        }
    }
    
    int cV() {
        int i;
        for(i=0; i<nv; ++i) 
            if(!check[i]) break;
        if(i == nv) return -1;
        return i;
    }
    
    void cC() { for(int i=0; i<nv; ++i) check[i] = 0; }
    
    int BFS() {
        int i, j; queue<int> q;
        i = cV();
        if(i == -1) return i; 
        q.push(i); check[i] = 1;
        printf("{ %d ", i);
        while(!q.empty()) {
            int t = q.front(); q.pop();
            for(j=0; j<nv; ++j) 
                if(g[t][j]==1 && !check[j]) {
                    check[j] = 1; printf("%d ", j);
                    q.push(j);
                }
        }
        printf("}
    "); return BFS();
    }
    
    void DFS(int vi) {
        check[vi] = 1;
        printf("%d ", vi);
        for(int j=0; j<nv; ++j)
            if(g[vi][j]==1 && !check[j]) DFS(j);
    }
    
    int listDFS() {
        if(cV() == -1) return -1; printf("{ ");
        DFS(cV()); printf("}
    ");
        return listDFS();
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("E:\Temp\input.txt", "r", stdin);
            freopen("E:\Temp\output.txt", "w", stdout);
        #endif
    
        bG(); listDFS(); cC(); BFS();
    
        return 0;
    }
    
    06-图2:Saving James Bond - Easy Version.
    Description:

    This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).

    Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether or not he can escape.

    Input:

    Each input file contains one test case. Each case starts with a line containing two positive integers N(<=100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x, y)(x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.

    Output:

    For each test case, print in a line "Yes" if James can escape, or "No" if not.

    SampleInput1:

    14 20
    25 -15
    -25 28
    8 49
    29 15
    -35 -2
    5 28
    27 -29
    -8 -28
    -20 -35
    -25 -20
    -13 29
    -30 15
    -35 40
    12 12

    SampleOutput1:

    Yes

    SampleInput2:

    4 13
    -12 12
    12 12
    -12 -12
    12 -12

    SampleOutput2:

    No

    Codes:
    //#define LOCAL
    
    #include <cstdio>
    #include <cmath>
    
    struct C { double x, y; };
    
    int rC(double d, C p) { return (15+d)*(15+d)>=p.x*p.x+p.y*p.y; }
    int rBe(double d, C p1, C p2) { return d*d>=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y); }
    int rBa(double d, C p) {return p.x<=-50+d||p.x>=50-d||p.y>=50-d||p.y<=d-50;}
    int DFS(double d, C *cr, int v, int *vi, int n) {
        if(rBa(d, cr[v])) return 1;
        for(int i=0; i<n; ++i) 
            if(!vi[i] && rBe(d, cr[v], cr[i])) {
                vi[i] = 1;
                if(DFS(d, cr, i, vi, n)) return 1;
        }
        return 0;
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("E:\Temp\input.txt", "r", stdin);
            freopen("E:\Temp\output.txt", "w", stdout);
        #endif
    
        int i, n, f = 0, vi[100] = {}; double d;
        scanf("%d%lf", &n, &d);
        if(d >= 35) { printf("Yes
    "); return 0; }
        C cr[102];
        for(i=0; i<n; ++i) scanf("%lf%lf", &cr[i].x, &cr[i].y);
    
        for(i=0; i<n; ++i) 
            if(!vi[i] && rC(d, cr[i])) {
                vi[i] = 1;
                if(DFS(d, cr, i, vi, n)) { printf("Yes
    "); f = 1; break; }
            }
        if(!f) printf("No
    ");
    
        return 0;
    }
    
    06-图3:六度空间.
    Description:

    “六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。

    假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。

    Input:

    输入第1行给出两个正整数,分别表示社交网络图的结点数N(1<N<=1000,表示人数)、边数M(<=33×N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。

    Output:

    对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。

    SampleInput:

    10 9
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    7 8
    8 9
    9 10

    SampleOutput:

    1: 70.00%
    2: 80.00%
    3: 90.00%
    4: 100.00%
    5: 100.00%
    6: 100.00%
    7: 100.00%
    8: 90.00%
    9: 80.00%
    10: 70.00%

    Codes:
    //#define LOCAL
    
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <vector>
    using namespace std;
    
    #define M 100010
    int m, n, vi[M];
    vector<int> v[M];
    
    int sds(int t) {
        queue<int> q; q.push(t); vi[t] = 1;
        int i, cN = 0, lN = 1, l = 0, cnt = 1;
        while(!q.empty()) {
            t = q.front(); q.pop(); --lN;
            for(i=0; i<v[t].size(); ++i) {
                if(!vi[v[t][i]]) {
                    vi[v[t][i]] = 1; 
                    q.push(v[t][i]); ++cN;
                }
            }
            if(!lN) {
                lN = cN; cN = 0;
                cnt += lN; ++l;
            }
            if(l == 6) break;
        }
        return cnt;
    }
    
    
    int main() {
        #ifdef LOCAL
            freopen("E:\Temp\input.txt", "r", stdin);
            freopen("E:\Temp\output.txt", "w", stdout);
        #endif
    
        int i, f, t, cnt;
        scanf("%d%d", &n, &m);
        while(m--) {
            scanf("%d%d", &f, &t);
            v[f].push_back(t); v[t].push_back(f);
        }
    
        for(i=1; i<=n; ++i) {
            cnt = sds(i);
            memset(vi, 0, sizeof(vi));
            printf("%d: %.2f%%
    ", i, cnt*100.0/n);
        } 
    
        return 0;
    }
  • 相关阅读:
    CodeGen用户定义的扩展令牌
    CodeGen编写自定义表达式标记
    CodeGen CreateFile实用程序
    CodeGen融合核心关系循环扩展
    CodeGen融合核心扩展定制文件
    CodeGen API分析
    CodeGen字段循环Field Loop
    CodeGen概述
    算子扫描与递归核
    算子本质与数学函数
  • 原文地址:https://www.cnblogs.com/VincentValentine/p/6853832.html
Copyright © 2011-2022 走看看