顯示具有 Topological sort 標籤的文章。 顯示所有文章
顯示具有 Topological sort 標籤的文章。 顯示所有文章

2012年9月25日 星期二

uva 11060 - Beverages


Problem E - Beverages

Time Limit: 1 second


Dilbert has just finished college and decided to go out with friends. Dilbert has some strange habits and thus he decided to celebrate this important moment of his life drinking a lot. He will start drinking beverages with low alcohol content, like beer, and then will move to a beverage that contains more alcohol, like wine, until there are no more available beverages. Once Dilbert starts to drink wine he will not drink beer again, so the alcohol content of the beverages never decreases with the time.
You should help Dilbert by indicating an order in which he can drink the beverages in the way he wishes.

Input

Each test case starts with 1 ≤ N ≤ 100, the number of available beverages. Then will follow N lines, informing the name of each beverage, a name has less than 51 characters and has no white spaces. Then there is another line with an integer 0 ≤ M ≤ 200 and M lines in the form B1 B2 will follow, indicating that beverage B2 has more alcohol that beverage B1, so Dilbert should drink beverage B1 before he starts drinking beverage B2. Be sure that this relation is transitive, so if there is also a relation B2 B3 you should drink B1 before you can drink B3. There is a blank line after each test case. In the case there is no relation between two beverages Dilbert should start drinking the one that appears first in the input. The input is terminated by end of file (EOF).

Output

For each test case you must print the message: Case #C: Dilbert should drink beverages in this order: B1 B2 ... BN., where C is the number of the test case, starting from 1, and B1 B2 ... BN is a list of the beverages such that the alcohol content of beverage Bi+1 is not less than the alcohol content of beverage Bi-1. After each test case you must print a blank line.

Sample Input

3
vodka
wine
beer
2
wine vodka
beer wine

5
wine
beer
rum
apple-juice
cachaca
6
beer cachaca
apple-juice beer
apple-juice rum
beer rum
beer wine
wine cachaca

10
cachaca
rum
apple-juice
tequila
whiskey
wine
vodka
beer
martini
gin
11
beer whiskey
apple-juice gin
rum cachaca
vodka tequila
apple-juice martini
rum gin
wine whiskey
apple-juice beer
beer rum
wine vodka
beer tequila

Sample Output

Case #1: Dilbert should drink beverages in this order: beer wine vodka.

Case #2: Dilbert should drink beverages in this order: apple-juice beer wine rum cachaca.

Case #3: Dilbert should drink beverages in this order: apple-juice wine vodka beer rum cachaca tequila whiskey martini gin.


Problem setter: Sérgio Queiroz de Medeiros
Topological sort + priority queue
#include<stdio.h>
#include<cstring>
#include<string>
#include<map>
#include<queue>
#include<vector>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i,n) REP(i, 0, n)
#define DBG 0
using namespace std;

int N,cnt[100],cases=1;
vector<int>egs[100];
map<string,int>mp;
vector<string>name;
void ans(){
vector<int>res;
int top=-1,cur,b;
priority_queue<pair<int,int> >q;
rep(i,N){
if(!cnt[i]){
if(DBG)printf("cnt[%d]=0\n",i);
q.push(make_pair(-1*i,i));
}
}
rep(i,N){
cur=q.top().second,q.pop();
if(DBG)printf("cur %d\n",cur+1);
res.push_back(cur);
rep(j,egs[cur].size()){
b=egs[cur][j];
cnt[b]--;
if(!cnt[b])q.push(make_pair(-1*b,b));
}
}
bool ll=0;
printf("Case #%d: Dilbert should drink beverages in this order: ",cases++);
rep(i,res.size()){if(ll)printf(" ");ll=1;printf("%s",name[res[i]].c_str());}
printf(".\n\n");
}
int main(){
char chs[100],chs2[100];
int m,a,b,num;
while(scanf("%d",&N)==1){
num=0,name.clear(),mp.clear(),memset(cnt,0,sizeof(cnt));
rep(i,100)egs[i].clear();
rep(i,N)scanf("%s",chs),mp[chs]=num++,name.push_back(chs);
scanf("%d",&m);
rep(i,m)scanf("%s%s",chs,chs2),a=mp[chs],b=mp[chs2],egs[a].push_back(b),cnt[b]++;
ans();
}
}

uva 872 - Ordering


Ordering
Background
   Order is an important concept in mathematics and in computer science. For example, Zorns Lemma states: a partially ordered set in which every chain has an upper bound contains a maximal element. Order is also important in reasoning about the fix-point semantics of programs.
    This problem involves neither Zorns Lemma nor fix-point semantics, but does involve order.
Problem
   Given a list of variable constraints of the form A < B, you are to write a program that prints all orderings of the variables that are consistent with the constraints. For example, given the contraints A < B and A < C there are two orderings of the variables A, B and C that are consistent with these constraints: ABC and ACB.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

   The input consists of two lines: a list of variables on one line, followed by a list of constraints of the form A < B on the next line. Variables and contraints are separated by single spaces.
   All variables are single character, upper-case letters. There will be at least two variables, and no more than 20 variables. There will be at least one constraint, and no more than 50 constraints. There will be no more than 300 orderings consistent with the contraints in a specification.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

   All orderings consistent with the constraints should be printed. Orderings are printed in alphabetical order, one per line. Characters on a line are separated by a space. If no ordering is possible, the output is a single line with the word NO.

Sample Input

1

A B F G
A<B B<F

Sample Output

A B F G
A B G F
A G B F
G A B F



Gabriel David, CPUP'2000 Round 2
Concurso de Programa��o da Universidade do Porto


Topological sort
#include<stdio.h>
#include<sstream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i,n) REP(i, 0, n)
#define DBG 0
using namespace std;

int N,cnt[26];
bool trav[26];
vector<int>sym;
vector<int>egs[26];
vector<string>res;
void topo(string s){
    if(s.size()==N){if(DBG)printf("s %s\n",s.c_str());res.push_back(s);return;}
    int b,cnt2[26];
    rep(i,N){
        if(!trav[sym[i]]&&!cnt[sym[i]]){
            trav[sym[i]]=1;
            memcpy(cnt2,cnt,sizeof(cnt));
            rep(j,egs[sym[i]].size()){
                b=egs[sym[i]][j];
                cnt[b]--;
            }
            topo(s+char('A'+sym[i]));
            memcpy(cnt,cnt2,sizeof(cnt));
            trav[sym[i]]=0;
        }
    }
}
void ans(){
    res.clear();
    topo("");
    sort(res.begin(),res.end());
    bool ll;
    if(res.size())
        rep(i,res.size()){
            ll=0;
            rep(j,res[i].size()){if(ll)printf(" ");ll=1;printf("%c",res[i][j]);}
            printf("\n");
        }
    else printf("NO\n");
}
void getO(string s){
    if(DBG)printf("%c<%c\n",s[0],s[2]);
    egs[s[0]-'A'].push_back(s[2]-'A');
    cnt[s[2]-'A']++;
}
int main(){
    char chs[200];
    stringstream buf;
    int n;
    bool ll=0;
    string s;
    scanf("%d",&n);
    rep(i,n){
        sym.clear();rep(i,20)egs[i].clear();
        memset(cnt,0,sizeof(cnt));
        scanf(" ");
        fgets(chs,200,stdin);if(chs[strlen(chs)-1]=='\n')chs[strlen(chs)-1]='\0';
        buf.clear(),buf.str(chs);
        while(!buf.eof())buf>>s,sym.push_back(s[0]-'A');
        if(DBG){printf("sym: ");rep(i,sym.size())printf("%c ",sym[i]+'A');printf("\n");}
        fgets(chs,200,stdin);if(chs[strlen(chs)-1]=='\n')chs[strlen(chs)-1]='\0';
        buf.clear(),buf.str(chs);
        while(!buf.eof())buf>>s,getO(s);
        N=sym.size();
        sort(sym.begin(),sym.end());
        if(ll)printf("\n");ll=1;
        ans();
    }
}