您的位置:首页 > 其它

poj 3687 Labeling Balls 【拓扑排序】

2015-08-25 20:49 246 查看
题目链接:http://poj.org/problem?id=3687

注意重边

代码:

[code]#include <stdio.h>  
#include <ctime>  
#include <math.h>  
#include <limits.h>  
#include <complex>  
#include <string>  
#include <functional>  
#include <iterator>  
#include <algorithm>  
#include <vector>  
#include <stack>  
#include <queue>  
#include <set>  
#include <map>  
#include <list>  
#include <bitset>  
#include <sstream>  
#include <iomanip>  
#include <fstream>  
#include <iostream>  
#include <ctime>  
#include <cmath>  
#include <cstring>  
#include <cstdio>  
#include <time.h>  
#include <ctype.h>  
#include <string.h>  
#include <assert.h>  

using namespace std;

int in[210];
int is[210][210];
int ans[210];
int n, m;

int main()
{
    int t;
    int n, m;
    scanf("%d", &t);
    while (t--)
    {
        memset(in, 0, sizeof(in));
        memset(is, 0, sizeof(is));
        memset(ans, 0, sizeof(ans));

        scanf("%d%d", &n, &m);
        int ok = 1;
        int a, b;
        while (m--)
        {
            scanf("%d%d", &a, &b);
            if (a == b) ok = 0;
            if (is[a][b] == 1) ok = 0;
            if (is[b][a] == 0) in[a]++;
            is[b][a] = 1;
        }
        int num = 1;
        int cnt = 0;
        if (!ok) puts("-1");
        else
        {
            for (int k = n;k >= 1 ;k--)
            {
                int i = n;
                while (in[i] != 0 && i>=1) i--;
                in[i] = -1;ans[i] = k;

                if (i >= 1) cnt++;

                for (int j = 1;j <= n;j++)
                {
                    if (is[i][j])
                        in[j]--;
                }
            }
            if (cnt != n) puts("-1");
            else
            {
                for (int i = 1;i < n;i++)
                    printf("%d ", ans[i]);
                printf("%d\n", ans
);
            }
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: