24h購物| | PChome| 登入
2012-09-02 12:54:04| 人氣795| 回應0 | 上一篇 | 下一篇

[UVA] 626 - Ecosystem

推薦 0 收藏 0 轉貼0 訂閱站台

  Ecosystem 

There are various food chains in any ecosystem, hawk, mouse and corn to name one. Mice feed on corn, hawks eat mice and life goes on. Given the relations between the species one can easily create food chains.

One can also determine whether a chain is a cyclic one with caterpillars, plants, fungi and bacteria, as an example of cyclic chain. Caterpillars feed on plants, plants use organic substances produced by bacteria and fungi by decomposition of dead bodies of organisms such as caterpillars. These constitute a 3-member cyclic food chain.


Your task is to find 3-member cyclic food chains.

Input 

Unlimited number of tests. Each test consists of: number of observer species (n), consecutive n lines containing coincidence matrix describing food relations between species. There is 0 in the (i,k)-th element when i-th species does not eat k-th species and respectively 1 when it does so.

Output 

Outputted cyclic chains, followed by word ``total:" and then number of cyclic chains. The chain consists of three species separated by spaces, for example 1 2 4 means that 1 eats 2, which eats 4, and 1 is a food for 4. With such a rule the chain 1 2 4 is identical with 4 1 2 and 2 4 1 but not with 4 2 1 (inverted sequence of prey and predator).

You should not print all identical chain (only that one with species sorted with ascend or descend order). Chains should be sorted. This means that the chains 1 2 4, 1 2 3 and 3 2 1 should be printed in the following order:

1 2 3
1 2 4
3 2 1

Output a blank line after each test case.

Assumptions: number of species in one test belongs to a range [3, 100]

Sample Input 

3
0 1 0
0 0 1
1 0 0
3
0 0 1
1 0 0
0 1 0
3
0 1 1
1 0 1
1 1 0
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0

Sample Output 

1 2 3
total:1

3 2 1
total:1

1 2 3
3 2 1
total:2

1 2 3
1 2 4
1 3 4
2 3 4
3 2 1
4 2 1
4 3 1
4 3 2
total:8


#include <stdio.h>
#include <vector>
using namespace std;
int main() {
    int n, g[100][100];
    int i, j, k;
    vector<int> gv[100];
    while(scanf("%d", &n) == 1) {
        for(i = 0; i < n; i++) {
            gv[i].clear();
            for(j = 0; j < n; j++) {
                scanf("%d", &g[i][j]);
                if(g[i][j])
                    gv[i].push_back(j);
            }
        }
        int time = 0;
        for(i = 0; i < n; i++) {
            for(j = 0; j < gv[i].size(); j++) {
                for(k = 0; k < gv[gv[i][j]].size(); k++) {
                    if(gv[gv[i][j]][k] != i && g[gv[gv[i][j]][k]][i] == 1) {
                        if((i+1 < gv[i][j]+1 && gv[i][j]+1 < gv[gv[i][j]][k]+1) ||
                            (i+1 > gv[i][j]+1 && gv[i][j]+1 > gv[gv[i][j]][k]+1) )
                            printf("%d %d %d\n", i+1, gv[i][j]+1, gv[gv[i][j]][k]+1), time++;
                    }
                }
            }
        }
        printf("total:%d\n", time);
        puts("");
    }
    return 0;
}

 

台長: Morris
人氣(795) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10677 - Base Equality
此分類上一篇:[UVA] 927 - Integer Sequences from Addition of Terms

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文