AtCoder-abc209 Problem E

      • 简单哈希 + 反向建图

      • 前向星或者邻接都可

      • 为什么反向? 由结论推原因,当确定一个状态就不可改了,以为这样才是最优

#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;

#define ll long long

const int N = 2e5 + 100;
vector<int> ans(N,-1);
vector<int> in(N);
vector<int> head(N);
vector<int> tail(N);
vector<int> G[N];

int get(char c){
    if (c >= 'A' && c <= 'Z')return (c - 'A' + 26);
    else return (c - 'a');
}

int get_hash(char a, char b, char c){
    a = get(a); b = get(b);  c = get(c);
    return a * 52 * 52 + b * 52 + c;
}

int main(){
    int n; cin >> n;
    string s;
    for (int i = 1, len; i <= n; i++) {
        cin >> s;    len = s.length();
        head[i] = get_hash(s[0], s[1], s[2]);
        tail[i] = get_hash(s[len - 3], s[len - 2], s[len - 1]);
        in[head[i]]++;
        G[tail[i]].push_back(head[i]);
    }

    queue<int> q;
    for (int i = 0; i <= 150000; i++)    {
        if (!in[i]){
            ans[i] = 0;
            q.push(i);
        }
    }

    while (!q.empty())    {
        int u = q.front();  q.pop();

        for (int i = 0; i<G[u].size(); i++) {
            int v = G[u][i];

            if (ans[v] == -1) {
                in[v]--;
                if (ans[u] == 0)    ans[v] = 1, q.push(v);
                else if (in[v] == 0)ans[v] = 0, q.push(v);
            }

        }
    }

    for (int i = 1; i <= n; i++){
        if (ans[tail[i]] == -1) printf("Draw\n");
        if (ans[tail[i]] == 0)  printf("Takahashi\n");
        if (ans[tail[i]] == 1)  printf("Aoki\n");
    }
    return 0;
}
/*
4
abcadc adcopq adcrst rstppp
*/


上一篇:Problem D. Ice Cream Tower 题解(二分+贪心)


下一篇:大数相加:A+B problem