81692024-01-12 15:32:13EsVagyTom és Jerry2 (60)cpp17Wrong answer 24/6054ms12340 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 (accessibleByTom[next])
        {
            if (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] && !accessibleByTom[neigh] && nodes[neigh].branch != nodes[next].branch)
                {
                    if (nodes[next].branch != -1)
                    {
                        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";
                }
            }
        }
    }

    clog << "---\n";
    for (int i = 0; i < n; i++)
    {
        clog << i + 1 << " - " << nodes[i].tomDist << " " << nodes[i].branch << " " << nodes[i].wrongNode + 1 << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base24/60
1Accepted0/03ms1832 KiB
2Wrong answer0/014ms4544 KiB
3Wrong answer0/23ms2272 KiB
4Wrong answer0/23ms2460 KiB
5Wrong answer0/23ms2676 KiB
6Wrong answer0/33ms2896 KiB
7Wrong answer0/23ms2988 KiB
8Wrong answer0/24ms3388 KiB
9Wrong answer0/23ms3308 KiB
10Wrong answer0/34ms3520 KiB
11Wrong answer0/34ms3468 KiB
12Wrong answer0/34ms4048 KiB
13Wrong answer0/37ms4352 KiB
14Wrong answer0/38ms4552 KiB
15Wrong answer0/39ms4928 KiB
16Wrong answer0/312ms5148 KiB
17Accepted3/314ms5840 KiB
18Accepted3/326ms8380 KiB
19Accepted4/435ms9196 KiB
20Accepted4/454ms12340 KiB
21Accepted5/537ms10948 KiB
22Accepted5/537ms11088 KiB