Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

reply qq_28476387

Posted by zhishicece at 2015-05-25 19:29:37 on Problem 3687 and last updated at 2015-05-25 19:33:16
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
vector<int> e[210];//
int n,m,in[210],mp[210][210];
int L[210],l;//
void topsort()
{
    priority_queue<int> q;
    l=n;//
    for(int i=1;i<=n;i++)
    {
        if(in[i]==0)
            q.push(i);
    }
    while(!q.empty())
    {
        int x=q.top();
        q.pop();
        L[x]=l;//
        l--;//
        for(int i=0;i<e[x].size();i++)
        {
            int v=e[x][i];
            if(--in[v]==0)
                q.push(v);
        }
    }
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        memset(in,0,sizeof(in));
        memset(mp,0,sizeof(mp));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            e[i].clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(!mp[y][x])
            {
                e[y].push_back(x);
                in[x]++;
          	    mp[y][x]=1;
			}
        }
        topsort();
        if(l!=0)
            printf("-1\n");
        else
        {
            for(int i=1;i<=n;++i)
                printf("%d ",L[i]);
            printf("\n");
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator