---
id: 450
lang: en
translation_of: index.md
source_sha256: 4739513b4346dbfe6d482efee74a478c38ad48b800d3fd01e8d5c453b93157fb
title: "P2444: [POI2000] Virus — Solution"
description: "This problem is definitely solved with an Aho–Corasick automaton: look for a cycle that avoids every unsafe end node."
tags: ["OI","Strings","DFS","Aho–Corasick Automaton"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "AC 自动机 rendered as “Aho–Corasick automaton”; 虚边 as “virtual edge”."
---

## Main Idea

This problem is definitely going to be implemented with an Aho–Corasick automaton. After we put all these strings into the automaton with the insert operation, how do we modify the build_AC_automation operation to handle this problem?

First, we notice that the fail pointers turn the whole trie into a graph (every node gets an extra **virtual edge**). So we find that as long as we can find a cycle in this graph such that none of the nodes on the cycle is the end node of some unsafe code, we can conclude that an infinitely long safe code exists. The reason is very simple to explain: if there is such a safe cycle, then the infinitely long code must loop around this cycle forever, and since the cycle is safe, this long string of code is guaranteed to be safe too.

See the code for details.

## Code

```cpp
// P2444.cpp
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int MX_N = 30020;
struct node
{
    node *nxt[2], *fail;
    bool sum, ins, vis;
    node() { nxt[0] = nxt[1] = fail = NULL, sum = 0, ins = vis = false; }
};
node *root;
int n, longest;
void insert(string str)
{
    int siz = str.length();
    node *p = root;
    for (int i = 0; i < siz; i++)
        p = (p->nxt[str[i] - '0'] == NULL) ? (p->nxt[str[i] - '0'] = new node()) : p->nxt[str[i] - '0'];
    p->sum = true;
}
void build_ac_automation()
{
    queue<node *> q;
    q.push(root);
    while (!q.empty())
    {
        node *curt = q.front();
        q.pop();
        for (int i = 0; i < 2; i++)
            if (curt->nxt[i] != NULL)
            {
                if (curt == root)
                    curt->nxt[i]->fail = root;
                else
                {
                    node *p = curt->fail;
                    while (p)
                        if (p->nxt[i] != NULL)
                        {
                            curt->nxt[i]->fail = p->nxt[i];
                            curt->nxt[i]->sum |= p->nxt[i]->sum;
                            break;
                        }
                        else
                            p = p->fail;
                    if (p == NULL)
                        curt->nxt[i]->fail = root;
                }
                q.push(curt->nxt[i]);
            }
            else if (curt->fail != NULL)
                curt->nxt[i] = curt->fail->nxt[i];
    }
}
bool dfs(node *curt)
{
    if (curt->ins)
        return true;
    if (curt->vis || curt->sum > 0)
        return false;
    curt->vis = curt->ins = true;
    for (int i = 0; i < 2; i++)
        if (curt->nxt[i] != NULL && dfs(curt->nxt[i]))
            return true;
    curt->ins = false;
    return false;
}
char buff[30020];
int main()
{
    scanf("%d", &n);
    root = new node();
    for (int i = 1; i <= n; i++)
        scanf("%s", buff), insert(buff);
    build_ac_automation();
    if (dfs(root))
        printf("TAK");
    else
        printf("NIE");
    return 0;
}
```
