POJ - 3687 Labeling Balls (topology)

(Click here to view the original title)

The meaning of problems

Where there are n lights, numbered 1 ~ n, the brightness of each lamp is unique, and in the range between 1 ~ n, m now known relationship between the lamp: ab, a description of the lamp brightness b smaller than the light, a luminance of each lamp is required lexicographically smallest (small number of lamp brightness as small as possible), so as to satisfy the relationship of m, and if not, outputs -1

Problem-solving ideas

Relationship between every pair of lamp: ab, a description of the lamp is smaller than the brightness b lamp, but also shows a completion time earlier than b (AOV Network concept), this relationship can be well treated with a topological sort, since the this topic requires a minimum order dictionary, for which the queue priority queue changed, so that a large number of light-first-out team and give them a greater topological order, so that we can minimize the lexicographical order, we will finally all points given topological order, and then the relationship m-one comparison, determines whether to meet the requirements, the resulting topological order to meet the output, otherwise, outputs -1

Code area

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip>

#define bug cout << "**********" << endl
#define show(x, y) cout<<"["<<x<<","<<y<<"] "
#define LOCAL = 1;
using namespace std;
typedef long long ll;
const ll inf = 1e18 + 10 ;
 const LL + 1E9 = MOD . 7 ;
 const  int Max = 1e6 + 10 ;
 const  int Max2 = 3e2 + 10 ; 

int n-, m; 
Vector < int > Edge [Max2];         // adjacency list built FIG 
pair < int , int > Way [Max];     // store the relationship m 
Map <pair < int , int >, BOOL > MP;     // for determining multiple edges 
int in_d [Max2];                 // record the degree 
int id[Max2], now;             //记录拓扑序

void init()
{
    for (int i = 0; i < Max2; i++)
        edge[i].clear();
    memset(id, 0, sizeof(id));
    now = n;
    memset(in_d,0,sizeof(in_d));
    mp.clear();
}

void topoSort()
{
    priority_queue<int> q;
    for (int i = n; i >= 1; i--)
    {
        if (in_d[i] == 0)
            q.push(i);
    }
    while (!q.empty())
    {
        int u = q.top();
        q.pop();
        id[u] = now--;
        for(int i = 0 ;i < (int)edge[u].size() ;i ++)
        {
            int v = edge[u][i];
            if(id[v]) continue;
            in_d[v]--;
            if(in_d[v] == 0)
            {
                q.push(v);
            }
        }
    }
}

int main()
{
#ifdef LOCAL
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        init();
        bool ok = true;
        for (int i = 1, u, v; i <= m; i++)
        {
            scanf("%d%d", &u, &v);
            if(u == v)
            {
                ok = false;
                continue;
            }
            if(!mp[make_pair(u,v)])
            {
                edge[v].push_back(u);
                in_d[u]++;
            }
            way[i] = make_pair(u, v);
            mp[way[i]] = true;
        }
        topoSort();

        for (int i = n; i >= 1; i--)
        {
            if (id[i] == 0)
            {
                id[i] = now--;
            }
        }
        for (int i = 1; i <= m; i++)
        {
            if (id[way[i].first] > id[way[i].second])
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            printf("-1\n");
        }
        else
        {
            for (int i = 1; i <= n; i++)
            {
                printf("%d%c", id[i], i != n ? ' ' : '\n');
            }
        }
    }
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/winter-bamboo/p/11419577.html