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
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DongGu.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值