codeforces 510c (拓扑排序)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 111111;

int topo[205];
struct node
{
    char a[105];
}e[105];

int n;
int g[30][30];
int f[30];
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i = 0; i < n; i++)
    {
        scanf("%s",e[i].a);
    }
    memset(f,0,sizeof(f));
    memset(g,0,sizeof(g));
    for(i = 1; i < n;i++)
    {
        int l1 = strlen( e[i].a);
        int l2 = strlen(e[i-1].a);
        for(j = 0; j < l1&& j < l2; j++)
        {
            if(e[i].a[j] != e[i-1].a[j])
            {
                int x = e[i].a[j] - ‘a‘;
                int y = e[i-1].a[j] - ‘a‘;
                if(g[x][y] == 0)
                f[y]++;
                g[x][y] = 1;

                break;
            }
        }
        if(l1 < l2 && j == l1)
        {
            printf("Impossible\n");
            return 0;
        }
    }
    queue<int> q;
    for(i = 0; i < 26; i++)
    {
        if(f[i] == 0)
        {
            q.push(i);
        }
    }
    int cnt = 0;
    while(!q.empty())
    {
        int w = q.front();
        q.pop();
        topo[cnt++] = w;
        for(i = 0; i < 26; i++)
        {
            if(g[w][i] == 1)
            {
                f[i]--;
                if(f[i] == 0)
                    q.push(i);
            }
        }
    }
    if(cnt != 26)
    {
        printf("Impossible\n");
        return 0;
    }
    for(i = cnt-1; i >=0; i--)
    {
        printf("%c",topo[i] + ‘a‘);
    }
    printf("\n");
    return 0;
}

  

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char name[120][120];
int mat[30][30];
int mark[30];
int topo[30];
int vis[30];
int cnt;
int main()
{
    int n;
    int i,j,k;
    while(scanf("%d",&n)!=EOF)
    {
        memset(mat,0,sizeof(mat));
        memset(mark,0,sizeof(mark));
        cnt=0;k=0;
        int ok=1;
        for(i=1;i<=n;i++)
        {
            scanf("%s",name[i]);
        }
        for(i=1;i<n;i++)
        {
            if(ok==0) break;
            int len1=strlen(name[i]);
            int len2=strlen(name[i+1]);
            for(j=0;j<len1&&j<len2;j++)
            {
                if(name[i][j]!=name[i+1][j])
                {
                    int x=name[i][j]-‘a‘;
                    int y=name[i+1][j]-‘a‘;
                    if(mat[x][y]==0)
                    {
                        mat[x][y]=1;
                        mark[y]++;
                    }
                    break;
                }
            }
            if(len1>len2&&j==len2)
            {
                ok=0;
            }
        }
        for(i=0;i<26;i++)
        {
                int flag=-1;
                for(j=0;j<26;j++)
                {
                   if(mark[j]==0)
                   {
                       topo[k++]=j;
                       mark[j]=-1;
                       flag=j;
                       break;
                   }
                }
                if(flag!=-1)
                {
                    for(j=0;j<26;j++)
                    {
                        if(mat[flag][j]==1)
                        {
                            mark[j]--;
                        }
                    }
                }
        }
        if(k<26) ok=0;
        if(ok)
        {
            for(i=0;i<26;i++)
            {
                printf("%c",topo[i]+‘a‘);
            }
            printf("\n");
        }
        else printf("Impossible\n");
    }
    return 0;
}

  

时间: 2024-10-12 15:20:48

codeforces 510c (拓扑排序)的相关文章

Codeforces 1159E 拓扑排序

题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL long long #define db double using namespace std; const int maxn = 500010; stack<pair<int ,int> >s; vector<int> G[maxn]; int a[maxn], ans[

codeforces 510C Fox And Names 拓扑排序

传送门:cf 510D 给定n个字符串,问能否存在这样的字母表,使得字符串的排序满足字典序.即依据新的字母表,排序满足字典序大小. 假设满足字典序,则我们可以依据已有的字符串得出各字母之间的大小关系,然后通过拓扑排序来判断是否存在可行解,输出任意解,因此只需要判断是否存在解即可. /****************************************************** * File Name: a.cpp * Author: kojimai * Create Time: 2

Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/29/C Description One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B

Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[],使得两个数组中最大的数尽量小 题解 按照偏序表,构造出从小到大的拓扑图 如何解决相等的数的偏序关系? 用并查集缩点后再进行拓扑排序 如何解决最大的数最小? 只需要使得同一层的数相同就行,可以一批处理栈中的元素,对于一批栈中的元素产生的新点,先放进一个容器里,然后等到这批栈清空了,再把这个容器中的点

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can

Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

Codeforces Round #363 Fix a Tree(树 拓扑排序)

先做拓扑排序,再bfs处理 #include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<map>#include<stack>#include<queue>#include<vector>#include<cmath&g

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie

Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)

D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil created a following problem about putting 1?×?2 tiles into an n?×?m grid: "There is a grid with some cells that a

Codeforces Round #290 (Div. 2) 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t