[UVA][合] 10475 - Help the Leaders@Morris' Blog|PChome Online 人新台
2013-07-22 17:25:09| 人911| 回0 | 上一篇 | 下一篇
<-- start of blogbody -->

[UVA][合] 10475 - Help the Leaders

0 收藏 0 0 站台


  Help the Leaders 

Have you ever listened to the speeches of leaders (Political or non-political)? People often find these speeches monotonous and hopeless. The leaders also sometimes feel very embarrassed about their speeches and they are often afraid of covering the same topics in two different speeches. You can save our intelligent leaders from these kinds of embarrassments. For example, let's consider that a leader has the following topics in his mind

a)
War
b)
Terror
c)
Peace
d)
Nuclear-Bomb
e)
Human-Right
f)
Food
g)
Oil-Crisis
h)
Equal-Right

He delivers speeches on any two of these topics at once but never wants to cover the topics ``Oil- Crisis" and ``War" in the same speech. The same thing applies for the topics ``Nuclear-Bomb" and ``Equal-Right". You can help him by making all possible combinations of topics (Of course considering the restrictions supplied by him) so that he can choose any one from them according to his convenience and can avoid any blunders. The specific details are given in the input and output specifications.

Input 

The first line of the input file contains an integer n(n$ le$100), which indicates how many sets of inputs are there. The description of each set is given in the paragraph below:

The first line of each set has three integers t( 0 < t < 16), p( 0$ le$p < t(t - 1)/2), s( 0 < s$ le$5). Here t denotes the number of topics in the leader's mind, p denotes the number of prohibited pairs and s is the total number of topics the leader wants to cover in a single speech. The next t lines contain t topics of the leader's mind. The topics are all different. Each topic will contain only alphanumeric characters and hyphens. Each of the next p lines contains two different topics in each line separated by a single space. These pairs of topics should not come in the same speech. Same pair of topics may appear more than once in the input. Each topic has a maximum length of 15.

For each set you should first print the serial of that set. Each of the next lines should contain all the possible topic groups. The topics in each group should be separated by a single space. Print a blank line after the output for each set of input. In a group the topics should be printed in the descending order of their lengths. If lengths of two topics are same the lexicographically smaller one should come first. This order should also be maintained in the printing order of the groups.

Topics should be considered case insensitive. While printing the topics print all the characters as uppercase. Print a blank line after the output for each case.

Sample Input 

2 8 2 2 WAR TERROR PEACE NUCLEAR-BOMB HUMAN-RIGHT FOOD OIL-CRISIS EQUAL-RIGHT WAR OIL-CRISIS EQUAL-RIGHT NUCLEAR-BOMB 8 0 1 WAR TERROR PEACE NUCLEAR-BOMB HUMAN-RIGHT FOOD OIL-CRISIS EQUAL-RIGHT 

Output for Sample Input 

Set 1: NUCLEAR-BOMB HUMAN-RIGHT NUCLEAR-BOMB OIL-CRISIS NUCLEAR-BOMB TERROR NUCLEAR-BOMB PEACE NUCLEAR-BOMB FOOD NUCLEAR-BOMB WAR EQUAL-RIGHT HUMAN-RIGHT EQUAL-RIGHT OIL-CRISIS EQUAL-RIGHT TERROR EQUAL-RIGHT PEACE EQUAL-RIGHT FOOD EQUAL-RIGHT WAR HUMAN-RIGHT OIL-CRISIS HUMAN-RIGHT TERROR HUMAN-RIGHT PEACE HUMAN-RIGHT FOOD HUMAN-RIGHT WAR OIL-CRISIS TERROR OIL-CRISIS PEACE OIL-CRISIS FOOD TERROR PEACE TERROR FOOD TERROR WAR PEACE FOOD PEACE WAR FOOD WAR Set 2: NUCLEAR-BOMB EQUAL-RIGHT HUMAN-RIGHT OIL-CRISIS TERROR PEACE FOOD WAR 



Problem-setter: Shahriar Manzoor, ACM Valladolid Online Judge


目定演,有些不同,而每次要演的目指定,
生成所有可能的合方式。必按照字典序出,在前先行排序。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int n, cond, m, g[20][20];
string topic[20];
int ch[20];
void dfs(int idx, int i) {
    if(idx == m) {
        for(i = 0; i < idx; i++) {
            if(i)   putchar(' ');
            printf("%s", topic[ch[i]].c_str());
        }
        puts("");
        return ;
    }
    int j;
    for(; i < n; i++) {
        for(j = 0; j < idx; j++)
            if(g[i][ch[j]] == 1)
                break;
        if(j == idx) {
            ch[idx] = i;
            dfs(idx+1, i+1);
        }
    }
}
bool cmp(string a, string b) {
    if(a.length() != b.length())
        return a.length() > b.length();
    return a < b;
}
int main() {
    int testcase, cases = 0;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d %d", &n, &cond, &m);
        memset(g, 0, sizeof(g));
        int i, j, k;
        for(i = 0; i < n; i++) {
            cin >> topic[i];
            for(j = 0; j < topic[i].length(); j++) {
                if(topic[i][j] >= 'a' && topic[i][j] <= 'z')
                    topic[i][j] -= 32;
            }
        }
        sort(topic, topic+n, cmp);
        map<string, int> R;
        for(i = 0; i < n; i++)
            R[topic[i]] = i;
        while(cond--) {
            string tx, ty;
            cin >> tx >> ty;
            for(i = 0; i < tx.length(); i++) {
                if(tx[i] >= 'a' && tx[i] <= 'z')
                    tx[i] -= 32;
            }
            for(i = 0; i < ty.length(); i++) {
                if(ty[i] >= 'a' && ty[i] <= 'z')
                    ty[i] -= 32;
            }
            int x = R[tx], y = R[ty];
            g[x][y] = g[y][x] = 1;
        }
        printf("Set %d:\n", ++cases);
        dfs(0, 0);
        puts("");
    }
    return 0;
}

台: Morris
人(911) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][搜索] 10506 - The Ouroboros problem
此分上一篇:[UVA][搜索] 301 - Transportation

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86