codeforce 510C Fox And Names (拓扑排序)

原题地址:http://codeforces.com/problemset/problem/510/C

 

题意:

 

题解

拓扑排序……

 

#include<bits/stdc++.h>

#define clr(x,y) memset((x),(y),sizeof(x))

using namespace std;
typedef long long LL;

const int maxn=300;

int c[maxn+5];
int topo[maxn+5];
int cnt;

vector <int> G[maxn+5];
char Name[105][105];

bool dfs(int u)
{
    c[u]=-1;
    for (int i=0;i<G[u].size();++i)
    {
        int v=G[u][i];
        if (c[v]<0) return false;
        else if (!c[v] && !dfs(v)) return false;
    }
    c[u]=1;
    topo[--cnt]=u;
    return true;
}

bool toposort(int n)
{
    cnt=n;
    clr(c,0);
    for (int u=0;u<n;++u)
    {
        if (!c[u] && !dfs(u)) return false;
    }
    return true;
}

int main(void)
{
    #ifdef ex
    freopen ("../in.txt","r",stdin);
    //freopen ("../out.txt","w",stdout);
    #endif

    int n;
    scanf("%d",&n);

    for (int i=1;i<=n;++i)
    {
        scanf("%s",Name[i]);
    }

    for (int i=1;i<=n-1;++i)
    {
        int len1=strlen(Name[i]);
        int len2=strlen(Name[i+1]);
        for (int j=0;j<min(len1,len2);++j)
        {
            int t1=Name[i][j]-'a';
            int t2=Name[i+1][j]-'a';
            if (t1!=t2)
            {
                G[t1].push_back(t2);
                //printf("%c %c\n",Name[i][j],Name[i+1][j]);
                break;
            }
            if (len1>len2 && j==min(len1,len2)-1)
            {
                printf("Impossible\n");
                return 0;
            }
        }
    }

    bool f=toposort(26);
    if (!f)
        printf("Impossible\n");
    else
    {
        for (int i=0;i<26;++i)
            printf("%c",topo[i]+'a');
    }
}

 

BFS+队列实现的拓扑排序

#include<bits/stdc++.h>

#define clr(x,y) memset((x),(y),sizeof(x))

using namespace std;
typedef long long LL;

const int maxn=300;

int topo[maxn+5];
int inq[maxn+5];
int deg[maxn+5];
queue <int> Q;

vector <int> G[maxn+5];
char Name[105][105];

bool toposort(int n)
{
    int cnt=0;
    for (int i=0;i<n;++i)
    {
        if (deg[i]==0)
        {
            Q.push(i);
            //inq[i]=1;
        }
    }

    while (!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        topo[cnt]=x;
        ++cnt;

        for (auto e:G[x])
        {
            //if (inq[e]) return false;
            //inq[e]=1;
            --deg[e];
            if (deg[e]==0) Q.push(e);
        }
    }

    if (cnt!=n) return false;
    return true;
}


int main(void)
{
    #ifdef ex
    freopen ("../in.txt","r",stdin);
    //freopen ("../out.txt","w",stdout);
    #endif

    int n;
    scanf("%d",&n);

    for (int i=1;i<=n;++i)
    {
        scanf("%s",Name[i]);
    }

    clr(deg,0);
    for (int i=1;i<=n-1;++i)
    {
        int len1=strlen(Name[i]);
        int len2=strlen(Name[i+1]);
        for (int j=0;j<min(len1,len2);++j)
        {
            int t1=Name[i][j]-'a';
            int t2=Name[i+1][j]-'a';
            if (t1!=t2)
            {
                G[t1].push_back(t2);
                ++deg[t2];
                //printf("%c %c\n",Name[i][j],Name[i+1][j]);
                break;
            }
            if (len1>len2 && j==min(len1,len2)-1)
            {
                printf("Impossible\n");
                return 0;
            }
        }
    }

    bool f=toposort(26);
    if (!f)
        printf("Impossible\n");
    else
    {
        for (int i=0;i<26;++i)
            printf("%c",topo[i]+'a');
    }
}

 

posted on 2016-06-09 12:44  zhong_wang  阅读(136)  评论(0编辑  收藏  举报

导航