81052024-01-12 13:35:19EsVagyTom és Jerry2 (60)cpp17Hibás válasz 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";
                }
            }
        }
    }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base30/60
1Elfogadva0/03ms1828 KiB
2Hibás válasz0/010ms4784 KiB
3Hibás válasz0/23ms2360 KiB
4Hibás válasz0/23ms2592 KiB
5Hibás válasz0/23ms2688 KiB
6Hibás válasz0/33ms2808 KiB
7Hibás válasz0/23ms3168 KiB
8Hibás válasz0/23ms3580 KiB
9Hibás válasz0/23ms3644 KiB
10Hibás válasz0/33ms3872 KiB
11Hibás válasz0/34ms4104 KiB
12Hibás válasz0/34ms4720 KiB
13Hibás válasz0/36ms5148 KiB
14Elfogadva3/38ms5836 KiB
15Hibás válasz0/38ms6464 KiB
16Elfogadva3/39ms6700 KiB
17Elfogadva3/310ms7800 KiB
18Elfogadva3/318ms10556 KiB
19Elfogadva4/429ms11956 KiB
20Elfogadva4/443ms15460 KiB
21Elfogadva5/527ms14888 KiB
22Elfogadva5/527ms15576 KiB