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

【贴个代码】感觉基本没有与我用一个方法的,写的方法好奇葩的说~~

Posted by WilliamACM at 2013-07-24 22:05:17 on Problem 3687
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N=201;
int box[N],kk;
struct node
{
    int t,next;
}e[N*N];
int n,m,in[N],base;
void add(int s,int t)
{
    in[t]++;
    e[kk].t=t,e[kk].next=box[s];
    box[s]=kk++;
}
int lab[N];
set<int>ar[N];
void dfs_up(int r,int x)
{
    if(in[r])return;
    if(ar[x].find(r)!=ar[x].end()) return;
    if(r>x)ar[x].insert(r);
    for(int i=box[r];~i;i=e[i].next)
    {
        dfs_up(e[i].t,x);
    }
}
void dfs(int r)
{
    dfs_up(r,r);
//    sort(ar[r].begin(),ar[r].end());
    set<int>::iterator p;
    for(p=ar[r].begin();p!=ar[r].end();p++)
    if(!in[*p])
    {
        dfs(*p);
    }
    ar[r].clear();
    lab[r]=++base;
    in[r]=1;
}
queue<int>q;
void init()
{
    scanf("%d%d",&n,&m);
    memset(box,-1,sizeof(box)),kk=0;
    memset(in,0,sizeof(in));
    for(int i=0;i<m;i++)
    {
        int s,t;scanf("%d%d",&s,&t);
       add(t,s);
    }

    for(int i=1;i<=n;i++)
    if(in[i]==0)q.push(i);
    int id=q.size();
    while(q.size())
    {

        for(int i=box[q.front()];~i;i=e[i].next)
        if(--in[e[i].t]==0) q.push(e[i].t),id++;

        q.pop();
    }
    if(id<n) puts("-1");
    else
    {
        memset(in,0,sizeof(in));
        base=0;
        for(int i=1;i<=n;i++)
        if(!in[i])
        {
            dfs(i);
        }
        for(int i=1;i<=n;i++)
        printf("%d%c",lab[i]," \n"[i==n]);
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    int ca;cin>>ca;
    while(ca--)
    {
        init();
    }
    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