81062024-01-12 13:36:58EsVagyTom és Jerry2 (60)cpp17Hibás válasz 24/6043ms12244 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] && !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";
                }
            }
        }
    }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base24/60
1Elfogadva0/03ms2100 KiB
2Hibás válasz0/09ms4596 KiB
3Hibás válasz0/23ms2352 KiB
4Hibás válasz0/23ms2456 KiB
5Hibás válasz0/23ms2516 KiB
6Hibás válasz0/33ms2732 KiB
7Hibás válasz0/23ms3084 KiB
8Hibás válasz0/23ms3056 KiB
9Hibás válasz0/23ms3172 KiB
10Hibás válasz0/33ms3392 KiB
11Hibás válasz0/33ms3716 KiB
12Hibás válasz0/34ms4088 KiB
13Hibás válasz0/36ms4340 KiB
14Hibás válasz0/38ms4928 KiB
15Hibás válasz0/38ms5284 KiB
16Hibás válasz0/39ms5376 KiB
17Elfogadva3/310ms6452 KiB
18Elfogadva3/318ms8732 KiB
19Elfogadva4/428ms9644 KiB
20Elfogadva4/443ms12244 KiB
21Elfogadva5/527ms11332 KiB
22Elfogadva5/526ms11540 KiB