POJ1129(贪心)

笔者休息娱乐。贪心即可,爱怎么暴力怎么暴力。莽WA,改了几下算法发现是输出中二了……

int T, color[26], tot;
bool adj[26][26];
string s;

void get(int cur) {
    set<int> s;
    rep(i, 0, T - 1)
        if (adj[cur][i] && color[i])
            s.insert(color[i]);
    
    rep(i, 1, 26)
        if (s.find(i) == s.end()) {
            color[cur] = i;
            return;
        }
}

int main() {
    while (cin >> T && T) {
        init(adj, 0);
        init(color, 0);
        tot = 0;

        rep(i, 1, T) {
            cin >> s;
            int a = s[0] - 'A';
            rep(j, 2, s.length() - 1) {
                int b = s[j] - 'A';
                adj[a][b] = 1;
            }
        }
        
        rep(i, 0, T - 1) {
            get(i);
            tot = max(tot, color[i]);
        }
        cout << tot << " channel" << (tot > 1 ? "s" : "") << " needed.\n";
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/AlphaWA/p/10672960.html