81052024-01-12 13:35:19EsVagyTom és Jerry2 (60)cpp17Wrong answer 30/6043ms15576 KiB
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <climits>
#include <queue>
#include <fstream>
#include <sstream>
#include <math.h>
#include <list>

using namespace std;

using ll = long long;

struct node
{
    int tomDist = -1;
    int branch = -1;
    int wrongNode = -1;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, t, j, h;
    cin >> n >> m >> t >> j >> h;
    t--; h--;
    vector<node> nodes(n, node());
    vector<vector<int>> tomEdges(n, vector<int>());
    vector<vector<int>> jerryEdges(n, vector<int>());
    vector<bool> visited(n, false);
    vector<bool> accessibleByTom(n, false);
    for (int i = 0; i < m; i++)
    {
        int a, b, w;
        cin >> a >> b >> w;
        a--; b--;
        if (w == 2)
        {
            tomEdges[a].push_back(b);
            tomEdges[b].push_back(a);
        }
        jerryEdges[a].push_back(b);
        jerryEdges[b].push_back(a);
    }

    queue<int> tomQueue;
    tomQueue.push(t);
    nodes[t].tomDist = 0;
    set<int> validBranches;
    accessibleByTom[t] = true;
    while (!tomQueue.empty())
    {
        int next = tomQueue.front();
        tomQueue.pop();
        for (int neigh : tomEdges[next])
        {
            if (neigh != h && nodes[neigh].tomDist == -1)
            {
                nodes[neigh].tomDist = nodes[next].tomDist + 1;
                tomQueue.push(neigh);
                accessibleByTom[neigh] = true;
            }
        }
    }

    queue<int> q;
    q.push(h);
    visited[h] = true;
    int branch = 0;
    while (!q.empty())
    {
        int next = q.front();
        q.pop();
        if (nodes[next].tomDist != -1 && nodes[next].branch == -1)
        {
            nodes[next].branch = branch;
            branch++;
            nodes[next].wrongNode = next;
        }
        bool isBranch = nodes[next].branch != -1;
        for (int neigh : jerryEdges[next])
        {
            if (!visited[neigh])
            {
                if (isBranch)
                {
                    nodes[neigh].branch = nodes[next].branch;
                    nodes[neigh].tomDist = max(0, nodes[next].tomDist - 1);
                    nodes[neigh].wrongNode = nodes[next].wrongNode;
                }
                q.push(neigh);
                visited[neigh] = true;
            }
            else
            {
                if (!accessibleByTom[next] && isBranch && nodes[neigh].branch != nodes[next].branch)
                {
                    validBranches.insert(nodes[next].branch);
                    if (nodes[neigh].branch != -1)
                    {
                        validBranches.insert(nodes[neigh].branch);
                    }
                }                
            }
        }
    }

    for (int i = 0; i < j; i++)
    {
        int nextNode;
        cin >> nextNode;
        nextNode--;
        if (nodes[nextNode].branch == -1)
        {
            cout << "0\n";
        }
        else
        {
            if (validBranches.count(nodes[nextNode].branch) != 0)
            {
                cout << "0\n";
            }
            else
            {
                if (nodes[nextNode].tomDist > 0)
                {
                    cout << "0\n";
                }
                else
                {
                    cout << nodes[nextNode].wrongNode + 1 << "\n";
                }
            }
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base30/60
1Accepted0/03ms1828 KiB
2Wrong answer0/010ms4784 KiB
3Wrong answer0/23ms2360 KiB
4Wrong answer0/23ms2592 KiB
5Wrong answer0/23ms2688 KiB
6Wrong answer0/33ms2808 KiB
7Wrong answer0/23ms3168 KiB
8Wrong answer0/23ms3580 KiB
9Wrong answer0/23ms3644 KiB
10Wrong answer0/33ms3872 KiB
11Wrong answer0/34ms4104 KiB
12Wrong answer0/34ms4720 KiB
13Wrong answer0/36ms5148 KiB
14Accepted3/38ms5836 KiB
15Wrong answer0/38ms6464 KiB
16Accepted3/39ms6700 KiB
17Accepted3/310ms7800 KiB
18Accepted3/318ms10556 KiB
19Accepted4/429ms11956 KiB
20Accepted4/443ms15460 KiB
21Accepted5/527ms14888 KiB
22Accepted5/527ms15576 KiB